From ebfde0a3da117fc9db66675343a68f0332265f0d Mon Sep 17 00:00:00 2001 From: CrazyPants Date: Tue, 29 Oct 2013 16:02:38 +0800 Subject: [PATCH] #192 Transceivers remember their channel when broken. --- .../machine/hypercube/BlockHyperCube.java | 71 ++++++++++++++----- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/common/crazypants/enderio/machine/hypercube/BlockHyperCube.java b/common/crazypants/enderio/machine/hypercube/BlockHyperCube.java index 38acafb4..dc62fe3f 100644 --- a/common/crazypants/enderio/machine/hypercube/BlockHyperCube.java +++ b/common/crazypants/enderio/machine/hypercube/BlockHyperCube.java @@ -13,6 +13,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Icon; import net.minecraft.world.IBlockAccess; @@ -107,7 +108,7 @@ public class BlockHyperCube extends Block implements ITileEntityProvider, IGuiHa @Override public void onBlockAdded(World world, int x, int y, int z) { - if(world.isRemote) { + if (world.isRemote) { return; } TileHyperCube tr = (TileHyperCube) world.getBlockTileEntity(x, y, z); @@ -116,7 +117,7 @@ public class BlockHyperCube extends Block implements ITileEntityProvider, IGuiHa @Override public void onNeighborBlockChange(World world, int x, int y, int z, int blockId) { - if(world.isRemote) { + if (world.isRemote) { return; } TileHyperCube te = (TileHyperCube) world.getBlockTileEntity(x, y, z); @@ -126,28 +127,65 @@ public class BlockHyperCube extends Block implements ITileEntityProvider, IGuiHa @Override public ArrayList getBlockDropped(World world, int x, int y, int z, int metadata, int fortune) { ArrayList ret = new ArrayList(); - if(!world.isRemote) { + if (!world.isRemote) { TileEntity te = world.getBlockTileEntity(x, y, z); - if(te instanceof TileHyperCube) { + if (te instanceof TileHyperCube) { TileHyperCube hc = (TileHyperCube) te; hc.onBreakBlock(); ItemStack itemStack = new ItemStack(this); PowerHandlerUtil.setStoredEnergyForItem(itemStack, hc.getInternalPowerHandler().getEnergyStored()); + setChannelOnItem(hc, itemStack); ret.add(itemStack); } } return ret; } + private void setChannelOnItem(TileHyperCube hc, ItemStack itemStack) { + Channel chan = hc.getChannel(); + if (chan != null) { + NBTTagCompound tag = itemStack.getTagCompound(); + if (tag == null) { + tag = new NBTTagCompound(); + itemStack.setTagCompound(tag); + } + tag.setString("channelName", chan.name); + tag.setBoolean("channelIsPublic", chan.isPublic()); + if(!chan.isPublic()) { + tag.setString("channelUser", chan.user); + } + } + } + + private Channel getChannelFromItem(ItemStack itemStack) { + NBTTagCompound tag = itemStack.getTagCompound(); + if(tag == null) { + return null; + } + + String channelName = tag.getString("channelName"); + if(channelName == null) { + return null; + } + + String user = null; + if(!tag.getBoolean("channelIsPublic")) { + user = tag.getString("channelUser"); + } + return new Channel(channelName, user); + + } + @Override public boolean removeBlockByPlayer(World world, EntityPlayer player, int x, int y, int z) { - if(!world.isRemote) { + if (!world.isRemote) { TileEntity te = world.getBlockTileEntity(x, y, z); - if(te instanceof TileHyperCube) { + if (te instanceof TileHyperCube) { TileHyperCube hc = (TileHyperCube) te; hc.onBreakBlock(); ItemStack itemStack = new ItemStack(this); PowerHandlerUtil.setStoredEnergyForItem(itemStack, hc.getInternalPowerHandler().getEnergyStored()); + setChannelOnItem(hc, itemStack); float f = 0.7F; double d0 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D; double d1 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D; @@ -162,16 +200,17 @@ public class BlockHyperCube extends Block implements ITileEntityProvider, IGuiHa @Override public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack stack) { - if(world.isRemote) { + if (world.isRemote) { return; } TileEntity te = world.getBlockTileEntity(x, y, z); - if(te instanceof TileHyperCube) { + if (te instanceof TileHyperCube) { TileHyperCube cb = (TileHyperCube) te; cb.getInternalPowerHandler().setEnergy(PowerHandlerUtil.getStoredEnergyForItem(stack)); - if(player instanceof EntityPlayerMP) { + if (player instanceof EntityPlayerMP) { cb.setOwner(((EntityPlayerMP) player).username); } + cb.setChannel(getChannelFromItem(stack)); } world.markBlockForUpdate(x, y, z); } @@ -189,23 +228,23 @@ public class BlockHyperCube extends Block implements ITileEntityProvider, IGuiHa @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) { - if(ConduitUtil.isToolEquipped(entityPlayer) && entityPlayer.isSneaking()) { - if(entityPlayer.getCurrentEquippedItem().getItem() instanceof IToolWrench) { + if (ConduitUtil.isToolEquipped(entityPlayer) && entityPlayer.isSneaking()) { + if (entityPlayer.getCurrentEquippedItem().getItem() instanceof IToolWrench) { IToolWrench wrench = (IToolWrench) entityPlayer.getCurrentEquippedItem().getItem(); - if(wrench.canWrench(entityPlayer, x, y, z)) { + if (wrench.canWrench(entityPlayer, x, y, z)) { removeBlockByPlayer(world, entityPlayer, x, y, z); - if(entityPlayer.getCurrentEquippedItem().getItem() instanceof IToolWrench) { + if (entityPlayer.getCurrentEquippedItem().getItem() instanceof IToolWrench) { ((IToolWrench) entityPlayer.getCurrentEquippedItem().getItem()).wrenchUsed(entityPlayer, x, y, z); } return true; } } } - if(entityPlayer.isSneaking()) { + if (entityPlayer.isSneaking()) { return false; } TileEntity te = world.getBlockTileEntity(x, y, z); - if(!(te instanceof TileHyperCube)) { + if (!(te instanceof TileHyperCube)) { return false; } entityPlayer.openGui(EnderIO.instance, GuiHandler.GUI_ID_HYPER_CUBE, world, x, y, z); @@ -220,7 +259,7 @@ public class BlockHyperCube extends Block implements ITileEntityProvider, IGuiHa @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity te = world.getBlockTileEntity(x, y, z); - if(te instanceof TileHyperCube) { + if (te instanceof TileHyperCube) { TileHyperCube hc = (TileHyperCube) te; return new GuiHyperCube(hc); }