Skip to content

Instantly share code, notes, and snippets.

@Unh0lyTigg
Created June 22, 2016 15:10
package xyz.unh0lytigg.mods.orangepower.world.gen;
import java.util.*;
import com.google.common.collect.*;
import com.google.gson.*;
import net.minecraft.block.*;
import net.minecraft.block.properties.*;
import net.minecraft.block.state.*;
import net.minecraft.util.*;
public class WorldGenConfigObject {
public String name;
public IBlockState blockStateToPlace;
public Block blockToOverride;
public int minY, maxY;
public int minVeinSize, maxVeinSize;
public int chancesToSpawn;
public List<Integer> enabledDimensions = Lists.newArrayList();
@SuppressWarnings({ "unchecked", "rawtypes" })
public static WorldGenConfigObject createFromJson(JsonElement element) {
if (!element.isJsonObject())
return null;
JsonObject object = element.getAsJsonObject();
WorldGenConfigObject obj = new WorldGenConfigObject();
obj.name = object.get("Name").getAsString();
JsonObject toPlaceObject = object.getAsJsonObject("Place");
JsonObject toPlaceResourceObject = toPlaceObject.getAsJsonObject("Resource");
ResourceLocation toPlaceBlockRegistryLocation = new ResourceLocation(toPlaceResourceObject.get("Domain").getAsString(), toPlaceResourceObject.get("Path").getAsString());
Block toPlaceBlock = Block.REGISTRY.getObject(toPlaceBlockRegistryLocation);
IBlockState state = toPlaceBlock.getDefaultState();
JsonObject toPlacePropertiesObject = object.getAsJsonObject("Properties");
Collection<IProperty<?>> properties = state.getPropertyNames();
for (IProperty<?> prop : properties) {
String propName = prop.getName();
if (!toPlacePropertiesObject.has(propName))
continue;
Class<?> valClass = prop.getValueClass();
if (valClass.equals(Integer.class)) {
IProperty<Integer> intProp = (IProperty<Integer>)prop;
state = state.withProperty(intProp, toPlacePropertiesObject.getAsJsonPrimitive(propName).getAsInt());
} else if (valClass.equals(Boolean.class)) {
IProperty<Boolean> boolProp = (IProperty<Boolean>)prop;
state = state.withProperty(boolProp, toPlacePropertiesObject.getAsJsonPrimitive(propName).getAsBoolean());
} else if (valClass.isEnum() && IStringSerializable.class.isAssignableFrom(valClass)) {
String val = toPlacePropertiesObject.getAsJsonPrimitive(propName).getAsString();
state = enumProp(state, prop, val, getClassOf(prop));
}
}
obj.blockStateToPlace = state;
JsonObject toOverrideObject = object.getAsJsonObject("Override");
JsonObject toOverrideResourceObject = toOverrideObject.getAsJsonObject("Resource");
ResourceLocation toOverrideBlockResourceLocation = new ResourceLocation(toOverrideResourceObject.get("Domain").getAsString(), toOverrideResourceObject.get("Path").getAsString());
obj.blockToOverride = Block.REGISTRY.getObject(toOverrideBlockResourceLocation);
obj.minY = object.getAsJsonPrimitive("MinY").getAsInt();
obj.maxY = object.getAsJsonPrimitive("MaxY").getAsInt();
obj.minVeinSize = object.getAsJsonPrimitive("MinVeinSize").getAsInt();
obj.maxVeinSize = object.getAsJsonPrimitive("MaxVeinSize").getAsInt();
obj.chancesToSpawn = object.getAsJsonPrimitive("ChancesToSpawn").getAsInt();
JsonArray dimensions = object.getAsJsonArray("EnabledDimensions");
return obj;
}
@SuppressWarnings("unchecked")
private static <E extends Enum<E> & IStringSerializable> IBlockState enumProp(IBlockState stateIn, IProperty<?> property, String value, Class<E> clazz) {
IProperty<E> realProp = (IProperty<E>)property;
for (E e : realProp.getAllowedValues()) {
if (e.name().equalsIgnoreCase(value)) {
return stateIn.withProperty(realProp, e);
}
}
throw new IllegalArgumentException("Could not find " + value + " in " + property.getValueClass().getName() + " values allowed by " + stateIn.getBlock().getRegistryName().toString() + "#" + property.getName());
}
@SuppressWarnings("unchecked")
private static <E extends Enum<E> & IStringSerializable> Class<E> getClassOf(IProperty<?> property) {
IProperty<E> real = (IProperty<E>)property;
return real.getValueClass();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment