From 307aafe0104f6ed721610d63fb7253cffdff6985 Mon Sep 17 00:00:00 2001 From: Marcel Konrad Date: Sat, 23 Jun 2018 17:39:11 +0200 Subject: [PATCH] Refactoring --- .../gui/button/persistence/ButtonValues.java | 10 ++ .../element/impl/ElementClickList.java | 136 ++++++------------ .../gui/content/element/logic/ILogic.java | 10 ++ .../element/logic/ILogicClickList.java | 18 +-- .../element/logic/ILogicColorMenu.java | 7 +- .../content/element/logic/ILogicPageList.java | 4 +- .../impl/ContentScoreboardObjectives.java | 108 ++++++++------ .../content/impl/ContentScoreboardTeams.java | 42 +++--- 8 files changed, 163 insertions(+), 172 deletions(-) create mode 100644 src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogic.java diff --git a/src/main/java/exopandora/worldhandler/gui/button/persistence/ButtonValues.java b/src/main/java/exopandora/worldhandler/gui/button/persistence/ButtonValues.java index 531ddab..8d0f444 100644 --- a/src/main/java/exopandora/worldhandler/gui/button/persistence/ButtonValues.java +++ b/src/main/java/exopandora/worldhandler/gui/button/persistence/ButtonValues.java @@ -24,11 +24,21 @@ public class ButtonValues this.index++; } + public void incrementIndexBy(int amount) + { + this.index += amount; + } + public void decrementIndex() { this.index--; } + public void decrementIndexBy(int amount) + { + this.index -= amount; + } + public T getObject() { return this.object; diff --git a/src/main/java/exopandora/worldhandler/gui/content/element/impl/ElementClickList.java b/src/main/java/exopandora/worldhandler/gui/content/element/impl/ElementClickList.java index 0758d8a..e3983d8 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/element/impl/ElementClickList.java +++ b/src/main/java/exopandora/worldhandler/gui/content/element/impl/ElementClickList.java @@ -6,6 +6,7 @@ import javax.annotation.Nullable; import exopandora.worldhandler.gui.button.EnumTooltip; import exopandora.worldhandler.gui.button.GuiButtonList; +import exopandora.worldhandler.gui.button.GuiButtonWorldHandler; import exopandora.worldhandler.gui.button.logic.IListButtonLogic; import exopandora.worldhandler.gui.button.persistence.ButtonValues; import exopandora.worldhandler.gui.container.Container; @@ -20,22 +21,32 @@ import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class ElementClickList extends Element { - private final int buttonId1; - private final int buttonId2; + private final int buttonId; private final List list; private final ILogicClickList logic; - private GuiButtonList button1; - private GuiButtonList button2; private final Content content; + private final ElementClickList parent; + private final int depth; + private final int maxDepth; - public ElementClickList(int x, int y, List list, int buttonId1, int buttonId2, Content content, ILogicClickList logic) + private GuiButtonList button; + private ElementClickList child; + + public ElementClickList(int x, int y, List list, int buttonId, int maxDepth, Content content, ILogicClickList logic) + { + this(x, y, list, buttonId, maxDepth, content, logic, null); + } + + private ElementClickList(int x, int y, List list, int buttonId, int maxDepth, Content content, ILogicClickList logic, ElementClickList parent) { super(x, y); this.list = list; - this.buttonId1 = buttonId1; - this.buttonId2 = buttonId2; + this.buttonId = buttonId; this.logic = logic; this.content = content; + this.parent = parent; + this.maxDepth = maxDepth; + this.depth = this.parent != null ? this.parent.depth + 1 : 1; } @Override @@ -47,12 +58,12 @@ public class ElementClickList extends Element @Override public void initButtons(Container container) { - container.add(this.button1 = new GuiButtonList(this.buttonId1, this.x, this.y, 114, 20, EnumTooltip.TOP_RIGHT, this.content, new IListButtonLogic() + container.add(this.button = new GuiButtonList(this.buttonId, this.x, this.y, 114, 20, EnumTooltip.TOP_RIGHT, this.content, new IListButtonLogic() { @Override public void actionPerformed(Container container, GuiButton button, ButtonValues values) { - content.getPersistence(listButtonLogic2.getId()).setIndex(0); + content.getPersistence(logic.getId() + (depth + 1)).setIndex(0); container.initButtons(); } @@ -67,11 +78,11 @@ public class ElementClickList extends Element { return list.get(index); } - + @Override public String getDisplayString(ButtonValues values) { - return logic.translate1(values.getObject().getKey()); + return logic.translate(getKeys()); } @Override @@ -88,101 +99,43 @@ public class ElementClickList extends Element @Override public String getId() { - return logic.getId() + 1; + return logic.getId() + depth; } })); - final Node node = this.getValues1().getObject(); - this.logic.consumeKey1(node.getKey()); + Node node = this.getValues().getObject(); + this.logic.consumeKey(this.getKeys()); if(node.getEntries() != null) { - container.add(this.button2 = new GuiButtonList(this.buttonId2, this.x, this.y + 24, 114, 20, EnumTooltip.TOP_RIGHT, this.content, this.listButtonLogic2)); - this.logic.consumeKey2(node.getKey(), node.getEntries().get(this.getValues2().getIndex()).getKey()); + this.child = new ElementClickList(this.x, this.y + 24, node.getEntries(), this.buttonId + 1, this.maxDepth, this.content, this.logic, this); + this.child.initButtons(container); } - else + else if(this.depth < this.maxDepth) { - container.add(this.button2 = new GuiButtonList(this.buttonId2, this.x, this.y + 24, 114, 20, EnumTooltip.TOP_RIGHT, this.content, this.listButtonLogic2)); - this.button2.enabled = false; + GuiButtonWorldHandler button = new GuiButtonWorldHandler(this.buttonId + 1, this.x, this.y + 24, 114, 20, null); + button.enabled = false; + container.add(button); } } - private final IListButtonLogic listButtonLogic2 = new IListButtonLogic() + private String[] getKeys() { - @Override - public void actionPerformed(Container container, GuiButton button, ButtonValues values) - { - container.initButtons(); - } - - @Override - public int getMax() - { - if(getValues1().getObject() != null) - { - return getValues1().getObject().getEntries().size(); - } - - return 0; - } - - @Override - public Node getObject(int index) - { - if(getValues1().getObject().getEntries() != null) - { - return getValues1().getObject().getEntries().get(index); - } - - return null; - } - - @Override - public String getDisplayString(ButtonValues values) - { - if(values.getObject() != null) - { - return logic.translate2(getValues1().getObject().getKey(), values.getObject().getKey()); - } - - return null; - } - - @Override - public String getTooltipString(ButtonValues values) - { - if(getValues1().getObject().getEntries() != null) - { - return values.getObject().getKey() + " (" + (values.getIndex() + 1) + "/" + getValues1().getObject().getEntries().size() + ")"; - } - - return null; - } - - @Override - public String getId() - { - return logic.getId() + 2; - } - }; + return this.getKeys(new String[this.depth]); + } - @Nullable - private ButtonValues getValues1() + private String[] getKeys(String[] keys) { - if(this.button1 != null) - { - return this.content.getPersistence(this.button1.getLogic().getId()); - } - - return null; + keys[this.depth - 1] = this.getValues().getObject().getKey(); + return this.parent != null ? this.parent.getKeys(keys) : keys; } @Nullable - private ButtonValues getValues2() + private ButtonValues getValues() { - if(this.button2 != null) + if(this.button != null) { - return this.content.getPersistence(this.button2.getLogic().getId()); + return this.content.getPersistence(this.button.getLogic().getId()); } return null; @@ -191,15 +144,14 @@ public class ElementClickList extends Element @Override public boolean actionPerformed(Container container, GuiButton button) { - if(button.id == this.buttonId1) + if(button.id == this.buttonId) { - this.button1.actionPerformed(container, button); + this.button.actionPerformed(container, button); return true; } - else if(button.id == this.buttonId2) + else if(this.child != null) { - this.button2.actionPerformed(container, button); - return true; + return this.child.actionPerformed(container, button); } return false; diff --git a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogic.java b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogic.java new file mode 100644 index 0000000..0d5851c --- /dev/null +++ b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogic.java @@ -0,0 +1,10 @@ +package exopandora.worldhandler.gui.content.element.logic; + +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public interface ILogic +{ + String getId(); +} diff --git a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicClickList.java b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicClickList.java index b08b730..7d6311c 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicClickList.java +++ b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicClickList.java @@ -4,21 +4,13 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) -public interface ILogicClickList +public interface ILogicClickList extends ILogic { - void consumeKey1(String key); + String translate(String... keys); + void consumeKey(String... keys); - default void consumeKey2(String key1, String key2) + default void consumeKeyImpl(String... keys) { - this.consumeKey1(key1 + "." + key2); + this.consumeKey(keys[0] + "." + keys[1]); } - - String translate1(String key); - - default String translate2(String key1, String key2) - { - return this.translate1(key2); - } - - String getId(); } diff --git a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicColorMenu.java b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicColorMenu.java index 851aff3..3a41108 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicColorMenu.java +++ b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicColorMenu.java @@ -3,7 +3,11 @@ package exopandora.worldhandler.gui.content.element.logic; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -public interface ILogicColorMenu +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public interface ILogicColorMenu extends ILogic { default Predicate getValidator() { @@ -20,6 +24,7 @@ public interface ILogicColorMenu return true; } + @Override default String getId() { return "color"; diff --git a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicPageList.java b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicPageList.java index be5db0a..4062537 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicPageList.java +++ b/src/main/java/exopandora/worldhandler/gui/content/element/logic/ILogicPageList.java @@ -5,7 +5,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) -public interface ILogicPageList +public interface ILogicPageList extends ILogic { String translate(T key); String getRegistryName(T key); @@ -14,6 +14,4 @@ public interface ILogicPageList void onRegister(int id, int x, int y, int width, int height, String display, String registryKey, boolean enabled, T value, Container container); T convert(K object); - - String getId(); } \ No newline at end of file diff --git a/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java b/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java index 9ba816b..bd3fc34 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java +++ b/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java @@ -48,47 +48,61 @@ public class ContentScoreboardObjectives extends ContentScoreboard if(this.selectedObjective.equals("create")) { - ElementClickList objectives = new ElementClickList(x + 118, y + 24, HELPER.getObjectives(), 7, 8, this, new ILogicClickList() + ElementClickList objectives = new ElementClickList(x + 118, y + 24, HELPER.getObjectives(), 7, 2, this, new ILogicClickList() { @Override - public void consumeKey1(String key) + public void consumeKey(String... keys) { - builderObjectives.setCriteria(key); + if(keys.length > 1) + { + this.consumeKeyImpl(keys); + } + else + { + builderObjectives.setCriteria(keys[0]); + } } @Override - public String translate1(String key) + public String translate(String... keys) { - String format = "gui.worldhandler.scoreboard.objectives.criteria." + key; - String result = I18n.format(format); - - if(result.equals(format)) + if(keys.length > 1) { - ResourceLocation location = new ResourceLocation(key); - - if(Item.REGISTRY.containsKey(location)) - { - result = I18n.format(Item.REGISTRY.getObject(location).getUnlocalizedName() + ".name"); - } - else if(Block.REGISTRY.containsKey(location)) - { - result = Block.REGISTRY.getObject(location).getLocalizedName(); - } - else if(EntityHelper.doesExist(key)) - { - result = I18n.format("entity." + key + ".name"); - } - else if(Arrays.stream(EnumColor.values()).map(EnumColor::getFormat).anyMatch(Predicates.equalTo(key))) - { - result = I18n.format("gui.worldhandler.color." + key); - } - else - { - result = I18n.format(key); - } + return this.translate(keys[1]); + } + else + { + String format = "gui.worldhandler.scoreboard.objectives.criteria." + keys[0]; + String result = I18n.format(format); + + if(result.equals(format)) + { + ResourceLocation location = new ResourceLocation(keys[0]); + + if(Item.REGISTRY.containsKey(location)) + { + result = I18n.format(Item.REGISTRY.getObject(location).getUnlocalizedName() + ".name"); + } + else if(Block.REGISTRY.containsKey(location)) + { + result = Block.REGISTRY.getObject(location).getLocalizedName(); + } + else if(EntityHelper.doesExist(keys[0])) + { + result = I18n.format("entity." + keys[0] + ".name"); + } + else if(Arrays.stream(EnumColor.values()).map(EnumColor::getFormat).anyMatch(Predicates.equalTo(keys[0]))) + { + result = I18n.format("gui.worldhandler.color." + keys[0]); + } + else + { + result = I18n.format(keys[0]); + } + } + + return result; } - - return result; } @Override @@ -102,24 +116,32 @@ public class ContentScoreboardObjectives extends ContentScoreboard } else if(this.selectedObjective.equals("display") || this.selectedObjective.equals("undisplay")) { - ElementClickList slots = new ElementClickList(x + 118, y + 24 + (this.selectedObjective.equals("undisplay") ? -12 : 0), HELPER.getSlots(), 9, 10, this, new ILogicClickList() + ElementClickList slots = new ElementClickList(x + 118, y + 24 + (this.selectedObjective.equals("undisplay") ? -12 : 0), HELPER.getSlots(), 9, 2, this, new ILogicClickList() { @Override - public String translate1(String key) + public String translate(String... keys) { - return I18n.format("gui.worldhandler.scoreboard.slot." + key); + if(keys.length > 1) + { + return I18n.format("gui.worldhandler.color." + keys[1]); + } + else + { + return I18n.format("gui.worldhandler.scoreboard.slot." + keys[0]); + } } @Override - public String translate2(String key1, String key2) + public void consumeKey(String... keys) { - return I18n.format("gui.worldhandler.color." + key2); - } - - @Override - public void consumeKey1(String key) - { - builderObjectives.setSlot(key); + if(keys.length > 1) + { + this.consumeKeyImpl(keys); + } + else + { + builderObjectives.setSlot(keys[0]); + } } @Override diff --git a/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardTeams.java b/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardTeams.java index ca6d344..6753f68 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardTeams.java +++ b/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardTeams.java @@ -47,37 +47,39 @@ public class ContentScoreboardTeams extends ContentScoreboard if(this.selectedTeam.equals("option")) { - ElementClickList options = new ElementClickList(x + 118, y + 24, HELPER.getOptions(), 6, 7, this, new ILogicClickList() + ElementClickList options = new ElementClickList(x + 118, y + 24, HELPER.getOptions(), 6, 2, this, new ILogicClickList() { @Override - public String translate1(String key) + public String translate(String... keys) { - return I18n.format("gui.worldhandler.scoreboard.team.options." + key); - } - - @Override - public String translate2(String key1, String key2) - { - if(Arrays.stream(EnumColor.values()).map(EnumColor::getFormat).anyMatch(Predicates.equalTo(key2))) + if(keys.length > 1) { - return I18n.format("gui.worldhandler.color." + key2); + if(Arrays.stream(EnumColor.values()).map(EnumColor::getFormat).anyMatch(Predicates.equalTo(keys[1]))) + { + return I18n.format("gui.worldhandler.color." + keys[1]); + } + + return I18n.format("gui.worldhandler.scoreboard.team.suboption." + keys[1]); + } + else + { + return I18n.format("gui.worldhandler.scoreboard.team.options." + keys[0]); } - - return I18n.format("gui.worldhandler.scoreboard.team.suboption." + key2); } @Override - public void consumeKey1(String key) + public void consumeKey(String... keys) { - builderTeams.setRule(key); + if(keys.length > 1) + { + builderTeams.setValue(keys[1]); + } + else + { + builderTeams.setRule(keys[0]); + } } - @Override - public void consumeKey2(String key1, String key2) - { - builderTeams.setValue(key2); - } - @Override public String getId() {