Add ComponentLocalizer#componentCreator

This commit is contained in:
NichtStudioCode 2023-04-13 12:59:32 +02:00
parent 4744638c4f
commit a40f9bb707
3 changed files with 17 additions and 16 deletions

@ -4,9 +4,10 @@ import net.kyori.adventure.text.*;
public class AdventureComponentLocalizer extends ComponentLocalizer<Component> {
public static final AdventureComponentLocalizer INSTANCE = new AdventureComponentLocalizer();
private static final AdventureComponentLocalizer INSTANCE = new AdventureComponentLocalizer();
private AdventureComponentLocalizer() {
super(Component::text);
}
public static AdventureComponentLocalizer getInstance() {
@ -49,9 +50,4 @@ public class AdventureComponentLocalizer extends ComponentLocalizer<Component> {
return Component.textOfChildren(children.toArray(ComponentLike[]::new)).style(component.style());
}
@Override
protected Component createTextComponent(String text) {
return Component.text(text);
}
}

@ -11,6 +11,7 @@ public class BungeeComponentLocalizer extends ComponentLocalizer<BaseComponent>
private static final BungeeComponentLocalizer INSTANCE = new BungeeComponentLocalizer();
private BungeeComponentLocalizer() {
super(TextComponent::new);
}
public static BungeeComponentLocalizer getInstance() {
@ -63,9 +64,4 @@ public class BungeeComponentLocalizer extends ComponentLocalizer<BaseComponent>
return result;
}
@Override
protected BaseComponent createTextComponent(String text) {
return new TextComponent(text);
}
}

@ -2,15 +2,24 @@ package xyz.xenondevs.inventoryaccess.component.i18n;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
abstract class ComponentLocalizer<C> {
private static final Pattern FORMAT_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)");
public abstract C localize(String lang, C component);
private Function<String, C> componentCreator;
protected abstract C createTextComponent(String text);
public ComponentLocalizer(Function<String, C> componentCreator) {
this.componentCreator = componentCreator;
}
public void setComponentCreator(Function<String, C> componentCreator) {
this.componentCreator = componentCreator;
}
public abstract C localize(String lang, C component);
protected List<C> decomposeFormatString(String lang, String formatString, C component, List<C> args) {
var matcher = FORMAT_PATTERN.matcher(formatString);
@ -46,9 +55,9 @@ abstract class ComponentLocalizer<C> {
// append the text before the argument
sb.append(formatString, i, start);
// add text component
components.add(createTextComponent(sb.toString()));
components.add(componentCreator.apply(sb.toString()));
// add argument component
components.add(args.size() <= argIdx ? createTextComponent("") : localize(lang, args.get(argIdx)));
components.add(args.size() <= argIdx ? componentCreator.apply("") : localize(lang, args.get(argIdx)));
// clear string builder
sb.setLength(0);
}
@ -60,7 +69,7 @@ abstract class ComponentLocalizer<C> {
// append the text after the last argument
if (i < formatString.length()) {
sb.append(formatString, i, formatString.length());
components.add(createTextComponent(sb.toString()));
components.add(componentCreator.apply(sb.toString()));
}
return components;