Skip to content

Commit

Permalink
Refactor BlockMachines: more common code refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
radfast committed Jan 26, 2020
1 parent 6278657 commit 0c3d90d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
Expand All @@ -23,7 +21,7 @@ public class BlockMachine extends BlockMachineBase
{
public static final PropertyEnum<EnumMachineType> TYPE = PropertyEnum.create("type", EnumMachineType.class);

public enum EnumMachineType implements IStringSerializable
public enum EnumMachineType implements EnumMachineBase, IStringSerializable
{
COAL_GENERATOR(0, "coal_generator", TileEntityCoalGenerator::new, "tile.coal_generator.description", "tile.machine.0"),
COMPRESSOR(3, "ingot_compressor", TileEntityIngotCompressor::new, "tile.compressor.description", "tile.machine.3"); // 3 for backwards compatibility
Expand All @@ -43,13 +41,15 @@ public enum EnumMachineType implements IStringSerializable
this.blockName = blockName;
}

@Override
public int getMetadata()
{
return this.meta * 4;
}

private final static EnumMachineType[] values = values();
public static EnumMachineType byMeta(int meta)
@Override
public EnumMachineType byMeta(int meta)
{
switch (meta)
{
Expand All @@ -60,17 +60,13 @@ public static EnumMachineType byMeta(int meta)
}
}

public static EnumMachineType getByMetadata(int meta)
{
return byMeta(meta / 4);
}

@Override
public String getName()
{
return this.name;
}

@Override
public TileEntity tileConstructor()
{
return this.tile.create();
Expand All @@ -82,11 +78,13 @@ private static interface TileConstructor
TileEntity create();
}

@Override
public String getShiftDescription()
{
return GCCoreUtil.translate(this.shiftDescriptionKey);
}

@Override
public String getUnlocalizedName()
{
return this.blockName;
Expand All @@ -98,6 +96,13 @@ public BlockMachine(String assetName)
super(assetName);
}

@Override
protected void initialiseTypes()
{
this.types = EnumMachineType.values;
this.typeBase = EnumMachineType.values[0];
}

@SideOnly(Side.CLIENT)
@Override
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
Expand Down Expand Up @@ -150,36 +155,15 @@ else if (meta == 8)
{
return new TileEntityElectricFurnace(); //Legacy code in case a block in game not yet converted to BlockMachineTiered
}
EnumMachineType type = EnumMachineType.getByMetadata(meta);
EnumMachineBase type = typeBase.fromMetadata(meta);
return type.tileConstructor();
}

@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list)
{
for (EnumMachineType type : EnumMachineType.values)
list.add(new ItemStack(this, 1, type.getMetadata()));
}

@Override
public String getShiftDescription(int meta)
{
EnumMachineType type = EnumMachineType.getByMetadata(meta);
return type.getShiftDescription();
}

@Override
public String getUnlocalizedName(int meta)
{
EnumMachineType type = EnumMachineType.getByMetadata(meta);
return type.getUnlocalizedName();
}

