Rewrite base blocks to be less weird

Added base ItemBlock classes for each base type (to be used later)
This commit is contained in:
Nicholas Ignoffo 2016-10-15 09:21:30 -07:00
parent ea43fbce7d
commit 3e0f3f5aa1
14 changed files with 265 additions and 209 deletions

View file

@ -1,28 +1,19 @@
package WayofTime.bloodmagic.block.base;
import java.util.Arrays;
import java.util.List;
import lombok.Getter;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import WayofTime.bloodmagic.block.property.PropertyString;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import WayofTime.bloodmagic.block.property.PropertyString;
import WayofTime.bloodmagic.block.property.UnlistedPropertyString;
import org.apache.commons.lang3.ArrayUtils;
/**
* Creates a block that has multiple meta-based states.
@ -30,30 +21,25 @@ import WayofTime.bloodmagic.block.property.UnlistedPropertyString;
* 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.
*
* For {@link net.minecraft.tileentity.TileEntity}'s, use
* {@link BlockStringContainer}.
*/
@Getter
public class BlockString extends Block
{
private final int maxMeta;
private final List<String> values;
protected final PropertyString stringProp;
protected final IUnlistedProperty unlistedStringProp;
private final BlockStateContainer realBlockState;
private final String[] types;
private final PropertyString property;
private final BlockStateContainer realStateContainer;
public BlockString(Material material, String[] values, String propName)
{
super(material);
this.maxMeta = values.length - 1;
this.values = Arrays.asList(values);
this.maxMeta = values.length;
this.types = values;
this.stringProp = PropertyString.create(propName, values);
this.unlistedStringProp = new UnlistedPropertyString(values, propName);
this.realBlockState = createRealBlockState();
setupStates();
this.property = PropertyString.create(propName, values);
this.realStateContainer = createStateContainer();
setDefaultState(getBlockState().getBaseState());
}
public BlockString(Material material, String[] values)
@ -62,60 +48,38 @@ public class BlockString extends Block
}
@Override
public IBlockState getStateFromMeta(int meta)
{
return getBlockState().getBaseState().withProperty(stringProp, values.get(meta));
protected final BlockStateContainer createBlockState() {
return new BlockStateContainer.Builder(this).build(); // Blank to avoid crashes
}
@Override
public int getMetaFromState(IBlockState state)
{
return values.indexOf(String.valueOf(state.getValue(stringProp)));
public final BlockStateContainer getBlockState() {
return realStateContainer;
}
@Override
public int damageDropped(IBlockState state)
{
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(property, types[meta]);
}
@Override
public int getMetaFromState(IBlockState state) {
return ArrayUtils.indexOf(types, state.getValue(property));
}
@Override
public int damageDropped(IBlockState state) {
return getMetaFromState(state);
}
@Override
public BlockStateContainer getBlockState()
{
return this.realBlockState;
}
@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player)
{
return new ItemStack(this, 1, this.getMetaFromState(world.getBlockState(pos)));
}
@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(Item item, CreativeTabs creativeTabs, List<ItemStack> list)
{
for (int i = 0; i < maxMeta + 1; i++)
list.add(new ItemStack(this, 1, i));
@Override
public void getSubBlocks(Item item, CreativeTabs tab, List<ItemStack> subBlocks) {
for (int i = 0; i < maxMeta; i++)
subBlocks.add(new ItemStack(item, 1, i));
}
protected void setupStates()
{
this.setDefaultState(getExtendedBlockState().withProperty(unlistedStringProp, values.get(0)).withProperty(stringProp, values.get(0)));
}
public ExtendedBlockState getBaseExtendedState()
{
return (ExtendedBlockState) this.getBlockState();
}
public IExtendedBlockState getExtendedBlockState()
{
return (IExtendedBlockState) this.getBaseExtendedState().getBaseState();
}
protected BlockStateContainer createRealBlockState()
{
return new ExtendedBlockState(this, new IProperty[] { stringProp }, new IUnlistedProperty[] { unlistedStringProp });
protected BlockStateContainer createStateContainer() {
return new BlockStateContainer.Builder(this).add(property).build();
}
}