Skip to content

Commit

Permalink
Refactor BlockMachines code for simpler code maintenance.
Browse files Browse the repository at this point in the history
  • Loading branch information
radfast committed Jan 26, 2020
1 parent 1864beb commit 8ffacaa
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@

import java.util.Random;

public class BlockMachine extends BlockTileGC implements IShiftDescription, ISortableBlock
public class BlockMachine extends BlockTileGC implements IShiftDescription, ISortableBlock, IMachineBase
{
public static final int COAL_GENERATOR_METADATA = 0;
public static final int COMPRESSOR_METADATA = 12;

public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public static final PropertyEnum<EnumMachineType> TYPE = PropertyEnum.create("type", EnumMachineType.class);

public enum EnumMachineType implements IStringSerializable
Expand Down Expand Up @@ -262,4 +261,10 @@ public EnumSortCategoryBlock getCategory(int meta)
{
return EnumSortCategoryBlock.MACHINE;
}

@Override
public String getUnlocalizedName(int typenum)
{
return this.getUnlocalizedName() + "." + (typenum / 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@

import java.util.Random;

public class BlockMachine2 extends BlockTileGC implements IShiftDescription, ISortableBlock
public class BlockMachine2 extends BlockTileGC implements IShiftDescription, ISortableBlock, IMachineBase
{
public static final int ELECTRIC_COMPRESSOR_METADATA = 0;
public static final int CIRCUIT_FABRICATOR_METADATA = 4;
public static final int OXYGEN_STORAGE_MODULE_METADATA = 8;
public static final int DECONSTRUCTOR_METADATA = 12;

public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
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;
Expand Down Expand Up @@ -105,7 +104,7 @@ public boolean isFullCube(IBlockState state)
@Override
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
{
TileEntity tile = worldIn.getTileEntity(pos);
// TileEntity tile = worldIn.getTileEntity(pos);

}

Expand Down Expand Up @@ -268,4 +267,27 @@ public boolean onSneakUseWrench(World world, BlockPos pos, EntityPlayer entityPl
}
return false;
}

@Override
public String getUnlocalizedName(int typenum)
{
int index = 6;
if (typenum == BlockMachine2.OXYGEN_STORAGE_MODULE_METADATA)
{
index = 6;
}
else if (typenum == BlockMachine2.CIRCUIT_FABRICATOR_METADATA)
{
index = 5;
}
else if (typenum == BlockMachine2.ELECTRIC_COMPRESSOR_METADATA)
{
index = 4;
}
else if (typenum == BlockMachine2.DECONSTRUCTOR_METADATA)
{
index = 10;
}
return this.getUnlocalizedName() + "." + index;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
* with a base building purpose - e.g. Painter
*
*/
public class BlockMachine3 extends BlockTileGC implements IShiftDescription, ISortableBlock
public class BlockMachine3 extends BlockTileGC implements IShiftDescription, ISortableBlock, IMachineBase
{
public static final int PAINTER_METADATA = 0;

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);
public static final PropertyEnum<EnumMachineBuildingType> TYPE = PropertyEnum.create("type", EnumMachineBuildingType.class);

public enum EnumMachineBuildingType implements IStringSerializable
Expand Down Expand Up @@ -204,4 +203,11 @@ public EnumSortCategoryBlock getCategory(int meta)
{
return EnumSortCategoryBlock.MACHINE;
}

@Override
public String getUnlocalizedName(int typenum)
{
int index = 9;
return this.getUnlocalizedName() + "." + index;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockMachineTiered extends BlockTileGC implements IShiftDescription, ISortableBlock
public class BlockMachineTiered extends BlockTileGC implements IShiftDescription, ISortableBlock, IMachineBase
{
public static final int STORAGE_MODULE_METADATA = 0;
public static final int ELECTRIC_FURNACE_METADATA = 4;
public static IMachineSidesProperties MACHINESIDES_RENDERTYPE = IMachineSidesProperties.TWOFACES_HORIZ;

public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public static final PropertyEnum<EnumTieredMachineType> TYPE = PropertyEnum.create("type", EnumTieredMachineType.class);
public static final PropertyInteger FILL_VALUE = PropertyInteger.create("fill_value", 0, 33);
public static final PropertyEnum SIDES = MACHINESIDES_RENDERTYPE.asProperty;
Expand Down Expand Up @@ -263,4 +262,27 @@ public boolean onSneakUseWrench(World world, BlockPos pos, EntityPlayer entityPl
}
return false;
}

@Override
public String getUnlocalizedName(int typenum)
{
if (typenum == BlockMachineTiered.ELECTRIC_FURNACE_METADATA)
{
return "tile.machine.2";
}
else if (typenum == BlockMachineTiered.STORAGE_MODULE_METADATA)
{
return "tile.machine.1";
}

//Tier 2 versions of the same
if (typenum == 8 + BlockMachineTiered.ELECTRIC_FURNACE_METADATA)
{
return "tile.machine.7";
}
else
{
return "tile.machine.8";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package micdoodle8.mods.galacticraft.core.blocks;

import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.EnumFacing;

public interface IMachineBase
{
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);

public String getUnlocalizedName(int typenum);

public static EnumFacing getFront(IBlockState state)
{
if (state.getBlock() instanceof IMachineBase)
{
return (state.getValue(IMachineBase.FACING));
}
return EnumFacing.NORTH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine2;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachineTiered;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.proxy.ClientProxyCore;
import net.minecraft.block.Block;
import net.minecraft.client.entity.EntityPlayerSP;
Expand Down Expand Up @@ -42,56 +42,11 @@ public String getUnlocalizedName(ItemStack itemstack)
int index = 0;
int typenum = itemstack.getItemDamage() & 12;

if (this.getBlock() == GCBlocks.machineBase)
if (this.getBlock() instanceof IMachineBase)
{
index = typenum / 4;
return ((IMachineBase) this.getBlock()).getUnlocalizedName(typenum);
}
else if (this.getBlock() == GCBlocks.machineTiered)
{
if (typenum == BlockMachineTiered.ELECTRIC_FURNACE_METADATA)
{
return "tile.machine.2";
}
else if (typenum == BlockMachineTiered.STORAGE_MODULE_METADATA)
{
return "tile.machine.1";
}

//Tier 2 versions of the same
if (typenum == 8 + BlockMachineTiered.ELECTRIC_FURNACE_METADATA)
{
return "tile.machine.7";
}
else if (typenum == 8 + BlockMachineTiered.STORAGE_MODULE_METADATA)
{
return "tile.machine.8";
}
}
else if (this.getBlock() == GCBlocks.machineBase2)
{
if (typenum == BlockMachine2.OXYGEN_STORAGE_MODULE_METADATA)
{
index = 6;
}
else if (typenum == BlockMachine2.CIRCUIT_FABRICATOR_METADATA)
{
index = 5;
}
else if (typenum == BlockMachine2.ELECTRIC_COMPRESSOR_METADATA)
{
index = 4;
}
else if (typenum == BlockMachine2.DECONSTRUCTOR_METADATA)
{
index = 10;
}
}
else //machineBase3
{
index = 9;
}

return this.getBlock().getUnlocalizedName() + "." + index;
return "tile.machine.0";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import micdoodle8.mods.galacticraft.api.recipe.CircuitFabricatorRecipes;
import micdoodle8.mods.galacticraft.api.world.IZeroGDimension;
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.GCItems;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine2;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.energy.item.ItemElectricBase;
import micdoodle8.mods.galacticraft.core.energy.tile.TileBaseElectricBlockWithInventory;
import micdoodle8.mods.galacticraft.core.items.ItemBasic;
Expand Down Expand Up @@ -241,12 +243,7 @@ public boolean shouldUseEnergy()
@Override
public EnumFacing getFront()
{
IBlockState state = this.world.getBlockState(getPos());
if (state.getBlock() instanceof BlockMachine2)
{
return (state.getValue(BlockMachine2.FACING));
}
return EnumFacing.NORTH;
return IMachineBase.getFront(this.world.getBlockState(getPos()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import micdoodle8.mods.galacticraft.api.transmission.NetworkType;
import micdoodle8.mods.galacticraft.api.transmission.tile.IConnector;
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.energy.tile.TileBaseUniversalElectricalSource;
import micdoodle8.mods.galacticraft.core.inventory.IInventoryDefaults;
import micdoodle8.mods.galacticraft.core.util.GCCoreUtil;
Expand Down Expand Up @@ -189,12 +191,7 @@ public EnumSet<EnumFacing> getElectricalOutputDirections()

public EnumFacing getFront()
{
IBlockState state = this.world.getBlockState(getPos());
if (state.getBlock() instanceof BlockMachine)
{
return state.getValue(BlockMachine.FACING);
}
return EnumFacing.NORTH;
return IMachineBase.getFront(this.world.getBlockState(getPos()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import micdoodle8.mods.galacticraft.api.GalacticraftRegistry;
import micdoodle8.mods.galacticraft.api.recipe.INasaWorkbenchRecipe;
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.GCItems;
import micdoodle8.mods.galacticraft.core.GalacticraftCore;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine2;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.client.sounds.GCSounds;
import micdoodle8.mods.galacticraft.core.energy.item.ItemElectricBase;
import micdoodle8.mods.galacticraft.core.energy.tile.TileBaseElectricBlock;
Expand Down Expand Up @@ -498,12 +500,7 @@ public boolean shouldUseEnergy()
@Override
public EnumFacing getFront()
{
IBlockState state = this.world.getBlockState(getPos());
if (state.getBlock() instanceof BlockMachine2)
{
return state.getValue(BlockMachine2.FACING);
}
return EnumFacing.NORTH;
return IMachineBase.getFront(this.world.getBlockState(getPos()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachineTiered;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.energy.item.ItemElectricBase;
import micdoodle8.mods.galacticraft.core.energy.tile.EnergyStorageTile;
import micdoodle8.mods.galacticraft.core.energy.tile.TileBaseElectricBlockWithInventory;
Expand Down Expand Up @@ -340,12 +341,7 @@ public boolean hasCustomName()
@Override
public EnumFacing getFront()
{
IBlockState state = this.world.getBlockState(getPos());
if (state.getBlock() instanceof BlockMachineTiered)
{
return state.getValue(BlockMachineTiered.FACING);
}
return EnumFacing.NORTH;
return IMachineBase.getFront(this.world.getBlockState(getPos()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import micdoodle8.mods.galacticraft.api.recipe.CompressorRecipes;
import micdoodle8.mods.galacticraft.api.recipe.ShapedRecipesGC;
import micdoodle8.mods.galacticraft.api.recipe.ShapelessOreRecipeGC;
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine2;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.energy.item.ItemElectricBase;
import micdoodle8.mods.galacticraft.core.energy.tile.TileBaseElectricBlock;
import micdoodle8.mods.galacticraft.core.inventory.IInventoryDefaults;
Expand Down Expand Up @@ -577,12 +579,7 @@ public boolean shouldUseEnergy()
@Override
public EnumFacing getFront()
{
IBlockState state = this.world.getBlockState(getPos());
if (state.getBlock() instanceof BlockMachine2)
{
return state.getValue(BlockMachine2.FACING);
}
return EnumFacing.NORTH;
return IMachineBase.getFront(this.world.getBlockState(getPos()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachine;
import micdoodle8.mods.galacticraft.core.blocks.BlockMachineTiered;
import micdoodle8.mods.galacticraft.core.blocks.IMachineBase;
import micdoodle8.mods.galacticraft.core.energy.item.ItemElectricBase;
import micdoodle8.mods.galacticraft.core.energy.tile.TileBaseUniversalElectricalSource;
import micdoodle8.mods.galacticraft.core.inventory.IInventoryDefaults;
Expand Down Expand Up @@ -239,16 +240,7 @@ public boolean canConnect(EnumFacing direction, NetworkType type)
@Override
public EnumFacing getFront()
{
IBlockState state = this.world.getBlockState(getPos());
if (state.getBlock() instanceof BlockMachineTiered)
{
return (state.getValue(BlockMachineTiered.FACING));
}
else if (state.getBlock() instanceof BlockMachine)
{
return (state.getValue(BlockMachine.FACING));
}
return EnumFacing.NORTH;
return IMachineBase.getFront(this.world.getBlockState(getPos()));
}

@Override
Expand Down
Loading

0 comments on commit 8ffacaa

Please sign in to comment.