From f2eade3b2d9a86f300715c826bbe681b14a816a4 Mon Sep 17 00:00:00 2001 From: Jackson Turner Date: Mon, 6 Jun 2016 17:52:06 -0500 Subject: [PATCH 1/2] Fixed mana batteries not keeping power when broken (fixes #1374) Mana batteries use metadata for rendering purposes (e.g in inventory) Fixed a power registry issue that would mix client and server data --- src/main/java/am2/AMClientEventHandler.java | 13 +- .../java/am2/blocks/BlockManaBattery.java | 116 +++++++----------- .../tileentities/TileEntityManaBattery.java | 7 +- .../java/am2/power/PowerNodeRegistry.java | 5 +- 4 files changed, 56 insertions(+), 85 deletions(-) diff --git a/src/main/java/am2/AMClientEventHandler.java b/src/main/java/am2/AMClientEventHandler.java index d8a03868b..b06848a54 100644 --- a/src/main/java/am2/AMClientEventHandler.java +++ b/src/main/java/am2/AMClientEventHandler.java @@ -181,14 +181,11 @@ public void onItemTooltip(ItemTooltipEvent event){ }else if (stack.getItem() instanceof ItemBlock){ if (((ItemBlock)stack.getItem()).field_150939_a == BlocksCommonProxy.manaBattery){ if (stack.hasTagCompound()){ - NBTTagList list = stack.stackTagCompound.getTagList("Lore", Constants.NBT.TAG_COMPOUND); - if (list != null){ - for (int i = 0; i < list.tagCount(); ++i){ - NBTBase tag = list.getCompoundTagAt(i); - if (tag instanceof NBTTagString){ - event.toolTip.add((((NBTTagString)tag).func_150285_a_())); - } - } + float batteryCharge = stack.stackTagCompound.getFloat("mana_battery_charge"); + PowerTypes powerType = PowerTypes.getByID(stack.stackTagCompound.getInteger("mana_battery_powertype")); + if (batteryCharge != 0) { + // TODO localize this tooltip + event.toolTip.add(String.format("\u00A7r\u00A79Contains \u00A75%.2f %s%s \u00A79etherium", batteryCharge, powerType.chatColor(), powerType.name())); } } } diff --git a/src/main/java/am2/blocks/BlockManaBattery.java b/src/main/java/am2/blocks/BlockManaBattery.java index 647b028fe..574a97ef3 100644 --- a/src/main/java/am2/blocks/BlockManaBattery.java +++ b/src/main/java/am2/blocks/BlockManaBattery.java @@ -3,11 +3,11 @@ import am2.AMCore; import am2.api.power.PowerTypes; import am2.blocks.tileentities.TileEntityManaBattery; -import am2.entities.EntityDummyCaster; import am2.power.PowerNodeRegistry; import am2.texture.ResourceManager; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; @@ -17,16 +17,12 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.nbt.NBTTagString; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatComponentText; import net.minecraft.util.IIcon; -import net.minecraft.world.Explosion; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; -import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -62,6 +58,7 @@ public boolean onBlockActivated(World par1World, int par2, int par3, int par4, E if (par1World.isRemote){ TileEntityManaBattery te = getTileEntity(par1World, par2, par3, par4); if (te != null){ + // TODO localize these messages if (AMCore.config.colourblindMode()){ par5EntityPlayer.addChatMessage(new ChatComponentText(String.format("Charge Level: %.2f %% [%s]", PowerNodeRegistry.For(par1World).getPower(te, te.getPowerType()) / te.getCapacity() * 100, getColorNameFromPowerType(te.getPowerType())))); }else{ @@ -110,58 +107,31 @@ public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, Entit } @Override - public void onBlockExploded(World world, int x, int y, int z, Explosion explosion){ - destroy(world, x, y, z); - super.onBlockExploded(world, x, y, z, explosion); - } - - @Override - public void harvestBlock(World world, EntityPlayer player, int x, int y, int z, int meta){ - destroy(world, x, y, z); - } - - @Override - public void onBlockHarvested(World par1World, int par2, int par3, int par4, int par5, EntityPlayer par6EntityPlayer){ - destroy(par1World, par2, par3, par4); - super.onBlockHarvested(par1World, par2, par3, par4, par5, par6EntityPlayer); - } - - @Override - public void onBlockPreDestroy(World par1World, int par2, int par3, int par4, int par5){ - //destroy(par1World, par2, par3, par4); - super.onBlockPreDestroy(par1World, par2, par3, par4, par5); - } - - private void destroy(World world, int i, int j, int k){ + public void breakBlock(World world, int i, int j, int k, Block theBlock, int metadata){ TileEntityManaBattery te = getTileEntity(world, i, j, k); if (te != null && !world.isRemote){ - float f = world.rand.nextFloat() * 0.8F + 0.1F; - float f1 = world.rand.nextFloat() * 0.8F + 0.1F; - float f2 = world.rand.nextFloat() * 0.8F + 0.1F; - int dmg = (int)((PowerNodeRegistry.For(world).getPower(te, te.getPowerType()) / te.getCapacity()) * 100); - if (dmg == 0) dmg = 1; - ItemStack stack = new ItemStack(this); - stack.damageItem(stack.getMaxDamage() - dmg, new EntityDummyCaster(world)); - stack.stackTagCompound = new NBTTagCompound(); - stack.stackTagCompound.setFloat("mana_battery_charge", PowerNodeRegistry.For(world).getPower(te, te.getPowerType())); - stack.stackTagCompound.setInteger("mana_battery_powertype", te.getPowerType().ID()); - - if (!stack.stackTagCompound.hasKey("Lore")) - stack.stackTagCompound.setTag("Lore", new NBTTagList()); - - NBTTagList tagList = new NBTTagList(); - PowerTypes powerType = te.getPowerType(); - float amt = PowerNodeRegistry.For(world).getPower(te, powerType); - tagList.appendTag(new NBTTagString(String.format("Contains %.2f %s%s etherium", amt, powerType.chatColor(), powerType.name()))); - stack.stackTagCompound.setTag("Lore", tagList); - - EntityItem entityitem = new EntityItem(world, i + f, j + f1, k + f2, stack); - float f3 = 0.05F; - entityitem.motionX = (float)world.rand.nextGaussian() * f3; - entityitem.motionY = (float)world.rand.nextGaussian() * f3 + 0.2F; - entityitem.motionZ = (float)world.rand.nextGaussian() * f3; - world.spawnEntityInWorld(entityitem); + if (PowerNodeRegistry.For(world).getPower(te, te.getPowerType()) != 0.0f){ + float f = world.rand.nextFloat() * 0.8F + 0.1F; + float f1 = world.rand.nextFloat() * 0.8F + 0.1F; + float f2 = world.rand.nextFloat() * 0.8F + 0.1F; + ItemStack stack = new ItemStack(this, 1, metadata); + // These currently don't do anything without a custom ItemBlock, so I'm removing them temporarily + // int dmg = (int)((PowerNodeRegistry.For(world).getPower(te, te.getPowerType()) / te.getCapacity()) * 100); + // if (dmg == 0) dmg = 1; + // stack.damageItem(stack.getMaxDamage() - dmg, new EntityDummyCaster(world)); + stack.stackTagCompound = new NBTTagCompound(); + stack.stackTagCompound.setFloat("mana_battery_charge", PowerNodeRegistry.For(world).getPower(te, te.getPowerType())); + stack.stackTagCompound.setInteger("mana_battery_powertype", te.getPowerType().ID()); + + EntityItem entityitem = new EntityItem(world, i + f, j + f1, k + f2, stack); + float f3 = 0.05F; + entityitem.motionX = (float)world.rand.nextGaussian() * f3; + entityitem.motionY = (float)world.rand.nextGaussian() * f3 + 0.2F; + entityitem.motionZ = (float)world.rand.nextGaussian() * f3; + world.spawnEntityInWorld(entityitem); + } } + super.breakBlock(world, i, j, k, theBlock, metadata); } @Override @@ -188,27 +158,28 @@ public boolean hasComparatorInputOverride(){ @SideOnly(Side.CLIENT) public void getSubBlocks(Item par1, CreativeTabs par2CreativeTabs, List par3List){ ItemStack stack = new ItemStack(this); - stack.stackTagCompound = new NBTTagCompound(); - stack.stackTagCompound.setFloat("mana_battery_charge", new TileEntityManaBattery().getCapacity()); - par3List.add(stack); + for (PowerTypes type : PowerTypes.all()){ + stack = new ItemStack(this, 1, type.ID()); + stack.stackTagCompound = new NBTTagCompound(); + stack.stackTagCompound.setFloat("mana_battery_charge", new TileEntityManaBattery().getCapacity()); + stack.stackTagCompound.setInteger("mana_battery_powertype", type.ID()); + par3List.add(stack); + } } @Override public int colorMultiplier(IBlockAccess blockAccess, int x, int y, int z){ - TileEntity te = blockAccess.getTileEntity(x, y, z); - if (te instanceof TileEntityManaBattery){ - TileEntityManaBattery battery = (TileEntityManaBattery)te; - if (battery.getPowerType() == PowerTypes.DARK) - return 0x850e0e; - else if (battery.getPowerType() == PowerTypes.LIGHT) - return 0x61cfc3; - else if (battery.getPowerType() == PowerTypes.NEUTRAL) - return 0x2683d2; - else - return 0xFFFFFF; - } - return 0xFFFFFF; + int metadata = blockAccess.getBlockMetadata(x, y, z); + PowerTypes type = PowerTypes.getByID(metadata); + if (type == PowerTypes.DARK) + return 0x850e0e; + else if (type == PowerTypes.LIGHT) + return 0x61cfc3; + else if (type == PowerTypes.NEUTRAL) + return 0x2683d2; + else + return 0xFFFFFF; } @Override @@ -220,9 +191,4 @@ public boolean renderAsNormalBlock(){ public int getRenderType(){ return BlocksCommonProxy.commonBlockRenderID; } - - @Override - public ArrayList getDrops(World arg0, int arg1, int arg2, int arg3, int arg4, int arg5){ - return new ArrayList(); - } } diff --git a/src/main/java/am2/blocks/tileentities/TileEntityManaBattery.java b/src/main/java/am2/blocks/tileentities/TileEntityManaBattery.java index b45bc30d7..eff7a7c65 100644 --- a/src/main/java/am2/blocks/tileentities/TileEntityManaBattery.java +++ b/src/main/java/am2/blocks/tileentities/TileEntityManaBattery.java @@ -48,11 +48,16 @@ public void updateEntity(){ this.setNoPowerRequests(); } - if (this.outputPowerType == PowerTypes.NONE && !this.worldObj.isRemote){ + if (!this.worldObj.isRemote){ PowerTypes highest = PowerNodeRegistry.For(worldObj).getHighestPowerType(this); float amt = PowerNodeRegistry.For(worldObj).getPower(this, highest); if (amt > 0){ this.outputPowerType = highest; + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, highest.ID(), 2); + worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + }else{ + this.outputPowerType = PowerTypes.NONE; + worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 2); worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); } } diff --git a/src/main/java/am2/power/PowerNodeRegistry.java b/src/main/java/am2/power/PowerNodeRegistry.java index 4a015c1cf..be3c27d18 100644 --- a/src/main/java/am2/power/PowerNodeRegistry.java +++ b/src/main/java/am2/power/PowerNodeRegistry.java @@ -17,7 +17,8 @@ public class PowerNodeRegistry{ - private static final HashMap dimensionPowerManagers = new HashMap(); + private static final HashMap serverDimensionPowerManagers = new HashMap(); + private static final HashMap clientDimensionPowerManagers = new HashMap(); private static final PowerNodeRegistry dummyRegistry = new PowerNodeRegistry(); static final int POWER_SEARCH_RADIUS = 10; //The literal power search radius @@ -28,6 +29,8 @@ public static final PowerNodeRegistry For(World world){ if (world == null) return dummyRegistry; + HashMap dimensionPowerManagers = world.isRemote ? clientDimensionPowerManagers : serverDimensionPowerManagers; + if (dimensionPowerManagers.containsKey(world.provider.dimensionId)){ return dimensionPowerManagers.get(world.provider.dimensionId); }else{ From cec20cda6a4bee1219237a8471bcf98d156b9fdc Mon Sep 17 00:00:00 2001 From: Jackson Turner Date: Mon, 6 Jun 2016 22:38:16 -0500 Subject: [PATCH 2/2] Fixed mana battery rendering Forgot to add this to the pull request Can be improved later, but this is what gets it working --- .../java/am2/blocks/renderers/SimpleBlockRenderHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/am2/blocks/renderers/SimpleBlockRenderHandler.java b/src/main/java/am2/blocks/renderers/SimpleBlockRenderHandler.java index 584b8eb99..45baac5cc 100644 --- a/src/main/java/am2/blocks/renderers/SimpleBlockRenderHandler.java +++ b/src/main/java/am2/blocks/renderers/SimpleBlockRenderHandler.java @@ -347,7 +347,7 @@ private void renderManaBattery(IBlockAccess world, int x, int y, int z, Block bl IBlockAccess old = renderer.blockAccess; renderer.blockAccess = this.blockAccess; this.blockAccess.setControllingTileEntity(te); - this.blockAccess.setFakeBlockAndMeta(BlocksCommonProxy.manaBattery, 0); + this.blockAccess.setFakeBlockAndMeta(BlocksCommonProxy.manaBattery, te.getPowerType().ID()); this.blockAccess.setOuterBlockAccess(world); this.blockAccess.setOverrideCoords(x, y, z); renderer.renderStandardBlock(BlocksCommonProxy.manaBattery, x, y, z);