Refactoring

This commit is contained in:
Marcel Konrad
2018-06-23 17:39:11 +02:00
parent 5716966c51
commit 307aafe010
8 changed files with 163 additions and 172 deletions

View File

@@ -24,11 +24,21 @@ public class ButtonValues<T>
this.index++; this.index++;
} }
public void incrementIndexBy(int amount)
{
this.index += amount;
}
public void decrementIndex() public void decrementIndex()
{ {
this.index--; this.index--;
} }
public void decrementIndexBy(int amount)
{
this.index -= amount;
}
public T getObject() public T getObject()
{ {
return this.object; return this.object;

View File

@@ -6,6 +6,7 @@ import javax.annotation.Nullable;
import exopandora.worldhandler.gui.button.EnumTooltip; import exopandora.worldhandler.gui.button.EnumTooltip;
import exopandora.worldhandler.gui.button.GuiButtonList; import exopandora.worldhandler.gui.button.GuiButtonList;
import exopandora.worldhandler.gui.button.GuiButtonWorldHandler;
import exopandora.worldhandler.gui.button.logic.IListButtonLogic; import exopandora.worldhandler.gui.button.logic.IListButtonLogic;
import exopandora.worldhandler.gui.button.persistence.ButtonValues; import exopandora.worldhandler.gui.button.persistence.ButtonValues;
import exopandora.worldhandler.gui.container.Container; import exopandora.worldhandler.gui.container.Container;
@@ -20,22 +21,32 @@ import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public class ElementClickList extends Element public class ElementClickList extends Element
{ {
private final int buttonId1; private final int buttonId;
private final int buttonId2;
private final List<Node> list; private final List<Node> list;
private final ILogicClickList logic; private final ILogicClickList logic;
private GuiButtonList button1;
private GuiButtonList button2;
private final Content content; private final Content content;
private final ElementClickList parent;
private final int depth;
private final int maxDepth;
public ElementClickList(int x, int y, List<Node> list, int buttonId1, int buttonId2, Content content, ILogicClickList logic) private GuiButtonList button;
private ElementClickList child;
public ElementClickList(int x, int y, List<Node> 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<Node> list, int buttonId, int maxDepth, Content content, ILogicClickList logic, ElementClickList parent)
{ {
super(x, y); super(x, y);
this.list = list; this.list = list;
this.buttonId1 = buttonId1; this.buttonId = buttonId;
this.buttonId2 = buttonId2;
this.logic = logic; this.logic = logic;
this.content = content; this.content = content;
this.parent = parent;
this.maxDepth = maxDepth;
this.depth = this.parent != null ? this.parent.depth + 1 : 1;
} }
@Override @Override
@@ -47,12 +58,12 @@ public class ElementClickList extends Element
@Override @Override
public void initButtons(Container container) 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<Node>() container.add(this.button = new GuiButtonList(this.buttonId, this.x, this.y, 114, 20, EnumTooltip.TOP_RIGHT, this.content, new IListButtonLogic<Node>()
{ {
@Override @Override
public void actionPerformed(Container container, GuiButton button, ButtonValues<Node> values) public void actionPerformed(Container container, GuiButton button, ButtonValues<Node> values)
{ {
content.getPersistence(listButtonLogic2.getId()).setIndex(0); content.getPersistence(logic.getId() + (depth + 1)).setIndex(0);
container.initButtons(); container.initButtons();
} }
@@ -67,11 +78,11 @@ public class ElementClickList extends Element
{ {
return list.get(index); return list.get(index);
} }
@Override @Override
public String getDisplayString(ButtonValues<Node> values) public String getDisplayString(ButtonValues<Node> values)
{ {
return logic.translate1(values.getObject().getKey()); return logic.translate(getKeys());
} }
@Override @Override
@@ -88,101 +99,43 @@ public class ElementClickList extends Element
@Override @Override
public String getId() public String getId()
{ {
return logic.getId() + 1; return logic.getId() + depth;
} }
})); }));
final Node node = this.getValues1().getObject(); Node node = this.getValues().getObject();
this.logic.consumeKey1(node.getKey()); this.logic.consumeKey(this.getKeys());
if(node.getEntries() != null) 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.child = new ElementClickList(this.x, this.y + 24, node.getEntries(), this.buttonId + 1, this.maxDepth, this.content, this.logic, this);
this.logic.consumeKey2(node.getKey(), node.getEntries().get(this.getValues2().getIndex()).getKey()); 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)); GuiButtonWorldHandler button = new GuiButtonWorldHandler(this.buttonId + 1, this.x, this.y + 24, 114, 20, null);
this.button2.enabled = false; button.enabled = false;
container.add(button);
} }
} }
private final IListButtonLogic<Node> listButtonLogic2 = new IListButtonLogic<Node>() private String[] getKeys()
{ {
@Override return this.getKeys(new String[this.depth]);
public void actionPerformed(Container container, GuiButton button, ButtonValues<Node> 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<Node> values)
{
if(values.getObject() != null)
{
return logic.translate2(getValues1().getObject().getKey(), values.getObject().getKey());
}
return null;
}
@Override
public String getTooltipString(ButtonValues<Node> 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;
}
};
@Nullable private String[] getKeys(String[] keys)
private ButtonValues<Node> getValues1()
{ {
if(this.button1 != null) keys[this.depth - 1] = this.getValues().getObject().getKey();
{ return this.parent != null ? this.parent.getKeys(keys) : keys;
return this.content.<Node>getPersistence(this.button1.getLogic().getId());
}
return null;
} }
@Nullable @Nullable
private ButtonValues<Node> getValues2() private ButtonValues<Node> getValues()
{ {
if(this.button2 != null) if(this.button != null)
{ {
return this.content.<Node>getPersistence(this.button2.getLogic().getId()); return this.content.<Node>getPersistence(this.button.getLogic().getId());
} }
return null; return null;
@@ -191,15 +144,14 @@ public class ElementClickList extends Element
@Override @Override
public boolean actionPerformed(Container container, GuiButton button) 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; return true;
} }
else if(button.id == this.buttonId2) else if(this.child != null)
{ {
this.button2.actionPerformed(container, button); return this.child.actionPerformed(container, button);
return true;
} }
return false; return false;

View File

@@ -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();
}