@Override
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getHorizontal(meta % 4);
EnumMachineType type = EnumMachineType.getByMetadata(meta);
EnumMachineType type = (EnumMachineType) typeBase.fromMetadata(meta);
return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(TYPE, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockMachine2 extends BlockMachineBase
{
public static final PropertyEnum<EnumMachineExtendedType> TYPE = PropertyEnum.create("type", EnumMachineExtendedType.class);
public static IMachineSidesProperties MACHINESIDES_RENDERTYPE = IMachineSidesProperties.TWOFACES_HORIZ;
public static final PropertyEnum SIDES = MACHINESIDES_RENDERTYPE.asProperty;

public enum EnumMachineExtendedType implements IStringSerializable
public enum EnumMachineExtendedType implements EnumMachineBase, IStringSerializable
{
ELECTRIC_COMPRESSOR(0, "electric_compressor", TileEntityElectricIngotCompressor::new, "tile.compressor_electric.description", "tile.machine2.4"),
CIRCUIT_FABRICATOR(1, "circuit_fabricator", TileEntityCircuitFabricator::new, "tile.circuit_fabricator.description", "tile.machine2.5"),
Expand All @@ -46,28 +43,26 @@ public enum EnumMachineExtendedType implements IStringSerializable
this.blockName = blockName;
}

@Override
public int getMetadata()
{
return this.meta * 4;
}

private final static EnumMachineExtendedType[] values = values();
public static EnumMachineExtendedType byMeta(int meta)
@Override
public EnumMachineExtendedType byMeta(int meta)
{
return values[meta % values.length];
}

public static EnumMachineExtendedType getByMetadata(int metadata)
{
return byMeta(metadata / 4);
}

@Override
public String getName()
{
return this.name;
}

@Override
public TileEntity tileConstructor()
{
return this.tile.create();
Expand All @@ -79,11 +74,13 @@ private static interface TileConstructor
TileEntity create();
}

@Override
public String getShiftDescription()
{
return GCCoreUtil.translate(this.shiftDescriptionKey);
}

@Override
public String getUnlocalizedName()
{
return this.blockName;
Expand All @@ -96,39 +93,17 @@ public BlockMachine2(String assetName)
}

@Override
public TileEntity createTileEntity(World world, IBlockState state)
{
int meta = getMetaFromState(state);
EnumMachineExtendedType type = EnumMachineExtendedType.getByMetadata(meta);
return type.tileConstructor();
}

@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list)
{
for (EnumMachineExtendedType type : EnumMachineExtendedType.values)
list.add(new ItemStack(this, 1, type.getMetadata()));
}

@Override
public String getShiftDescription(int meta)
{
EnumMachineExtendedType type = EnumMachineExtendedType.getByMetadata(meta);
return type.getShiftDescription();
}

@Override
public String getUnlocalizedName(int meta)
protected void initialiseTypes()
{
EnumMachineExtendedType type = EnumMachineExtendedType.getByMetadata(meta);
return type.getUnlocalizedName();
this.types = EnumMachineExtendedType.values;
this.typeBase = EnumMachineExtendedType.values[0];
}

@Override
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getHorizontal(meta % 4);
EnumMachineExtendedType type = EnumMachineExtendedType.getByMetadata(meta);
EnumMachineExtendedType type = (EnumMachineExtendedType) typeBase.fromMetadata(meta);
return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(TYPE, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;

/**
* A block for several types of Galacticraft machine
Expand All @@ -22,7 +18,7 @@ public class BlockMachine3 extends BlockMachineBase
{
public static final PropertyEnum<EnumMachineBuildingType> TYPE = PropertyEnum.create("type", EnumMachineBuildingType.class);

public enum EnumMachineBuildingType implements IStringSerializable
public enum EnumMachineBuildingType implements EnumMachineBase, IStringSerializable
{
PAINTER(0, "painter", TileEntityPainter::new, "tile.painter.description", "tile.machine3.9");

Expand All @@ -41,28 +37,26 @@ public enum EnumMachineBuildingType implements IStringSerializable
this.blockName = blockName;
}

@Override
public int getMetadata()
{
return this.meta * 4;
}

private final static EnumMachineBuildingType[] values = values();
public static EnumMachineBuildingType byMeta(int meta)
@Override
public EnumMachineBuildingType byMeta(int meta)
{
return values[meta % values.length];
}

public static EnumMachineBuildingType getByMetadata(int metadata)
{
return byMeta(metadata / 4);
}

@Override
public String getName()
{
return this.name;
}

@Override
public TileEntity tileConstructor()
{
return this.tile.create();
Expand All @@ -74,11 +68,13 @@ private static interface TileConstructor
TileEntity create();
}

@Override
public String getShiftDescription()
{
return GCCoreUtil.translate(this.shiftDescriptionKey);
}

@Override
public String getUnlocalizedName()
{
return this.blockName;
Expand All @@ -91,39 +87,17 @@ public BlockMachine3(String assetName)
}

@Override
public TileEntity createTileEntity(World world, IBlockState state)
{
int meta = getMetaFromState(state);
EnumMachineBuildingType type = EnumMachineBuildingType.getByMetadata(meta);
return type.tileConstructor();
}

@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list)
{
for (EnumMachineBuildingType type : EnumMachineBuildingType.values)
list.add(new ItemStack(this, 1, type.getMetadata()));
}

@Override
public String getShiftDescription(int meta)
{
EnumMachineBuildingType type = EnumMachineBuildingType.getByMetadata(meta);
return type.getShiftDescription();
}

@Override
public String getUnlocalizedName(int meta)
protected void initialiseTypes()
{
EnumMachineBuildingType type = EnumMachineBuildingType.getByMetadata(meta);
return type.getUnlocalizedName();
this.types = EnumMachineBuildingType.values;
this.typeBase = EnumMachineBuildingType.values[0];
}

@Override
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getHorizontal(meta % 4);
EnumMachineBuildingType type = EnumMachineBuildingType.getByMetadata(meta);
EnumMachineBuildingType type = (EnumMachineBuildingType) typeBase.fromMetadata(meta);
return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(TYPE, type);
}

Expand Down
Loading

0 comments on commit 0c3d90d

Please sign in to comment.