BloodMagic/src/main/java/WayofTime/bloodmagic/block/base/BlockString.java

98 lines
2.9 KiB
Java
Raw Normal View History

2015-11-17 23:52:31 +00:00
package WayofTime.bloodmagic.block.base;
2017-08-16 04:30:48 +00:00
import WayofTime.bloodmagic.block.property.PropertyString;
2015-11-17 23:52:31 +00:00
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
2016-03-18 19:01:58 +00:00
import net.minecraft.block.state.BlockStateContainer;
2015-11-17 23:52:31 +00:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
2017-01-02 05:43:34 +00:00
import net.minecraft.util.NonNullList;
2015-11-17 23:52:31 +00:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.ArrayUtils;
2015-11-17 23:52:31 +00:00
/**
* Creates a block that has multiple meta-based states.
2017-08-16 04:30:48 +00:00
* <p>
* These states will be named after the given string array. Somewhere along the
* way, each value is {@code toLowerCase()}'ed, so the blockstate JSON needs all
* values to be lowercase.
2015-11-17 23:52:31 +00:00
*/
2017-08-16 04:30:48 +00:00
public class BlockString extends Block {
2015-11-17 23:52:31 +00:00
private final int maxMeta;
private final String[] types;
private final PropertyString property;
private final BlockStateContainer realStateContainer;
2015-11-17 23:52:31 +00:00
2017-08-16 04:30:48 +00:00
public BlockString(Material material, String[] values, String propName) {
2015-11-17 23:52:31 +00:00
super(material);
this.maxMeta = values.length;
this.types = values;
2015-11-17 23:52:31 +00:00
this.property = PropertyString.create(propName, values);
this.realStateContainer = createStateContainer();
setDefaultState(getBlockState().getBaseState());
2015-11-17 23:52:31 +00:00
}
2017-08-16 04:30:48 +00:00
public BlockString(Material material, String[] values) {
2015-11-17 23:52:31 +00:00
this(material, values, "type");
}
@Override
2017-08-16 04:30:48 +00:00
protected final BlockStateContainer createBlockState() {
return new BlockStateContainer.Builder(this).build(); // Blank to avoid crashes
2015-11-17 23:52:31 +00:00
}
@Override
2017-08-16 04:30:48 +00:00
public final BlockStateContainer getBlockState() {
return realStateContainer;
2015-11-17 23:52:31 +00:00
}
@Override
2017-08-16 04:30:48 +00:00
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(property, types[meta]);
2015-11-17 23:52:31 +00:00
}
@Override
2017-08-16 04:30:48 +00:00
public int getMetaFromState(IBlockState state) {
return ArrayUtils.indexOf(types, state.getValue(property));
2015-11-17 23:52:31 +00:00
}
@Override
2017-08-16 04:30:48 +00:00
public int damageDropped(IBlockState state) {
return getMetaFromState(state);
2015-11-17 23:52:31 +00:00
}
@SideOnly(Side.CLIENT)
@Override
2017-08-16 04:30:48 +00:00
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> subBlocks) {
for (int i = 0; i < maxMeta; i++)
subBlocks.add(new ItemStack(this, 1, i));
2015-11-17 23:52:31 +00:00
}
2017-08-16 04:30:48 +00:00
protected BlockStateContainer createStateContainer() {
System.out.println("");
BlockStateContainer ctn = new BlockStateContainer.Builder(this).add(property).build();
System.out.println("Number of states: " + ctn.getValidStates().size());
return ctn;
2015-11-17 23:52:31 +00:00
}
public int getMaxMeta() {
return maxMeta;
}
public String[] getTypes() {
return types;
}
public PropertyString getProperty() {
return property;
}
public BlockStateContainer getRealStateContainer() {
return realStateContainer;
}
2015-11-17 23:52:31 +00:00
}