Skip to content

Commit

Permalink
Fix custom array entry class for config GUI being ignored when adding…
Browse files Browse the repository at this point in the history
… new entries (MinecraftForge#3697)
  • Loading branch information
BlayTheNinth authored and LexManos committed Apr 7, 2017
1 parent 9619be4 commit 52409e1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import net.minecraftforge.fml.client.config.ConfigGuiType;
import net.minecraftforge.fml.client.config.DummyConfigElement;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.GuiEditArray;
import net.minecraftforge.fml.client.config.GuiEditArrayEntries;
import net.minecraftforge.fml.client.config.IConfigElement;
import net.minecraftforge.fml.client.config.DummyConfigElement.DummyCategoryElement;
import net.minecraftforge.fml.client.config.DummyConfigElement.DummyListElement;
Expand Down Expand Up @@ -79,7 +81,8 @@ private static List<IConfigElement> getConfigElements()
listsList.add(new DummyListElement("stringListFixed", new String[] {"A", "fixed", "length", "array", "of", "string", "values"}, ConfigGuiType.STRING, "fml.config.sample.stringListFixed", true));
listsList.add(new DummyListElement("stringListMax", new String[] {"An", "array", "of", "string", "values", "with", "a", "max", "length", "of", "15"}, ConfigGuiType.STRING, "fml.config.sample.stringListMax", 15));
listsList.add(new DummyListElement("stringListPattern", new String[] {"Valid", "Not Valid", "Is, Valid", "Comma, Separated, Value"}, ConfigGuiType.STRING, "fml.config.sample.stringListPattern", commaDelimitedPattern));

listsList.add(new DummyListElement("stringListCustom", new Object[0], ConfigGuiType.STRING, "fml.config.sample.stringListCustom").setArrayEntryClass(CustomArrayEntry.class));

list.add(new DummyCategoryElement("lists", "fml.config.sample.ctgy.lists", listsList));

// Strings category
Expand Down Expand Up @@ -112,6 +115,21 @@ public boolean hasConfigGui()
return true;
}

public static class CustomArrayEntry extends GuiEditArrayEntries.StringEntry
{
public CustomArrayEntry(GuiEditArray owningScreen, GuiEditArrayEntries owningEntryList, IConfigElement configElement, Object value)
{
super(owningScreen, owningEntryList, configElement, value);
}

@Override
public void drawEntry(int slotIndex, int x, int y, int listWidth, int slotHeight, int mouseX, int mouseY, boolean isSelected)
{
textFieldValue.setTextColor((int) (Math.random() * 0xFFFFFF));
super.drawEntry(slotIndex, x, y, listWidth, slotHeight, mouseX, mouseY, isSelected);
}
}

@Override
public void initialize(Minecraft minecraftInstance)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraftforge.fml.client.config.GuiConfigEntries.ArrayEntry;
import net.minecraftforge.fml.common.FMLLog;

import org.apache.logging.log4j.Level;
import org.lwjgl.input.Keyboard;

import static net.minecraftforge.fml.client.config.GuiUtils.INVALID;
Expand Down Expand Up @@ -80,8 +81,7 @@ public GuiEditArrayEntries(GuiEditArray parent, Minecraft mc, IConfigElement con
}
catch (Throwable e)
{
FMLLog.severe("There was a critical error instantiating the custom IGuiEditListEntry for property %s.", configElement.getName());
e.printStackTrace();
FMLLog.log(Level.ERROR, e, "There was a critical error instantiating the custom IGuiEditListEntry for property %s.", configElement.getName());
}
}
}
Expand Down Expand Up @@ -132,7 +132,34 @@ protected int getSize()

public void addNewEntry(int index)
{
if (configElement.isList() && configElement.getType() == ConfigGuiType.BOOLEAN)
if (configElement.isList() && configElement.getArrayEntryClass() != null)
{
Class<? extends IArrayEntry> clazz = configElement.getArrayEntryClass();
try
{
Object value;
switch (configElement.getType())
{
case BOOLEAN:
value = true;
break;
case INTEGER:
value = 0;
break;
case DOUBLE:
value = 0.0D;
break;
default:
value = "";
}
listEntries.add(index, clazz.getConstructor(GuiEditArray.class, GuiEditArrayEntries.class, IConfigElement.class, Object.class).newInstance(this.owningGui, this, configElement, value));
}
catch (Throwable e)
{
FMLLog.log(Level.ERROR, e, "There was a critical error instantiating the custom IGuiEditListEntry for property %s.", configElement.getName());
}
}
else if (configElement.isList() && configElement.getType() == ConfigGuiType.BOOLEAN)
listEntries.add(index, new BooleanEntry(this.owningGui, this, this.configElement, true));
else if (configElement.isList() && configElement.getType() == ConfigGuiType.INTEGER)
listEntries.add(index, new IntegerEntry(this.owningGui, this, this.configElement, 0));
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/forge/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fml.config.sample.stringListMax.tooltip=A string list that has a max length of 1
fml.config.sample.stringListMax=Max Length String List
fml.config.sample.stringListPattern.tooltip=A string list that validates each value using a Pattern object.
fml.config.sample.stringListPattern=Pattern Validated String List
fml.config.sample.stringListCustom=Custom Entry List
fml.config.sample.title=This is for playing with the Config GUI behavior. Changes are not saved.

fml.configgui.applyGlobally=Apply globally
Expand Down

0 comments on commit 52409e1

Please sign in to comment.