From 0c3d90d7fee66b55d8e6d84ff987f2d0e188bf10 Mon Sep 17 00:00:00 2001 From: radfast Date: Sun, 26 Jan 2020 15:12:43 +0000 Subject: [PATCH] Refactor BlockMachines: more common code refactored. --- .../core/blocks/BlockMachine.java | 48 ++++++------------ .../core/blocks/BlockMachine2.java | 47 ++++------------- .../core/blocks/BlockMachine3.java | 48 ++++-------------- .../core/blocks/BlockMachineBase.java | 50 ++++++++++++++++++- .../core/blocks/BlockMachineTiered.java | 49 ++++++------------ 5 files changed, 101 insertions(+), 141 deletions(-) diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine.java b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine.java index 9781cb365a..214e394d00 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine.java @@ -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; @@ -23,7 +21,7 @@ public class BlockMachine extends BlockMachineBase { public static final PropertyEnum 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 @@ -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) { @@ -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(); @@ -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; @@ -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) @@ -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 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); } diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine2.java b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine2.java index b447c75077..ac27cd185f 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine2.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine2.java @@ -10,13 +10,10 @@ 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 { @@ -24,7 +21,7 @@ public class BlockMachine2 extends BlockMachineBase 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"), @@ -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(); @@ -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; @@ -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 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); } diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine3.java b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine3.java index 5762009b47..f291e50ece 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine3.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachine3.java @@ -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 @@ -22,7 +18,7 @@ public class BlockMachine3 extends BlockMachineBase { public static final PropertyEnum 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"); @@ -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(); @@ -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; @@ -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 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); } diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineBase.java b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineBase.java index 13f6482d6e..6da169753b 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineBase.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineBase.java @@ -16,6 +16,8 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; +import net.minecraft.util.IStringSerializable; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; @@ -24,6 +26,8 @@ public abstract class BlockMachineBase extends BlockTileGC implements IShiftDesc { public static final int METADATA_MASK = 0x0c; //Used to select the machine type from metadata public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); + protected EnumMachineBase[] types; + protected EnumMachineBase typeBase; public BlockMachineBase(String assetName) { @@ -31,8 +35,11 @@ public BlockMachineBase(String assetName) this.setHardness(1.0F); this.setSoundType(SoundType.METAL); this.setUnlocalizedName(assetName); + this.initialiseTypes(); } + protected abstract void initialiseTypes(); + @Override public CreativeTabs getCreativeTabToDisplayOn() { @@ -83,14 +90,33 @@ public boolean onSneakUseWrench(World world, BlockPos pos, EntityPlayer entityPl } return false; } - + + @Override + public TileEntity createTileEntity(World world, IBlockState state) + { + int meta = getMetaFromState(state); + EnumMachineBase type = typeBase.fromMetadata(meta); + return type.tileConstructor(); + } + @Override public int damageDropped(IBlockState state) { return getMetaFromState(state) & BlockMachineBase.METADATA_MASK; } - public abstract String getUnlocalizedName(int typenum); + public String getUnlocalizedName(int meta) + { + EnumMachineBase type = typeBase.fromMetadata(meta); + return type.getUnlocalizedName(); + } + + @Override + public String getShiftDescription(int meta) + { + EnumMachineBase type = typeBase.fromMetadata(meta); + return type.getShiftDescription(); + } @Override public boolean showDescription(int meta) @@ -112,4 +138,24 @@ public EnumSortCategoryBlock getCategory(int meta) { return EnumSortCategoryBlock.MACHINE; } + + @Override + public void getSubBlocks(CreativeTabs tab, NonNullList list) + { + for (EnumMachineBase type : types) + list.add(new ItemStack(this, 1, type.getMetadata())); + } + + static interface EnumMachineBase & IStringSerializable> + { + int getMetadata(); + default EnumMachineBase fromMetadata(int metadata) + { + return byMeta(metadata / 4); + } + EnumMachineBase byMeta(int meta); + String getShiftDescription(); + String getUnlocalizedName(); + TileEntity tileConstructor(); + } } diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineTiered.java b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineTiered.java index 5c5b897b69..f1b2320e36 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineTiered.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/blocks/BlockMachineTiered.java @@ -9,12 +9,9 @@ import net.minecraft.block.properties.PropertyInteger; 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.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; @@ -26,7 +23,7 @@ public class BlockMachineTiered extends BlockMachineBase public static final PropertyEnum SIDES = MACHINESIDES_RENDERTYPE.asProperty; public static final PropertyInteger FILL_VALUE = PropertyInteger.create("fill_value", 0, 33); - public enum EnumTieredMachineType implements IStringSerializable + public enum EnumTieredMachineType implements EnumMachineBase, IStringSerializable { STORAGE_MODULE(0, "energy_storage", TileEntityEnergyStorageModule::new, "tile.energy_storage_module_tier1.description", "tile.machine.1"), ELECTRIC_FURNACE(1, "electric_furnace", TileEntityElectricFurnace::new, "tile.electric_furnace_tier1.description", "tile.machine.2"), @@ -48,28 +45,26 @@ public enum EnumTieredMachineType implements IStringSerializable this.blockName = blockName; } + @Override public int getMetadata() { return this.meta * 4; } private final static EnumTieredMachineType[] values = values(); - public static EnumTieredMachineType byMeta(int meta) + @Override + public EnumMachineBase byMeta(int meta) { return values[meta % values.length]; } - public static EnumTieredMachineType getByMetadata(int metadata) - { - return byMeta(metadata / 4); - } - @Override public String getName() { return this.name; } + @Override public TileEntity tileConstructor() { int tier = this.meta / 2 + 1; @@ -82,11 +77,13 @@ private static interface TileConstructor TileEntity create(int tier); } + @Override public String getShiftDescription() { return GCCoreUtil.translate(this.shiftDescriptionKey); } + @Override public String getUnlocalizedName() { return this.blockName; @@ -99,41 +96,25 @@ public BlockMachineTiered(String assetName) } @Override - public TileEntity createTileEntity(World world, IBlockState state) - { - int meta = getMetaFromState(state); - EnumTieredMachineType type = EnumTieredMachineType.getByMetadata(meta); - TileEntity tile = type.tileConstructor(); - tile.setWorld(world); - return tile; - } - - @Override - public void getSubBlocks(CreativeTabs tab, NonNullList list) + protected void initialiseTypes() { - for (EnumTieredMachineType type : EnumTieredMachineType.values) - list.add(new ItemStack(this, 1, type.getMetadata())); + this.types = EnumTieredMachineType.values; + this.typeBase = EnumTieredMachineType.values[0]; } @Override - public String getShiftDescription(int meta) - { - EnumTieredMachineType type = EnumTieredMachineType.getByMetadata(meta); - return type.getShiftDescription(); - } - - @Override - public String getUnlocalizedName(int meta) + public TileEntity createTileEntity(World world, IBlockState state) { - EnumTieredMachineType type = EnumTieredMachineType.getByMetadata(meta); - return type.getUnlocalizedName(); + TileEntity tile = super.createTileEntity(world, state); + tile.setWorld(world); + return tile; } @Override public IBlockState getStateFromMeta(int meta) { EnumFacing enumfacing = EnumFacing.getHorizontal(meta % 4); - EnumTieredMachineType type = EnumTieredMachineType.getByMetadata(meta); + EnumTieredMachineType type = (EnumTieredMachineType) typeBase.fromMetadata(meta); return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(TYPE, type); }