Skip to content

Commit

Permalink
Refactor BlockMachines: refactor Mars Tier2 machines.
Browse files Browse the repository at this point in the history
  • Loading branch information
radfast committed Jan 26, 2020
1 parent 0c3d90d commit c5a06b6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 278 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package micdoodle8.mods.galacticraft.core.blocks;

import micdoodle8.mods.galacticraft.core.tile.TileEntityCoalGenerator;
import micdoodle8.mods.galacticraft.core.tile.TileEntityElectricFurnace;
import micdoodle8.mods.galacticraft.core.tile.TileEntityEnergyStorageModule;
import micdoodle8.mods.galacticraft.core.tile.TileEntityIngotCompressor;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
Expand All @@ -24,7 +21,7 @@ public class BlockMachine extends BlockMachineBase
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
COMPRESSOR(12, "ingot_compressor", TileEntityIngotCompressor::new, "tile.compressor.description", "tile.machine.3"); // 3 for backwards compatibility

private final int meta;
private final String name;
Expand All @@ -44,14 +41,14 @@ public enum EnumMachineType implements EnumMachineBase, IStringSerializable
@Override
public int getMetadata()
{
return this.meta * 4;
return this.meta;
}

private final static EnumMachineType[] values = values();
@Override
public EnumMachineType byMeta(int meta)
public EnumMachineType fromMetadata(int meta)
{
switch (meta)
switch (meta / 4)
{
case 3:
return COMPRESSOR;
Expand Down Expand Up @@ -79,9 +76,9 @@ private static interface TileConstructor
}

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

@Override
Expand Down Expand Up @@ -143,22 +140,6 @@ public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos,
}
}

@Override
public TileEntity createTileEntity(World world, IBlockState state)
{
int meta = getMetaFromState(state) & BlockMachineBase.METADATA_MASK;
if (meta == 4)
{
return new TileEntityEnergyStorageModule(); //Legacy code in case a block in game not yet converted to BlockMachineTiered
}
else if (meta == 8)
{
return new TileEntityElectricFurnace(); //Legacy code in case a block in game not yet converted to BlockMachineTiered
}
EnumMachineBase type = typeBase.fromMetadata(meta);
return type.tileConstructor();
}

@Override
public IBlockState getStateFromMeta(int meta)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import micdoodle8.mods.galacticraft.core.tile.TileEntityDeconstructor;
import micdoodle8.mods.galacticraft.core.tile.TileEntityElectricIngotCompressor;
import micdoodle8.mods.galacticraft.core.tile.TileEntityOxygenStorageModule;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
Expand All @@ -24,9 +23,9 @@ public class BlockMachine2 extends BlockMachineBase
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"),
OXYGEN_STORAGE(2, "oxygen_storage", TileEntityOxygenStorageModule::new, "tile.oxygen_storage_module.description", "tile.machine2.6"),
DECONSTRUCTOR(3, "deconstructor", TileEntityDeconstructor::new, "tile.deconstructor.description", "tile.machine2.10");
CIRCUIT_FABRICATOR(4, "circuit_fabricator", TileEntityCircuitFabricator::new, "tile.circuit_fabricator.description", "tile.machine2.5"),
OXYGEN_STORAGE(8, "oxygen_storage", TileEntityOxygenStorageModule::new, "tile.oxygen_storage_module.description", "tile.machine2.6"),
DECONSTRUCTOR(12, "deconstructor", TileEntityDeconstructor::new, "tile.deconstructor.description", "tile.machine2.10");

private final int meta;
private final String name;
Expand All @@ -46,14 +45,14 @@ public enum EnumMachineExtendedType implements EnumMachineBase, IStringSerializa
@Override
public int getMetadata()
{
return this.meta * 4;
return this.meta;
}

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

@Override
Expand All @@ -75,9 +74,9 @@ private static interface TileConstructor
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package micdoodle8.mods.galacticraft.core.blocks;

import micdoodle8.mods.galacticraft.core.tile.TileEntityPainter;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -40,14 +39,14 @@ public enum EnumMachineBuildingType implements EnumMachineBase, IStringSerializa
@Override
public int getMetadata()
{
return this.meta * 4;
return this.meta;
}

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

