From 20280309639d753f6894ed37a5e3c0827d13efd6 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sat, 6 Apr 2013 23:09:25 -0400 Subject: [PATCH] v5.5.4 Beta #9 *Better pumping code, tested and I'm unable to trick it anymore. *Gave the Electric Pump an active state, lights up now when it's pumping. *Sounds now are removed and disabled whenever their chunk is unloaded. *Added some missing javadocs. *Cleanups. --- .../mekanism/client/SoundHandler.java | 30 +++++- .../mekanism/common/BlockMachine.java | 7 +- .../mekanism/common/MekanismUtils.java | 44 +++++++++ .../common/TileEntityElectricPump.java | 93 +++++++++++++++---- .../generators/common/BlockGenerator.java | 5 +- 5 files changed, 153 insertions(+), 26 deletions(-) diff --git a/src/minecraft/mekanism/client/SoundHandler.java b/src/minecraft/mekanism/client/SoundHandler.java index 861a7442a..29d39104b 100644 --- a/src/minecraft/mekanism/client/SoundHandler.java +++ b/src/minecraft/mekanism/client/SoundHandler.java @@ -8,6 +8,9 @@ import java.util.Random; import mekanism.common.IActiveState; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.ForgeSubscribe; +import net.minecraftforge.event.world.ChunkEvent; import paulscode.sound.SoundSystem; import cpw.mods.fml.client.FMLClientHandler; @@ -34,6 +37,7 @@ public class SoundHandler if(soundSystem == null) { soundSystem = FMLClientHandler.instance().instance().getClient().sndManager.sndSystem; + MinecraftForge.EVENT_BUS.register(this); System.out.println("[Mekanism] Successfully set up SoundHandler."); } } @@ -84,7 +88,6 @@ public class SoundHandler for(Sound sound : soundsToRemove) { - System.out.println("[Mekanism] Removed dead sound '" + sound.identifier + ".'"); sound.remove(); TileEntity tileEntity = FMLClientHandler.instance().getClient().theWorld.getBlockTileEntity(sound.xCoord, sound.yCoord, sound.zCoord); @@ -130,4 +133,29 @@ public class SoundHandler return "Mekanism_" + sounds.size() + "_" + new Random().nextInt(10000); } } + + @ForgeSubscribe + public void onChunkUnload(ChunkEvent.Unload event) + { + if(event.getChunk() != null) + { + for(Object obj : event.getChunk().chunkTileEntityMap.values()) + { + if(obj instanceof TileEntity) + { + TileEntity tileEntity = (TileEntity)obj; + + if(tileEntity instanceof IHasSound) + { + if(sounds.contains(((IHasSound)tileEntity).getSound())) + { + sounds.remove(((IHasSound)tileEntity).getSound()); + } + + ((IHasSound)tileEntity).removeSound(); + } + } + } + } + } } diff --git a/src/minecraft/mekanism/common/BlockMachine.java b/src/minecraft/mekanism/common/BlockMachine.java index ed6ff067f..a64056b18 100644 --- a/src/minecraft/mekanism/common/BlockMachine.java +++ b/src/minecraft/mekanism/common/BlockMachine.java @@ -131,7 +131,7 @@ public class BlockMachine extends BlockContainer implements IDismantleable public void randomDisplayTick(World world, int x, int y, int z, Random random) { TileEntityElectricBlock tileEntity = (TileEntityElectricBlock)world.getBlockTileEntity(x, y, z); - if(MekanismUtils.isActive(world, x, y, z)) + if(MekanismUtils.isActive(world, x, y, z) && !(tileEntity instanceof TileEntityElectricPump)) { float xRandom = (float)x + 0.5F; float yRandom = (float)y + 0.0F + random.nextFloat() * 6.0F / 16.0F; @@ -747,11 +747,8 @@ public class BlockMachine extends BlockContainer implements IDismantleable public TileEntity create() { - TileEntity tileEntity; - try { - tileEntity = tileEntityClass.newInstance(); - return tileEntity; + return tileEntityClass.newInstance(); } catch(Exception e) { System.err.println("[Mekanism] Unable to indirectly create tile entity."); e.printStackTrace(); diff --git a/src/minecraft/mekanism/common/MekanismUtils.java b/src/minecraft/mekanism/common/MekanismUtils.java index 021024127..bbdd01b35 100644 --- a/src/minecraft/mekanism/common/MekanismUtils.java +++ b/src/minecraft/mekanism/common/MekanismUtils.java @@ -658,6 +658,50 @@ public final class MekanismUtils return null; } + /** + * Gets the liquid ID at a certain location, 0 if there isn't one + * @param world - world the block is in + * @param x - x coordinate + * @param y - y coordinate + * @param z - z coordinate + * @return liquid ID + */ + public static int getLiquidId(World world, int x, int y, int z) + { + int id = world.getBlockId(x, y, z); + int meta = world.getBlockMetadata(x, y, z); + + if(id == 0) + { + return 0; + } + + if(id == Block.waterStill.blockID || id == Block.waterMoving.blockID) + { + return Block.waterStill.blockID; + } + else if(id == Block.lavaStill.blockID || id == Block.lavaMoving.blockID) + { + return Block.lavaStill.blockID; + } + else if(Block.blocksList[id] instanceof ILiquid) + { + ILiquid liquid = (ILiquid)Block.blocksList[id]; + + return liquid.stillLiquidId(); + } + + return 0; + } + + /** + * Whether or not a block is a dead liquid. + * @param world - world the block is in + * @param x - x coordinate + * @param y - y coordinate + * @param z - z coordinate + * @return if the block is a dead liquid + */ public static boolean isDeadLiquid(World world, int x, int y, int z) { int id = world.getBlockId(x, y, z); diff --git a/src/minecraft/mekanism/common/TileEntityElectricPump.java b/src/minecraft/mekanism/common/TileEntityElectricPump.java index 3979acd9e..133e5d79e 100644 --- a/src/minecraft/mekanism/common/TileEntityElectricPump.java +++ b/src/minecraft/mekanism/common/TileEntityElectricPump.java @@ -32,7 +32,7 @@ import net.minecraftforge.liquids.LiquidContainerRegistry; import net.minecraftforge.liquids.LiquidStack; import net.minecraftforge.liquids.LiquidTank; -public class TileEntityElectricPump extends TileEntityElectricBlock implements ITankContainer, ISustainedTank +public class TileEntityElectricPump extends TileEntityElectricBlock implements ITankContainer, ISustainedTank, IActiveState { /** This pump's tank */ public LiquidTank liquidTank; @@ -43,6 +43,12 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I /** The nodes that have already been sucked up, but are held on to in order to remove dead blocks */ public Set cleaningNodes = new HashSet(); + /** Whether or not this block is in it's active state. */ + public boolean isActive; + + /** The previous active state for this block. */ + public boolean prevActive; + /** Random for this pump */ public Random random = new Random(); @@ -123,11 +129,24 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I } } - if(!suck(true) && !clean(true)) + if(!suck(true)) { - cleaningNodes.clear(); + if(!clean(true) && !cleaningNodes.isEmpty()) + { + if(!worldObj.isRemote) + { + setActive(false); + } + + cleaningNodes.clear(); + } } else { + if(!worldObj.isRemote) + { + setActive(true); + } + clean(true); } @@ -195,9 +214,12 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I } else if(MekanismUtils.isDeadLiquid(worldObj, wrapper.x, wrapper.y, wrapper.z)) { - if(take) + if(liquidTank.getLiquid() != null && MekanismUtils.getLiquidId(worldObj, wrapper.x, wrapper.y, wrapper.z) == liquidTank.getLiquid().itemID) { - worldObj.setBlockToAir(wrapper.x, wrapper.y, wrapper.z); + if(take) + { + worldObj.setBlockToAir(wrapper.x, wrapper.y, wrapper.z); + } } } @@ -207,7 +229,7 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I int y = MekanismUtils.getCoords(wrapper, orientation)[1]; int z = MekanismUtils.getCoords(wrapper, orientation)[2]; - if(MekanismUtils.getDistance(BlockWrapper.get(this), new BlockWrapper(x, y, z)) <= 2340) + if(MekanismUtils.getDistance(BlockWrapper.get(this), new BlockWrapper(x, y, z)) <= 80) { if(MekanismUtils.isLiquid(worldObj, x, y, z)) { @@ -226,9 +248,12 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I } else if(MekanismUtils.isDeadLiquid(worldObj, x, y, z)) { - if(take) + if(liquidTank.getLiquid() != null && MekanismUtils.getLiquidId(worldObj, x, y, z) == liquidTank.getLiquid().itemID) { - worldObj.setBlockToAir(x, y, z); + if(take) + { + worldObj.setBlockToAir(x, y, z); + } } } } @@ -263,9 +288,12 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I } else if(MekanismUtils.isDeadLiquid(worldObj, x, y, z)) { - if(take) + if(liquidTank.getLiquid() != null && MekanismUtils.getLiquidId(worldObj, x, y, z) == liquidTank.getLiquid().itemID) { - worldObj.setBlockToAir(x, y, z); + if(take) + { + worldObj.setBlockToAir(x, y, z); + } } } } @@ -285,10 +313,13 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { if(MekanismUtils.isDeadLiquid(worldObj, wrapper.x, wrapper.y, wrapper.z)) { - took = true; - if(take) + if(liquidTank.getLiquid() != null && MekanismUtils.getLiquidId(worldObj, wrapper.x, wrapper.y, wrapper.z) == liquidTank.getLiquid().itemID) { - worldObj.setBlockToAir(wrapper.x, wrapper.y, wrapper.z); + took = true; + if(take) + { + worldObj.setBlockToAir(wrapper.x, wrapper.y, wrapper.z); + } } } } @@ -297,10 +328,13 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { if(MekanismUtils.isDeadLiquid(worldObj, wrapper.x, wrapper.y, wrapper.z)) { - took = true; - if(take) + if(liquidTank.getLiquid() != null && MekanismUtils.getLiquidId(worldObj, wrapper.x, wrapper.y, wrapper.z) == liquidTank.getLiquid().itemID) { - worldObj.setBlockToAir(wrapper.x, wrapper.y, wrapper.z); + took = true; + if(take) + { + worldObj.setBlockToAir(wrapper.x, wrapper.y, wrapper.z); + } } } } @@ -314,6 +348,8 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { super.handlePacketData(dataStream); + isActive = dataStream.readBoolean(); + try { int amount = dataStream.readInt(); int itemID = dataStream.readInt(); @@ -331,6 +367,8 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { super.getNetworkedData(data); + data.add(isActive); + if(liquidTank.getLiquid() != null) { data.add(liquidTank.getLiquid().amount); @@ -356,6 +394,8 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { super.writeToNBT(nbtTags); + nbtTags.setBoolean("isActive", isActive); + if(liquidTank.getLiquid() != null) { nbtTags.setTag("liquidTank", liquidTank.writeToNBT(new NBTTagCompound())); @@ -395,6 +435,8 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { super.readFromNBT(nbtTags); + isActive = nbtTags.getBoolean("isActive"); + if(nbtTags.hasKey("liquidTank")) { liquidTank.readFromNBT(nbtTags.getCompoundTag("liquidTank")); @@ -533,4 +575,23 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I { return true; } + + @Override + public void setActive(boolean active) + { + isActive = active; + + if(prevActive != active) + { + PacketHandler.sendTileEntityPacketToClients(this, 0, getNetworkedData(new ArrayList())); + } + + prevActive = active; + } + + @Override + public boolean getActive() + { + return isActive; + } } diff --git a/src/minecraft/mekanism/generators/common/BlockGenerator.java b/src/minecraft/mekanism/generators/common/BlockGenerator.java index 3c17cbc3b..88b3d55b0 100644 --- a/src/minecraft/mekanism/generators/common/BlockGenerator.java +++ b/src/minecraft/mekanism/generators/common/BlockGenerator.java @@ -593,11 +593,8 @@ public class BlockGenerator extends BlockContainer implements IDismantleable public TileEntity create() { - TileEntity tileEntity; - try { - tileEntity = tileEntityClass.newInstance(); - return tileEntity; + return tileEntityClass.newInstance(); } catch(Exception e) { System.err.println("[Mekanism] Unable to indirectly create tile entity."); e.printStackTrace();