Skip to content

Commit

Permalink
Refactor BlockMachines: make constructors the Java 8 way.
Browse files Browse the repository at this point in the history
  • Loading branch information
radfast committed Jan 26, 2020
1 parent b574e20 commit 6278657
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class BlockMachine extends BlockMachineBase

public enum EnumMachineType implements IStringSerializable
{
COAL_GENERATOR(0, "coal_generator", TileEntityCoalGenerator.class, "tile.coal_generator.description", "tile.machine.0"),
COMPRESSOR(3, "ingot_compressor", TileEntityIngotCompressor.class, "tile.compressor.description", "tile.machine.3"); // 3 for backwards compatibility
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

private final int meta;
private final String name;
private final Class tile;
private final TileConstructor tile;
private final String shiftDescriptionKey;
private final String blockName;

EnumMachineType(int meta, String name, Class tile, String key, String blockName)
EnumMachineType(int meta, String name, TileConstructor tile, String key, String blockName)
{
this.meta = meta;
this.name = name;
Expand Down Expand Up @@ -73,13 +73,13 @@ public String getName()

public TileEntity tileConstructor()
{
try
{
return (TileEntity) this.tile.newInstance();
} catch (InstantiationException | IllegalAccessException ex)
{
return null;
}
return this.tile.create();
}

@FunctionalInterface
private static interface TileConstructor
{
TileEntity create();
}

public String getShiftDescription()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import java.util.Random;

public class BlockMachine2 extends BlockMachineBase
{
public static final PropertyEnum<EnumMachineExtendedType> TYPE = PropertyEnum.create("type", EnumMachineExtendedType.class);
Expand All @@ -28,18 +26,18 @@ public class BlockMachine2 extends BlockMachineBase

public enum EnumMachineExtendedType implements IStringSerializable
{
ELECTRIC_COMPRESSOR(0, "electric_compressor", TileEntityElectricIngotCompressor.class, "tile.compressor_electric.description", "tile.machine2.4"),
CIRCUIT_FABRICATOR(1, "circuit_fabricator", TileEntityCircuitFabricator.class, "tile.circuit_fabricator.description", "tile.machine2.5"),
OXYGEN_STORAGE(2, "oxygen_storage", TileEntityOxygenStorageModule.class, "tile.oxygen_storage_module.description", "tile.machine2.6"),
DECONSTRUCTOR(3, "deconstructor", TileEntityDeconstructor.class, "tile.deconstructor.description", "tile.machine2.10");
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");

private final int meta;
private final String name;
private final Class tile;
private final TileConstructor tile;
private final String shiftDescriptionKey;
private final String blockName;

EnumMachineExtendedType(int meta, String name, Class tile, String key, String blockName)
EnumMachineExtendedType(int meta, String name, TileConstructor tile, String key, String blockName)
{
this.meta = meta;
this.name = name;
Expand Down Expand Up @@ -72,13 +70,13 @@ public String getName()

public TileEntity tileConstructor()
{
try
{
return (TileEntity) this.tile.newInstance();
} catch (InstantiationException | IllegalAccessException ex)
{
return null;
}
return this.tile.create();
}

@FunctionalInterface
private static interface TileConstructor
{
TileEntity create();
}

public String getShiftDescription()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class BlockMachine3 extends BlockMachineBase

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

private final int meta;
private final String name;
private final Class tile;
private final TileConstructor tile;
private final String shiftDescriptionKey;
private final String blockName;

EnumMachineBuildingType(int meta, String name, Class tile, String key, String blockName)
EnumMachineBuildingType(int meta, String name, TileConstructor tile, String key, String blockName)
{
this.meta = meta;
this.name = name;
Expand Down Expand Up @@ -65,13 +65,13 @@ public String getName()

public TileEntity tileConstructor()
{
try
{
return (TileEntity) this.tile.newInstance();
} catch (InstantiationException | IllegalAccessException ex)
{
return null;
}
return this.tile.create();
}

@FunctionalInterface
private static interface TileConstructor
{
TileEntity create();
}

public String getShiftDescription()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package micdoodle8.mods.galacticraft.core.blocks;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import micdoodle8.mods.galacticraft.core.tile.IMachineSides;
import micdoodle8.mods.galacticraft.core.tile.IMachineSidesProperties;
import micdoodle8.mods.galacticraft.core.tile.TileEntityElectricFurnace;
Expand Down Expand Up @@ -31,29 +28,22 @@ public class BlockMachineTiered extends BlockMachineBase

public enum EnumTieredMachineType implements IStringSerializable
{
STORAGE_MODULE(0, "energy_storage", TileEntityEnergyStorageModule.class, "tile.energy_storage_module_tier1.description", "tile.machine.1"),
ELECTRIC_FURNACE(1, "electric_furnace", TileEntityElectricFurnace.class, "tile.electric_furnace_tier1.description", "tile.machine.2"),
STORAGE_CLUSTER(2, "cluster_storage", TileEntityEnergyStorageModule.class, "tile.energy_storage_module_tier2.description", "tile.machine.8"),
ARC_FURNACE(3, "arc_furnace", TileEntityElectricFurnace.class, "tile.electric_furnace_tier2.description", "tile.machine.7");
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");

private final int meta;
private final String name;
private Constructor tile;
private TileConstructor tile;
private final String shiftDescriptionKey;
private final String blockName;

EnumTieredMachineType(int meta, String name, Class tile, String key, String blockName)
EnumTieredMachineType(int meta, String name, TileConstructor tc, String key, String blockName)
{
this.meta = meta;
this.name = name;
try
{
this.tile = tile.getConstructor(int.class);
} catch (NoSuchMethodException | SecurityException e)
{
e.printStackTrace();
this.tile = null;
}
this.tile = tc;
this.shiftDescriptionKey = key;
this.blockName = blockName;
}
Expand Down Expand Up @@ -83,13 +73,13 @@ public String getName()
public TileEntity tileConstructor()
{
int tier = this.meta / 2 + 1;
try
{
return (TileEntity) this.tile.newInstance(tier);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
{
return null;
}
return this.tile.create(tier);
}

@FunctionalInterface
private static interface TileConstructor
{
TileEntity create(int tier);
}

public String getShiftDescription()
Expand Down

0 comments on commit 6278657

Please sign in to comment.