Skip to content

Commit

Permalink
Add Wig-Wag Signals (Taiga #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
CSX8600 committed Aug 1, 2019
1 parent e810303 commit b26a131
Show file tree
Hide file tree
Showing 13 changed files with 807 additions and 214 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.


version = "0.0.5"
version = "0.0.6a"
group = "com.clussmanproductions.trafficcontrol" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "trafficcontrol"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.clussmanproductions.trafficcontrol.blocks.BlockSign;
import com.clussmanproductions.trafficcontrol.blocks.BlockStreetLightDouble;
import com.clussmanproductions.trafficcontrol.blocks.BlockStreetLightSingle;
import com.clussmanproductions.trafficcontrol.blocks.BlockWigWag;

import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder;
Expand Down Expand Up @@ -86,6 +87,8 @@ public class ModBlocks {
public static BlockLightSource light_source;
@ObjectHolder("street_light_double")
public static BlockStreetLightDouble street_light_double;
@ObjectHolder("wig_wag")
public static BlockWigWag wig_wag;

@SideOnly(Side.CLIENT)
public static void initModels(ModelRegistryEvent e)
Expand All @@ -107,5 +110,6 @@ public static void initModels(ModelRegistryEvent e)
drum.initModel();
street_light_single.initModel();
street_light_double.initModel();
wig_wag.initModel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.clussmanproductions.trafficcontrol.blocks;

import com.clussmanproductions.trafficcontrol.ModBlocks;
import com.clussmanproductions.trafficcontrol.ModTrafficControl;
import com.clussmanproductions.trafficcontrol.tileentity.WigWagTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.render.RendererWigWag;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.client.registry.ClientRegistry;

public class BlockWigWag extends Block implements ITileEntityProvider {
public static PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public static PropertyBool ACTIVE = PropertyBool.create("active");
public BlockWigWag()
{
super(Material.IRON);
setRegistryName("wig_wag");
setUnlocalizedName(ModTrafficControl.MODID + ".wig_wag");
setHardness(2f);
setCreativeTab(ModTrafficControl.CREATIVE_TAB);
}

public void initModel()
{
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));

ClientRegistry.bindTileEntitySpecialRenderer(WigWagTileEntity.class, new RendererWigWag());
}

@Override
public int getMetaFromState(IBlockState state) {
int modifier = 0;
if (state.getValue(ACTIVE))
{
modifier = 4;
}
return state.getValue(FACING).getHorizontalIndex() + modifier;
}

@Override
public IBlockState getStateFromMeta(int meta) {
boolean active = meta >= 4;
meta = active ? meta - 4 : meta;
return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(ACTIVE, active);
}

@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, FACING, ACTIVE);
}

@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
if (state.getBlock() != ModBlocks.wig_wag)
{
return FULL_BLOCK_AABB;
}

switch(state.getValue(FACING))
{
case WEST:
return new AxisAlignedBB(0.375,0,0.375,0.625,1.25,1.3125);
case EAST:
return new AxisAlignedBB(0.375,0,0.5625,0.625,1.25,-0.375);
case NORTH:
return new AxisAlignedBB(-0.375,0,0.625,0.5625,1.25,0.375);
case SOUTH:
return new AxisAlignedBB(0.4375,0,0.625,1.375,1.25,0.375);
}
return super.getBoundingBox(state, source, pos);
}

@Override
public int getLightValue(IBlockState state) {
return state.getValue(ACTIVE) ? 15 : 0;
}

@Override
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
return getDefaultState().withProperty(FACING, placer.getHorizontalFacing()).withProperty(ACTIVE, false);
}

@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}

@Override
public boolean isNormalCube(IBlockState state, IBlockAccess world, BlockPos pos) {
return false;
}

@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new WigWagTileEntity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.clussmanproductions.trafficcontrol.tileentity.BellBaseTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.CrossingGateGateTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.RelayTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.WigWagTileEntity;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
Expand Down Expand Up @@ -197,5 +198,17 @@ private void checkUseOnTileEntity(World world, TileEntity te, RelayTileEntity re
player.sendMessage(new TextComponentString("Unpaired Bell from Relay Box"));
}
}

