mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-30 20:39:18 +00:00
Remove InvUI
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* This file is part of InvUI, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) 2021 NichtStudioCode
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.bukkit.adventure;
|
||||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Languages {
|
||||
|
||||
private static final Languages INSTANCE = new Languages();
|
||||
private final Map<String, Map<String, String>> translations = new HashMap<>();
|
||||
private Function<Player, Locale> languageProvider = Player::locale;
|
||||
private boolean serverSideTranslations = true;
|
||||
|
||||
private Languages() {
|
||||
}
|
||||
|
||||
public static Languages getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public void addLanguage(@NotNull String lang, @NotNull Map<String, String> translations) {
|
||||
this.translations.put(lang, translations);
|
||||
}
|
||||
|
||||
public void loadLanguage(@NotNull String lang, @NotNull Reader reader) throws IOException {
|
||||
var translations = new HashMap<String, String>();
|
||||
try (var jsonReader = new JsonReader(reader)) {
|
||||
jsonReader.beginObject();
|
||||
while (jsonReader.hasNext()) {
|
||||
var key = jsonReader.nextName();
|
||||
var value = jsonReader.nextString();
|
||||
translations.put(key, value);
|
||||
}
|
||||
|
||||
addLanguage(lang, translations);
|
||||
}
|
||||
}
|
||||
|
||||
public void loadLanguage(@NotNull String lang, @NotNull File file, @NotNull Charset charset) throws IOException {
|
||||
try (var reader = new FileReader(file, charset)) {
|
||||
loadLanguage(lang, reader);
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable String getFormatString(@NotNull String lang, @NotNull String key) {
|
||||
var map = translations.get(lang);
|
||||
if (map == null)
|
||||
return null;
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public void setLanguageProvider(@NotNull Function<Player, Locale> languageProvider) {
|
||||
this.languageProvider = languageProvider;
|
||||
}
|
||||
|
||||
public @NotNull Locale getLanguage(@NotNull Player player) {
|
||||
return languageProvider.apply(player);
|
||||
}
|
||||
|
||||
public void enableServerSideTranslations(boolean enable) {
|
||||
serverSideTranslations = enable;
|
||||
}
|
||||
|
||||
public boolean doesServerSideTranslations() {
|
||||
return serverSideTranslations;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* This file is part of InvUI, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) 2021 NichtStudioCode
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.bukkit.adventure;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.Style;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
|
||||
public class ShadedAdventureComponentUtils {
|
||||
|
||||
private static final Style FORMATTING_TEMPLATE = Style.style()
|
||||
.color(NamedTextColor.WHITE)
|
||||
.decoration(TextDecoration.ITALIC, false)
|
||||
.decoration(TextDecoration.BOLD, false)
|
||||
.decoration(TextDecoration.STRIKETHROUGH, false)
|
||||
.decoration(TextDecoration.UNDERLINED, false)
|
||||
.decoration(TextDecoration.OBFUSCATED, false)
|
||||
.build();
|
||||
|
||||
public static Component withoutPreFormatting(Component component) {
|
||||
return component.style(component.style().merge(FORMATTING_TEMPLATE, Style.Merge.Strategy.IF_ABSENT_ON_TARGET));
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* This file is part of InvUI, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) 2021 NichtStudioCode
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.bukkit.adventure;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper;
|
||||
|
||||
public class ShadedAdventureComponentWrapper implements ComponentWrapper {
|
||||
|
||||
public static final ShadedAdventureComponentWrapper EMPTY = new ShadedAdventureComponentWrapper(Component.empty());
|
||||
|
||||
private final Component component;
|
||||
|
||||
public ShadedAdventureComponentWrapper(Component component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String serializeToJson() {
|
||||
return GsonComponentSerializer.gson().serialize(component);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ComponentWrapper localized(@NotNull String lang) {
|
||||
if (!Languages.getInstance().doesServerSideTranslations())
|
||||
return this;
|
||||
|
||||
return new ShadedAdventureComponentWrapper(ShadedAdventureShadedComponentLocalizer.getInstance().localize(lang, component));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ComponentWrapper withoutPreFormatting() {
|
||||
return new ShadedAdventureComponentWrapper(ShadedAdventureComponentUtils.withoutPreFormatting(component));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ShadedAdventureComponentWrapper clone() {
|
||||
try {
|
||||
return (ShadedAdventureComponentWrapper) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* This file is part of InvUI, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) 2021 NichtStudioCode
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.bukkit.adventure;
|
||||
|
||||
import net.kyori.adventure.text.*;
|
||||
|
||||
public class ShadedAdventureShadedComponentLocalizer extends ShadedComponentLocalizer<Component> {
|
||||
|
||||
private static final ShadedAdventureShadedComponentLocalizer INSTANCE = new ShadedAdventureShadedComponentLocalizer();
|
||||
|
||||
private ShadedAdventureShadedComponentLocalizer() {
|
||||
super(Component::text);
|
||||
}
|
||||
|
||||
public static ShadedAdventureShadedComponentLocalizer getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Override
|
||||
public Component localize(String lang, Component component) {
|
||||
if (!(component instanceof BuildableComponent))
|
||||
throw new IllegalStateException("Component is not a BuildableComponent");
|
||||
|
||||
return localize(lang, (BuildableComponent) component);
|
||||
}
|
||||
|
||||
@SuppressWarnings("NonExtendableApiUsage")
|
||||
private <C extends BuildableComponent<C, B>, B extends ComponentBuilder<C, B>> BuildableComponent<?, ?> localize(String lang, BuildableComponent<C, B> component) {
|
||||
ComponentBuilder<?, ?> builder;
|
||||
if (component instanceof TranslatableComponent) {
|
||||
builder = localizeTranslatable(lang, (TranslatableComponent) component).toBuilder();
|
||||
} else {
|
||||
builder = component.toBuilder();
|
||||
}
|
||||
|
||||
builder.mapChildrenDeep(child -> {
|
||||
if (child instanceof TranslatableComponent)
|
||||
return localizeTranslatable(lang, (TranslatableComponent) child);
|
||||
return child;
|
||||
});
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private BuildableComponent<?, ?> localizeTranslatable(String lang, TranslatableComponent component) {
|
||||
var formatString = Languages.getInstance().getFormatString(lang, component.key());
|
||||
if (formatString == null)
|
||||
return component;
|
||||
|
||||
var children = decomposeFormatString(lang, formatString, component, component.args());
|
||||
return Component.textOfChildren(children.toArray(ComponentLike[]::new)).style(component.style());
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* This file is part of InvUI, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) 2021 NichtStudioCode
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.bukkit.adventure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
abstract class ShadedComponentLocalizer<T> {
|
||||
|
||||
private static final Pattern FORMAT_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)");
|
||||
|
||||
private Function<String, T> componentCreator;
|
||||
|
||||
public ShadedComponentLocalizer(Function<String, T> componentCreator) {
|
||||
this.componentCreator = componentCreator;
|
||||
}
|
||||
|
||||
public void setComponentCreator(Function<String, T> componentCreator) {
|
||||
this.componentCreator = componentCreator;
|
||||
}
|
||||
|
||||
public abstract T localize(String lang, T component);
|
||||
|
||||
protected List<T> decomposeFormatString(String lang, String formatString, T component, List<T> args) {
|
||||
var matcher = FORMAT_PATTERN.matcher(formatString);
|
||||
|
||||
var components = new ArrayList<T>();
|
||||
var sb = new StringBuilder();
|
||||
var nextArgIdx = 0;
|
||||
|
||||
var i = 0;
|
||||
while (matcher.find(i)) {
|
||||
var start = matcher.start();
|
||||
var end = matcher.end();
|
||||
|
||||
// check for escaped %
|
||||
var matchedStr = formatString.substring(i, start);
|
||||
if ("%%".equals(matchedStr)) {
|
||||
sb.append('%');
|
||||
} else {
|
||||
// check for invalid format, only %s is supported
|
||||
var argType = matcher.group(2);
|
||||
if (!"s".equals(argType)) {
|
||||
throw new IllegalStateException("Unsupported format: '" + matchedStr + "'");
|
||||
}
|
||||
|
||||
// retrieve argument index
|
||||
var argIdxStr = matcher.group(1);
|
||||
var argIdx = argIdxStr == null ? nextArgIdx++ : Integer.parseInt(argIdxStr) - 1;
|
||||
|
||||
// validate argument index
|
||||
if (argIdx < 0)
|
||||
throw new IllegalStateException("Invalid argument index: " + argIdx);
|
||||
|
||||
// append the text before the argument
|
||||
sb.append(formatString, i, start);
|
||||
// add text component
|
||||
components.add(componentCreator.apply(sb.toString()));
|
||||
// add argument component
|
||||
components.add(args.size() <= argIdx ? componentCreator.apply("") : localize(lang, args.get(argIdx)));
|
||||
// clear string builder
|
||||
sb.setLength(0);
|
||||
}
|
||||
|
||||
// start next search after matcher end index
|
||||
i = end;
|
||||
}
|
||||
|
||||
// append the text after the last argument
|
||||
if (i < formatString.length()) {
|
||||
sb.append(formatString, i, formatString.length());
|
||||
components.add(componentCreator.apply(sb.toString()));
|
||||
}
|
||||
|
||||
return components;
|
||||
}
|
||||
}
|
||||
@@ -112,6 +112,7 @@ public class Migration {
|
||||
.replace("{price}", "{price_formatted}")
|
||||
.replace("{PRICE}", "{price}")
|
||||
.replace("{loot}", "{id}")
|
||||
.replace("{tension}", "{progress}")
|
||||
);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
Reference in New Issue
Block a user