Skip to content

Commit a40f9bb

Browse files
Add ComponentLocalizer#componentCreator
1 parent 4744638 commit a40f9bb

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/i18n/AdventureComponentLocalizer.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
public class AdventureComponentLocalizer extends ComponentLocalizer<Component> {
66

7-
public static final AdventureComponentLocalizer INSTANCE = new AdventureComponentLocalizer();
7+
private static final AdventureComponentLocalizer INSTANCE = new AdventureComponentLocalizer();
88

99
private AdventureComponentLocalizer() {
10+
super(Component::text);
1011
}
1112

1213
public static AdventureComponentLocalizer getInstance() {
@@ -49,9 +50,4 @@ public Component localize(String lang, Component component) {
4950
return Component.textOfChildren(children.toArray(ComponentLike[]::new)).style(component.style());
5051
}
5152

52-
@Override
53-
protected Component createTextComponent(String text) {
54-
return Component.text(text);
55-
}
56-
5753
}

inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/i18n/BungeeComponentLocalizer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class BungeeComponentLocalizer extends ComponentLocalizer<BaseComponent>
1111
private static final BungeeComponentLocalizer INSTANCE = new BungeeComponentLocalizer();
1212

1313
private BungeeComponentLocalizer() {
14+
super(TextComponent::new);
1415
}
1516

1617
public static BungeeComponentLocalizer getInstance() {
@@ -63,9 +64,4 @@ private BaseComponent localizeTranslatable(String lang, TranslatableComponent co
6364
return result;
6465
}
6566

66-
@Override
67-
protected BaseComponent createTextComponent(String text) {
68-
return new TextComponent(text);
69-
}
70-
7167
}

inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/i18n/ComponentLocalizer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.function.Function;
56
import java.util.regex.Pattern;
67

78
abstract class ComponentLocalizer<C> {
89

910
private static final Pattern FORMAT_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)");
1011

11-
public abstract C localize(String lang, C component);
12+
private Function<String, C> componentCreator;
13+
14+
public ComponentLocalizer(Function<String, C> componentCreator) {
15+
this.componentCreator = componentCreator;
16+
}
1217

13-
protected abstract C createTextComponent(String text);
18+
public void setComponentCreator(Function<String, C> componentCreator) {
19+
this.componentCreator = componentCreator;
20+
}
21+
22+
public abstract C localize(String lang, C component);
1423

1524
protected List<C> decomposeFormatString(String lang, String formatString, C component, List<C> args) {
1625
var matcher = FORMAT_PATTERN.matcher(formatString);
@@ -46,9 +55,9 @@ protected List<C> decomposeFormatString(String lang, String formatString, C comp
4655
// append the text before the argument
4756
sb.append(formatString, i, start);
4857
// add text component
49-
components.add(createTextComponent(sb.toString()));
58+
components.add(componentCreator.apply(sb.toString()));
5059
// add argument component
51-
components.add(args.size() <= argIdx ? createTextComponent("") : localize(lang, args.get(argIdx)));
60+
components.add(args.size() <= argIdx ? componentCreator.apply("") : localize(lang, args.get(argIdx)));
5261
// clear string builder
5362
sb.setLength(0);
5463
}
@@ -60,7 +69,7 @@ protected List<C> decomposeFormatString(String lang, String formatString, C comp
6069
// append the text after the last argument
6170
if (i < formatString.length()) {
6271
sb.append(formatString, i, formatString.length());
63-
components.add(createTextComponent(sb.toString()));
72+
components.add(componentCreator.apply(sb.toString()));
6473
}
6574

6675
return components;

0 commit comments

Comments
 (0)