View File

@@ -4,21 +4,13 @@ import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT) @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();
} }

View File

@@ -3,7 +3,11 @@ package exopandora.worldhandler.gui.content.element.logic;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; 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<String> getValidator() default Predicate<String> getValidator()
{ {
@@ -20,6 +24,7 @@ public interface ILogicColorMenu
return true; return true;
} }
@Override
default String getId() default String getId()
{ {
return "color"; return "color";

View File

@@ -5,7 +5,7 @@ import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public interface ILogicPageList<T, K> public interface ILogicPageList<T, K> extends ILogic
{ {
String translate(T key); String translate(T key);
String getRegistryName(T key); String getRegistryName(T key);
@@ -14,6 +14,4 @@ public interface ILogicPageList<T, K>
void onRegister(int id, int x, int y, int width, int height, String display, String registryKey, boolean enabled, T value, Container container); 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); T convert(K object);
String getId();
} }

View File

@@ -48,47 +48,61 @@ public class ContentScoreboardObjectives extends ContentScoreboard
if(this.selectedObjective.equals("create")) 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 @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 @Override
public String translate1(String key) public String translate(String... keys)
{ {
String format = "gui.worldhandler.scoreboard.objectives.criteria." + key; if(keys.length > 1)
String result = I18n.format(format);
if(result.equals(format))
{ {
ResourceLocation location = new ResourceLocation(key); return this.translate(keys[1]);
}
if(Item.REGISTRY.containsKey(location)) else
{ {
result = I18n.format(Item.REGISTRY.getObject(location).getUnlocalizedName() + ".name"); String format = "gui.worldhandler.scoreboard.objectives.criteria." + keys[0];
} String result = I18n.format(format);
else if(Block.REGISTRY.containsKey(location))
{ if(result.equals(format))
result = Block.REGISTRY.getObject(location).getLocalizedName(); {
} ResourceLocation location = new ResourceLocation(keys[0]);
else if(EntityHelper.doesExist(key))
{ if(Item.REGISTRY.containsKey(location))
result = I18n.format("entity." + key + ".name"); {
} result = I18n.format(Item.REGISTRY.getObject(location).getUnlocalizedName() + ".name");
else if(Arrays.stream(EnumColor.values()).map(EnumColor::getFormat).anyMatch(Predicates.equalTo(key))) }
{ else if(Block.REGISTRY.containsKey(location))
result = I18n.format("gui.worldhandler.color." + key); {
} result = Block.REGISTRY.getObject(location).getLocalizedName();
else }
{ else if(EntityHelper.doesExist(keys[0]))
result = I18n.format(key); {
} 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 @Override
@@ -102,24 +116,32 @@ public class ContentScoreboardObjectives extends ContentScoreboard
} }
else if(this.selectedObjective.equals("display") || this.selectedObjective.equals("undisplay")) 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 @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 @Override
public String translate2(String key1, String key2) public void consumeKey(String... keys)
{ {
return I18n.format("gui.worldhandler.color." + key2); if(keys.length > 1)
} {
this.consumeKeyImpl(keys);
@Override }
public void consumeKey1(String key) else
{ {
builderObjectives.setSlot(key); builderObjectives.setSlot(keys[0]);
}
} }
@Override @Override

View File

@@ -47,37 +47,39 @@ public class ContentScoreboardTeams extends ContentScoreboard
if(this.selectedTeam.equals("option")) 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 @Override
public String translate1(String key) public String translate(String... keys)
{ {
return I18n.format("gui.worldhandler.scoreboard.team.options." + key); if(keys.length > 1)
}
@Override
public String translate2(String key1, String key2)
{
if(Arrays.stream(EnumColor.values()).map(EnumColor::getFormat).anyMatch(Predicates.equalTo(key2)))
{ {
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 @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 @Override
public String getId() public String getId()
{ {