@Override
Expand All @@ -69,9 +68,9 @@ private static interface TileConstructor
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import micdoodle8.mods.galacticraft.core.items.IShiftDescription;
import micdoodle8.mods.galacticraft.core.tile.IMachineSides;
import micdoodle8.mods.galacticraft.core.util.EnumSortCategoryBlock;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.IBlockState;
Expand All @@ -21,6 +22,8 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class BlockMachineBase extends BlockTileGC implements IShiftDescription, ISortableBlock
{
Expand All @@ -41,6 +44,7 @@ public BlockMachineBase(String assetName)
protected abstract void initialiseTypes();

@Override
@SideOnly(Side.CLIENT)
public CreativeTabs getCreativeTabToDisplayOn()
{
return GalacticraftCore.galacticraftBlocksTab;
Expand Down Expand Up @@ -115,7 +119,7 @@ public String getUnlocalizedName(int meta)
public String getShiftDescription(int meta)
{
EnumMachineBase type = typeBase.fromMetadata(meta);
return type.getShiftDescription();
return GCCoreUtil.translate(type.getShiftDescriptionKey());
}

@Override
Expand Down Expand Up @@ -146,15 +150,11 @@ public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list)
list.add(new ItemStack(this, 1, type.getMetadata()));
}

static interface EnumMachineBase <T extends Enum<T> & IStringSerializable>
public static interface EnumMachineBase <T extends Enum<T> & IStringSerializable>
{
int getMetadata();
default EnumMachineBase fromMetadata(int metadata)
{
return byMeta(metadata / 4);
}
EnumMachineBase byMeta(int meta);
String getShiftDescription();
EnumMachineBase fromMetadata(int meta);
String getShiftDescriptionKey();
String getUnlocalizedName();
TileEntity tileConstructor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import micdoodle8.mods.galacticraft.core.tile.IMachineSidesProperties;
import micdoodle8.mods.galacticraft.core.tile.TileEntityElectricFurnace;
import micdoodle8.mods.galacticraft.core.tile.TileEntityEnergyStorageModule;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockStateContainer;
Expand All @@ -26,9 +25,9 @@ public class BlockMachineTiered extends BlockMachineBase
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"),
STORAGE_CLUSTER(2, "cluster_storage", TileEntityEnergyStorageModule::new, "tile.energy_storage_module_tier2.description", "tile.machine.8"),
ARC_FURNACE(3, "arc_furnace", TileEntityElectricFurnace::new, "tile.electric_furnace_tier2.description", "tile.machine.7");
ELECTRIC_FURNACE(4, "electric_furnace", TileEntityElectricFurnace::new, "tile.electric_furnace_tier1.description", "tile.machine.2"),
STORAGE_CLUSTER(8, "cluster_storage", TileEntityEnergyStorageModule::new, "tile.energy_storage_module_tier2.description", "tile.machine.8"),
ARC_FURNACE(12, "arc_furnace", TileEntityElectricFurnace::new, "tile.electric_furnace_tier2.description", "tile.machine.7");

private final int meta;
private final String name;
Expand All @@ -48,14 +47,14 @@ public enum EnumTieredMachineType implements EnumMachineBase, IStringSerializabl
@Override
public int getMetadata()
{
return this.meta * 4;
return this.meta;
}

private final static EnumTieredMachineType[] values = values();
@Override
public EnumMachineBase byMeta(int meta)
public EnumMachineBase fromMetadata(int meta)
{
return values[meta % values.length];
return values[(meta / 4) % values.length];
}

@Override
Expand All @@ -67,20 +66,20 @@ public String getName()
@Override
public TileEntity tileConstructor()
{
int tier = this.meta / 2 + 1;
int tier = this.meta / 8 + 1;
return this.tile.create(tier);
}

@FunctionalInterface
private static interface TileConstructor
{
TileEntity create(int tier);
TileEntity create(int tier); //Note this variant picks up the new TileEntityStorageModule(int tier) constructor, and forces the ::new above to that
}

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

@Override
Expand Down
Loading

0 comments on commit c5a06b6

Please sign in to comment.