diff --git a/src/main/java/com/brandon3055/draconicevolution/common/blocks/machine/PlayerDetectorAdvanced.java b/src/main/java/com/brandon3055/draconicevolution/common/blocks/machine/PlayerDetectorAdvanced.java index b99daa3b..c7980f8f 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/blocks/machine/PlayerDetectorAdvanced.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/blocks/machine/PlayerDetectorAdvanced.java @@ -28,9 +28,13 @@ import cpw.mods.fml.relauncher.SideOnly; public class PlayerDetectorAdvanced extends BlockCustomDrop { + @SideOnly(Side.CLIENT) IIcon side_inactive; + @SideOnly(Side.CLIENT) IIcon side_active; + @SideOnly(Side.CLIENT) IIcon top; + @SideOnly(Side.CLIENT) IIcon bottom; public PlayerDetectorAdvanced() { @@ -41,8 +45,8 @@ public class PlayerDetectorAdvanced extends BlockCustomDrop { ModBlocks.register(this); } - @SideOnly(Side.CLIENT) @Override + @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister iconRegister) { side_inactive = iconRegister .registerIcon(References.RESOURCESPREFIX + "advanced_player_detector_side_inactive"); @@ -52,8 +56,7 @@ public class PlayerDetectorAdvanced extends BlockCustomDrop { } @Override - public boolean isBlockSolid(IBlockAccess p_149747_1_, int p_149747_2_, int p_149747_3_, int p_149747_4_, - int p_149747_5_) { + public boolean isBlockSolid(IBlockAccess world, int x, int y, int z, int side) { return true; } @@ -62,47 +65,51 @@ public class PlayerDetectorAdvanced extends BlockCustomDrop { return true; } - @SideOnly(Side.CLIENT) @Override + @SideOnly(Side.CLIENT) public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) { - IIcon side_icon; - TileEntity tile = world.getTileEntity(x, y, z); - TilePlayerDetectorAdvanced detector = (tile != null && tile instanceof TilePlayerDetectorAdvanced) - ? (TilePlayerDetectorAdvanced) tile - : null; - if (detector != null && detector.getStackInSlot(0) != null) { - ItemStack stack = detector.getStackInSlot(0); - Block block = Block.getBlockFromItem(stack.getItem()); - if (block != null && block.renderAsNormalBlock()) return block.getIcon(side, stack.getItemDamage()); - } else { - if (detector != null && detector.output) side_icon = side_active; - else side_icon = side_inactive; - - if (side == 0) return bottom; - else if (side == 1) return top; - else return side_icon; + if (tile instanceof TilePlayerDetectorAdvanced) { + TilePlayerDetectorAdvanced detector = (TilePlayerDetectorAdvanced) tile; + if (detector.getStackInSlot(0) != null) { + ItemStack stack = detector.getStackInSlot(0); + Block block = Block.getBlockFromItem(stack.getItem()); + if (block != null && block.renderAsNormalBlock()) { + return block.getIcon(side, stack.getItemDamage()); + } + } else { + if (side == 0) { + return bottom; + } + if (side == 1) { + return top; + } + return detector.shouldOutput() ? side_active : side_inactive; + } } - - return null; + return side_active; } + @Override @SideOnly(Side.CLIENT) - @Override - public IIcon getIcon(int side, int meta) { - if (side == 0) return bottom; - else if (side == 1) return top; - else return side_active; + public IIcon getIcon(int side, int metadata) { + if (side == 0) { + return bottom; + } + if (side == 1) { + return top; + } + return side_active; } @Override - public TileEntity createNewTileEntity(World var1, int var2) { + public TileEntity createNewTileEntity(World world, int metadata) { return new TilePlayerDetectorAdvanced(); } @Override - public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, - float p_149727_7_, float p_149727_8_, float p_149727_9_) { + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float subX, + float subY, float subZ) { if (!world.isRemote) { FMLNetworkHandler .openGui(player, DraconicEvolution.instance, GuiHandler.GUIID_PLAYERDETECTOR, world, x, y, z); @@ -112,8 +119,7 @@ public class PlayerDetectorAdvanced extends BlockCustomDrop { @Override public boolean canConnectRedstone(IBlockAccess world, int x, int y, int z, int side) { - if (side == 0 || side == 1) return false; - else return true; + return side >= 0; } @Override @@ -122,25 +128,15 @@ public class PlayerDetectorAdvanced extends BlockCustomDrop { } @Override - public int isProvidingWeakPower(IBlockAccess world, int x, int y, int z, int meta) { - TileEntity te = world.getTileEntity(x, y, z); - TilePlayerDetectorAdvanced detector = (te != null && te instanceof TilePlayerDetectorAdvanced) - ? (TilePlayerDetectorAdvanced) te - : null; - if (detector != null) if (!detector.outputInverted) return detector.output ? 15 : 0; - else return detector.output ? 0 : 15; - else return 0; + public int isProvidingWeakPower(IBlockAccess world, int x, int y, int z, int side) { + return isProvidingStrongPower(world, x, y, z, side); } @Override - public int isProvidingStrongPower(IBlockAccess world, int x, int y, int z, int meta) { - TileEntity te = world.getTileEntity(x, y, z); - TilePlayerDetectorAdvanced detector = (te != null && te instanceof TilePlayerDetectorAdvanced) - ? (TilePlayerDetectorAdvanced) te - : null; - if (detector != null) if (!detector.outputInverted) return detector.output ? 15 : 0; - else return detector.output ? 0 : 15; - else return 0; + public int isProvidingStrongPower(IBlockAccess world, int x, int y, int z, int side) { + TileEntity tile = world.getTileEntity(x, y, z); + return tile instanceof TilePlayerDetectorAdvanced && ((TilePlayerDetectorAdvanced) tile).shouldOutput() ? 15 + : 0; } @Override @@ -154,17 +150,30 @@ public class PlayerDetectorAdvanced extends BlockCustomDrop { } @Override - protected void getCustomTileEntityDrops(TileEntity te, List droppes) {} + protected void getCustomTileEntityDrops(TileEntity tile, List drops) {} @Override - public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { - TileEntity te = world.getTileEntity(x, y, z); - TilePlayerDetectorAdvanced detector = (te != null && te instanceof TilePlayerDetectorAdvanced) - ? (TilePlayerDetectorAdvanced) te - : null; - if (detector != null && detector.getStackInSlot(0) != null) { - return detector.getStackInSlot(0); + public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z, EntityPlayer player) { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof TilePlayerDetectorAdvanced) { + ItemStack stackInSlot = ((TilePlayerDetectorAdvanced) tile).getStackInSlot(0); + if (stackInSlot != null) { + return stackInSlot; + } + } + return super.getPickBlock(target, world, x, y, z, player); + } + + @Override + public void breakBlock(World world, int x, int y, int z, Block blockBroken, int metadata) { + super.breakBlock(world, x, y, z, blockBroken, metadata); + world.notifyBlocksOfNeighborChange(x, y, z, blockBroken); + for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { + world.notifyBlocksOfNeighborChange( + x + direction.offsetX, + y + direction.offsetY, + z + direction.offsetZ, + blockBroken); } - return super.getPickBlock(target, world, x, y, z); } } diff --git a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlayerDetectorAdvanced.java b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlayerDetectorAdvanced.java index 7c8247ab..6c874d91 100644 --- a/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlayerDetectorAdvanced.java +++ b/src/main/java/com/brandon3055/draconicevolution/common/tileentities/TilePlayerDetectorAdvanced.java @@ -1,10 +1,8 @@ package com.brandon3055.draconicevolution.common.tileentities; -import java.util.Iterator; import java.util.List; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; @@ -16,101 +14,107 @@ import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; +import net.minecraftforge.common.util.ForgeDirection; import com.brandon3055.draconicevolution.common.container.ContainerPlayerDetector; public class TilePlayerDetectorAdvanced extends TileEntity implements IInventory { + public static final int MAXIMUM_RANGE = 20; + public static final int MINIMUM_RANGE = 1; + private static final int ScanRate = 5; + public String[] names = new String[42]; - private ItemStack[] items; - public boolean whiteList = false; + public boolean isInWhiteListMode = false; + public boolean isOutputInverted = false; public int range = 10; + private final ItemStack[] items; private int tick = 0; - private int scanRate = 5; - public boolean output = false; - public boolean outputInverted = false; - private List EntityList; + private boolean shouldOutput = false; + private List entityList; public TilePlayerDetectorAdvanced() { - for (int i = 0; i < names.length; i++) if (names[i] == null) names[i] = ""; - + for (int i = 0; i < names.length; i++) { + if (names[i] == null) { + names[i] = ""; + } + } items = new ItemStack[1]; } @Override public void updateEntity() { - if (worldObj.isRemote) return; - - if (tick >= scanRate) { - - tick = 0; - - if (shouldEmit()) { - if (!output) setOutput(true); - } else { - if (output) setOutput(false); - } - } else tick++; - } - - public boolean shouldEmit() { - findEntitys(); - - boolean b = false; - Iterator i = EntityList.iterator(); - while (i.hasNext()) { - Entity ent = i.next(); - if (!(ent instanceof EntityPlayer)) return false; - - String name = ((EntityPlayer) ent).getCommandSenderName(); - if (whiteList) { - if (isPlayerListed(name)) return true; - } else { - if (!isPlayerListed(name)) return true; - } + if (worldObj.isRemote) { + return; + } + + if (tick >= ScanRate) { + tick = 0; + if (shouldEmit()) { + if (!shouldOutput) setShouldOutput(true); + } else { + if (shouldOutput) setShouldOutput(false); + } + } else { + tick++; } - return b; } - private void findEntitys() { - double x1 = xCoord + 0.5 - range; - double y1 = yCoord + 0.5 - range; - double z1 = zCoord + 0.5 - range; - double x2 = xCoord + 0.5 + range; - double y2 = yCoord + 0.5 + range; - double z2 = zCoord + 0.5 + range; + private boolean shouldEmit() { + findEntities(); - // System.out.println(x1 + " " + y1 + " " + z1 + " " + x2 + " " + y2 + " " + z2); - - // ScanBox = AxisAlignedBB.getBoundingBox(x1, y1, z1, x2, y2, z2); - // ScanBox = AxisAlignedBB.getAABBPool().getAABB(xCoord + 0.5 - range, yCoord + 0.5 - range, zCoord + 0.5 - - // range, xCoord + 0.5 + range, yCoord + 0.5 + range, zCoord + 0.5 + range); - EntityList = worldObj - .getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(x1, y1, z1, x2, y2, z2)); - // System.out.println(EntityList); + for (EntityLivingBase entity : entityList) { + if (!(entity instanceof EntityPlayer)) { + return false; + } + String name = entity.getCommandSenderName(); + return (isInWhiteListMode == isPlayerListed(name)) != isOutputInverted; + } + return isOutputInverted; } - private void setOutput(boolean out) { - output = out; - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + private void findEntities() { + double startX = xCoord + 0.5 - range; + double startY = yCoord + 0.5 - range; + double startZ = zCoord + 0.5 - range; + double endX = xCoord + 0.5 + range; + double endY = yCoord + 0.5 + range; + double endZ = zCoord + 0.5 + range; + entityList = worldObj.getEntitiesWithinAABB( + EntityPlayer.class, + AxisAlignedBB.getBoundingBox(startX, startY, startZ, endX, endY, endZ)); + } + + public boolean shouldOutput() { + return shouldOutput; + } + + public void setShouldOutput(boolean shouldOutputSignal) { + shouldOutput = shouldOutputSignal; updateBlocks(); } - public void updateBlocks() { - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.notifyBlocksOfNeighborChange(xCoord - 1, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.notifyBlocksOfNeighborChange(xCoord + 1, yCoord, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord - 1, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord + 1, zCoord, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord - 1, worldObj.getBlock(xCoord, yCoord, zCoord)); - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord + 1, worldObj.getBlock(xCoord, yCoord, zCoord)); + private void updateBlocks() { + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, blockType); + for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { + worldObj.notifyBlocksOfNeighborChange( + xCoord + direction.offsetX, + yCoord + direction.offsetY, + zCoord + direction.offsetZ, + blockType); + } } - public boolean isPlayerListed(String name) { - if (name == null) return false; + private boolean isPlayerListed(String name) { + if (name == null) { + return false; + } - for (int i = 0; i < names.length; i++) { - if (names[i].equals(name)) return true; + for (String listedName : names) { + if (listedName.equals(name)) { + return true; + } } return false; } @@ -121,39 +125,40 @@ public class TilePlayerDetectorAdvanced extends TileEntity implements IInventory } @Override - public ItemStack getStackInSlot(int i) { - return items[i]; + public ItemStack getStackInSlot(int slot) { + return items[slot]; } @Override - public ItemStack decrStackSize(int i, int count) { - ItemStack itemstack = getStackInSlot(i); - - if (itemstack != null) { - if (itemstack.stackSize <= count) { - setInventorySlotContents(i, null); + public ItemStack decrStackSize(int slot, int count) { + ItemStack stack = items[slot]; + if (stack != null) { + if (stack.stackSize <= count) { + setInventorySlotContents(slot, null); } else { - itemstack = itemstack.splitStack(count); - if (itemstack.stackSize == 0) { - setInventorySlotContents(i, null); + stack = stack.splitStack(count); + if (stack.stackSize == 0) { + setInventorySlotContents(slot, null); } } } - return itemstack; + return stack; } @Override - public ItemStack getStackInSlotOnClosing(int i) { - ItemStack item = getStackInSlot(i); - if (item != null) setInventorySlotContents(i, null); - return item; + public ItemStack getStackInSlotOnClosing(int slot) { + ItemStack stack = getStackInSlot(slot); + if (stack != null) { + setInventorySlotContents(slot, null); + } + return stack; } @Override - public void setInventorySlotContents(int i, ItemStack itemstack) { - items[i] = itemstack; - if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) { - itemstack.stackSize = getInventoryStackLimit(); + public void setInventorySlotContents(int slot, ItemStack stack) { + items[slot] = stack; + if (stack != null && stack.stackSize > getInventoryStackLimit()) { + stack.stackSize = getInventoryStackLimit(); } worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); } @@ -191,7 +196,7 @@ public class TilePlayerDetectorAdvanced extends TileEntity implements IInventory public void closeInventory() {} @Override - public boolean isItemValidForSlot(int i, ItemStack itemstack) { + public boolean isItemValidForSlot(int slot, ItemStack stack) { return false; } @@ -206,11 +211,10 @@ public class TilePlayerDetectorAdvanced extends TileEntity implements IInventory @Override public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) { readFromNBT(pkt.func_148857_g()); - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); } - public Container getGuiContainer(InventoryPlayer inventoryplayer) { - return new ContainerPlayerDetector(inventoryplayer, this); + public Container getGuiContainer(InventoryPlayer playerInventory) { + return new ContainerPlayerDetector(playerInventory, this); } @Override @@ -232,10 +236,10 @@ public class TilePlayerDetectorAdvanced extends TileEntity implements IInventory compound.setString("Name_" + i, name); } - compound.setBoolean("WhiteList", whiteList); - compound.setBoolean("Output", output); + compound.setBoolean("WhiteList", isInWhiteListMode); + compound.setBoolean("Output", shouldOutput); compound.setInteger("Range", range); - compound.setBoolean("OutputInverted", outputInverted); + compound.setBoolean("OutputInverted", isOutputInverted); super.writeToNBT(compound); } @@ -251,10 +255,10 @@ public class TilePlayerDetectorAdvanced extends TileEntity implements IInventory for (int i = 0; i < names.length; i++) names[i] = compound.getString("Name_" + i); - whiteList = compound.getBoolean("WhiteList"); + isInWhiteListMode = compound.getBoolean("WhiteList"); range = compound.getInteger("Range"); - output = compound.getBoolean("Output"); - outputInverted = compound.getBoolean("OutputInverted"); + shouldOutput = compound.getBoolean("Output"); + isOutputInverted = compound.getBoolean("OutputInverted"); super.readFromNBT(compound); }