Skip to content

Instantly share code, notes, and snippets.

@StillManic
Last active January 3, 2016 00:55
Show Gist options
  • Save StillManic/5deeaebb22ec0e3cfc19 to your computer and use it in GitHub Desktop.
Save StillManic/5deeaebb22ec0e3cfc19 to your computer and use it in GitHub Desktop.
public static class GroupConfigHandler
{
public Map<String, GroupConfig> configMap = Maps.newHashMap();
public ImmutableList<String> groupNames;
private GroupConfigHandler() {}
public GroupConfigHandler duplicate()
{
GroupConfigHandler ret = new GroupConfigHandler();
ret.configMap = Maps.newHashMap(this.configMap);
ret.groupNames = ImmutableList.copyOf(this.groupNames);
return ret;
}
private void setGroupNameList(ImmutableList<String> groupNames)
{
this.groupNames = groupNames;
}
public GroupConfig getConfig(String configName)
{
return this.configMap.get(configName);
}
public ImmutableMap<String, GroupConfig> getConfigMap()
{
return ImmutableMap.copyOf(this.configMap);
}
public GroupConfig getMergedConfigs(List<String> configsToMerge)
{
if (configsToMerge == null || configsToMerge.isEmpty()) return null;
if (configsToMerge.size() == 1) return this.configMap.get(configsToMerge.get(0));
GroupConfigBuilder builder = new GroupConfigBuilder(this.groupNames);
return builder.build();
}
public void process(JsonObject jsonObject)
{
GroupConfigBuilder builder = new GroupConfigBuilder(this.groupNames);
for (Entry<String, JsonElement> e : jsonObject.entrySet())
{
builder.startNew(e.getKey());
JsonObject configObj = e.getValue().getAsJsonObject();
String[] show = new Gson().fromJson(configObj.getAsJsonArray("show"), String[].class);
String[] hide = new Gson().fromJson(configObj.getAsJsonArray("hide"), String[].class);
if (hide == null)
{
builder.hideAll(ArrayUtils.nullToEmpty(show));
}
else if (show == null)
{
builder.showAll(hide);
}
else
{
ArrayUtils.removeElements(show, hide);
builder.hideAll(show);
}
this.configMap.put(builder.name, builder.build());
}
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
for (Entry<String, GroupConfig> e : this.configMap.entrySet())
{
builder.append(String.format("%n%s%n", e.getValue()));
}
return builder.toString();
}
}
public static class GroupConfig
{
public static final String DEFAULT_CONFIG_NAME = "OBJModel.Default.Config.Key";
private String name = DEFAULT_CONFIG_NAME;
private Map<String, Boolean> visMap;
protected GroupConfig(String name, Map<String, Boolean> visMap)
{
this.name = name;
this.visMap = visMap;
}
public String getName()
{
return this.name;
}
public ImmutableMap<String, Boolean> getVisMap()
{
return ImmutableMap.copyOf(this.visMap);
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder(String.format("Name: %s%n", this.name));
builder.append("[ ");
for (Iterator<Entry<String, Boolean>> iterator = this.visMap.entrySet().iterator(); iterator.hasNext();)
{
Entry<String, Boolean> e = iterator.next();
builder.append(String.format("%s: %b%s", e.getKey(), e.getValue(), iterator.hasNext() ? ", " : " ]"));
}
return builder.toString();
}
}
private static class GroupConfigBuilder
{
private String name;
private Map<String, Boolean> visMap = Maps.newHashMap();
private GroupConfigBuilder(ImmutableList<String> groupNames)
{
initMap(groupNames);
}
private void initMap(ImmutableList<String> groupNames)
{
for (String s : groupNames)
{
this.visMap.put(s, true);
}
}
private void resetMap()
{
for (Entry<String, Boolean> e : this.visMap.entrySet())
{
e.setValue(true);
}
}
public GroupConfigBuilder startNew(String configName)
{
this.name = configName;
this.resetMap();
return this;
}
public GroupConfigBuilder show(String... names)
{
if (names == null || names[0] == null) return this;
for (String name : names)
{
if (this.visMap.containsKey(name))
{
this.visMap.put(name, true);
}
}
return this;
}
public GroupConfigBuilder showAll(String... except)
{
for (Entry<String, Boolean> e : this.visMap.entrySet())
{
e.setValue(!Arrays.asList(except).contains(e.getKey()));
}
return this;
}
public GroupConfigBuilder hide(String... names)
{
if (names == null || names.length == 0 || names[0] == null) return this;
for (String name : names)
{
if (this.visMap.containsKey(name))
{
this.visMap.put(name, false);
}
}
return this;
}
public GroupConfigBuilder hideAll(String... except)
{
for (Entry<String, Boolean> e : this.visMap.entrySet())
{
e.setValue(Arrays.asList(except).contains(e.getKey()));
}
return this;
}
public GroupConfig build()
{
GroupConfig ret = new GroupConfig(this.name, Maps.newHashMap(this.visMap));
this.resetMap();
return ret;
}
}
{
"forge_marker": 1,
"defaults": {
"textures": {},
"model": "forgedebugmodelloaderregistry:group_test.obj",
"transform": "forge:default-block",
"custom": {
"groupConfigs": {
"center": { "show": ["center"] },
"up": { "show": ["up"] },
"down": { "show": ["down"] },
"north": { "show": ["north"] },
"south": { "show": ["south"] },
"east": { "show": ["east"] },
"west": { "show": ["west"] }
}
}
},
"variants": {
"normal": [{}],
"inventory": [{}],
"is_white": {
"true": {},
"false": {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment