Skip to content

Commit 4cff4b4

Browse files
committed
simplifying menu variables
1 parent c8052ce commit 4cff4b4

24 files changed

Lines changed: 362 additions & 301 deletions

src/main/java/me/hsgamer/bettergui/api/element/MenuElement.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
import me.hsgamer.bettergui.api.menu.Menu;
44
import me.hsgamer.hscore.common.StringReplacer;
5-
import org.jetbrains.annotations.NotNull;
6-
import org.jetbrains.annotations.Nullable;
7-
8-
import java.util.UUID;
95

106
/**
117
* The element of the menu
128
*/
13-
public interface MenuElement extends StringReplacer {
9+
public interface MenuElement {
1410
/**
1511
* Get the parent element of this element
1612
*
@@ -25,16 +21,13 @@ public interface MenuElement extends StringReplacer {
2521
*/
2622
String getName();
2723

28-
@Override
29-
@Nullable
30-
default String replace(@NotNull String arguments) {
31-
return null;
32-
}
33-
34-
@Override
35-
@Nullable
36-
default String replace(@NotNull String arguments, @NotNull UUID uuid) {
37-
return StringReplacer.super.replace(arguments, uuid);
24+
/**
25+
* Get the string replacer of this element
26+
*
27+
* @return the string replacer
28+
*/
29+
default StringReplacer getStringReplacer() {
30+
return StringReplacer.DUMMY;
3831
}
3932

4033
/**

src/main/java/me/hsgamer/bettergui/api/menu/Menu.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package me.hsgamer.bettergui.api.menu;
22

3-
import me.hsgamer.bettergui.BetterGUI;
43
import me.hsgamer.bettergui.api.element.MenuElement;
54
import me.hsgamer.hscore.common.StringReplacer;
65
import me.hsgamer.hscore.config.Config;
7-
import me.hsgamer.hscore.variable.VariableManager;
86
import org.bukkit.entity.Player;
97
import org.jetbrains.annotations.NotNull;
108
import org.jetbrains.annotations.Nullable;
@@ -21,7 +19,6 @@ public abstract class Menu implements MenuElement {
2119
public static final String MENU_SETTINGS_PATH = "menu-settings";
2220

2321
protected final Config config;
24-
protected final VariableManager variableManager = new VariableManager();
2522
private final Map<UUID, Menu> parentMenu = new HashMap<>();
2623

2724
/**
@@ -31,9 +28,6 @@ public abstract class Menu implements MenuElement {
3128
*/
3229
protected Menu(Config config) {
3330
this.config = config;
34-
variableManager.register("current-menu", original -> getName(), true);
35-
variableManager.register("parent-menu", StringReplacer.of((original, uuid) -> getParentMenu(uuid).map(Menu::getName).orElse("")));
36-
variableManager.addExternalReplacer(BetterGUI.getInstance().get(VariableManager.class));
3731
}
3832

3933
/**
@@ -55,24 +49,25 @@ public String getName() {
5549
return config.getName();
5650
}
5751

58-
/**
59-
* Get the variable manager of the menu
60-
*
61-
* @return the variable manager
62-
*/
63-
@Deprecated
64-
public VariableManager getVariableManager() {
65-
return variableManager;
66-
}
67-
68-
@Override
69-
public @Nullable String replace(@NotNull String original) {
70-
return variableManager.replace(original);
71-
}
72-
7352
@Override
74-
public @Nullable String replace(@NotNull String original, @NotNull UUID uuid) {
75-
return variableManager.replace(original, uuid);
53+
public StringReplacer getStringReplacer() {
54+
return new StringReplacer() {
55+
@Override
56+
public @Nullable String replace(@NotNull String original) {
57+
if (original.equalsIgnoreCase("current-menu")) {
58+
return getName();
59+
}
60+
return null;
61+
}
62+
63+
@Override
64+
public @Nullable String replace(@NotNull String original, @NotNull UUID uuid) {
65+
if (original.equalsIgnoreCase("parent-menu")) {
66+
return getParentMenu(uuid).map(Menu::getName).orElse(null);
67+
}
68+
return replace(original);
69+
}
70+
};
7671
}
7772

7873
@Override

src/main/java/me/hsgamer/bettergui/api/element/WithElementLookupStringReplacer.java renamed to src/main/java/me/hsgamer/bettergui/api/replacer/ElementLookupStringReplacer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package me.hsgamer.bettergui.api.element;
1+
package me.hsgamer.bettergui.api.replacer;
22

3+
import me.hsgamer.bettergui.api.element.MenuElement;
34
import me.hsgamer.hscore.common.Pair;
45
import me.hsgamer.hscore.common.StringReplacer;
56
import org.jetbrains.annotations.Nullable;
67

78
import java.util.List;
89

910
/**
10-
* An extension of {@link WithLookupStringReplacer} for cases where we want to choose replacers based on {@link MenuElement}'s name
11+
* An extension of {@link StringReplacer} for cases where we want to choose replacers based on {@link MenuElement}'s name
1112
*/
12-
public interface WithElementLookupStringReplacer<T extends MenuElement> extends WithLookupStringReplacer {
13+
public interface ElementLookupStringReplacer<T extends MenuElement> extends LookupStringReplacer {
1314
/**
1415
* Get the elements
1516
*
@@ -50,6 +51,6 @@ default Pair<StringReplacer, String> lookup(String original) {
5051
if (found == null) {
5152
return null;
5253
}
53-
return Pair.of(found, original.substring(found.getName().length()));
54+
return Pair.of(found.getStringReplacer(), original.substring(found.getName().length()));
5455
}
5556
}

src/main/java/me/hsgamer/bettergui/api/element/WithLookupStringReplacer.java renamed to src/main/java/me/hsgamer/bettergui/api/replacer/LookupStringReplacer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package me.hsgamer.bettergui.api.element;
1+
package me.hsgamer.bettergui.api.replacer;
22

3+
import me.hsgamer.bettergui.api.element.MenuElement;
34
import me.hsgamer.hscore.common.Pair;
45
import me.hsgamer.hscore.common.StringReplacer;
56
import org.jetbrains.annotations.NotNull;
@@ -8,9 +9,9 @@
89
import java.util.UUID;
910

1011
/**
11-
* An extension of {@link MenuElement} for cases where we want to choose specific replacers based on the prefix
12+
* An extension of {@link StringReplacer} for cases where we want to choose specific replacers based on the prefix
1213
*/
13-
public interface WithLookupStringReplacer extends MenuElement {
14+
public interface LookupStringReplacer extends StringReplacer {
1415
static String normalizeRemaining(String remaining) {
1516
return remaining.startsWith("_") ? remaining.substring(1) : remaining;
1617
}

src/main/java/me/hsgamer/bettergui/argument/ArgumentHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
import me.hsgamer.bettergui.BetterGUI;
44
import me.hsgamer.bettergui.api.argument.ArgumentProcessor;
55
import me.hsgamer.bettergui.api.element.MenuElement;
6-
import me.hsgamer.bettergui.api.element.WithElementLookupStringReplacer;
6+
import me.hsgamer.bettergui.api.replacer.ElementLookupStringReplacer;
77
import me.hsgamer.bettergui.builder.ArgumentProcessorBuilder;
88
import me.hsgamer.hscore.common.MapUtils;
99
import me.hsgamer.hscore.common.Pair;
10+
import me.hsgamer.hscore.common.StringReplacer;
1011

1112
import java.util.*;
1213

1314
/**
1415
* The handler for arguments
1516
*/
16-
public class ArgumentHandler implements ArgumentProcessor, WithElementLookupStringReplacer<ArgumentProcessor> {
17+
public class ArgumentHandler implements ArgumentProcessor {
1718
private final MenuElement menuElement;
1819
private final List<ArgumentProcessor> processors = new ArrayList<>();
1920

@@ -86,7 +87,7 @@ public String getName() {
8687
}
8788

8889
@Override
89-
public List<ArgumentProcessor> getElements() {
90-
return processors;
90+
public StringReplacer getStringReplacer() {
91+
return (ElementLookupStringReplacer<ArgumentProcessor>) () -> processors;
9192
}
9293
}

src/main/java/me/hsgamer/bettergui/argument/type/SingleArgumentProcessor.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.hsgamer.bettergui.util.ProcessApplierConstants;
55
import me.hsgamer.bettergui.util.SchedulerUtil;
66
import me.hsgamer.hscore.common.Pair;
7+
import me.hsgamer.hscore.common.StringReplacer;
78
import me.hsgamer.hscore.task.BatchRunnable;
89
import org.jetbrains.annotations.NotNull;
910
import org.jetbrains.annotations.Nullable;
@@ -57,22 +58,24 @@ public Optional<String[]> process(UUID uuid, String[] args) {
5758
}
5859

5960
@Override
60-
public @Nullable String replace(@NotNull String query, @NotNull UUID uuid) {
61-
if (query.equalsIgnoreCase("raw")) {
62-
return rawMap.getOrDefault(uuid, "");
63-
}
64-
65-
Optional<T> object = getObject(uuid);
66-
if (!object.isPresent()) {
67-
return "";
68-
}
69-
70-
T obj = object.get();
71-
if (query.isEmpty()) {
72-
return getArgumentValue(obj);
73-
} else {
74-
return getValue(query, uuid, obj);
75-
}
61+
public StringReplacer getStringReplacer() {
62+
return StringReplacer.of((query, uuid) -> {
63+
if (query.equalsIgnoreCase("raw")) {
64+
return rawMap.getOrDefault(uuid, "");
65+
}
66+
67+
Optional<T> object = getObject(uuid);
68+
if (!object.isPresent()) {
69+
return "";
70+
}
71+
72+
T obj = object.get();
73+
if (query.isEmpty()) {
74+
return getArgumentValue(obj);
75+
} else {
76+
return getValue(query, uuid, obj);
77+
}
78+
});
7679
}
7780

7881
@Override

src/main/java/me/hsgamer/bettergui/argument/type/StoreArgumentProcessor.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
import me.hsgamer.bettergui.builder.ArgumentProcessorBuilder;
44
import me.hsgamer.bettergui.util.ProcessApplierConstants;
55
import me.hsgamer.bettergui.util.SchedulerUtil;
6-
import me.hsgamer.hscore.common.CollectionUtils;
7-
import me.hsgamer.hscore.common.MapUtils;
8-
import me.hsgamer.hscore.common.Pair;
9-
import me.hsgamer.hscore.common.Validate;
6+
import me.hsgamer.hscore.common.*;
107
import me.hsgamer.hscore.task.BatchRunnable;
11-
import org.jetbrains.annotations.NotNull;
12-
import org.jetbrains.annotations.Nullable;
138

149
import java.math.BigDecimal;
1510
import java.util.*;
@@ -82,8 +77,8 @@ public Optional<String[]> process(UUID uuid, String[] args) {
8277
}
8378

8479
@Override
85-
public @Nullable String replace(@NotNull String query, @NotNull UUID uuid) {
86-
return map.getOrDefault(uuid, "");
80+
public StringReplacer getStringReplacer() {
81+
return StringReplacer.of((query, uuid) -> map.getOrDefault(uuid, ""));
8782
}
8883

8984
@Override

src/main/java/me/hsgamer/bettergui/button/LegacyMenuButton.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
import io.github.projectunified.craftux.button.PredicateButton;
44
import io.github.projectunified.craftux.common.Button;
55
import me.hsgamer.bettergui.api.button.MenuButton;
6-
import me.hsgamer.bettergui.api.element.WithLookupStringReplacer;
6+
import me.hsgamer.bettergui.api.replacer.LookupStringReplacer;
77
import me.hsgamer.bettergui.builder.ButtonBuilder;
88
import me.hsgamer.hscore.common.MapUtils;
99
import me.hsgamer.hscore.common.Pair;
1010
import me.hsgamer.hscore.common.StringReplacer;
11-
import org.jetbrains.annotations.Nullable;
1211

1312
import java.util.Map;
1413
import java.util.Set;
1514
import java.util.UUID;
1615
import java.util.concurrent.ConcurrentSkipListSet;
1716

18-
public class LegacyMenuButton extends MenuButton implements WithLookupStringReplacer {
17+
public class LegacyMenuButton extends MenuButton {
1918
private final Set<UUID> checked = new ConcurrentSkipListSet<>();
2019

2120
public LegacyMenuButton(ButtonBuilder.Input input) {
@@ -44,23 +43,25 @@ public void refresh(UUID uuid) {
4443
}
4544

4645
@Override
47-
public @Nullable Pair<StringReplacer, String> lookup(String original) {
48-
if (this.button == null) {
49-
return null;
50-
}
51-
WrappedPredicateButton.PredicateClickButton predicateButton = (WrappedPredicateButton.PredicateClickButton) this.button;
52-
if (original.startsWith("button")) {
53-
Button button = predicateButton.getPredicateButton().getButton();
54-
if (button instanceof MenuButton) {
55-
return Pair.of((MenuButton) button, original.substring("button".length()));
46+
public StringReplacer getStringReplacer() {
47+
return (LookupStringReplacer) original -> {
48+
if (this.button == null) {
49+
return null;
5650
}
57-
}
58-
if (original.startsWith("click")) {
59-
return Pair.of(predicateButton.getClickRequirements(), original.substring("click".length()));
60-
}
61-
if (original.startsWith("view")) {
62-
return Pair.of(predicateButton.getViewRequirement(), original.substring("view".length()));
63-
}
64-
return null;
51+
WrappedPredicateButton.PredicateClickButton predicateButton = (WrappedPredicateButton.PredicateClickButton) this.button;
52+
if (original.startsWith("button")) {
53+
Button button = predicateButton.getPredicateButton().getButton();
54+
if (button instanceof MenuButton) {
55+
return Pair.of(((MenuButton) button).getStringReplacer(), original.substring("button".length()));
56+
}
57+
}
58+
if (original.startsWith("click") && predicateButton.getClickRequirements() != null) {
59+
return Pair.of(predicateButton.getClickRequirements().getStringReplacer(), original.substring("click".length()));
60+
}
61+
if (original.startsWith("view") && predicateButton.getViewRequirement() != null) {
62+
return Pair.of(predicateButton.getViewRequirement().getStringReplacer(), original.substring("view".length()));
63+
}
64+
return null;
65+
};
6566
}
6667
}

src/main/java/me/hsgamer/bettergui/button/TemplateButton.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import me.hsgamer.bettergui.api.button.MenuButton;
55
import me.hsgamer.bettergui.builder.ButtonBuilder;
66
import me.hsgamer.bettergui.config.TemplateConfig;
7-
import org.jetbrains.annotations.NotNull;
8-
import org.jetbrains.annotations.Nullable;
7+
import me.hsgamer.hscore.common.StringReplacer;
98

109
import java.util.Map;
11-
import java.util.UUID;
1210

1311
public class TemplateButton extends MenuButton {
1412
private Map<String, Object> finalOptions;
@@ -35,14 +33,10 @@ public Map<String, Object> getOptions() {
3533
}
3634

3735
@Override
38-
public @Nullable String replace(@NotNull String arguments) {
39-
if (this.button == null) return null;
40-
return ((MenuButton) this.button).replace(arguments);
41-
}
42-
43-
@Override
44-
public @Nullable String replace(@NotNull String arguments, @NotNull UUID uuid) {
45-
if (this.button == null) return null;
46-
return ((MenuButton) this.button).replace(arguments, uuid);
36+
public StringReplacer getStringReplacer() {
37+
if (this.button instanceof MenuButton) {
38+
return ((MenuButton) this.button).getStringReplacer();
39+
}
40+
return super.getStringReplacer();
4741
}
4842
}

0 commit comments

Comments
 (0)