if (te instanceof WigWagTileEntity)
{
if (relay.addOrRemoveWigWag(te.getPos()))
{
player.sendMessage(new TextComponentString("Paired Wig Wag to Relay Box"));
}
else
{
player.sendMessage(new TextComponentString("Unpaired Wig Wag from Relay Box"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.clussmanproductions.trafficcontrol.blocks.BlockSign;
import com.clussmanproductions.trafficcontrol.blocks.BlockStreetLightDouble;
import com.clussmanproductions.trafficcontrol.blocks.BlockStreetLightSingle;
import com.clussmanproductions.trafficcontrol.blocks.BlockWigWag;
import com.clussmanproductions.trafficcontrol.gui.GuiProxy;
import com.clussmanproductions.trafficcontrol.item.ItemCrossingRelayBox;
import com.clussmanproductions.trafficcontrol.item.ItemCrossingRelayTuner;
Expand All @@ -40,6 +41,7 @@
import com.clussmanproductions.trafficcontrol.tileentity.SignTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.StreetLightDoubleTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.StreetLightSingleTileEntity;
import com.clussmanproductions.trafficcontrol.tileentity.WigWagTileEntity;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -85,6 +87,7 @@ public static void registerBlocks(RegistryEvent.Register<Block> e)
e.getRegistry().register(new BlockStreetLightSingle());
e.getRegistry().register(new BlockLightSource());
e.getRegistry().register(new BlockStreetLightDouble());
e.getRegistry().register(new BlockWigWag());

GameRegistry.registerTileEntity(CrossingGateGateTileEntity.class, ModTrafficControl.MODID + "_crossinggategate");
GameRegistry.registerTileEntity(SafetranType3TileEntity.class, ModTrafficControl.MODID + "_safetrantyp3");
Expand All @@ -93,6 +96,7 @@ public static void registerBlocks(RegistryEvent.Register<Block> e)
GameRegistry.registerTileEntity(SignTileEntity.class, ModTrafficControl.MODID + "_sign");
GameRegistry.registerTileEntity(StreetLightSingleTileEntity.class, ModTrafficControl.MODID + "_streetsignsingle");
GameRegistry.registerTileEntity(StreetLightDoubleTileEntity.class, ModTrafficControl.MODID + "_streetlightdouble");
GameRegistry.registerTileEntity(WigWagTileEntity.class, ModTrafficControl.MODID + "_wigwag");
}

@SubscribeEvent
Expand All @@ -118,6 +122,7 @@ public static void registerItems(RegistryEvent.Register<Item> e)
e.getRegistry().register(new ItemBlock(ModBlocks.drum).setRegistryName(ModBlocks.drum.getRegistryName()));
e.getRegistry().register(new ItemBlock(ModBlocks.street_light_single).setRegistryName(ModBlocks.street_light_single.getRegistryName()));
e.getRegistry().register(new ItemBlock(ModBlocks.street_light_double).setRegistryName(ModBlocks.street_light_double.getRegistryName()));
e.getRegistry().register(new ItemBlock(ModBlocks.wig_wag).setRegistryName(ModBlocks.wig_wag.getRegistryName()));
}

public static void registerSounds(RegistryEvent.Register<SoundEvent> e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.clussmanproductions.trafficcontrol.blocks.BlockCrossingGateLamps;
import com.clussmanproductions.trafficcontrol.blocks.BlockLampBase;
import com.clussmanproductions.trafficcontrol.blocks.BlockLampBase.EnumState;
import com.clussmanproductions.trafficcontrol.blocks.BlockWigWag;
import com.clussmanproductions.trafficcontrol.tileentity.CrossingGateGateTileEntity.EnumStatuses;
import com.clussmanproductions.trafficcontrol.util.AnyStatement;

Expand Down Expand Up @@ -34,9 +35,13 @@ public class RelayTileEntity extends TileEntity implements ITickable {
// Bell information
private boolean alreadyNotifiedBells;

// Wig Wag information
private boolean alreadyNotifiedWigWags;

private ArrayList<BlockPos> crossingLampLocations = new ArrayList<BlockPos>();
private ArrayList<BlockPos> crossingGateLocations = new ArrayList<BlockPos>();
private ArrayList<BlockPos> bellLocations = new ArrayList<BlockPos>();
private ArrayList<BlockPos> wigWagLocations = new ArrayList<BlockPos>();

private ArrayList<CrossingGateGateTileEntity> crossingGates = new ArrayList<CrossingGateGateTileEntity>();
private ArrayList<BellBaseTileEntity> bells = new ArrayList<BellBaseTileEntity>();
Expand Down Expand Up @@ -65,41 +70,23 @@ public void readFromNBT(NBTTagCompound compound) {
// Bell information
alreadyNotifiedBells = compound.getBoolean("alreadynotifiedbells");

fillArrayListFromNBT("lamps", bellLocations, compound);
fillArrayListFromNBT("gate", bellLocations, compound);
fillArrayListFromNBT("bell", bellLocations, compound);
fillArrayListFromNBT("wigwags", wigWagLocations, compound);
}

private void fillArrayListFromNBT(String key, ArrayList<BlockPos> list, NBTTagCompound tag)
{
int i = 0;
while (true) {
if (!compound.hasKey("lamps" + i)) {
if (!tag.hasKey(key + i)) {
break;
}

int[] blockPos = compound.getIntArray("lamps" + i);
BlockPos lampPos = new BlockPos(blockPos[0], blockPos[1], blockPos[2]);
crossingLampLocations.add(lampPos);

i++;
}

i = 0;
while (true) {
if (!compound.hasKey("gate" + i)) {
break;
}

int[] blockPos = compound.getIntArray("gate" + i);
BlockPos gatePos = new BlockPos(blockPos[0], blockPos[1], blockPos[2]);
crossingGateLocations.add(gatePos);

i++;
}

i = 0;
while (true) {
if (!compound.hasKey("bell" + i)) {
break;
}

int[] blockPos = compound.getIntArray("bell" + i);
BlockPos bellPos = new BlockPos(blockPos[0], blockPos[1], blockPos[2]);
bellLocations.add(bellPos);
int[] blockPos = tag.getIntArray(key + i);
BlockPos pos = new BlockPos(blockPos[0], blockPos[1], blockPos[2]);
list.add(pos);

i++;
}
Expand All @@ -124,6 +111,9 @@ public NBTTagCompound writeToNBT(NBTTagCompound compound) {

// Bell information
nbt.setBoolean("alreadynotifiedbells", alreadyNotifiedBells);

// Wig Wag information
nbt.setBoolean("alreadynotifiedwigwags", alreadyNotifiedWigWags);

for (int i = 0; i < crossingLampLocations.size(); i++) {
BlockPos lamps = crossingLampLocations.get(i);
Expand All @@ -145,6 +135,13 @@ public NBTTagCompound writeToNBT(NBTTagCompound compound) {
int[] bellPos = new int[] { bell.getPos().getX(), bell.getPos().getY(), bell.getPos().getZ() };
nbt.setIntArray("bell" + i, bellPos);
}

for (int i = 0; i < wigWagLocations.size(); i++) {
BlockPos wigWag = wigWagLocations.get(i);

int[] wigWagPos = new int[] { wigWag.getX(), wigWag.getY(), wigWag.getZ() };
nbt.setIntArray("wigwags" + i, wigWagPos);
}

return nbt;
}
Expand All @@ -160,6 +157,7 @@ public void update() {
notifyGates();
updateLamps();
updateBells();
notifyWigWags();
}

private void verifyLocations()
Expand Down Expand Up @@ -323,6 +321,35 @@ private void updateBells()
}
}

private void notifyWigWags()
{
if (!alreadyNotifiedWigWags)
{
ArrayList<BlockPos> positionsToRemove = new ArrayList<BlockPos>();
for(BlockPos pos : wigWagLocations)
{
try
{
IBlockState currentState = world.getBlockState(pos);
world.setBlockState(pos, currentState.withProperty(BlockWigWag.ACTIVE, isPowered));
}
catch (Exception ex)
{
positionsToRemove.add(pos);
}
}

for(BlockPos pos : positionsToRemove)
{
wigWagLocations.remove(pos);

ModTrafficControl.logger.error("Wig Wag at " + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + " has been unpaired due to an error");
}

alreadyNotifiedWigWags = true;
}
}

public void setMaster() {
isMaster = true;
markDirty();
Expand Down Expand Up @@ -417,11 +444,26 @@ public boolean addOrRemoveBell(BellBaseTileEntity bell) {
return true;
}

public boolean addOrRemoveWigWag(BlockPos wigWagPos)
{
if (wigWagLocations.contains(wigWagPos))
{
wigWagLocations.remove(wigWagPos);
return false;
}
else
{
wigWagLocations.add(wigWagPos);
return true;
}
}

public void setPowered(boolean isPowered)
{
this.isPowered = isPowered;
alreadyNotifiedGates = false;
alreadyNotifiedBells = false;
alreadyNotifiedWigWags = false;

markDirty();
}
Expand Down
Loading

0 comments on commit b26a131

Please sign in to comment.