Fix state parsing (#1573)

This commit is contained in:
Nicholas Ignoffo 2019-04-14 08:23:02 -07:00
parent 53b6030ba9
commit f832103386

View file

@ -8,30 +8,31 @@ import net.minecraft.init.Blocks;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
public class StateUtil
{
public static IBlockState parseState(String blockInfo)
{
String[] split = blockInfo.split("\\[");
split[1] = split[1].substring(0, split[1].lastIndexOf("]")); // Make sure brackets are removed from state
public class StateUtil {
public static IBlockState parseState(String state) {
if (state.contains("[")) {
String[] split = state.split("\\[");
split[1] = split[1].substring(0, split[1].lastIndexOf("]")); // Make sure brackets are removed from state
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(split[0])); // Find the block
if (block == Blocks.AIR)
return Blocks.AIR.getDefaultState(); // The block is air, so we're looking at invalid data
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(split[0]));
if (block == Blocks.AIR)
return block.getDefaultState();
BlockStateContainer blockState = block.getBlockState();
IBlockState returnState = blockState.getBaseState();
BlockStateContainer blockState = block.getBlockState();
IBlockState returnState = blockState.getBaseState();
// Force our values into the state
String[] stateValues = split[1].split(","); // Splits up each value
for (String value : stateValues)
{
String[] valueSplit = value.split("="); // Separates property and value
IProperty property = blockState.getProperty(valueSplit[0]);
if (property != null)
returnState = returnState.withProperty(property, (Comparable) property.parseValue(valueSplit[1]).get()); // Force the property into the state
// Force our values into the state
String[] stateValues = split[1].split(","); // Splits up each value
for (String value : stateValues) {
String[] valueSplit = value.split("=");
IProperty property = blockState.getProperty(valueSplit[0]);
if (property != null)
returnState = returnState.withProperty(property, (Comparable) property.parseValue(valueSplit[1]).get());
}
return returnState;
} else {
return ForgeRegistries.BLOCKS.getValue(new ResourceLocation(state)).getDefaultState();
}
return returnState;
}
}