From 3488a39016311c0a1cc669b90d8b4ca5aeb98641 Mon Sep 17 00:00:00 2001 From: mezz Date: Sat, 20 Jun 2015 01:45:12 -0700 Subject: [PATCH] Fix #295 Display energy stored on machines correctly --- .../java/forestry/core/config/Config.java | 6 +- .../forestry/core/fluids/TankManager.java | 10 ++-- .../forestry/core/gadgets/TileForestry.java | 4 +- .../forestry/core/gadgets/TilePowered.java | 16 ++++++ .../forestry/core/gui/ContainerForestry.java | 10 ++++ .../java/forestry/core/gui/ContainerTile.java | 29 ++++++++-- .../java/forestry/core/gui/GuiForestry.java | 2 +- .../core/network/PacketGuiEnergy.java | 57 +++++++++++++++++++ .../forestry/core/network/PacketHandler.java | 8 ++- .../java/forestry/core/network/PacketId.java | 3 +- ...eger.java => PacketProgressBarUpdate.java} | 12 ++-- .../factory/gui/ContainerCarpenter.java | 8 +-- .../factory/gui/ContainerWorktable.java | 7 +-- 13 files changed, 135 insertions(+), 37 deletions(-) create mode 100644 src/main/java/forestry/core/network/PacketGuiEnergy.java rename src/main/java/forestry/core/network/{PacketGuiInteger.java => PacketProgressBarUpdate.java} (80%) diff --git a/src/main/java/forestry/core/config/Config.java b/src/main/java/forestry/core/config/Config.java index 472b220c8..1c717b425 100755 --- a/src/main/java/forestry/core/config/Config.java +++ b/src/main/java/forestry/core/config/Config.java @@ -92,7 +92,7 @@ public class Config { // Hints public static boolean disableHints = false; public static final HashMap hints = new HashMap(); - public static boolean disableEnergyStat = false; + public static boolean enableEnergyStat = true; public static boolean isStructureEnabled(String uid) { return !Config.disabledStructures.contains(uid); @@ -237,10 +237,10 @@ public class Config { Config.disableHints = Boolean.parseBoolean(property.value); property = config.get("tweaks.energystat.disabled", CATEGORY_COMMON, true); property.comment = "set to true to disable energy statistics on energy consumers."; - Config.disableEnergyStat = !isDebug && Boolean.parseBoolean(property.value); + Config.enableEnergyStat = !Boolean.parseBoolean(property.value); property = config.get("tweaks.tooltip.liquidamount.disabled", CATEGORY_COMMON, false); property.comment = "set to true to disable displaying liquid amounts in tank tooltips."; - Config.tooltipLiquidAmount = isDebug || !Boolean.parseBoolean(property.value); + Config.tooltipLiquidAmount = !Boolean.parseBoolean(property.value); property = config.get("tweaks.permissions.disabled", CATEGORY_COMMON, false); property.comment = "set to true to disable access restrictions on forestry machines."; diff --git a/src/main/java/forestry/core/fluids/TankManager.java b/src/main/java/forestry/core/fluids/TankManager.java index d7bafa10d..b1aa35a3d 100644 --- a/src/main/java/forestry/core/fluids/TankManager.java +++ b/src/main/java/forestry/core/fluids/TankManager.java @@ -37,7 +37,7 @@ import net.minecraftforge.fluids.IFluidHandler; import forestry.core.fluids.tanks.FakeTank; import forestry.core.fluids.tanks.StandardTank; import forestry.core.inventory.ITileFilter; -import forestry.core.network.PacketGuiInteger; +import forestry.core.network.PacketProgressBarUpdate; import forestry.core.proxy.Proxies; import forestry.core.utils.NBTUtil; import forestry.core.utils.NBTUtil.NBTList; @@ -177,7 +177,7 @@ public class TankManager extends ForwardingList implements IFluidH } player.sendProgressBarUpdate(container, tankIndex * NETWORK_DATA, fluidId); - PacketGuiInteger packet = new PacketGuiInteger(container.windowId, tankIndex * NETWORK_DATA + 1, fluidAmount); + PacketProgressBarUpdate packet = new PacketProgressBarUpdate(container.windowId, tankIndex * NETWORK_DATA + 1, fluidAmount); Proxies.net.sendToPlayer(packet, (EntityPlayerMP) player); } @@ -203,18 +203,18 @@ public class TankManager extends ForwardingList implements IFluidH fluidAmount = fluidStack.amount; } player.sendProgressBarUpdate(container, tankIndex * NETWORK_DATA, fluidId); - PacketGuiInteger packet = new PacketGuiInteger(container.windowId, tankIndex * NETWORK_DATA + 1, fluidAmount); + PacketProgressBarUpdate packet = new PacketProgressBarUpdate(container.windowId, tankIndex * NETWORK_DATA + 1, fluidAmount); Proxies.net.sendToPlayer(packet, player); } else if (fluidStack != null && prev != null) { if (fluidStack.getFluid() != prev.getFluid()) { player.sendProgressBarUpdate(container, tankIndex * NETWORK_DATA, fluidStack.getFluid().getID()); } if (fluidStack.amount != prev.amount) { - PacketGuiInteger packet = new PacketGuiInteger(container.windowId, tankIndex * NETWORK_DATA + 1, fluidStack.amount); + PacketProgressBarUpdate packet = new PacketProgressBarUpdate(container.windowId, tankIndex * NETWORK_DATA + 1, fluidStack.amount); Proxies.net.sendToPlayer(packet, player); } if (color != pColor) { - PacketGuiInteger packet = new PacketGuiInteger(container.windowId, tankIndex * NETWORK_DATA + 2, color); + PacketProgressBarUpdate packet = new PacketProgressBarUpdate(container.windowId, tankIndex * NETWORK_DATA + 2, color); Proxies.net.sendToPlayer(packet, player); } } diff --git a/src/main/java/forestry/core/gadgets/TileForestry.java b/src/main/java/forestry/core/gadgets/TileForestry.java index c5abd33ab..7d91aaa14 100755 --- a/src/main/java/forestry/core/gadgets/TileForestry.java +++ b/src/main/java/forestry/core/gadgets/TileForestry.java @@ -248,7 +248,7 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IR Proxies.net.sendToPlayer(packet, player); } - public final void writeGuiData(DataOutputStreamForestry data) throws IOException { + public void writeGuiData(DataOutputStreamForestry data) throws IOException { writeErrorData(data); if (owner == null) { @@ -261,7 +261,7 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IR } } - public final void readGuiData(DataInputStreamForestry data) throws IOException { + public void readGuiData(DataInputStreamForestry data) throws IOException { readErrorData(data); byte accessOrdinal = data.readByte(); diff --git a/src/main/java/forestry/core/gadgets/TilePowered.java b/src/main/java/forestry/core/gadgets/TilePowered.java index 4b605a27e..81c4e7e2e 100644 --- a/src/main/java/forestry/core/gadgets/TilePowered.java +++ b/src/main/java/forestry/core/gadgets/TilePowered.java @@ -10,6 +10,8 @@ ******************************************************************************/ package forestry.core.gadgets; +import java.io.IOException; + import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; @@ -19,6 +21,8 @@ import cpw.mods.fml.common.Optional; import forestry.core.EnumErrorCode; import forestry.core.interfaces.IPowerHandler; import forestry.core.interfaces.IRenderableMachine; +import forestry.core.network.DataInputStreamForestry; +import forestry.core.network.DataOutputStreamForestry; import forestry.core.utils.EnumTankLevel; import forestry.energy.EnergyManager; @@ -92,6 +96,18 @@ public abstract class TilePowered extends TileBase implements IRenderableMachine energyManager.readFromNBT(nbt); } + @Override + public void writeGuiData(DataOutputStreamForestry data) throws IOException { + super.writeGuiData(data); + energyManager.writeData(data); + } + + @Override + public void readGuiData(DataInputStreamForestry data) throws IOException { + super.readGuiData(data); + energyManager.readData(data); + } + // / ADDITIONAL LIQUID HANDLING @Override public EnumTankLevel getPrimaryLevel() { diff --git a/src/main/java/forestry/core/gui/ContainerForestry.java b/src/main/java/forestry/core/gui/ContainerForestry.java index 37b686910..6b599b4e8 100644 --- a/src/main/java/forestry/core/gui/ContainerForestry.java +++ b/src/main/java/forestry/core/gui/ContainerForestry.java @@ -17,6 +17,8 @@ import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import forestry.core.gui.slots.SlotForestry; +import forestry.core.network.ForestryPacket; +import forestry.core.proxy.Proxies; import forestry.core.utils.SlotHelper; public abstract class ContainerForestry extends Container { @@ -62,4 +64,12 @@ public abstract class ContainerForestry extends Container { } protected abstract boolean canAccess(EntityPlayer player); + + protected final void sendPacketToCrafters(ForestryPacket packet) { + for (Object crafter : crafters) { + if (crafter instanceof EntityPlayer) { + Proxies.net.sendToPlayer(packet, (EntityPlayer) crafter); + } + } + } } diff --git a/src/main/java/forestry/core/gui/ContainerTile.java b/src/main/java/forestry/core/gui/ContainerTile.java index 5f23173fc..5372c667e 100644 --- a/src/main/java/forestry/core/gui/ContainerTile.java +++ b/src/main/java/forestry/core/gui/ContainerTile.java @@ -17,8 +17,10 @@ import net.minecraft.entity.player.InventoryPlayer; import forestry.api.core.IErrorState; import forestry.core.gadgets.TileForestry; +import forestry.core.interfaces.IPowerHandler; import forestry.core.network.PacketErrorUpdate; -import forestry.core.proxy.Proxies; +import forestry.core.network.PacketGuiEnergy; +import forestry.energy.EnergyManager; public class ContainerTile extends ContainerForestry { @@ -41,6 +43,7 @@ public class ContainerTile extends ContainerForestry { } private ImmutableSet previousErrorStates; + private int previousEnergyManagerData = 0; @Override public void detectAndSendChanges() { @@ -50,13 +53,27 @@ public class ContainerTile extends ContainerForestry { if ((previousErrorStates != null) && !errorStates.equals(previousErrorStates)) { PacketErrorUpdate packet = new PacketErrorUpdate(tile); - for (Object crafter : crafters) { - if (crafter instanceof EntityPlayer) { - Proxies.net.sendToPlayer(packet, (EntityPlayer) crafter); - } - } + sendPacketToCrafters(packet); } previousErrorStates = errorStates; + + if (tile instanceof IPowerHandler) { + EnergyManager energyManager = ((IPowerHandler) tile).getEnergyManager(); + int energyManagerData = energyManager.toGuiInt(); + if (energyManagerData != previousEnergyManagerData) { + PacketGuiEnergy packet = new PacketGuiEnergy(windowId, energyManagerData); + sendPacketToCrafters(packet); + + previousEnergyManagerData = energyManagerData; + } + } + } + + public void onGuiEnergy(int energyStored) { + if (tile instanceof IPowerHandler) { + EnergyManager energyManager = ((IPowerHandler) tile).getEnergyManager(); + energyManager.fromGuiInt(energyStored); + } } } diff --git a/src/main/java/forestry/core/gui/GuiForestry.java b/src/main/java/forestry/core/gui/GuiForestry.java index f662baa05..4888fa3a9 100755 --- a/src/main/java/forestry/core/gui/GuiForestry.java +++ b/src/main/java/forestry/core/gui/GuiForestry.java @@ -89,7 +89,7 @@ public abstract class GuiForestry ext ledgerManager.add(new ClimateLedger(ledgerManager, (IClimatised) inventory)); } - if (!Config.disableEnergyStat && inventory instanceof IPowerHandler && ((IPowerHandler) inventory).getEnergyManager().getMaxEnergyStored() > 0) { + if (Config.enableEnergyStat && inventory instanceof IPowerHandler && ((IPowerHandler) inventory).getEnergyManager().getMaxEnergyStored() > 0) { ledgerManager.add(new PowerLedger(ledgerManager, (IPowerHandler) inventory)); } diff --git a/src/main/java/forestry/core/network/PacketGuiEnergy.java b/src/main/java/forestry/core/network/PacketGuiEnergy.java new file mode 100644 index 000000000..3d7b5b259 --- /dev/null +++ b/src/main/java/forestry/core/network/PacketGuiEnergy.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.network; + +import java.io.IOException; + +import net.minecraft.client.entity.EntityClientPlayerMP; + +import cpw.mods.fml.client.FMLClientHandler; + +import forestry.core.gui.ContainerTile; + +public class PacketGuiEnergy extends ForestryPacket { + + private int windowId; + private int value; + + public static void onPacketData(DataInputStreamForestry data) throws IOException { + new PacketGuiEnergy(data); + } + + public PacketGuiEnergy(DataInputStreamForestry data) throws IOException { + super(data); + } + + public PacketGuiEnergy(int windowId, int value) { + super(PacketId.GUI_ENERGY); + this.windowId = windowId; + this.value = value; + } + + @Override + public void writeData(DataOutputStreamForestry data) throws IOException { + data.writeVarInt(windowId); + data.writeVarInt(value); + } + + @Override + protected void readData(DataInputStreamForestry data) throws IOException { + windowId = data.readVarInt(); + value = data.readVarInt(); + + EntityClientPlayerMP player = FMLClientHandler.instance().getClient().thePlayer; + + if (player.openContainer instanceof ContainerTile && player.openContainer.windowId == windowId) { + ((ContainerTile) player.openContainer).onGuiEnergy(value); + } + } +} diff --git a/src/main/java/forestry/core/network/PacketHandler.java b/src/main/java/forestry/core/network/PacketHandler.java index 9367ab031..d0bb8b503 100755 --- a/src/main/java/forestry/core/network/PacketHandler.java +++ b/src/main/java/forestry/core/network/PacketHandler.java @@ -145,14 +145,18 @@ public class PacketHandler { onGenomeTrackerUpdate(packet); return true; } - case GUI_INTEGER: { - PacketGuiInteger packet = new PacketGuiInteger(data); + case GUI_PROGRESS_BAR: { + PacketProgressBarUpdate.onPacketData(data); return true; } case GUI_ITEMSTACK: { PacketItemStackDisplay.onPacketData(data); return true; } + case GUI_ENERGY: { + PacketGuiEnergy.onPacketData(data); + return true; + } default: { for (IPacketHandler handler : PluginManager.packetHandlers) { if (handler.onPacketData(packetId, data, player)) { diff --git a/src/main/java/forestry/core/network/PacketId.java b/src/main/java/forestry/core/network/PacketId.java index cb9cbc376..2dcdf8a03 100644 --- a/src/main/java/forestry/core/network/PacketId.java +++ b/src/main/java/forestry/core/network/PacketId.java @@ -28,8 +28,9 @@ public enum PacketId { IMPRINT_SELECTION_GET, GUI_SELECTION_SET, GUI_LAYOUT_SELECT, - GUI_INTEGER, + GUI_PROGRESS_BAR, GUI_ITEMSTACK, + GUI_ENERGY, PIPETTE_CLICK, diff --git a/src/main/java/forestry/core/network/PacketGuiInteger.java b/src/main/java/forestry/core/network/PacketProgressBarUpdate.java similarity index 80% rename from src/main/java/forestry/core/network/PacketGuiInteger.java rename to src/main/java/forestry/core/network/PacketProgressBarUpdate.java index f2e0bc256..54192228e 100644 --- a/src/main/java/forestry/core/network/PacketGuiInteger.java +++ b/src/main/java/forestry/core/network/PacketProgressBarUpdate.java @@ -20,16 +20,20 @@ import cpw.mods.fml.client.FMLClientHandler; * @author CovertJaguar */ -public class PacketGuiInteger extends ForestryPacket { +public class PacketProgressBarUpdate extends ForestryPacket { private int windowId, dataId, value; - public PacketGuiInteger(DataInputStreamForestry data) throws IOException { + public static void onPacketData(DataInputStreamForestry data) throws IOException { + new PacketProgressBarUpdate(data); + } + + public PacketProgressBarUpdate(DataInputStreamForestry data) throws IOException { super(data); } - public PacketGuiInteger(int windowId, int dataId, int value) { - super(PacketId.GUI_INTEGER); + public PacketProgressBarUpdate(int windowId, int dataId, int value) { + super(PacketId.GUI_PROGRESS_BAR); this.windowId = windowId; this.dataId = dataId; this.value = value; diff --git a/src/main/java/forestry/factory/gui/ContainerCarpenter.java b/src/main/java/forestry/factory/gui/ContainerCarpenter.java index 26730b855..7d937e275 100755 --- a/src/main/java/forestry/factory/gui/ContainerCarpenter.java +++ b/src/main/java/forestry/factory/gui/ContainerCarpenter.java @@ -10,7 +10,6 @@ ******************************************************************************/ package forestry.factory.gui; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; @@ -23,7 +22,6 @@ import forestry.core.gui.slots.SlotLocked; import forestry.core.gui.slots.SlotOutput; import forestry.core.interfaces.IContainerCrafting; import forestry.core.network.PacketItemStackDisplay; -import forestry.core.proxy.Proxies; import forestry.factory.gadgets.MachineCarpenter; public class ContainerCarpenter extends ContainerLiquidTanks implements IContainerCrafting { @@ -74,11 +72,7 @@ public class ContainerCarpenter extends ContainerLiquidTanks i oldCraftPreview = newCraftPreview; PacketItemStackDisplay packet = new PacketItemStackDisplay(tile, newCraftPreview); - for (Object crafter : crafters) { - if (crafter instanceof EntityPlayer) { - Proxies.net.sendToPlayer(packet, (EntityPlayer) crafter); - } - } + sendPacketToCrafters(packet); } } diff --git a/src/main/java/forestry/factory/gui/ContainerWorktable.java b/src/main/java/forestry/factory/gui/ContainerWorktable.java index deba2a264..52dc930ab 100755 --- a/src/main/java/forestry/factory/gui/ContainerWorktable.java +++ b/src/main/java/forestry/factory/gui/ContainerWorktable.java @@ -23,7 +23,6 @@ import forestry.core.gui.slots.SlotCrafter; import forestry.core.interfaces.IContainerCrafting; import forestry.core.network.PacketGuiSelect; import forestry.core.network.PacketId; -import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; import forestry.core.utils.StackUtils; import forestry.factory.gadgets.TileWorktable; @@ -72,11 +71,7 @@ public class ContainerWorktable extends ContainerTile implements lastUpdate = tile.getMemory().getLastUpdate(); PacketWorktableMemoryUpdate packet = new PacketWorktableMemoryUpdate(tile); - for (Object crafter : crafters) { - if (crafter instanceof EntityPlayer) { - Proxies.net.sendToPlayer(packet, (EntityPlayer) crafter); - } - } + sendPacketToCrafters(packet); } @Override