diff --git a/src/main/java/forestry/apiculture/gadgets/TileAlveary.java b/src/main/java/forestry/apiculture/gadgets/TileAlveary.java index 933e4f9e9..1600f9a0c 100755 --- a/src/main/java/forestry/apiculture/gadgets/TileAlveary.java +++ b/src/main/java/forestry/apiculture/gadgets/TileAlveary.java @@ -10,6 +10,9 @@ ******************************************************************************/ package forestry.apiculture.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.Collection; import java.util.LinkedList; @@ -31,7 +34,6 @@ import forestry.core.config.ForestryBlock; import forestry.core.gadgets.TileForestry; import forestry.core.inventory.FakeInventoryAdapter; import forestry.core.inventory.IInventoryAdapter; -import forestry.core.network.PacketPayload; import forestry.core.proxy.Proxies; import buildcraft.api.statements.ITriggerExternal; @@ -106,24 +108,23 @@ public abstract class TileAlveary extends TileForestry implements IAlvearyCompon } @Override - public PacketPayload getPacketPayload() { - PacketPayload payload = new PacketPayload(0, 2); - payload.shortPayload[0] = (short) (isMaster() ? 1 : 0); + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeBoolean(isMaster); // so the client can know if it is part of an integrated structure - payload.shortPayload[1] = (short) this.masterY; - - return payload; + data.writeShort(masterY); } @Override - public void fromPacketPayload(PacketPayload payload) { - if (payload.shortPayload[0] > 0) { + public void readData(DataInputStream data) throws IOException { + super.readData(data); + if (data.readBoolean()) { makeMaster(); } // so the client can know if it is part of an integrated structure - this.masterY = payload.shortPayload[1]; + masterY = data.readShort(); } /* ITILESTRUCTURE */ diff --git a/src/main/java/forestry/apiculture/gadgets/TileAlvearyClimatiser.java b/src/main/java/forestry/apiculture/gadgets/TileAlvearyClimatiser.java index 7af6b1e1c..f8693a4a4 100755 --- a/src/main/java/forestry/apiculture/gadgets/TileAlvearyClimatiser.java +++ b/src/main/java/forestry/apiculture/gadgets/TileAlvearyClimatiser.java @@ -10,12 +10,15 @@ ******************************************************************************/ package forestry.apiculture.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import forestry.api.apiculture.IAlvearyComponent; -import forestry.core.network.PacketPayload; import forestry.energy.EnergyManager; import cofh.api.energy.IEnergyHandler; @@ -109,24 +112,23 @@ public abstract class TileAlvearyClimatiser extends TileAlveary implements IEner nbttagcompound.setInteger("Heating", workingTime); } - /* NETWORK */ @Override - public void fromPacketPayload(PacketPayload payload) { - short workingTime = payload.shortPayload[0]; + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeShort(workingTime); + } + + @Override + public void readData(DataInputStream data) throws IOException { + super.readData(data); + short workingTime = data.readShort(); if (this.workingTime != workingTime) { this.workingTime = workingTime; worldObj.func_147479_m(xCoord, yCoord, zCoord); } } - @Override - public PacketPayload getPacketPayload() { - PacketPayload payload = new PacketPayload(0, 1); - payload.shortPayload[0] = (short) workingTime; - return payload; - } - /* IEnergyHandler */ @Override public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) { diff --git a/src/main/java/forestry/apiculture/gadgets/TileAlvearyPlain.java b/src/main/java/forestry/apiculture/gadgets/TileAlvearyPlain.java index 9ef234f68..1b97a9145 100755 --- a/src/main/java/forestry/apiculture/gadgets/TileAlvearyPlain.java +++ b/src/main/java/forestry/apiculture/gadgets/TileAlvearyPlain.java @@ -48,10 +48,9 @@ import forestry.core.inventory.IInventoryAdapter; import forestry.core.inventory.InvTools; import forestry.core.inventory.TileInventoryAdapter; import forestry.core.network.GuiId; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.network.PacketInventoryStack; import forestry.core.proxy.Proxies; -import forestry.core.utils.Utils; public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, IBeeHousing, IClimatised, IHintSource { @@ -59,7 +58,7 @@ public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, IB public static final int SLOT_QUEEN = 0; public static final int SLOT_DRONE = 1; public static final int SLOT_PRODUCT_1 = 2; - public static final int SLOT_PRODUCTION_COUNT = 7; + public static final int SLOT_PRODUCT_COUNT = 7; public static final int BLOCK_META = 0; // / MEMBERS @@ -444,7 +443,7 @@ public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, IB return false; } - return InvTools.tryAddStack(inventory, product, SLOT_PRODUCT_1, inventory.getSizeInventory() - SLOT_PRODUCT_1, all); + return InvTools.tryAddStack(inventory, product, SLOT_PRODUCT_1, SLOT_PRODUCT_COUNT, all); } @Deprecated @@ -469,8 +468,8 @@ public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, IB return; } - Proxies.net.sendNetworkPacket(new PacketInventoryStack(PacketIds.IINVENTORY_STACK, xCoord, yCoord, zCoord, SLOT_QUEEN, queenStack), xCoord, yCoord, - zCoord); + PacketInventoryStack packet = new PacketInventoryStack(PacketId.IINVENTORY_STACK, xCoord, yCoord, zCoord, SLOT_QUEEN, queenStack); + Proxies.net.sendNetworkPacket(packet); for (IBeeListener eventHandler : eventHandlers) { eventHandler.onQueenChange(queenStack); @@ -654,16 +653,10 @@ public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, IB return Config.hints.get("alveary"); } - /* IOWNABLE */ - @Override - public boolean isOwnable() { - return true; - } - /* IHousing */ @Override public GameProfile getOwnerName() { - return this.getOwnerProfile(); + return this.getOwner(); } private static class AlvearyInventoryAdapter extends TileInventoryAdapter { diff --git a/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java b/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java index 53f6b7adb..9024548c7 100755 --- a/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java +++ b/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java @@ -10,6 +10,9 @@ ******************************************************************************/ package forestry.apiculture.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.Map.Entry; import java.util.Stack; @@ -32,7 +35,6 @@ import forestry.core.inventory.TileInventoryAdapter; import forestry.core.inventory.wrappers.IInvSlot; import forestry.core.inventory.wrappers.InventoryIterator; import forestry.core.network.GuiId; -import forestry.core.network.PacketPayload; import forestry.core.utils.StackUtils; public class TileAlvearySwarmer extends TileAlveary implements ISidedInventory { @@ -41,7 +43,7 @@ public class TileAlvearySwarmer extends TileAlveary implements ISidedInventory { public static final int BLOCK_META = 2; private final Stack pendingSpawns = new Stack(); - private boolean isActive; + private boolean active; public TileAlvearySwarmer() { super(BLOCK_META); @@ -144,27 +146,28 @@ public class TileAlvearySwarmer extends TileAlveary implements ISidedInventory { } private void setIsActive(boolean isActive) { - if (this.isActive != isActive) { - this.isActive = isActive; + if (this.active != isActive) { + this.active = isActive; setNeedsNetworkUpdate(); } } /* NETWORK */ + @Override - public void fromPacketPayload(PacketPayload payload) { - boolean act = payload.shortPayload[0] > 0; - if (this.isActive != act) { - this.isActive = act; - worldObj.func_147479_m(xCoord, yCoord, zCoord); - } + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeBoolean(active); } @Override - public PacketPayload getPacketPayload() { - PacketPayload payload = new PacketPayload(0, 1); - payload.shortPayload[0] = (short) (isActive ? 1 : 0); - return payload; + public void readData(DataInputStream data) throws IOException { + super.readData(data); + boolean active = data.readBoolean(); + if (this.active != active) { + this.active = active; + worldObj.func_147479_m(xCoord, yCoord, zCoord); + } } @Override @@ -179,7 +182,7 @@ public class TileAlvearySwarmer extends TileAlveary implements ISidedInventory { return BlockAlveary.BOTTOM; } - if (isActive) { + if (active) { return BlockAlveary.ALVEARY_SWARMER_ON; } else { return BlockAlveary.ALVEARY_SWARMER_OFF; diff --git a/src/main/java/forestry/apiculture/gadgets/TileBeehouse.java b/src/main/java/forestry/apiculture/gadgets/TileBeehouse.java index 129c19d64..33c23e29e 100755 --- a/src/main/java/forestry/apiculture/gadgets/TileBeehouse.java +++ b/src/main/java/forestry/apiculture/gadgets/TileBeehouse.java @@ -37,18 +37,16 @@ import forestry.core.EnumErrorCode; import forestry.core.config.Config; import forestry.core.config.ForestryItem; import forestry.core.gadgets.TileBase; -import forestry.core.gadgets.TileForestry; import forestry.core.interfaces.IClimatised; import forestry.core.inventory.IInventoryAdapter; import forestry.core.inventory.InvTools; import forestry.core.inventory.TileInventoryAdapter; import forestry.core.network.GuiId; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.network.PacketInventoryStack; -import forestry.core.network.PacketTileUpdate; +import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; import forestry.core.utils.GuiUtil; -import forestry.core.utils.Utils; public class TileBeehouse extends TileBase implements IBeeHousing, IClimatised { // CONSTANTS @@ -200,9 +198,8 @@ public class TileBeehouse extends TileBase implements IBeeHousing, IClimatised { return; } - Proxies.net.sendNetworkPacket(new PacketInventoryStack(PacketIds.IINVENTORY_STACK, xCoord, yCoord, zCoord, SLOT_QUEEN, queenStack), xCoord, yCoord, - zCoord); - Proxies.net.sendNetworkPacket(new PacketTileUpdate(this), xCoord, yCoord, zCoord); + Proxies.net.sendNetworkPacket(new PacketInventoryStack(PacketId.IINVENTORY_STACK, xCoord, yCoord, zCoord, SLOT_QUEEN, queenStack)); + Proxies.net.sendNetworkPacket(new PacketTileStream(this)); } /* STATE INFORMATION */ @@ -409,7 +406,7 @@ public class TileBeehouse extends TileBase implements IBeeHousing, IClimatised { /* IHousing */ @Override public GameProfile getOwnerName() { - return this.getOwnerProfile(); + return this.getOwner(); } protected static class BeehouseInventoryAdapter extends TileInventoryAdapter { diff --git a/src/main/java/forestry/apiculture/gui/ContainerImprinter.java b/src/main/java/forestry/apiculture/gui/ContainerImprinter.java index 624514c41..bb6ec36b6 100755 --- a/src/main/java/forestry/apiculture/gui/ContainerImprinter.java +++ b/src/main/java/forestry/apiculture/gui/ContainerImprinter.java @@ -20,9 +20,9 @@ import forestry.core.gui.ContainerItemInventory; import forestry.core.gui.IGuiSelectable; import forestry.core.gui.slots.SlotFiltered; import forestry.core.gui.slots.SlotOutput; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.ForestryPacket; +import forestry.core.network.PacketGuiSelect; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; public class ContainerImprinter extends ContainerItemInventory implements IGuiSelectable { @@ -57,50 +57,36 @@ public class ContainerImprinter extends ContainerItemInventory 0) { - indices = packet.payload.intPayload; - } - - worldObj.func_147479_m(xCoord, yCoord, zCoord); + Proxies.net.sendNetworkPacket(new PacketRipeningUpdate(this)); } /* IFRUITBEARER */ @@ -183,7 +158,6 @@ public class TileFruitPod extends TileEntity implements INetworkedEntity, IFruit return new ArrayList(); } - Collection fruits = Arrays.asList(getDrop()); maturity = 0; sendNetworkUpdateRipening(); @@ -201,4 +175,24 @@ public class TileFruitPod extends TileEntity implements INetworkedEntity, IFruit sendNetworkUpdateRipening(); } + @Override + public void writeData(DataOutputStream data) throws IOException { + data.writeShort(maturity); + data.writeShort(indices.length); + for (int i : indices) { + data.writeInt(i); + } + } + + @Override + public void readData(DataInputStream data) throws IOException { + maturity = data.readShort(); + int indicesLength = data.readShort(); + indices = new int[indicesLength]; + for (int i = 0; i < indicesLength; i++) { + indices[i] = data.readInt(); + } + + worldObj.func_147479_m(xCoord, yCoord, zCoord); + } } diff --git a/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java b/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java index 98b6a65a8..5b956a153 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileLeaves.java @@ -12,6 +12,9 @@ package forestry.arboriculture.gadgets; import com.google.common.collect.ImmutableSet; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -33,6 +36,7 @@ import net.minecraftforge.common.EnumPlantType; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import forestry.api.arboriculture.EnumTreeChromosome; import forestry.api.arboriculture.IAlleleTreeSpecies; import forestry.api.arboriculture.IFruitProvider; import forestry.api.arboriculture.ILeafTickHandler; @@ -43,6 +47,7 @@ import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.api.core.IErrorState; import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; import forestry.api.genetics.IEffectData; import forestry.api.genetics.IFruitBearer; import forestry.api.genetics.IFruitFamily; @@ -52,11 +57,10 @@ import forestry.api.lepidopterology.IButterfly; import forestry.api.lepidopterology.IButterflyGenome; import forestry.api.lepidopterology.IButterflyNursery; import forestry.api.lepidopterology.IButterflyRoot; -import forestry.arboriculture.network.PacketLeaf; import forestry.arboriculture.network.PacketRipeningUpdate; import forestry.core.EnumErrorCode; import forestry.core.genetics.alleles.Allele; -import forestry.core.network.ForestryPacket; +import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; import forestry.core.render.TextureManager; import forestry.core.utils.GeneticsUtil; @@ -330,13 +334,11 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru /* NETWORK */ @Override public Packet getDescriptionPacket() { - return new PacketLeaf(this).getPacket(); + return new PacketTileStream(this).getPacket(); } - @Override public void sendNetworkUpdate() { - PacketLeaf packet = new PacketLeaf(this); - Proxies.net.sendNetworkPacket(packet, xCoord, yCoord, zCoord); + Proxies.net.sendNetworkPacket(new PacketTileStream(this)); } private void sendNetworkUpdateRipening() { @@ -347,31 +349,76 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru colourFruits = newColourFruits; PacketRipeningUpdate ripeningUpdate = new PacketRipeningUpdate(this); - Proxies.net.sendNetworkPacket(ripeningUpdate, xCoord, yCoord, zCoord); + Proxies.net.sendNetworkPacket(ripeningUpdate); + } + + private static final short hasFruitFlag = 1; + private static final short isPollinatedFlag = 1 << 1; + + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + + byte leafState = 0; + boolean hasFruit = hasFruit(); + + if (isPollinated()) { + leafState |= isPollinatedFlag; + } + + if (hasFruit) { + leafState |= hasFruitFlag; + } + + data.writeByte(leafState); + + if (hasFruit) { + String fruitAlleleUID = getTree().getGenome().getActiveAllele(EnumTreeChromosome.FRUITS).getUID(); + int colourFruits = getFruitColour(); + + data.writeUTF(fruitAlleleUID); + data.writeInt(colourFruits); + } } @Override - public void fromPacket(ForestryPacket packetRaw) { - PacketLeaf packet = (PacketLeaf) packetRaw; + public void readData(DataInputStream data) throws IOException { - isFruitLeaf = packet.isFruitLeaf(); - isPollinatedState = packet.isPollinated(); + String speciesUID = data.readUTF(); // this is called instead of super.readData, be careful! - colourFruits = packet.getColourFruits(); + byte leafState = data.readByte(); + boolean hasFruit = (leafState & hasFruitFlag) > 0; + boolean isPollinated = (leafState & isPollinatedFlag) > 0; + String fruitAlleleUID = null; - ITree tree = getTree(); - if (!packet.matchesTree(tree)) { - ITree newTree = packet.getTree(); - if (newTree != null) { - setTree(newTree); + if (hasFruit) { + fruitAlleleUID = data.readUTF(); + colourFruits = data.readInt(); + } + + IAllele[] treeTemplate = PluginArboriculture.treeInterface.getTemplate(speciesUID); + + if (fruitAlleleUID != null) { + IAllele fruitAllele = AlleleManager.alleleRegistry.getAllele(fruitAlleleUID); + if (fruitAllele != null) { + treeTemplate[EnumTreeChromosome.FRUITS.ordinal()] = fruitAllele; } } - worldObj.func_147479_m(xCoord, yCoord, zCoord); + if (treeTemplate != null) { + ITree tree = PluginArboriculture.treeInterface.templateAsIndividual(treeTemplate); + if (isPollinated) { + tree.mate(tree); + } + + setTree(tree); + + worldObj.func_147479_m(xCoord, yCoord, zCoord); + } } public void fromRipeningPacket(PacketRipeningUpdate packet) { - int newColourFruits = packet.getColourFruits(); + int newColourFruits = packet.getValue(); if (newColourFruits == colourFruits) { return; } @@ -465,7 +512,7 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru } } - private void attemptButterflySpawn(World world, IButterfly butterfly, double x, double y, double z) { + private static void attemptButterflySpawn(World world, IButterfly butterfly, double x, double y, double z) { IButterflyRoot butterflyRoot = (IButterflyRoot) AlleleManager.alleleRegistry.getSpeciesRoot("rootButterflies"); if (butterflyRoot.spawnButterflyInWorld(world, butterfly.copy(), x, y + 0.1f, z) != null) { Proxies.log.finest("A caterpillar '%s' hatched at %s/%s/%s.", butterfly.getDisplayName(), x, y, z); @@ -560,6 +607,6 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru /* IHousing */ @Override public GameProfile getOwnerName() { - return this.getOwnerProfile(); + return this.getOwner(); } } diff --git a/src/main/java/forestry/arboriculture/gadgets/TileSapling.java b/src/main/java/forestry/arboriculture/gadgets/TileSapling.java index e2953144f..10dbdec2c 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileSapling.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileSapling.java @@ -14,10 +14,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.Packet; import net.minecraft.world.gen.feature.WorldGenerator; -import forestry.api.arboriculture.ITree; -import forestry.arboriculture.network.PacketSapling; -import forestry.core.network.ForestryPacket; -import forestry.core.proxy.Proxies; +import forestry.core.network.PacketTileStream; import forestry.plugins.PluginArboriculture; public class TileSapling extends TileTreeContainer { @@ -67,7 +64,7 @@ public class TileSapling extends TileTreeContainer { WorldGenerator generator = this.getTree().getTreeGenerator(worldObj, xCoord, yCoord, zCoord, bonemealed); if (generator.generate(worldObj, worldObj.rand, xCoord, yCoord, zCoord)) { - PluginArboriculture.treeInterface.getBreedingTracker(worldObj, getOwnerProfile()).registerBirth(getTree()); + PluginArboriculture.treeInterface.getBreedingTracker(worldObj, getOwner()).registerBirth(getTree()); return 2; } @@ -76,31 +73,6 @@ public class TileSapling extends TileTreeContainer { @Override public Packet getDescriptionPacket() { - return new PacketSapling(this).getPacket(); + return new PacketTileStream(this).getPacket(); } - - @Override - public void sendNetworkUpdate() { - PacketSapling saplingPacket = new PacketSapling(this); - Proxies.net.sendNetworkPacket(saplingPacket, xCoord, yCoord, zCoord); - } - - @Override - public void fromPacket(ForestryPacket packetRaw) { - PacketSapling packet = (PacketSapling) packetRaw; - - ITree tree = getTree(); - if (packet.matchesTree(tree)) { - return; - } - - ITree newTree = packet.getTree(); - if (newTree == null) { - return; - } - - setTree(newTree); - worldObj.func_147479_m(xCoord, yCoord, zCoord); - } - } diff --git a/src/main/java/forestry/arboriculture/gadgets/TileStairs.java b/src/main/java/forestry/arboriculture/gadgets/TileStairs.java index acc451eb0..034644ce3 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileStairs.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileStairs.java @@ -10,27 +10,29 @@ ******************************************************************************/ package forestry.arboriculture.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import forestry.arboriculture.WoodType; -import forestry.core.network.ForestryPacket; -import forestry.core.network.INetworkedEntity; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketTileNBT; +import forestry.core.network.IStreamable; +import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; -public class TileStairs extends TileEntity implements INetworkedEntity { +public class TileStairs extends TileEntity implements IStreamable { - private WoodType type; + private WoodType woodType; - public WoodType getType() { - return this.type; + public WoodType getWoodType() { + return this.woodType; } - public void setType(WoodType type) { - this.type = type; + public void setWoodType(WoodType woodType) { + this.woodType = woodType; sendNetworkUpdate(); } @@ -40,7 +42,7 @@ public class TileStairs extends TileEntity implements INetworkedEntity { super.readFromNBT(nbttagcompound); if (nbttagcompound.hasKey("WT")) { - type = WoodType.VALUES[nbttagcompound.getShort("WT")]; + woodType = WoodType.VALUES[nbttagcompound.getShort("WT")]; } } @@ -48,8 +50,8 @@ public class TileStairs extends TileEntity implements INetworkedEntity { public void writeToNBT(NBTTagCompound nbttagcompound) { super.writeToNBT(nbttagcompound); - if (type != null) { - nbttagcompound.setShort("WT", (short) type.ordinal()); + if (woodType != null) { + nbttagcompound.setShort("WT", (short) woodType.ordinal()); } } @@ -66,19 +68,22 @@ public class TileStairs extends TileEntity implements INetworkedEntity { /* INETWORKEDENTITY */ @Override public Packet getDescriptionPacket() { - return new PacketTileNBT(PacketIds.TILE_NBT, this).getPacket(); + return new PacketTileStream(this).getPacket(); } - @Override public void sendNetworkUpdate() { - Proxies.net.sendNetworkPacket(new PacketTileNBT(PacketIds.TILE_NBT, this), xCoord, yCoord, zCoord); + Proxies.net.sendNetworkPacket(new PacketTileStream(this)); } @Override - public void fromPacket(ForestryPacket packetRaw) { - PacketTileNBT packet = (PacketTileNBT) packetRaw; - this.readFromNBT(packet.getTagCompound()); + public void writeData(DataOutputStream data) throws IOException { + data.writeShort(woodType.ordinal()); + } + + @Override + public void readData(DataInputStream data) throws IOException { + short woodTypeOrdinal = data.readShort(); + woodType = WoodType.values()[woodTypeOrdinal]; worldObj.func_147479_m(xCoord, yCoord, zCoord); } - } diff --git a/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java b/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java index 242c93523..893e24a56 100755 --- a/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java +++ b/src/main/java/forestry/arboriculture/gadgets/TileTreeContainer.java @@ -10,6 +10,10 @@ ******************************************************************************/ package forestry.arboriculture.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTUtil; @@ -19,17 +23,19 @@ import net.minecraft.tileentity.TileEntity; import com.mojang.authlib.GameProfile; import forestry.api.arboriculture.ITree; +import forestry.api.genetics.IAllele; import forestry.arboriculture.genetics.Tree; import forestry.core.interfaces.IOwnable; -import forestry.core.network.INetworkedEntity; +import forestry.core.network.IStreamable; import forestry.core.utils.PlayerUtil; +import forestry.plugins.PluginArboriculture; /** * This is the base TE class for any block that needs to contain tree genome information. * * @author SirSengir */ -public abstract class TileTreeContainer extends TileEntity implements INetworkedEntity, IOwnable { +public abstract class TileTreeContainer extends TileEntity implements IStreamable, IOwnable { private ITree containedTree; @@ -44,7 +50,6 @@ public abstract class TileTreeContainer extends TileEntity implements INetworked if (nbttagcompound.hasKey("owner")) { owner = NBTUtil.func_152459_a(nbttagcompound.getCompoundTag("owner")); } - } @Override @@ -61,7 +66,31 @@ public abstract class TileTreeContainer extends TileEntity implements INetworked NBTUtil.func_152460_a(nbt, owner); nbttagcompound.setTag("owner", nbt); } + } + @Override + public void writeData(DataOutputStream data) throws IOException { + String speciesUID = ""; + ITree tree = getTree(); + if (tree != null) { + speciesUID = tree.getIdent(); + } + data.writeUTF(speciesUID); + } + + @Override + public void readData(DataInputStream data) throws IOException { + String speciesUID = data.readUTF(); + ITree tree = getTree(speciesUID); + setTree(tree); + } + + public static ITree getTree(String speciesUID) { + IAllele[] treeTemplate = PluginArboriculture.treeInterface.getTemplate(speciesUID); + if (treeTemplate == null) { + return null; + } + return PluginArboriculture.treeInterface.templateAsIndividual(treeTemplate); } /* CLIENT INFORMATION */ @@ -102,18 +131,13 @@ public abstract class TileTreeContainer extends TileEntity implements INetworked return true; } - @Override - public boolean isOwnable() { - return false; - } - @Override public boolean isOwned() { return owner != null; } @Override - public GameProfile getOwnerProfile() { + public GameProfile getOwner() { return owner; } diff --git a/src/main/java/forestry/arboriculture/items/ItemStairs.java b/src/main/java/forestry/arboriculture/items/ItemStairs.java index 0b24e6b6c..eec9357dc 100755 --- a/src/main/java/forestry/arboriculture/items/ItemStairs.java +++ b/src/main/java/forestry/arboriculture/items/ItemStairs.java @@ -60,7 +60,7 @@ public class ItemStairs extends ItemForestryBlock { return false; } - ((TileStairs) tile).setType(type); + ((TileStairs) tile).setWoodType(type); return true; } diff --git a/src/main/java/forestry/arboriculture/network/PacketLeaf.java b/src/main/java/forestry/arboriculture/network/PacketLeaf.java deleted file mode 100644 index 1e85d6f15..000000000 --- a/src/main/java/forestry/arboriculture/network/PacketLeaf.java +++ /dev/null @@ -1,105 +0,0 @@ -/******************************************************************************* - * 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.arboriculture.network; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import forestry.api.arboriculture.EnumTreeChromosome; -import forestry.api.arboriculture.ITree; -import forestry.api.genetics.AlleleManager; -import forestry.api.genetics.IAllele; -import forestry.arboriculture.gadgets.TileLeaves; -import forestry.core.network.PacketIds; -import forestry.plugins.PluginArboriculture; - -public class PacketLeaf extends PacketTreeContainer { - - private static final short hasFruitFlag = 1; - private static final short isPollinatedFlag = 1 << 1; - - private byte leafState = 0; - private int colourFruits = -1; - private String fruitAlleleUID; - - public PacketLeaf() { - - } - - public PacketLeaf(TileLeaves leaves) { - super(PacketIds.LEAF, leaves); - - if (leaves.hasFruit()) { - leafState |= hasFruitFlag; - fruitAlleleUID = leaves.getTree().getGenome().getActiveAllele(EnumTreeChromosome.FRUITS).getUID(); - colourFruits = leaves.getFruitColour(); - } - if (leaves.isPollinated()) { - leafState |= isPollinatedFlag; - } - } - - @Override - public void writeData(DataOutputStream data) throws IOException { - super.writeData(data); - data.writeByte(leafState); - - if (isFruitLeaf()) { - data.writeUTF(fruitAlleleUID); - data.writeInt(colourFruits); - } - } - - @Override - public void readData(DataInputStream data) throws IOException { - super.readData(data); - leafState = data.readByte(); - - if (isFruitLeaf()) { - fruitAlleleUID = data.readUTF(); - colourFruits = data.readInt(); - } - } - - public boolean isFruitLeaf() { - return (leafState & hasFruitFlag) > 0; - } - - public boolean isPollinated() { - return (leafState & isPollinatedFlag) > 0; - } - - public int getColourFruits() { - return colourFruits; - } - - @Override - public ITree getTree() { - IAllele[] treeTemplate = PluginArboriculture.treeInterface.getTemplate(speciesUID); - if (treeTemplate == null) { - return null; - } - - if (fruitAlleleUID != null) { - IAllele fruitAllele = AlleleManager.alleleRegistry.getAllele(fruitAlleleUID); - if (fruitAllele != null) { - treeTemplate[EnumTreeChromosome.FRUITS.ordinal()] = fruitAllele; - } - } - - ITree tree = PluginArboriculture.treeInterface.templateAsIndividual(treeTemplate); - if (isPollinated()) { - tree.mate(tree); - } - return tree; - } -} diff --git a/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java b/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java index 04c28b822..c8e4f85ca 100644 --- a/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java +++ b/src/main/java/forestry/arboriculture/network/PacketRipeningUpdate.java @@ -14,54 +14,42 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.World; - +import forestry.arboriculture.gadgets.TileFruitPod; import forestry.arboriculture.gadgets.TileLeaves; -import forestry.core.network.ForestryPacket; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketCoordinates; +import forestry.core.network.PacketId; -public class PacketRipeningUpdate extends ForestryPacket { +public class PacketRipeningUpdate extends PacketCoordinates { - private int colourFruits; - private int posX, posY, posZ; + private int value; - public PacketRipeningUpdate() { + public PacketRipeningUpdate(DataInputStream data) throws IOException { + super(data); + } + + public PacketRipeningUpdate(TileFruitPod fruitPod) { + super(PacketId.RIPENING_UPDATE, fruitPod); + value = fruitPod.getMaturity(); } public PacketRipeningUpdate(TileLeaves leaves) { - super(PacketIds.RIPENING_UPDATE); - posX = leaves.xCoord; - posY = leaves.yCoord; - posZ = leaves.zCoord; - colourFruits = leaves.getFruitColour(); + super(PacketId.RIPENING_UPDATE, leaves); + value = leaves.getFruitColour(); } @Override public void writeData(DataOutputStream data) throws IOException { super.writeData(data); - - data.writeInt(posX); - data.writeInt(posY); - data.writeInt(posZ); - data.writeInt(colourFruits); + data.writeInt(value); } @Override public void readData(DataInputStream data) throws IOException { super.readData(data); - - posX = data.readInt(); - posY = data.readInt(); - posZ = data.readInt(); - colourFruits = data.readInt(); + value = data.readInt(); } - public int getColourFruits() { - return colourFruits; - } - - public TileEntity getTarget(World world) { - return world.getTileEntity(posX, posY, posZ); + public int getValue() { + return value; } } diff --git a/src/main/java/forestry/arboriculture/network/PacketSapling.java b/src/main/java/forestry/arboriculture/network/PacketSapling.java deleted file mode 100644 index 71a50964d..000000000 --- a/src/main/java/forestry/arboriculture/network/PacketSapling.java +++ /dev/null @@ -1,16 +0,0 @@ -package forestry.arboriculture.network; - -import forestry.arboriculture.gadgets.TileSapling; -import forestry.core.network.PacketIds; - -public class PacketSapling extends PacketTreeContainer { - - public PacketSapling() { - - } - - public PacketSapling(TileSapling sapling) { - super(PacketIds.SAPLING, sapling); - } - -} diff --git a/src/main/java/forestry/arboriculture/network/PacketTreeContainer.java b/src/main/java/forestry/arboriculture/network/PacketTreeContainer.java deleted file mode 100644 index 4fafd8dc0..000000000 --- a/src/main/java/forestry/arboriculture/network/PacketTreeContainer.java +++ /dev/null @@ -1,57 +0,0 @@ -package forestry.arboriculture.network; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import forestry.api.arboriculture.ITree; -import forestry.api.genetics.IAllele; -import forestry.arboriculture.gadgets.TileTreeContainer; -import forestry.core.network.PacketCoordinates; -import forestry.plugins.PluginArboriculture; - -public class PacketTreeContainer extends PacketCoordinates { - - protected String speciesUID = ""; - - public PacketTreeContainer() { - - } - - public PacketTreeContainer(int id, TileTreeContainer treeContainer) { - super(id, treeContainer); - - ITree tree = treeContainer.getTree(); - if (tree != null) { - speciesUID = tree.getIdent(); - } - } - - @Override - public void writeData(DataOutputStream data) throws IOException { - super.writeData(data); - data.writeUTF(speciesUID); - } - - @Override - public void readData(DataInputStream data) throws IOException { - super.readData(data); - speciesUID = data.readUTF(); - } - - public boolean matchesTree(ITree tree) { - if (tree == null) { - return speciesUID.isEmpty(); - } else { - return tree.getIdent().equals(speciesUID); - } - } - - public ITree getTree() { - IAllele[] treeTemplate = PluginArboriculture.treeInterface.getTemplate(speciesUID); - if (treeTemplate != null) { - return PluginArboriculture.treeInterface.templateAsIndividual(treeTemplate); - } - return null; - } -} diff --git a/src/main/java/forestry/arboriculture/worldgen/WorldGenArboriculture.java b/src/main/java/forestry/arboriculture/worldgen/WorldGenArboriculture.java index f2b61e9a8..833745702 100755 --- a/src/main/java/forestry/arboriculture/worldgen/WorldGenArboriculture.java +++ b/src/main/java/forestry/arboriculture/worldgen/WorldGenArboriculture.java @@ -65,7 +65,7 @@ public abstract class WorldGenArboriculture extends WorldGenBase { private GameProfile getOwner() { TileEntity tile = world.getTileEntity(startX, startY, startZ); if (tile instanceof TileSapling) { - return ((TileSapling) tile).getOwnerProfile(); + return ((TileSapling) tile).getOwner(); } else { return null; } diff --git a/src/main/java/forestry/core/circuits/ContainerSolderingIron.java b/src/main/java/forestry/core/circuits/ContainerSolderingIron.java index 77e543d6f..c150efde5 100755 --- a/src/main/java/forestry/core/circuits/ContainerSolderingIron.java +++ b/src/main/java/forestry/core/circuits/ContainerSolderingIron.java @@ -18,9 +18,9 @@ import forestry.core.gui.ContainerItemInventory; import forestry.core.gui.IGuiSelectable; import forestry.core.gui.slots.SlotFiltered; import forestry.core.gui.slots.SlotOutput; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketGuiSelect; +import forestry.core.network.PacketId; +import forestry.core.network.PacketString; import forestry.core.proxy.Proxies; public class ContainerSolderingIron extends ContainerItemInventory implements IGuiSelectable { @@ -46,38 +46,26 @@ public class ContainerSolderingIron extends ContainerItemInventory 0; - energyManager.fromPacketInt(payload.intPayload[1]); - heat = payload.intPayload[2]; - - pistonSpeedServer = payload.floatPayload[0]; - } - - public boolean isActive = false; // Used for smp. + public boolean active = false; // Used for smp. /** * Indicates whether the piston is receding from or approaching the * combustion chamber @@ -118,7 +95,7 @@ public abstract class Engine extends TileBase implements IEnergyConnection { stagePiston = 0; progress = 0; } - } else if (this.isActive) { + } else if (this.active) { stagePiston = 1; } } @@ -181,11 +158,11 @@ public abstract class Engine extends TileBase implements IEnergyConnection { } private void setActive(boolean isActive) { - if (this.isActive == isActive) { + if (this.active == isActive) { return; } - this.isActive = isActive; + this.active = isActive; setNeedsNetworkUpdate(); } @@ -296,6 +273,25 @@ public abstract class Engine extends TileBase implements IEnergyConnection { nbt.setBoolean("ForceCooldown", forceCooldown); } + /* NETWORK */ + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeBoolean(active); + data.writeInt(heat); + data.writeFloat(pistonSpeedServer); + energyManager.writeData(data); + } + + @Override + public void readData(DataInputStream data) throws IOException { + super.readData(data); + active = data.readBoolean(); + heat = data.readInt(); + pistonSpeedServer = data.readFloat(); + energyManager.readData(data); + } + /* SMP GUI */ public abstract void getGUINetworkData(int i, int j); diff --git a/src/main/java/forestry/core/gadgets/Mill.java b/src/main/java/forestry/core/gadgets/Mill.java index 270b2d445..13a9d8a44 100755 --- a/src/main/java/forestry/core/gadgets/Mill.java +++ b/src/main/java/forestry/core/gadgets/Mill.java @@ -10,7 +10,9 @@ ******************************************************************************/ package forestry.core.gadgets; -import forestry.core.network.PacketPayload; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; public abstract class Mill extends TileBase { @@ -34,21 +36,19 @@ public abstract class Mill extends TileBase { } @Override - public PacketPayload getPacketPayload() { - PacketPayload payload = new PacketPayload(2, 1, 0); - payload.intPayload[0] = charge; - payload.intPayload[1] = stage; - - payload.floatPayload[0] = speed; - return payload; + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeInt(charge); + data.writeFloat(speed); + data.writeInt(stage); } @Override - public void fromPacketPayload(PacketPayload payload) { - charge = payload.intPayload[0]; - stage = payload.intPayload[1]; - - speed = payload.floatPayload[0]; + public void readData(DataInputStream data) throws IOException { + super.readData(data); + charge = data.readInt(); + speed = data.readFloat(); + stage = data.readInt(); } private void update(boolean isSimulating) { diff --git a/src/main/java/forestry/core/gadgets/NaturalistGame.java b/src/main/java/forestry/core/gadgets/NaturalistGame.java index 4f25ac960..cdb1504fc 100755 --- a/src/main/java/forestry/core/gadgets/NaturalistGame.java +++ b/src/main/java/forestry/core/gadgets/NaturalistGame.java @@ -10,8 +10,13 @@ ******************************************************************************/ package forestry.core.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Random; import net.minecraft.inventory.IInventory; @@ -23,14 +28,16 @@ import forestry.api.core.INBTTagable; import forestry.api.genetics.AlleleManager; import forestry.api.genetics.IIndividual; import forestry.api.genetics.ISpeciesRoot; +import forestry.core.network.IStreamable; +import forestry.core.network.PacketHelper; import forestry.core.utils.StringUtil; import forestry.core.utils.Utils; -public class NaturalistGame implements INBTTagable { +public class NaturalistGame implements INBTTagable, IStreamable { public static final int BOUNTY_MAX = 16; - public static class GameToken implements INBTTagable { + public static class GameToken implements INBTTagable, IStreamable { private static final String[] OVERLAY_NONE = new String[0]; private static final String[] OVERLAY_FAILED = new String[]{"errors/errored"}; @@ -41,6 +48,10 @@ public class NaturalistGame implements INBTTagable { protected boolean isProbed = false; protected boolean isRevealed = false; + public GameToken() { + + } + public GameToken(ItemStack tokenStack) { this.tokenStack = tokenStack; } @@ -73,6 +84,25 @@ public class NaturalistGame implements INBTTagable { } } + /* NETWORK */ + @Override + public void writeData(DataOutputStream data) throws IOException { + data.writeBoolean(isFailed); + data.writeBoolean(isProbed); + data.writeBoolean(isRevealed); + + PacketHelper.writeItemStack(tokenStack, data); + } + + @Override + public void readData(DataInputStream data) throws IOException { + isFailed = data.readBoolean(); + isProbed = data.readBoolean(); + isRevealed = data.readBoolean(); + + tokenStack = PacketHelper.readItemStack(data); + } + public boolean isVisible() { return isRevealed; } @@ -163,6 +193,29 @@ public class NaturalistGame implements INBTTagable { lastUpdate = System.currentTimeMillis(); } + /* NETWORK */ + @Override + public void writeData(DataOutputStream data) throws IOException { + data.writeBoolean(isEnded); + data.writeInt(bountyLevel); + data.writeLong(lastUpdate); + + List gameTokensList = gameTokens == null ? null : Arrays.asList(gameTokens); + PacketHelper.writeStreamables(gameTokensList, data); + } + + @Override + public void readData(DataInputStream data) throws IOException { + isEnded = data.readBoolean(); + bountyLevel = data.readInt(); + lastUpdate = data.readLong(); + + List gameTokensList = PacketHelper.readStreamables(GameToken.class, data); + if (gameTokensList != null) { + this.gameTokens = gameTokensList.toArray(new GameToken[gameTokensList.size()]); + } + } + /* INTERACTION */ public void initialize(ItemStack specimen) { IIndividual individual = AlleleManager.alleleRegistry.getIndividual(specimen); @@ -284,8 +337,9 @@ public class NaturalistGame implements INBTTagable { continue; } - gameTokens[tokenIndices[processedTokens]].isProbed = true; - gameTokens[tokenIndices[processedTokens]].isRevealed = true; + GameToken token = gameTokens[tokenIndices[processedTokens]]; + token.isProbed = true; + token.isRevealed = true; processedTokens++; if (processedTokens >= tokenIndices.length) { @@ -296,32 +350,34 @@ public class NaturalistGame implements INBTTagable { lastUpdate = System.currentTimeMillis(); } - public void choose(int tokenIndex) { + public void choose(final int tokenIndex) { if (isEnded) { return; } if (gameTokens == null || tokenIndex >= gameTokens.length) { return; } - if (gameTokens[tokenIndex].isRevealed && !gameTokens[tokenIndex].isProbed) { + + GameToken token = gameTokens[tokenIndex]; + if (token.isRevealed && !token.isProbed) { return; } hideProbedTokens(); - Collection singles = getRevealedSingles(gameTokens[tokenIndex]); + Collection singles = getRevealedSingles(token); if (singles.size() > 0) { boolean matched = false; for (GameToken single : singles) { - if (single.matches(gameTokens[tokenIndex])) { + if (single.matches(token)) { matched = true; break; } } if (!matched) { - gameTokens[tokenIndex].isFailed = true; + token.isFailed = true; } } - gameTokens[tokenIndex].isRevealed = true; + token.isRevealed = true; checkGameEnd(); lastUpdate = System.currentTimeMillis(); } @@ -420,10 +476,6 @@ public class NaturalistGame implements INBTTagable { } /* RETRIEVAL */ - public int getBoardSize() { - return gameTokens != null ? gameTokens.length : 0; - } - public int getSampleSize() { if (gameTokens == null) { return 0; @@ -436,11 +488,4 @@ public class NaturalistGame implements INBTTagable { public GameToken getToken(int index) { return gameTokens != null ? index < gameTokens.length ? gameTokens[index] : null : null; } - - public void setToken(int index, NBTTagCompound nbttagcompound) { - if (gameTokens == null) { - gameTokens = new GameToken[24]; - } - gameTokens[index] = new GameToken(nbttagcompound); - } } diff --git a/src/main/java/forestry/core/gadgets/TileAnalyzer.java b/src/main/java/forestry/core/gadgets/TileAnalyzer.java index 2f2eaf0fd..c2dd1b122 100755 --- a/src/main/java/forestry/core/gadgets/TileAnalyzer.java +++ b/src/main/java/forestry/core/gadgets/TileAnalyzer.java @@ -10,6 +10,9 @@ ******************************************************************************/ package forestry.core.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.HashSet; import java.util.Set; @@ -38,7 +41,6 @@ import forestry.core.fluids.FluidHelper; import forestry.core.fluids.Fluids; import forestry.core.fluids.TankManager; import forestry.core.fluids.tanks.FilteredTank; -import forestry.core.interfaces.IErrorSource; import forestry.core.interfaces.ILiquidTankContainer; import forestry.core.inventory.InvTools; import forestry.core.inventory.TileInventoryAdapter; @@ -46,7 +48,6 @@ import forestry.core.inventory.wrappers.IInvSlot; import forestry.core.inventory.wrappers.InventoryIterator; import forestry.core.inventory.wrappers.InventoryMapper; import forestry.core.network.GuiId; -import forestry.core.network.PacketPayload; import forestry.core.utils.GeneticsUtil; import forestry.core.utils.GuiUtil; @@ -197,30 +198,33 @@ public class TileAnalyzer extends TilePowered implements ISidedInventory, ILiqui } /* Network */ - public PacketPayload getPacketPayload() { + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); ItemStack displayStack = getIndividualOnDisplay(); if (displayStack == null) { - return null; + data.writeBoolean(false); + } else { + data.writeBoolean(true); + + IIndividual displayIndividual = AlleleManager.alleleRegistry.getIndividual(displayStack); + int type = BeeManager.beeRoot.getType(displayStack).ordinal(); + + data.writeUTF(displayIndividual.getIdent()); + data.writeShort(type); + data.writeShort(displayStack.stackSize); } - - IIndividual displayIndividual = AlleleManager.alleleRegistry.getIndividual(displayStack); - int type = BeeManager.beeRoot.getType(displayStack).ordinal(); - - PacketPayload payload = new PacketPayload(); - payload.stringPayload = new String[1]; - payload.shortPayload = new short[2]; - payload.stringPayload[0] = displayIndividual.getIdent(); - payload.shortPayload[0] = (short) type; - payload.shortPayload[1] = (short) displayStack.stackSize; - return payload; } - public void fromPacketPayload(PacketPayload payload) { + @Override + public void readData(DataInputStream data) throws IOException { + super.readData(data); + ItemStack newIndividualOnDisplay = null; - if (!payload.isEmpty()) { - String individualUID = payload.stringPayload[0]; - int type = payload.shortPayload[0]; - int stackSize = payload.shortPayload[1]; + if (data.readBoolean()) { + String individualUID = data.readUTF(); + int type = data.readShort(); + int stackSize = data.readShort(); IAllele[] template = BeeManager.beeRoot.getTemplate(individualUID); IIndividual individual = BeeManager.beeRoot.templateAsIndividual(template); diff --git a/src/main/java/forestry/core/gadgets/TileBase.java b/src/main/java/forestry/core/gadgets/TileBase.java index 4179262b6..76e01aaf0 100755 --- a/src/main/java/forestry/core/gadgets/TileBase.java +++ b/src/main/java/forestry/core/gadgets/TileBase.java @@ -13,31 +13,14 @@ package forestry.core.gadgets; import net.minecraft.entity.player.EntityPlayer; import forestry.core.interfaces.IHintSource; -import forestry.core.network.PacketPayload; -import forestry.core.proxy.Proxies; public abstract class TileBase extends TileForestry implements IHintSource { - @Override - public boolean isOwnable() { - return true; - } - /* UPDATING */ @Override public void initialize() { } - /* NETWORK */ - @Override - public PacketPayload getPacketPayload() { - return null; - } - - @Override - public void fromPacketPayload(PacketPayload payload) { - } - /* INTERACTION */ public abstract void openGui(EntityPlayer player, TileBase tile); diff --git a/src/main/java/forestry/core/gadgets/TileEscritoire.java b/src/main/java/forestry/core/gadgets/TileEscritoire.java index f3d3cebc2..f33565dc1 100755 --- a/src/main/java/forestry/core/gadgets/TileEscritoire.java +++ b/src/main/java/forestry/core/gadgets/TileEscritoire.java @@ -10,6 +10,10 @@ ******************************************************************************/ package forestry.core.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; @@ -24,11 +28,8 @@ import forestry.core.interfaces.ICrafter; import forestry.core.interfaces.IRenderableMachine; import forestry.core.inventory.InvTools; import forestry.core.inventory.TileInventoryAdapter; -import forestry.core.network.ForestryPacket; import forestry.core.network.GuiId; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketTileNBT; -import forestry.core.network.PacketTileUpdate; +import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; import forestry.core.utils.EnumTankLevel; import forestry.core.utils.GeneticsUtil; @@ -56,13 +57,6 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender } /* SAVING & LOADING */ - @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - super.writeToNBT(nbttagcompound); - - game.writeToNBT(nbttagcompound); - } - @Override public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); @@ -70,6 +64,13 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender game.readFromNBT(nbttagcompound); } + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + super.writeToNBT(nbttagcompound); + + game.writeToNBT(nbttagcompound); + } + /* GAME */ public NaturalistGame getGame() { return game; @@ -109,19 +110,21 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender } /* NETWORK */ - @Override - public void fromPacket(ForestryPacket packetRaw) { - if (packetRaw instanceof PacketTileUpdate) { - super.fromPacket(packetRaw); - return; - } - PacketTileNBT packet = (PacketTileNBT) packetRaw; - readFromNBT(packet.getTagCompound()); + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + game.writeData(data); + } + + @Override + public void readData(DataInputStream data) throws IOException { + super.readData(data); + game.readData(data); } public void sendBoard(EntityPlayer player) { - Proxies.net.sendToPlayer(new PacketTileNBT(PacketIds.TILE_NBT, this), player); + Proxies.net.sendToPlayer(new PacketTileStream(this), player); } @Override diff --git a/src/main/java/forestry/core/gadgets/TileForestry.java b/src/main/java/forestry/core/gadgets/TileForestry.java index d7e3670c7..d2d388d3a 100755 --- a/src/main/java/forestry/core/gadgets/TileForestry.java +++ b/src/main/java/forestry/core/gadgets/TileForestry.java @@ -12,10 +12,14 @@ package forestry.core.gadgets; import com.google.common.collect.ImmutableSet; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.Collection; import java.util.HashSet; import java.util.Random; import java.util.Set; +import java.util.UUID; import net.minecraft.block.Block; import net.minecraft.entity.EntityLivingBase; @@ -35,18 +39,20 @@ import net.minecraftforge.common.util.ForgeDirection; import cpw.mods.fml.common.Optional; +import forestry.api.core.ErrorStateRegistry; import forestry.api.core.IErrorState; import forestry.core.config.Config; import forestry.core.config.Defaults; +import forestry.core.fluids.TankManager; import forestry.core.interfaces.IErrorSource; import forestry.core.interfaces.IFilterSlotDelegate; +import forestry.core.interfaces.ILiquidTankContainer; import forestry.core.interfaces.IRestrictedAccess; import forestry.core.inventory.FakeInventoryAdapter; import forestry.core.inventory.IInventoryAdapter; -import forestry.core.network.ForestryPacket; -import forestry.core.network.INetworkedEntity; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketTileUpdate; +import forestry.core.network.IStreamable; +import forestry.core.network.PacketErrorUpdate; +import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; import forestry.core.utils.AdjacentTileCache; import forestry.core.utils.EnumAccess; @@ -59,15 +65,17 @@ import buildcraft.api.statements.ITriggerInternal; import buildcraft.api.statements.ITriggerProvider; @Optional.Interface(iface = "buildcraft.api.statements.ITriggerProvider", modid = "BuildCraftAPI|statements") -public abstract class TileForestry extends TileEntity implements INetworkedEntity, IRestrictedAccess, IErrorSource, ITriggerProvider, ISidedInventory, IFilterSlotDelegate { +public abstract class TileForestry extends TileEntity implements IStreamable, IRestrictedAccess, IErrorSource, ITriggerProvider, ISidedInventory, IFilterSlotDelegate { private static final Random rand = new Random(); - protected final AdjacentTileCache tileCache = new AdjacentTileCache(this); - protected boolean isInited = false; private IInventoryAdapter inventory = FakeInventoryAdapter.instance(); private int tickCount = rand.nextInt(256); private boolean needsNetworkUpdate = false; + private ForgeDirection orientation = ForgeDirection.WEST; + + protected final AdjacentTileCache tileCache = new AdjacentTileCache(this); + protected boolean isInited = false; public AdjacentTileCache getTileCache() { return tileCache; @@ -110,6 +118,8 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit } + private ImmutableSet previousErrorStates; + // / UPDATING @Override public final void updateEntity() { @@ -126,10 +136,16 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit updateClientSide(); } + ImmutableSet errorStates = getErrorStates(); + if (needsNetworkUpdate) { needsNetworkUpdate = false; sendNetworkUpdate(); + } else if (!errorStates.equals(previousErrorStates)) { + sendErrorUpdate(); } + + previousErrorStates = errorStates; } protected void updateClientSide() { @@ -184,37 +200,84 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit } @Override - public Packet getDescriptionPacket() { - PacketTileUpdate packet = new PacketTileUpdate(this); + public final Packet getDescriptionPacket() { + PacketTileStream packet = new PacketTileStream(this); return packet.getPacket(); } /* INetworkedEntity */ - @Override - public void sendNetworkUpdate() { - PacketTileUpdate packet = new PacketTileUpdate(this); - Proxies.net.sendNetworkPacket(packet, xCoord, yCoord, zCoord); + public final void sendNetworkUpdate() { + PacketTileStream packet = new PacketTileStream(this); + Proxies.net.sendNetworkPacket(packet); } - @Override - public void fromPacket(ForestryPacket packetRaw) { - PacketTileUpdate packet = (PacketTileUpdate) packetRaw; - if (orientation != packet.getOrientation()) { - orientation = packet.getOrientation(); - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + public final void sendErrorUpdate() { + PacketErrorUpdate packet = new PacketErrorUpdate(this); + Proxies.net.sendNetworkPacket(packet); + } + + public void writeData(DataOutputStream data) throws IOException { + data.writeByte(orientation.ordinal()); + + writeErrorData(data); + + // TODO: Should this really be sent to the client? Huge network cost. + // As far as I know, only GUIs need it, and there are better ways to get the information to a GUI. + // -CovertJaguar + if (owner == null) { + data.writeBoolean(false); + } else { + data.writeBoolean(true); + data.writeByte(access.ordinal()); + data.writeLong(owner.getId().getMostSignificantBits()); + data.writeLong(owner.getId().getLeastSignificantBits()); + data.writeUTF(owner.getName()); } - errorStates.clear(); - errorStates.addAll(packet.getErrorStates()); - - owner = packet.getOwner(); - access = packet.getAccess(); - fromPacketPayload(packet.payload); + if (this instanceof ILiquidTankContainer) { + TankManager tankManager = ((ILiquidTankContainer) this).getTankManager(); + if (tankManager != null) { + tankManager.writePacketData(data); + } + } } - public abstract PacketPayload getPacketPayload(); + public void readData(DataInputStream data) throws IOException { + orientation = ForgeDirection.getOrientation(data.readByte()); - public abstract void fromPacketPayload(PacketPayload payload); + readErrorData(data); + + if (data.readBoolean()) { + byte accessOrdinal = data.readByte(); + access = EnumAccess.values()[accessOrdinal]; + owner = new GameProfile(new UUID(data.readLong(), data.readLong()), data.readUTF()); + } + + if (this instanceof ILiquidTankContainer) { + TankManager tankManager = ((ILiquidTankContainer) this).getTankManager(); + if (tankManager != null) { + tankManager.readPacketData(data); + } + } + } + + public final void writeErrorData(DataOutputStream data) throws IOException { + data.writeShort(errorStates.size()); + for (IErrorState errorState : errorStates) { + data.writeShort(errorState.getID()); + } + } + + public final void readErrorData(DataInputStream data) throws IOException { + errorStates.clear(); + + short errorStateCount = data.readShort(); + for (int i = 0; i < errorStateCount; i++) { + short errorStateId = data.readShort(); + IErrorState errorState = ErrorStateRegistry.getErrorState(errorStateId); + errorStates.add(errorState); + } + } public void onRemoval() { } @@ -233,17 +296,11 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit } // / REDSTONE INFO - - /** - * @return true if tile is activated by redstone current. - */ public boolean isRedstoneActivated() { return worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord); } // / ORIENTATION - private ForgeDirection orientation = ForgeDirection.WEST; - public ForgeDirection getOrientation() { return this.orientation; } @@ -265,29 +322,20 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit public final boolean setErrorCondition(boolean condition, IErrorState errorState) { if (condition) { - if (errorStates.add(errorState)) { - setNeedsNetworkUpdate(); - } + errorStates.add(errorState); } else { - if (errorStates.remove(errorState)) { - setNeedsNetworkUpdate(); - } + errorStates.remove(errorState); } return condition; } @Deprecated protected final void addErrorState(IErrorState state) { - if (errorStates.add(state)) { - setNeedsNetworkUpdate(); - } + errorStates.add(state); } @Deprecated protected final void removeErrorStates() { - if (errorStates.size() > 0) { - setNeedsNetworkUpdate(); - } errorStates.clear(); } @@ -310,7 +358,7 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit @Override public final boolean allowsRemoval(EntityPlayer player) { - return Config.disablePermissions || getAccess() == EnumAccess.SHARED || !isOwnable() || !isOwned() || isOwner(player) || Proxies.common.isOp(player); + return Config.disablePermissions || getAccess() == EnumAccess.SHARED || !isOwned() || isOwner(player) || Proxies.common.isOp(player); } @Override @@ -332,18 +380,13 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit return access; } - @Override - public boolean isOwnable() { - return false; - } - @Override public boolean isOwned() { return owner != null; } @Override - public GameProfile getOwnerProfile() { + public GameProfile getOwner() { return owner; } @@ -391,7 +434,7 @@ public abstract class TileForestry extends TileEntity implements INetworkedEntit */ public String getUnlocalizedName() { String blockUnlocalizedName = getBlockType().getUnlocalizedName().replace("tile.for.", ""); - return blockUnlocalizedName + "." + getBlockMetadata() + ".name"; + return blockUnlocalizedName + '.' + getBlockMetadata() + ".name"; } /* INVENTORY BASICS */ diff --git a/src/main/java/forestry/core/genetics/BreedingTracker.java b/src/main/java/forestry/core/genetics/BreedingTracker.java index f12f3b01f..0874e37c9 100755 --- a/src/main/java/forestry/core/genetics/BreedingTracker.java +++ b/src/main/java/forestry/core/genetics/BreedingTracker.java @@ -28,7 +28,7 @@ import forestry.api.genetics.IBreedingTracker; import forestry.api.genetics.IIndividual; import forestry.api.genetics.IMutation; import forestry.api.genetics.ISpeciesRoot; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.network.PacketNBT; import forestry.core.proxy.Proxies; @@ -77,7 +77,7 @@ public abstract class BreedingTracker extends WorldSavedData implements IBreedin NBTTagCompound nbttagcompound = new NBTTagCompound(); encodeToNBT(nbttagcompound); - Proxies.net.sendToPlayer(new PacketNBT(PacketIds.GENOME_TRACKER_UPDATE, nbttagcompound), player); + Proxies.net.sendToPlayer(new PacketNBT(PacketId.GENOME_TRACKER_UPDATE, nbttagcompound), player); } /* HELPER FUNCTIONS TO PREVENT OBFUSCATION OF INTERFACE METHODS */ diff --git a/src/main/java/forestry/core/gui/ContainerEscritoire.java b/src/main/java/forestry/core/gui/ContainerEscritoire.java index a9a2dd239..f6d24bd98 100755 --- a/src/main/java/forestry/core/gui/ContainerEscritoire.java +++ b/src/main/java/forestry/core/gui/ContainerEscritoire.java @@ -15,10 +15,7 @@ import net.minecraft.entity.player.EntityPlayer; import forestry.core.gadgets.TileEscritoire; import forestry.core.gui.slots.SlotFiltered; import forestry.core.gui.slots.SlotOutput; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; -import forestry.core.proxy.Proxies; +import forestry.core.network.PacketGuiSelect; public class ContainerEscritoire extends ContainerTile implements IGuiSelectable { @@ -56,35 +53,20 @@ public class ContainerEscritoire extends ContainerTile implement tile.sendBoard(player); } - public void sendTokenClick(int index) { - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = index; - PacketUpdate packet = new PacketUpdate(PacketIds.GUI_SELECTION_CHANGE, payload); - Proxies.net.sendToServer(packet); - } - - public void sendProbeClick() { - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = -1; - PacketUpdate packet = new PacketUpdate(PacketIds.GUI_SELECTION_CHANGE, payload); - Proxies.net.sendToServer(packet); - } - @Override - public void handleSelectionChange(EntityPlayer player, PacketUpdate packet) { + public void handleSelectionChange(EntityPlayer player, PacketGuiSelect packet) { if (!tile.getGame().isEnded()) { - int index = packet.payload.intPayload[0]; + int index = packet.getPrimaryIndex(); if (index == -1) { tile.probe(); } else { - tile.getGame().choose(packet.payload.intPayload[0]); + tile.getGame().choose(index); tile.processTurnResult(player.getGameProfile()); } } } @Override - public void setSelection(PacketUpdate packet) { + public void setSelection(PacketGuiSelect packet) { } - } diff --git a/src/main/java/forestry/core/gui/ContainerLiquidTanks.java b/src/main/java/forestry/core/gui/ContainerLiquidTanks.java index 384f6d120..d09bf55fb 100755 --- a/src/main/java/forestry/core/gui/ContainerLiquidTanks.java +++ b/src/main/java/forestry/core/gui/ContainerLiquidTanks.java @@ -26,9 +26,8 @@ import forestry.api.core.IToolPipette; import forestry.core.fluids.tanks.StandardTank; import forestry.core.gadgets.TileForestry; import forestry.core.interfaces.ILiquidTankContainer; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketId; +import forestry.core.network.PacketSlotClick; import forestry.core.proxy.Proxies; public class ContainerLiquidTanks extends ContainerTile { @@ -44,9 +43,7 @@ public class ContainerLiquidTanks return; } - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = slot; - Proxies.net.sendToServer(new PacketUpdate(PacketIds.PIPETTE_CLICK, payload)); + Proxies.net.sendToServer(new PacketSlotClick(PacketId.PIPETTE_CLICK, tile, slot)); } public void handlePipetteClick(int slot, EntityPlayerMP player) { diff --git a/src/main/java/forestry/core/gui/ContainerNaturalistInventory.java b/src/main/java/forestry/core/gui/ContainerNaturalistInventory.java index a0e448ca4..738d09095 100755 --- a/src/main/java/forestry/core/gui/ContainerNaturalistInventory.java +++ b/src/main/java/forestry/core/gui/ContainerNaturalistInventory.java @@ -15,7 +15,7 @@ import net.minecraft.entity.player.InventoryPlayer; import forestry.core.gadgets.TileNaturalistChest; import forestry.core.gui.slots.SlotFiltered; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketGuiSelect; public class ContainerNaturalistInventory extends ContainerTile implements IGuiSelectable { @@ -31,11 +31,11 @@ public class ContainerNaturalistInventory extends ContainerTile extends Con public void handleChipsetClick(int slot, EntityPlayer player, ItemStack itemstack) { if (!Proxies.common.isSimulating(player.worldObj)) { - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = slot; - Proxies.net.sendToServer(new PacketUpdate(PacketIds.CHIPSET_CLICK, payload)); + PacketSlotClick packet = new PacketSlotClick(PacketId.CHIPSET_CLICK, tile, slot); + Proxies.net.sendToServer(packet); player.inventory.setItemStack(null); return; } @@ -50,18 +47,16 @@ public class ContainerSocketed extends Con player.inventory.setItemStack(null); } Proxies.net.inventoryChangeNotify(player); - - TileEntity te = (TileEntity) tile; - Proxies.net.sendToPlayer(new PacketSocketUpdate(PacketIds.SOCKET_UPDATE, te.xCoord, te.yCoord, te.zCoord, tile), player); - } + PacketSocketUpdate packet = new PacketSocketUpdate(PacketId.SOCKET_UPDATE, tile); + Proxies.net.sendToPlayer(packet, player); + } } public void handleSolderingIronClick(int slot, EntityPlayer player, ItemStack itemstack) { if (!Proxies.common.isSimulating(player.worldObj)) { - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = slot; - Proxies.net.sendToServer(new PacketUpdate(PacketIds.SOLDERING_IRON_CLICK, payload)); + PacketSlotClick packet = new PacketSlotClick(PacketId.SOLDERING_IRON_CLICK, tile, slot); + Proxies.net.sendToServer(packet); return; } @@ -82,9 +77,7 @@ public class ContainerSocketed extends Con player.inventory.setItemStack(null); } - TileEntity te = (TileEntity) tile; - Proxies.net.sendToPlayer(new PacketSocketUpdate(PacketIds.SOCKET_UPDATE, te.xCoord, te.yCoord, te.zCoord, tile), player); - + PacketSocketUpdate packet = new PacketSocketUpdate(PacketId.SOCKET_UPDATE, tile); + Proxies.net.sendToPlayer(packet, player); } - } diff --git a/src/main/java/forestry/core/gui/GuiEscritoire.java b/src/main/java/forestry/core/gui/GuiEscritoire.java index 309c61374..2e3cd1727 100755 --- a/src/main/java/forestry/core/gui/GuiEscritoire.java +++ b/src/main/java/forestry/core/gui/GuiEscritoire.java @@ -28,6 +28,8 @@ import forestry.core.gadgets.NaturalistGame; import forestry.core.gadgets.NaturalistGame.GameToken; import forestry.core.gadgets.TileEscritoire; import forestry.core.gui.widgets.Widget; +import forestry.core.network.PacketGuiSelect; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.core.render.SpriteSheet; import forestry.core.render.TextureManager; @@ -120,7 +122,8 @@ public class GuiEscritoire extends GuiForestry ext ledgerManager.add(new HintLedger(ledgerManager, (IHintSource) inventory)); } - if (inventory instanceof IOwnable && ((IOwnable) inventory).isOwnable()) { + if (inventory instanceof IOwnable && ((IOwnable) inventory).isOwned()) { ledgerManager.add(new OwnerLedger(ledgerManager, (IOwnable) inventory)); } diff --git a/src/main/java/forestry/core/gui/GuiNaturalistInventory.java b/src/main/java/forestry/core/gui/GuiNaturalistInventory.java index 6e19c0db4..a570b053c 100755 --- a/src/main/java/forestry/core/gui/GuiNaturalistInventory.java +++ b/src/main/java/forestry/core/gui/GuiNaturalistInventory.java @@ -30,9 +30,9 @@ import forestry.core.config.Defaults; import forestry.core.genetics.EnumMutateChance; import forestry.core.gui.buttons.GuiBetterButton; import forestry.core.gui.buttons.StandardButtonTextureSets; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.ForestryPacket; +import forestry.core.network.PacketGuiSelect; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.core.utils.StringUtil; @@ -93,10 +93,8 @@ public class GuiNaturalistInventory extends GuiForestry= PacketId.VALUES.length) { + return false; + } + PacketId packetId = PacketId.VALUES[packetIdOrdinal]; switch (packetId) { - case PacketIds.TILE_FORESTRY_UPDATE: - PacketTileUpdate packetT = new PacketTileUpdate(); - packetT.readData(data); - onTileUpdate(packetT); + case TILE_FORESTRY_UPDATE: { + PacketTileStream packet = new PacketTileStream(data); return true; - case PacketIds.TILE_UPDATE: - PacketUpdate packetUpdate = new PacketUpdate(); - packetUpdate.readData(data); - onTileUpdate(packetUpdate); + } + case TILE_FORESTRY_ERROR_UPDATE: { + PacketErrorUpdate packet = new PacketErrorUpdate(data); return true; - case PacketIds.TILE_NBT: - PacketTileNBT packetN = new PacketTileNBT(); - packetN.readData(data); - onTileUpdate(packetN); - return true; - case PacketIds.SOCKET_UPDATE: - PacketSocketUpdate packetS = new PacketSocketUpdate(); - packetS.readData(data); + } + case SOCKET_UPDATE: { + PacketSocketUpdate packetS = new PacketSocketUpdate(data); onSocketUpdate(packetS); return true; - case PacketIds.IINVENTORY_STACK: - PacketInventoryStack packetQ = new PacketInventoryStack(); - packetQ.readData(data); - onInventoryStack(packetQ); + } + case IINVENTORY_STACK: { + PacketInventoryStack packet = new PacketInventoryStack(data); + onInventoryStack(packet); return true; - case PacketIds.FX_SIGNAL: - PacketFXSignal packetF = new PacketFXSignal(); - packetF.readData(data); - packetF.executeFX(); + } + case FX_SIGNAL: { + PacketFXSignal packet = new PacketFXSignal(data); + packet.executeFX(); return true; + } - case PacketIds.PIPETTE_CLICK: - packetU = new PacketUpdate(); - packetU.readData(data); - onPipetteClick(packetU, player); + case PIPETTE_CLICK: { + PacketSlotClick packet = new PacketSlotClick(data); + onPipetteClick(packet, player); return true; - case PacketIds.SOLDERING_IRON_CLICK: - packetU = new PacketUpdate(); - packetU.readData(data); - onSolderingIronClick(packetU, player); + } + case SOLDERING_IRON_CLICK: { + PacketSlotClick packet = new PacketSlotClick(data); + onSolderingIronClick(packet, player); return true; - case PacketIds.CHIPSET_CLICK: - packetU = new PacketUpdate(); - packetU.readData(data); - onChipsetClick(packetU, player); + } + case CHIPSET_CLICK: { + PacketSlotClick packet = new PacketSlotClick(data); + onChipsetClick(packet, player); return true; - case PacketIds.ACCESS_SWITCH: - PacketCoordinates packetC = new PacketCoordinates(); - packetC.readData(data); - onAccessSwitch(packetC, player); + } + case ACCESS_SWITCH: { + PacketCoordinates packet = new PacketCoordinates(data); + onAccessSwitch(packet, player); return true; - case PacketIds.GUI_SELECTION: - PacketUpdate packetI = new PacketUpdate(); - packetI.readData(data); - onGuiSelection(packetI); + } + case GUI_SELECTION_SET: { + PacketGuiSelect packet = new PacketGuiSelect(data); + onGuiSelection(packet); return true; - case PacketIds.GUI_SELECTION_CHANGE: - PacketUpdate packetZ = new PacketUpdate(); - packetZ.readData(data); - onGuiChange(player, packetZ); + } + case GUI_SELECTION_CHANGE: { + PacketGuiSelect packet = new PacketGuiSelect(data); + onGuiChange(player, packet); return true; - case PacketIds.GENOME_TRACKER_UPDATE: - PacketNBT packetTR = new PacketNBT(); - packetTR.readData(data); - onGenomeTrackerUpdate(packetTR); + } + case GUI_LAYOUT_SELECT: { + PacketString packet = new PacketString(data); + onGuiLayoutSelect(packet); return true; - case PacketIds.GUI_INTEGER: - PacketGuiInteger packet = new PacketGuiInteger(); - packet.readData(data); + } + case GENOME_TRACKER_UPDATE: { + PacketNBT packet = new PacketNBT(data); + onGenomeTrackerUpdate(packet); return true; - default: - for (forestry.core.interfaces.IPacketHandler handler : PluginManager.packetHandlers) { + } + case GUI_INTEGER: { + PacketGuiInteger packet = new PacketGuiInteger(data); + return true; + } + default: { + for (IPacketHandler handler : PluginManager.packetHandlers) { if (handler.onPacketData(packetId, data, player)) { return true; } } + } } } catch (Exception ex) { ex.printStackTrace(); @@ -157,19 +162,11 @@ public class PacketHandler { return false; } - public void sendPacket(FMLProxyPacket packet) { - if (FMLCommonHandler.instance().getEffectiveSide().isClient()) { - channel.sendToServer(packet); - } else { - channel.sendToAll(packet); - } - } - public void sendPacket(FMLProxyPacket packet, EntityPlayerMP player) { channel.sendTo(packet, player); } - private void onGenomeTrackerUpdate(PacketNBT packet) { + private static void onGenomeTrackerUpdate(PacketNBT packet) { assert FMLCommonHandler.instance().getEffectiveSide().isClient(); EntityPlayer player = Proxies.common.getPlayer(); @@ -186,7 +183,7 @@ public class PacketHandler { } } - private void onGuiChange(EntityPlayer player, PacketUpdate packet) { + private static void onGuiChange(EntityPlayer player, PacketGuiSelect packet) { assert FMLCommonHandler.instance().getEffectiveSide().isServer(); if (!(player.openContainer instanceof IGuiSelectable)) { @@ -196,7 +193,7 @@ public class PacketHandler { ((IGuiSelectable) player.openContainer).handleSelectionChange(player, packet); } - private void onGuiSelection(PacketUpdate packet) { + private static void onGuiSelection(PacketGuiSelect packet) { assert FMLCommonHandler.instance().getEffectiveSide().isClient(); EntityPlayer player = Proxies.common.getPlayer(); @@ -207,10 +204,22 @@ public class PacketHandler { } ((IGuiSelectable) container).setSelection(packet); - } - private void onSocketUpdate(PacketSocketUpdate packet) { + private static void onGuiLayoutSelect(PacketString packet) { + assert FMLCommonHandler.instance().getEffectiveSide().isClient(); + + EntityPlayer player = Proxies.common.getPlayer(); + + Container container = player.openContainer; + if (!(container instanceof ContainerSolderingIron)) { + return; + } + + ((ContainerSolderingIron) container).setLayout(packet.getString()); + } + + private static void onSocketUpdate(PacketSocketUpdate packet) { assert FMLCommonHandler.instance().getEffectiveSide().isClient(); TileEntity tile = packet.getTarget(Proxies.common.getRenderWorld()); @@ -219,21 +228,12 @@ public class PacketHandler { } ISocketable socketable = (ISocketable) tile; - for (int i = 0; i < packet.itemstacks.length; i++) { - socketable.setSocket(i, packet.itemstacks[i]); + for (int i = 0; i < packet.itemStacks.length; i++) { + socketable.setSocket(i, packet.itemStacks[i]); } } - private void onTileUpdate(ForestryPacket packet) { - - TileEntity tile = ((ILocatedPacket) packet).getTarget(Proxies.common.getRenderWorld()); - if (tile instanceof INetworkedEntity) { - ((INetworkedEntity) tile).fromPacket(packet); - } - - } - - private void onInventoryStack(PacketInventoryStack packet) { + private static void onInventoryStack(PacketInventoryStack packet) { TileEntity tile = packet.getTarget(Proxies.common.getRenderWorld()); if (tile == null) { @@ -245,7 +245,7 @@ public class PacketHandler { } } - private void onChipsetClick(PacketUpdate packet, EntityPlayer player) { + private static void onChipsetClick(PacketSlotClick packet, EntityPlayer player) { assert FMLCommonHandler.instance().getEffectiveSide().isServer(); if (!(player.openContainer instanceof ContainerSocketed)) { @@ -256,11 +256,10 @@ public class PacketHandler { return; } - ((ContainerSocketed) player.openContainer).handleChipsetClick(packet.payload.intPayload[0], player, itemstack); - + ((ContainerSocketed) player.openContainer).handleChipsetClick(packet.getSlot(), player, itemstack); } - private void onSolderingIronClick(PacketUpdate packet, EntityPlayer player) { + private static void onSolderingIronClick(PacketSlotClick packet, EntityPlayer player) { assert FMLCommonHandler.instance().getEffectiveSide().isServer(); if (!(player.openContainer instanceof ContainerSocketed)) { @@ -268,10 +267,10 @@ public class PacketHandler { } ItemStack itemstack = player.inventory.getItemStack(); - ((ContainerSocketed) player.openContainer).handleSolderingIronClick(packet.payload.intPayload[0], player, itemstack); + ((ContainerSocketed) player.openContainer).handleSolderingIronClick(packet.getSlot(), player, itemstack); } - private void onAccessSwitch(PacketCoordinates packet, EntityPlayer playerEntity) { + private static void onAccessSwitch(PacketCoordinates packet, EntityPlayer playerEntity) { assert FMLCommonHandler.instance().getEffectiveSide().isServer(); TileForestry tile = (TileForestry) packet.getTarget(playerEntity.worldObj); @@ -282,15 +281,13 @@ public class PacketHandler { tile.switchAccessRule(playerEntity); } - private void onPipetteClick(PacketUpdate packet, EntityPlayerMP player) { + private static void onPipetteClick(PacketSlotClick packet, EntityPlayerMP player) { assert FMLCommonHandler.instance().getEffectiveSide().isServer(); if (!(player.openContainer instanceof ContainerLiquidTanks)) { return; } - ((ContainerLiquidTanks) player.openContainer).handlePipetteClick(packet.payload.intPayload[0], player); + ((ContainerLiquidTanks) player.openContainer).handlePipetteClick(packet.getSlot(), player); } - - private final FMLEventChannel channel; } diff --git a/src/main/java/forestry/core/network/PacketHelper.java b/src/main/java/forestry/core/network/PacketHelper.java new file mode 100644 index 000000000..006ded949 --- /dev/null +++ b/src/main/java/forestry/core/network/PacketHelper.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * 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.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.inventory.IInventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; + +import cpw.mods.fml.common.registry.GameData; + +import forestry.core.proxy.Proxies; + +public class PacketHelper { + + public static ItemStack readItemStack(DataInputStream data) throws IOException { + ItemStack itemstack = null; + String itemName = data.readUTF(); + + if (!itemName.isEmpty()) { + Item item = GameData.getItemRegistry().getRaw(itemName); + byte stackSize = data.readByte(); + short meta = data.readShort(); + itemstack = new ItemStack(item, stackSize, meta); + + if (item.isDamageable() || item.getShareTag()) { + itemstack.stackTagCompound = readNBTTagCompound(data); + } + } + + return itemstack; + } + + public static void writeItemStack(ItemStack itemstack, DataOutputStream data) throws IOException { + + if (itemstack == null) { + data.writeUTF(""); + } else { + data.writeUTF(GameData.getItemRegistry().getNameForObject(itemstack.getItem())); + data.writeByte(itemstack.stackSize); + data.writeShort(itemstack.getItemDamage()); + + if (itemstack.getItem().isDamageable() || itemstack.getItem().getShareTag()) { + writeNBTTagCompound(itemstack.stackTagCompound, data); + } + } + } + + public static ItemStack[] readItemStacks(DataInputStream data) throws IOException { + int stackCount = data.readShort(); + ItemStack[] itemStacks = new ItemStack[stackCount]; + for (int i = 0; i < stackCount; i++) { + itemStacks[i] = PacketHelper.readItemStack(data); + } + return itemStacks; + } + + public static void writeItemStacks(ItemStack[] itemStacks, DataOutputStream data) throws IOException { + data.writeShort(itemStacks.length); + for (ItemStack itemstack : itemStacks) { + PacketHelper.writeItemStack(itemstack, data); + } + } + + public static void writeInventory(IInventory inventory, DataOutputStream data) throws IOException { + int size = inventory.getSizeInventory(); + data.writeShort(size); + + for (int i = 0; i < size; i++) { + ItemStack stack = inventory.getStackInSlot(i); + PacketHelper.writeItemStack(stack, data); + } + } + + public static void readInventory(IInventory inventory, DataInputStream data) throws IOException { + int size = data.readShort(); + + for (int i = 0; i < size; i++) { + ItemStack stack = PacketHelper.readItemStack(data); + inventory.setInventorySlotContents(i, stack); + } + } + + public static void writeStreamables(List streamables, DataOutputStream data) throws IOException { + if (streamables == null) { + data.writeInt(0); + } else { + data.writeInt(streamables.size()); + for (IStreamable streamable : streamables) { + if (streamable != null) { + data.writeBoolean(true); + streamable.writeData(data); + } else { + data.writeBoolean(false); + } + } + } + } + + public static List readStreamables(Class streamableClass, DataInputStream data) throws IOException { + int length = data.readInt(); + List streamables = new ArrayList(length); + + try { + if (length > 0) { + for (int i = 0; i < length; i++) { + if (data.readBoolean()) { + T streamable = streamableClass.newInstance(); + streamable.readData(data); + streamables.add(streamable); + } else { + streamables.add(null); + } + } + } + } catch (ReflectiveOperationException e) { + Proxies.log.severe("Failed to read Streamables for class " + streamableClass + " with error " + e); + } + return streamables; + } + + private static NBTTagCompound readNBTTagCompound(DataInputStream data) throws IOException { + + short length = data.readShort(); + + if (length < 0) { + return null; + } else { + byte[] compressed = new byte[length]; + data.readFully(compressed); + return CompressedStreamTools.readCompressed(new ByteArrayInputStream(compressed)); + } + } + + private static void writeNBTTagCompound(NBTTagCompound nbttagcompound, DataOutputStream data) throws IOException { + + if (nbttagcompound == null) { + data.writeShort(-1); + } else { + byte[] compressed = CompressedStreamTools.compress(nbttagcompound); + data.writeShort((short) compressed.length); + data.write(compressed); + } + } +} diff --git a/src/main/java/forestry/core/network/PacketId.java b/src/main/java/forestry/core/network/PacketId.java new file mode 100644 index 000000000..7bbcf4fc4 --- /dev/null +++ b/src/main/java/forestry/core/network/PacketId.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * 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; + +public enum PacketId { + INVALID, + + TILE_FORESTRY_UPDATE, + TILE_FORESTRY_ERROR_UPDATE, + IINVENTORY_STACK, + FX_SIGNAL, + + HABITAT_BIOME_POINTER, + CANDLE, + + GUI_SELECTION_CHANGE, + IMPRINT_SELECTION_GET, + GUI_SELECTION_SET, + + GUI_LAYOUT_SELECT, + + PIPETTE_CLICK, + + ACCESS_SWITCH, + + GENOME_TRACKER_UPDATE, + GAME_TOKEN_SET, + + CHIPSET_CLICK, + SOLDERING_IRON_CLICK, + SOCKET_UPDATE, + + LETTER_REQUEST_INFO, + LETTER_TEXT, + TRADING_ADDRESS_SET, + LETTER_INFO, + POBOX_INFO, + POBOX_INFO_REQUEST, + + RIPENING_UPDATE, + + GUI_INTEGER, + + // Propolis pipe + PROP_SEND_FILTER_SET, + PROP_REQUEST_FILTER_SET, + PROP_SEND_FILTER_CHANGE_GENOME, + PROP_SEND_FILTER_CHANGE_TYPE + ; + + public static final PacketId[] VALUES = values(); +} diff --git a/src/main/java/forestry/core/network/PacketIds.java b/src/main/java/forestry/core/network/PacketIds.java deleted file mode 100755 index 7550c9214..000000000 --- a/src/main/java/forestry/core/network/PacketIds.java +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************************************* - * 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; - -/** - * Valid ids: 0 - 127 - */ -public class PacketIds { - - public static final int TILE_FORESTRY_UPDATE = 0; - public static final int TILE_UPDATE = 1; - public static final int TILE_DESCRIPTION = 2; - public static final int IINVENTORY_STACK = 3; - public static final int FX_SIGNAL = 4; - public static final int TILE_NBT = 5; - - public static final int HABITAT_BIOME_POINTER = 20; - public static final int CANDLE = 21; - - public static final int GUI_SELECTION_CHANGE = 30; - public static final int IMPRINT_SELECTION_GET = 31; - public static final int GUI_SELECTION = 32; - - public static final int PIPETTE_CLICK = 40; - - public static final int ACCESS_SWITCH = 50; - - public static final int GENOME_TRACKER_UPDATE = 60; - public static final int GAME_TOKEN_SET = 61; - - public static final int CHIPSET_CLICK = 70; - public static final int SOLDERING_IRON_CLICK = 71; - public static final int SOCKET_UPDATE = 72; - - public static final int LETTER_RECIPIENT = 80; - public static final int LETTER_TEXT = 81; - public static final int TRADING_ADDRESS_SET = 82; - public static final int LETTER_INFO = 83; - public static final int POBOX_INFO = 85; - public static final int POBOX_INFO_REQUEST = 86; - - public static final int SAPLING = 89; - public static final int LEAF = 90; - public static final int RIPENING_UPDATE = 91; - - public static final int GUI_INTEGER = 92; - - // Propolis pipe - public static final int PROP_SEND_FILTER_SET = 100; - public static final int PROP_REQUEST_FILTER_SET = 101; - public static final int PROP_SEND_FILTER_CHANGE_GENOME = 102; - public static final int PROP_SEND_FILTER_CHANGE_TYPE = 103; - - -} diff --git a/src/main/java/forestry/core/network/PacketInventoryStack.java b/src/main/java/forestry/core/network/PacketInventoryStack.java index fbc8cdbf2..0320aec25 100755 --- a/src/main/java/forestry/core/network/PacketInventoryStack.java +++ b/src/main/java/forestry/core/network/PacketInventoryStack.java @@ -21,27 +21,27 @@ public class PacketInventoryStack extends PacketCoordinates { public int slotIndex; public ItemStack itemstack; - public PacketInventoryStack() { + public PacketInventoryStack(DataInputStream data) throws IOException { + super(data); } - public PacketInventoryStack(int id, int posX, int posY, int posZ, int slotIndex, ItemStack itemstack) { + public PacketInventoryStack(PacketId id, int posX, int posY, int posZ, int slotIndex, ItemStack itemstack) { super(id, posX, posY, posZ); this.slotIndex = slotIndex; this.itemstack = itemstack; } @Override - public void writeData(DataOutputStream data) throws IOException { + protected void writeData(DataOutputStream data) throws IOException { super.writeData(data); data.writeInt(slotIndex); - writeItemStack(itemstack, data); + PacketHelper.writeItemStack(itemstack, data); } @Override - public void readData(DataInputStream data) throws IOException { + protected void readData(DataInputStream data) throws IOException { super.readData(data); slotIndex = data.readInt(); - itemstack = readItemStack(data); + itemstack = PacketHelper.readItemStack(data); } - } diff --git a/src/main/java/forestry/core/network/PacketNBT.java b/src/main/java/forestry/core/network/PacketNBT.java index c221b484b..39122075b 100755 --- a/src/main/java/forestry/core/network/PacketNBT.java +++ b/src/main/java/forestry/core/network/PacketNBT.java @@ -22,14 +22,11 @@ public class PacketNBT extends ForestryPacket { protected NBTTagCompound nbttagcompound; - public PacketNBT() { + public PacketNBT(DataInputStream data) throws IOException { + super(data); } - public PacketNBT(int id) { - super(id); - } - - public PacketNBT(int id, NBTTagCompound nbttagcompound) { + public PacketNBT(PacketId id, NBTTagCompound nbttagcompound) { super(id); this.nbttagcompound = nbttagcompound; } @@ -42,7 +39,7 @@ public class PacketNBT extends ForestryPacket { } @Override - public void readData(DataInputStream data) throws IOException { + protected void readData(DataInputStream data) throws IOException { short length = data.readShort(); byte[] compressed = new byte[length]; data.readFully(compressed); diff --git a/src/main/java/forestry/core/network/PacketPayload.java b/src/main/java/forestry/core/network/PacketPayload.java deleted file mode 100755 index 4b1deb2d8..000000000 --- a/src/main/java/forestry/core/network/PacketPayload.java +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************* - * 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 forestry.core.utils.Utils; - -public class PacketPayload { - public int[] intPayload = new int[0]; - public short[] shortPayload = new short[0]; - public float[] floatPayload = new float[0]; - public String[] stringPayload = new String[0]; - - public PacketPayload() { - } - - public PacketPayload(int intSize, int floatSize, int stringSize) { - intPayload = new int[intSize]; - floatPayload = new float[floatSize]; - stringPayload = new String[stringSize]; - } - - public PacketPayload(int intSize, int shortSize) { - intPayload = new int[intSize]; - shortPayload = new short[shortSize]; - } - - public void append(PacketPayload other) { - if (other == null) { - return; - } - - if (other.intPayload.length > 0) { - this.intPayload = Utils.concat(this.intPayload, other.intPayload); - } - if (other.shortPayload.length > 0) { - this.shortPayload = Utils.concat(this.shortPayload, other.shortPayload); - } - if (other.floatPayload.length > 0) { - this.floatPayload = Utils.concat(this.floatPayload, other.floatPayload); - } - if (other.stringPayload.length > 0) { - this.stringPayload = Utils.concat(this.stringPayload, other.stringPayload); - } - - } - - public void append(int[] other) { - if (other == null || other.length < 0) { - return; - } - - this.intPayload = Utils.concat(this.intPayload, other); - } - - public boolean isEmpty() { - return intPayload.length == 0 && shortPayload.length == 0 && floatPayload.length == 0 && stringPayload.length == 0; - } -} diff --git a/src/main/java/forestry/core/network/PacketSlotClick.java b/src/main/java/forestry/core/network/PacketSlotClick.java new file mode 100644 index 000000000..a388d09d5 --- /dev/null +++ b/src/main/java/forestry/core/network/PacketSlotClick.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * 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.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.tileentity.TileEntity; + +public class PacketSlotClick extends PacketCoordinates { + private int slot; + + public PacketSlotClick(DataInputStream data) throws IOException { + super(data); + } + + public PacketSlotClick(PacketId id, TileEntity tile, int slot) { + super(id, tile); + this.slot = slot; + } + + @Override + protected void readData(DataInputStream data) throws IOException { + super.readData(data); + this.slot = data.readInt(); + } + + @Override + protected void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeInt(slot); + } + + public int getSlot() { + return slot; + } +} diff --git a/src/main/java/forestry/core/network/PacketSocketUpdate.java b/src/main/java/forestry/core/network/PacketSocketUpdate.java index 0a7e8167b..120999afd 100755 --- a/src/main/java/forestry/core/network/PacketSocketUpdate.java +++ b/src/main/java/forestry/core/network/PacketSocketUpdate.java @@ -16,42 +16,37 @@ import java.io.IOException; import net.minecraft.item.ItemStack; +import forestry.core.gadgets.TileForestry; import forestry.core.interfaces.ISocketable; public class PacketSocketUpdate extends PacketCoordinates { - public ItemStack[] itemstacks; + public ItemStack[] itemStacks; - public PacketSocketUpdate() { + public PacketSocketUpdate(DataInputStream data) throws IOException { + super(data); } - public PacketSocketUpdate(int id, int posX, int posY, int posZ, ISocketable tile) { - super(id, posX, posY, posZ); + public PacketSocketUpdate(PacketId id, T tile) { + super(id, tile); - itemstacks = new ItemStack[tile.getSocketCount()]; + itemStacks = new ItemStack[tile.getSocketCount()]; for (int i = 0; i < tile.getSocketCount(); i++) { - itemstacks[i] = tile.getSocket(i); + itemStacks[i] = tile.getSocket(i); } } @Override - public void writeData(DataOutputStream data) throws IOException { + protected void writeData(DataOutputStream data) throws IOException { super.writeData(data); - data.writeShort(itemstacks.length); - for (ItemStack itemstack : itemstacks) { - writeItemStack(itemstack, data); - } + PacketHelper.writeItemStacks(itemStacks, data); } @Override - public void readData(DataInputStream data) throws IOException { + protected void readData(DataInputStream data) throws IOException { super.readData(data); - int sockets = data.readShort(); - itemstacks = new ItemStack[sockets]; - for (int i = 0; i < sockets; i++) { - itemstacks[i] = readItemStack(data); - } + itemStacks = PacketHelper.readItemStacks(data); } } diff --git a/src/main/java/forestry/core/network/PacketString.java b/src/main/java/forestry/core/network/PacketString.java new file mode 100644 index 000000000..0834ec2c4 --- /dev/null +++ b/src/main/java/forestry/core/network/PacketString.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * 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.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class PacketString extends ForestryPacket { + + private String string; + + public PacketString(DataInputStream data) throws IOException { + super(data); + } + + public PacketString(PacketId id, String string) { + super(id); + this.string = string; + } + + @Override + protected void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeUTF(string); + } + + @Override + protected void readData(DataInputStream data) throws IOException { + super.readData(data); + string = data.readUTF(); + } + + public String getString() { + return string; + } +} diff --git a/src/main/java/forestry/core/network/PacketTileStream.java b/src/main/java/forestry/core/network/PacketTileStream.java new file mode 100644 index 000000000..376678bb4 --- /dev/null +++ b/src/main/java/forestry/core/network/PacketTileStream.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * 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.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.tileentity.TileEntity; + +import forestry.core.proxy.Proxies; + +public class PacketTileStream extends PacketCoordinates { + + private IStreamable streamable; + + public PacketTileStream(DataInputStream data) throws IOException { + super(data); + } + + public PacketTileStream(T streamable) { + super(PacketId.TILE_FORESTRY_UPDATE, streamable); + this.streamable = streamable; + } + + @Override + protected void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + streamable.writeData(data); + } + + @Override + protected void readData(DataInputStream data) throws IOException { + super.readData(data); + + TileEntity tile = getTarget(Proxies.common.getRenderWorld()); + if (tile instanceof IStreamable) { + ((IStreamable) tile).readData(data); + } + } +} diff --git a/src/main/java/forestry/core/network/PacketTileUpdate.java b/src/main/java/forestry/core/network/PacketTileUpdate.java deleted file mode 100755 index ce60dfa76..000000000 --- a/src/main/java/forestry/core/network/PacketTileUpdate.java +++ /dev/null @@ -1,123 +0,0 @@ -/******************************************************************************* - * 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.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - -import com.mojang.authlib.GameProfile; - -import net.minecraftforge.common.util.ForgeDirection; - -import forestry.api.core.ErrorStateRegistry; -import forestry.api.core.IErrorState; -import forestry.core.gadgets.TileForestry; -import forestry.core.utils.EnumAccess; - -public class PacketTileUpdate extends PacketUpdate { - - private ForgeDirection orientation = ForgeDirection.WEST; - private final Set errorStates; - - private boolean isOwnable = false; - private EnumAccess access = EnumAccess.SHARED; - private GameProfile owner = null; - - public PacketTileUpdate() { - errorStates = new HashSet(); - } - - public PacketTileUpdate(TileForestry tile) { - super(PacketIds.TILE_FORESTRY_UPDATE, tile); - - orientation = tile.getOrientation(); - errorStates = tile.getErrorStates(); - - isOwnable = tile.isOwnable(); - access = tile.getAccess(); - owner = tile.getOwnerProfile(); - } - - @Override - public void writeData(DataOutputStream data) throws IOException { - super.writeData(data); - - data.writeByte(orientation.ordinal()); - - data.writeShort(errorStates.size()); - for (IErrorState errorState : errorStates) { - data.writeShort(errorState.getID()); - } - - // TODO: Should this really be sent to the client? Huge network cost. - // As far as I know, only GUIs need it, and there are better ways to get the information to a GUI. - // -CovertJaguar - if (isOwnable) { - data.writeByte(access.ordinal()); - if (owner == null) { - data.writeBoolean(false); - } else { - data.writeBoolean(true); - data.writeLong(owner.getId().getMostSignificantBits()); - data.writeLong(owner.getId().getLeastSignificantBits()); - data.writeUTF(owner.getName()); - } - } else { - data.writeInt(-1); - } - } - - @Override - public void readData(DataInputStream data) throws IOException { - super.readData(data); - - orientation = ForgeDirection.getOrientation(data.readByte()); - - short errorStateCount = data.readShort(); - for (int i = 0; i < errorStateCount; i++) { - short errorStateId = data.readShort(); - IErrorState errorState = ErrorStateRegistry.getErrorState(errorStateId); - errorStates.add(errorState); - } - - int ordinal = data.readByte(); - isOwnable = ordinal >= 0; - if (isOwnable) { - access = EnumAccess.values()[ordinal]; - - if (data.readBoolean()) { - owner = new GameProfile(new UUID(data.readLong(), data.readLong()), data.readUTF()); - } else { - owner = null; - } - } - } - - public ForgeDirection getOrientation() { - return orientation; - } - - public Set getErrorStates() { - return errorStates; - } - - public EnumAccess getAccess() { - return access; - } - - public GameProfile getOwner() { - return owner; - } -} diff --git a/src/main/java/forestry/core/network/PacketUpdate.java b/src/main/java/forestry/core/network/PacketUpdate.java deleted file mode 100755 index cbb8079dd..000000000 --- a/src/main/java/forestry/core/network/PacketUpdate.java +++ /dev/null @@ -1,120 +0,0 @@ -/******************************************************************************* - * 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.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import forestry.core.gadgets.TileForestry; - -public class PacketUpdate extends PacketCoordinates { - - public PacketPayload payload; - - public PacketUpdate() { - } - - public PacketUpdate(int id) { - this(id, 0, 0, 0, null); - } - - public PacketUpdate(int id, TileForestry tile) { - super(id, tile); - this.payload = tile.getPacketPayload(); - } - - public PacketUpdate(int id, PacketPayload payload) { - this(id, 0, 0, 0, payload); - } - - public PacketUpdate(int id, int posX, int posY, int posZ, PacketPayload payload) { - super(id, posX, posY, posZ); - - this.payload = payload; - } - - public PacketUpdate(int id, int posX, int posY, int posZ, short val) { - super(id, posX, posY, posZ); - - this.payload = new PacketPayload(0, 1); - this.payload.shortPayload[0] = val; - } - - public PacketUpdate(int id, int posX, int posY, int posZ, int val) { - super(id, posX, posY, posZ); - - this.payload = new PacketPayload(1, 0); - this.payload.intPayload[0] = val; - } - - @Override - public void writeData(DataOutputStream data) throws IOException { - - super.writeData(data); - - // No payload means no data - if (payload == null) { - data.writeInt(0); - data.writeInt(0); - data.writeInt(0); - data.writeInt(0); - return; - } - - data.writeInt(payload.intPayload.length); - data.writeInt(payload.shortPayload.length); - data.writeInt(payload.floatPayload.length); - data.writeInt(payload.stringPayload.length); - - for (int intData : payload.intPayload) { - data.writeInt(intData); - } - for (int shortData : payload.shortPayload) { - data.writeShort(shortData); - } - for (float floatData : payload.floatPayload) { - data.writeFloat(floatData); - } - for (String stringData : payload.stringPayload) { - data.writeUTF(stringData); - } - - } - - @Override - public void readData(DataInputStream data) throws IOException { - - super.readData(data); - - payload = new PacketPayload(); - - payload.intPayload = new int[data.readInt()]; - payload.shortPayload = new short[data.readInt()]; - payload.floatPayload = new float[data.readInt()]; - payload.stringPayload = new String[data.readInt()]; - - for (int i = 0; i < payload.intPayload.length; i++) { - payload.intPayload[i] = data.readInt(); - } - for (int i = 0; i < payload.shortPayload.length; i++) { - payload.shortPayload[i] = data.readShort(); - } - for (int i = 0; i < payload.floatPayload.length; i++) { - payload.floatPayload[i] = data.readFloat(); - } - for (int i = 0; i < payload.stringPayload.length; i++) { - payload.stringPayload[i] = data.readUTF(); - } - - } - -} diff --git a/src/main/java/forestry/core/proxy/ProxyCommon.java b/src/main/java/forestry/core/proxy/ProxyCommon.java index 021da79eb..88970dd9a 100755 --- a/src/main/java/forestry/core/proxy/ProxyCommon.java +++ b/src/main/java/forestry/core/proxy/ProxyCommon.java @@ -55,7 +55,7 @@ import forestry.core.config.ForestryBlock; import forestry.core.config.ForestryItem; import forestry.core.network.PacketCoordinates; import forestry.core.network.PacketFXSignal; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.render.SpriteSheet; import forestry.core.utils.StringUtil; import forestry.plugins.PluginManager; @@ -145,7 +145,7 @@ public class ProxyCommon { public void setHabitatLocatorCoordinates(Entity player, ChunkCoordinates coordinates) { if (coordinates != null) { - Forestry.packetHandler.sendPacket(new PacketCoordinates(PacketIds.HABITAT_BIOME_POINTER, coordinates).getPacket(), (EntityPlayerMP) player); + Forestry.packetHandler.sendPacket(new PacketCoordinates(PacketId.HABITAT_BIOME_POINTER, coordinates).getPacket(), (EntityPlayerMP) player); } } @@ -203,7 +203,7 @@ public class ProxyCommon { } public void playSoundFX(World world, int x, int y, int z, Block block) { - Proxies.net.sendNetworkPacket(new PacketFXSignal(PacketFXSignal.SoundFXType.LEAF, x, y, z, block, 0), x, y, z); + Proxies.net.sendNetworkPacket(new PacketFXSignal(PacketFXSignal.SoundFXType.LEAF, x, y, z, block, 0)); } public void playSoundFX(World world, int x, int y, int z, String sound, float volume, float pitch) { @@ -235,7 +235,7 @@ public class ProxyCommon { public void sendFXSignal(PacketFXSignal.VisualFXType visualFX, PacketFXSignal.SoundFXType soundFX, World world, int xCoord, int yCoord, int zCoord, Block block, int i) { if (Proxies.common.isSimulating(world)) { - Proxies.net.sendNetworkPacket(new PacketFXSignal(visualFX, soundFX, xCoord, yCoord, zCoord, block, i), xCoord, yCoord, zCoord); + Proxies.net.sendNetworkPacket(new PacketFXSignal(visualFX, soundFX, xCoord, yCoord, zCoord, block, i)); } } diff --git a/src/main/java/forestry/core/proxy/ProxyNetwork.java b/src/main/java/forestry/core/proxy/ProxyNetwork.java index f60c4da2a..a9a6a23f7 100755 --- a/src/main/java/forestry/core/proxy/ProxyNetwork.java +++ b/src/main/java/forestry/core/proxy/ProxyNetwork.java @@ -20,27 +20,35 @@ import net.minecraftforge.common.util.FakePlayer; import forestry.Forestry; import forestry.core.config.Defaults; import forestry.core.network.ForestryPacket; +import forestry.core.network.ILocatedPacket; public class ProxyNetwork { - public void sendNetworkPacket(ForestryPacket packet, int x, int y, int z) { + public

void sendNetworkPacket(P packet) { if (packet == null) { return; } + int x = packet.getPosX(); + int y = packet.getPosY(); + int z = packet.getPosZ(); World[] worlds = DimensionManager.getWorlds(); for (World world : worlds) { for (int j = 0; j < world.playerEntities.size(); j++) { EntityPlayerMP player = (EntityPlayerMP) world.playerEntities.get(j); - - if (Math.abs(player.posX - x) <= Defaults.NET_MAX_UPDATE_DISTANCE && Math.abs(player.posY - y) <= Defaults.NET_MAX_UPDATE_DISTANCE - && Math.abs(player.posZ - z) <= Defaults.NET_MAX_UPDATE_DISTANCE) { + if (isPlayerInRange(player, x, y, z)) { Forestry.packetHandler.sendPacket(packet.getPacket(), player); } } } } + private static boolean isPlayerInRange(EntityPlayerMP player, int x, int y, int z) { + return Math.abs(player.posX - x) <= Defaults.NET_MAX_UPDATE_DISTANCE && + Math.abs(player.posY - y) <= Defaults.NET_MAX_UPDATE_DISTANCE && + Math.abs(player.posZ - z) <= Defaults.NET_MAX_UPDATE_DISTANCE; + } + public void sendToPlayer(ForestryPacket packet, EntityPlayer entityplayer) { if (!(entityplayer instanceof EntityPlayerMP) || (entityplayer instanceof FakePlayer)) { return; diff --git a/src/main/java/forestry/core/utils/PlayerUtil.java b/src/main/java/forestry/core/utils/PlayerUtil.java index a2acc6aa5..6d91b70ef 100644 --- a/src/main/java/forestry/core/utils/PlayerUtil.java +++ b/src/main/java/forestry/core/utils/PlayerUtil.java @@ -26,7 +26,7 @@ public class PlayerUtil { } public static String getOwnerName(IOwnable ownable) { - GameProfile profile = ownable.getOwnerProfile(); + GameProfile profile = ownable.getOwner(); if (profile == null) { return StringUtil.localize("gui.derelict"); } else { diff --git a/src/main/java/forestry/core/utils/StringUtil.java b/src/main/java/forestry/core/utils/StringUtil.java index 0c6df8902..337923ce1 100755 --- a/src/main/java/forestry/core/utils/StringUtil.java +++ b/src/main/java/forestry/core/utils/StringUtil.java @@ -102,7 +102,7 @@ public class StringUtil { public static String line(int length) { StringBuilder line = new StringBuilder(); for (int i = 0; i < length; i++) { - line.append("-"); + line.append('-'); } return line.toString(); diff --git a/src/main/java/forestry/core/utils/Utils.java b/src/main/java/forestry/core/utils/Utils.java index 613e74e7c..f6145d565 100755 --- a/src/main/java/forestry/core/utils/Utils.java +++ b/src/main/java/forestry/core/utils/Utils.java @@ -10,7 +10,6 @@ ******************************************************************************/ package forestry.core.utils; -import java.util.Arrays; import java.util.Random; import net.minecraft.block.Block; @@ -25,7 +24,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MathHelper; import net.minecraft.world.World; -import net.minecraft.world.biome.BiomeGenBase; import cpw.mods.fml.common.registry.EntityRegistry; @@ -104,30 +102,6 @@ public class Utils { } - public static T[] concat(T[] first, T[] second) { - T[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - public static int[] concat(int[] first, int[] second) { - int[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - public static short[] concat(short[] first, short[] second) { - short[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - public static float[] concat(float[] first, float[] second) { - float[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - public static boolean canWrench(EntityPlayer player, int x, int y, int z) { ItemStack itemstack = player.getCurrentEquippedItem(); diff --git a/src/main/java/forestry/energy/EnergyManager.java b/src/main/java/forestry/energy/EnergyManager.java index c60e5631c..a4b782b25 100644 --- a/src/main/java/forestry/energy/EnergyManager.java +++ b/src/main/java/forestry/energy/EnergyManager.java @@ -1,18 +1,23 @@ package forestry.energy; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; import forestry.core.GameMode; +import forestry.core.network.IStreamable; import forestry.core.utils.BlockUtil; import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyHandler; import cofh.api.energy.IEnergyReceiver; -public class EnergyManager implements IEnergyHandler { +public class EnergyManager implements IEnergyHandler, IStreamable { private enum EnergyTransferMode { EXTRACT, RECEIVE, BOTH } @@ -79,11 +84,23 @@ public class EnergyManager implements IEnergyHandler { } /* Packets */ - public int toPacketInt() { + @Override + public void writeData(DataOutputStream data) throws IOException { + int energyStored = energyStorage.getEnergyStored(); + data.writeInt(energyStored); + } + + @Override + public void readData(DataInputStream data) throws IOException { + int energyStored = data.readInt(); + energyStorage.setEnergyStored(energyStored); + } + + public int toGuiInt() { return energyStorage.getEnergyStored(); } - public void fromPacketInt(int packetInt) { + public void fromGuiInt(int packetInt) { energyStorage.setEnergyStored(packetInt); } diff --git a/src/main/java/forestry/energy/GuiHandlerEnergy.java b/src/main/java/forestry/energy/GuiHandlerEnergy.java index b32319167..9aec261d9 100755 --- a/src/main/java/forestry/energy/GuiHandlerEnergy.java +++ b/src/main/java/forestry/energy/GuiHandlerEnergy.java @@ -15,7 +15,7 @@ import net.minecraft.world.World; import forestry.core.GuiHandlerBase; import forestry.core.network.GuiId; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.network.PacketSocketUpdate; import forestry.core.proxy.Proxies; import forestry.energy.gadgets.EngineBronze; @@ -76,7 +76,7 @@ public class GuiHandlerEnergy extends GuiHandlerBase { case EngineTinGUI: EngineTin tile = (EngineTin) getTileForestry(world, x, y, z); - Proxies.net.sendToPlayer(new PacketSocketUpdate(PacketIds.SOCKET_UPDATE, x, y, z, tile), player); + Proxies.net.sendToPlayer(new PacketSocketUpdate(PacketId.SOCKET_UPDATE, tile), player); return new ContainerEngineTin(player.inventory, tile); case GeneratorGUI: diff --git a/src/main/java/forestry/energy/gadgets/EngineBronze.java b/src/main/java/forestry/energy/gadgets/EngineBronze.java index 16044ce28..62c229c79 100755 --- a/src/main/java/forestry/energy/gadgets/EngineBronze.java +++ b/src/main/java/forestry/energy/gadgets/EngineBronze.java @@ -10,6 +10,10 @@ ******************************************************************************/ package forestry.energy.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; @@ -40,34 +44,12 @@ import forestry.core.interfaces.ILiquidTankContainer; import forestry.core.inventory.IInventoryAdapter; import forestry.core.inventory.TileInventoryAdapter; import forestry.core.network.GuiId; -import forestry.core.network.PacketPayload; public class EngineBronze extends Engine implements ISidedInventory, ILiquidTankContainer { /* CONSTANTS */ public static final short SLOT_CAN = 0; - /* NETWORK */ - @Override - public PacketPayload getPacketPayload() { - PacketPayload payload = super.getPacketPayload(); - - if (shutdown) { - payload.append(new int[]{1}); - } else { - payload.append(new int[]{0}); - } - - return payload; - } - - @Override - public void fromPacketPayload(PacketPayload payload) { - super.fromPacketPayload(payload); - - shutdown = payload.intPayload[3] > 0; - } - private final FilteredTank fuelTank; private final FilteredTank heatingTank; private final TankManager tankManager; @@ -156,7 +138,7 @@ public class EngineBronze extends Engine implements ISidedInventory, ILiquidTank currentOutput = determineFuelValue(FluidRegistry.getFluid(currentFluidId)); energyManager.generateEnergy(currentOutput); } else { - burnTime = totalTime = this.determineBurnTime(fuelTank.getFluid().getFluid()); + burnTime = totalTime = determineBurnTime(fuelTank.getFluid().getFluid()); currentFluidId = fuelTank.getFluid().getFluid().getID(); fuelTank.drain(Defaults.BUCKET_VOLUME, true); } @@ -228,7 +210,7 @@ public class EngineBronze extends Engine implements ISidedInventory, ILiquidTank /** * Returns the fuel value (power per cycle) an item of the passed fluid */ - private int determineFuelValue(Fluid fluid) { + private static int determineFuelValue(Fluid fluid) { if (FuelManager.bronzeEngineFuel.containsKey(fluid)) { return FuelManager.bronzeEngineFuel.get(fluid).powerPerCycle; } else { @@ -239,7 +221,7 @@ public class EngineBronze extends Engine implements ISidedInventory, ILiquidTank /** * @return Duration of burn cycle of one bucket */ - private int determineBurnTime(Fluid fluid) { + private static int determineBurnTime(Fluid fluid) { if (FuelManager.bronzeEngineFuel.containsKey(fluid)) { return FuelManager.bronzeEngineFuel.get(fluid).burnDuration; } else { @@ -299,6 +281,19 @@ public class EngineBronze extends Engine implements ISidedInventory, ILiquidTank tankManager.writeTanksToNBT(nbt); } + /* NETWORK */ + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeBoolean(shutdown); + } + + @Override + public void readData(DataInputStream data) throws IOException { + super.readData(data); + shutdown = data.readBoolean(); + } + /* GUI */ @Override public void getGUINetworkData(int id, int data) { @@ -315,7 +310,7 @@ public class EngineBronze extends Engine implements ISidedInventory, ILiquidTank currentOutput = data; break; case 3: - energyManager.fromPacketInt(data); + energyManager.fromGuiInt(data); break; case 4: heat = data; @@ -332,7 +327,7 @@ public class EngineBronze extends Engine implements ISidedInventory, ILiquidTank iCrafting.sendProgressBarUpdate(containerEngine, i, burnTime); iCrafting.sendProgressBarUpdate(containerEngine, i + 1, totalTime); iCrafting.sendProgressBarUpdate(containerEngine, i + 2, currentOutput); - iCrafting.sendProgressBarUpdate(containerEngine, i + 3, energyManager.toPacketInt()); + iCrafting.sendProgressBarUpdate(containerEngine, i + 3, energyManager.toGuiInt()); iCrafting.sendProgressBarUpdate(containerEngine, i + 4, heat); iCrafting.sendProgressBarUpdate(containerEngine, i + 5, currentFluidId); } diff --git a/src/main/java/forestry/energy/gadgets/EngineClockwork.java b/src/main/java/forestry/energy/gadgets/EngineClockwork.java index 626c69512..c887f5ffc 100755 --- a/src/main/java/forestry/energy/gadgets/EngineClockwork.java +++ b/src/main/java/forestry/energy/gadgets/EngineClockwork.java @@ -155,7 +155,10 @@ public class EngineClockwork extends Engine { } float fromClockwork = (tension / ENGINE_CLOCKWORK_WIND_MAX) * Defaults.ENGINE_PISTON_SPEED_MAX; - return fromClockwork >= 0.01f ? fromClockwork : 0; + + fromClockwork = Math.round(fromClockwork * 100f) / 100f; + + return fromClockwork; } @Override diff --git a/src/main/java/forestry/energy/gadgets/EngineCopper.java b/src/main/java/forestry/energy/gadgets/EngineCopper.java index 3ef734989..98fb11851 100755 --- a/src/main/java/forestry/energy/gadgets/EngineCopper.java +++ b/src/main/java/forestry/energy/gadgets/EngineCopper.java @@ -328,7 +328,7 @@ public class EngineCopper extends Engine implements ISidedInventory { currentOutput = j; break; case 3: - energyManager.fromPacketInt(j); + energyManager.fromGuiInt(j); break; case 4: heat = j; @@ -341,7 +341,7 @@ public class EngineCopper extends Engine implements ISidedInventory { iCrafting.sendProgressBarUpdate(containerEngine, 0, burnTime); iCrafting.sendProgressBarUpdate(containerEngine, 1, totalBurnTime); iCrafting.sendProgressBarUpdate(containerEngine, 2, currentOutput); - iCrafting.sendProgressBarUpdate(containerEngine, 3, energyManager.toPacketInt()); + iCrafting.sendProgressBarUpdate(containerEngine, 3, energyManager.toGuiInt()); iCrafting.sendProgressBarUpdate(containerEngine, 4, heat); } diff --git a/src/main/java/forestry/energy/gadgets/EngineTin.java b/src/main/java/forestry/energy/gadgets/EngineTin.java index f509594bb..872dc177f 100755 --- a/src/main/java/forestry/energy/gadgets/EngineTin.java +++ b/src/main/java/forestry/energy/gadgets/EngineTin.java @@ -31,7 +31,6 @@ import forestry.core.inventory.InventoryAdapter; import forestry.core.inventory.TileInventoryAdapter; import forestry.core.network.GuiId; import forestry.core.utils.DelayTimer; -import forestry.core.utils.EnumTankLevel; import forestry.plugins.PluginIC2; import ic2.api.energy.prefab.BasicSink; @@ -227,21 +226,6 @@ public class EngineTin extends Engine implements ISocketable, IInventory { return Math.min(i, (int) (ic2EnergySink.getEnergyStored() * i) / ic2EnergySink.getCapacity()); } - public static EnumTankLevel rateLevel(int scaled) { - - if (scaled < 5) { - return EnumTankLevel.EMPTY; - } else if (scaled < 30) { - return EnumTankLevel.LOW; - } else if (scaled < 60) { - return EnumTankLevel.MEDIUM; - } else if (scaled < 90) { - return EnumTankLevel.HIGH; - } else { - return EnumTankLevel.MAXIMUM; - } - } - // / SMP GUI @Override public void getGUINetworkData(int i, int j) { @@ -252,7 +236,7 @@ public class EngineTin extends Engine implements ISocketable, IInventory { currentOutput = j; break; case 1: - energyManager.fromPacketInt(j); + energyManager.fromGuiInt(j); break; case 2: heat = j; @@ -269,7 +253,7 @@ public class EngineTin extends Engine implements ISocketable, IInventory { @Override public void sendGUINetworkData(Container containerEngine, ICrafting iCrafting) { iCrafting.sendProgressBarUpdate(containerEngine, 0, currentOutput); - iCrafting.sendProgressBarUpdate(containerEngine, 1, energyManager.toPacketInt()); + iCrafting.sendProgressBarUpdate(containerEngine, 1, energyManager.toGuiInt()); iCrafting.sendProgressBarUpdate(containerEngine, 2, heat); if (ic2EnergySink != null) { iCrafting.sendProgressBarUpdate(containerEngine, 3, (short) ic2EnergySink.getEnergyStored()); diff --git a/src/main/java/forestry/energy/gui/GuiEngineTin.java b/src/main/java/forestry/energy/gui/GuiEngineTin.java index b24ddb3c6..42b5594b0 100755 --- a/src/main/java/forestry/energy/gui/GuiEngineTin.java +++ b/src/main/java/forestry/energy/gui/GuiEngineTin.java @@ -31,11 +31,26 @@ public class GuiEngineTin extends GuiEngine { EngineTin engine = inventory; int storageHeight = engine.getStorageScaled(46); int storageMaxHeight = engine.getStorageScaled(100); - EnumTankLevel rated = engine.rateLevel(storageMaxHeight); + EnumTankLevel rated = rateLevel(storageMaxHeight); drawHealthMeter(guiLeft + 74, guiTop + 25, storageHeight, rated); } + public static EnumTankLevel rateLevel(int scaled) { + + if (scaled < 5) { + return EnumTankLevel.EMPTY; + } else if (scaled < 30) { + return EnumTankLevel.LOW; + } else if (scaled < 60) { + return EnumTankLevel.MEDIUM; + } else if (scaled < 90) { + return EnumTankLevel.HIGH; + } else { + return EnumTankLevel.MAXIMUM; + } + } + private void drawHealthMeter(int x, int y, int height, EnumTankLevel rated) { int i = 176; int k = 0; diff --git a/src/main/java/forestry/factory/gadgets/MachineCarpenter.java b/src/main/java/forestry/factory/gadgets/MachineCarpenter.java index c45ce470d..2597f8d9e 100755 --- a/src/main/java/forestry/factory/gadgets/MachineCarpenter.java +++ b/src/main/java/forestry/factory/gadgets/MachineCarpenter.java @@ -435,7 +435,7 @@ public class MachineCarpenter extends TilePowered implements ISidedInventory, IL } private boolean removeSets(int count, ItemStack[] set) { - EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwnerProfile()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwner()); return InvTools.removeSets(getInternalInventory(), count, set, SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT, player, true, true, true); } diff --git a/src/main/java/forestry/factory/gadgets/MachineSqueezer.java b/src/main/java/forestry/factory/gadgets/MachineSqueezer.java index 6153497ce..b53d5ca0b 100755 --- a/src/main/java/forestry/factory/gadgets/MachineSqueezer.java +++ b/src/main/java/forestry/factory/gadgets/MachineSqueezer.java @@ -282,7 +282,7 @@ public class MachineSqueezer extends TilePowered implements ISidedInventory, ILi } private boolean removeResources(ItemStack[] stacks) { - EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwnerProfile()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwner()); return InvTools.removeSets(getInternalInventory(), 1, stacks, SLOT_RESOURCE_1, SLOTS_RESOURCE_COUNT, player, false, true, true); } diff --git a/src/main/java/forestry/factory/gadgets/TileWorktable.java b/src/main/java/forestry/factory/gadgets/TileWorktable.java index 327a3e591..18fe92d4d 100755 --- a/src/main/java/forestry/factory/gadgets/TileWorktable.java +++ b/src/main/java/forestry/factory/gadgets/TileWorktable.java @@ -10,6 +10,10 @@ ******************************************************************************/ package forestry.factory.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryCrafting; @@ -23,11 +27,8 @@ import forestry.core.interfaces.ICrafter; import forestry.core.inventory.InvTools; import forestry.core.inventory.InventoryAdapter; import forestry.core.inventory.TileInventoryAdapter; -import forestry.core.network.ForestryPacket; import forestry.core.network.GuiId; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketTileNBT; -import forestry.core.network.PacketTileUpdate; +import forestry.core.network.PacketTileStream; import forestry.core.proxy.Proxies; import forestry.core.utils.GuiUtil; import forestry.core.utils.RecipeUtil; @@ -66,7 +67,6 @@ public class TileWorktable extends TileBase implements ICrafter { super.writeToNBT(nbttagcompound); craftingInventory.writeToNBT(nbttagcompound); - memorized.writeToNBT(nbttagcompound); } @@ -75,24 +75,28 @@ public class TileWorktable extends TileBase implements ICrafter { super.readFromNBT(nbttagcompound); craftingInventory.readFromNBT(nbttagcompound); - memorized.readFromNBT(nbttagcompound); } /* NETWORK */ @Override - public void fromPacket(ForestryPacket packetRaw) { - if (packetRaw instanceof PacketTileUpdate) { - super.fromPacket(packetRaw); - return; - } + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); - PacketTileNBT packet = (PacketTileNBT) packetRaw; - readFromNBT(packet.getTagCompound()); + craftingInventory.writeData(data); + memorized.writeData(data); + } + + @Override + public void readData(DataInputStream data) throws IOException { + super.readData(data); + + craftingInventory.readData(data); + memorized.readData(data); } public void sendAll(EntityPlayer player) { - Proxies.net.sendToPlayer(new PacketTileNBT(PacketIds.TILE_NBT, this), player); + Proxies.net.sendToPlayer(new PacketTileStream(this), player); } @Override diff --git a/src/main/java/forestry/factory/gui/ContainerWorktable.java b/src/main/java/forestry/factory/gui/ContainerWorktable.java index c0bd94948..77e8440d9 100755 --- a/src/main/java/forestry/factory/gui/ContainerWorktable.java +++ b/src/main/java/forestry/factory/gui/ContainerWorktable.java @@ -21,9 +21,8 @@ import forestry.core.gui.IGuiSelectable; import forestry.core.gui.slots.SlotCraftMatrix; import forestry.core.gui.slots.SlotCrafter; import forestry.core.interfaces.IContainerCrafting; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketGuiSelect; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.core.utils.StackUtils; import forestry.factory.gadgets.TileWorktable; @@ -105,26 +104,23 @@ public class ContainerWorktable extends ContainerTile implements sendRecipeClick(0, tile.getMemory().capacity); } - public void sendRecipeClick(int mouseButton, int recipeIndex) { - PacketPayload payload = new PacketPayload(2, 0, 0); - payload.intPayload[0] = mouseButton; - payload.intPayload[1] = recipeIndex; - PacketUpdate packet = new PacketUpdate(PacketIds.GUI_SELECTION_CHANGE, payload); + public static void sendRecipeClick(int mouseButton, int recipeIndex) { + PacketGuiSelect packet = new PacketGuiSelect(PacketId.GUI_SELECTION_CHANGE, mouseButton, recipeIndex); Proxies.net.sendToServer(packet); } @Override - public void handleSelectionChange(EntityPlayer player, PacketUpdate packet) { - if (packet.payload.intPayload[0] > 0) { - tile.getMemory().toggleLock(player.worldObj, packet.payload.intPayload[1]); + public void handleSelectionChange(EntityPlayer player, PacketGuiSelect packet) { + if (packet.getPrimaryIndex() > 0) { + tile.getMemory().toggleLock(player.worldObj, packet.getSecondaryIndex()); } else { - tile.chooseRecipe(packet.payload.intPayload[1]); + tile.chooseRecipe(packet.getSecondaryIndex()); updateMatrix(); updateRecipe(); } } @Override - public void setSelection(PacketUpdate packet) { + public void setSelection(PacketGuiSelect packet) { } } diff --git a/src/main/java/forestry/factory/recipes/RecipeMemory.java b/src/main/java/forestry/factory/recipes/RecipeMemory.java index 3d1d0a79c..15a41949f 100644 --- a/src/main/java/forestry/factory/recipes/RecipeMemory.java +++ b/src/main/java/forestry/factory/recipes/RecipeMemory.java @@ -10,6 +10,9 @@ ******************************************************************************/ package forestry.factory.recipes; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -28,17 +31,22 @@ import net.minecraft.world.World; import forestry.api.core.INBTTagable; import forestry.core.gui.ContainerDummy; import forestry.core.inventory.InventoryAdapter; +import forestry.core.network.IStreamable; +import forestry.core.network.PacketHelper; import forestry.core.utils.PlainInventory; -public class RecipeMemory implements INBTTagable { +public class RecipeMemory implements INBTTagable, IStreamable { - public static final class Recipe implements INBTTagable { + public static final class Recipe implements INBTTagable, IStreamable { private InventoryAdapter matrix; private long lastUsed; private boolean locked; private ItemStack cachedRecipeOutput; + public Recipe() { + } + public Recipe(InventoryCrafting crafting) { this.matrix = new InventoryAdapter(new PlainInventory(crafting)); } @@ -106,6 +114,23 @@ public class RecipeMemory implements INBTTagable { nbttagcompound.setBoolean("Locked", locked); } + @Override + public void writeData(DataOutputStream data) throws IOException { + sanitizeMatrix(); + matrix.writeData(data); + data.writeLong(lastUsed); + data.writeBoolean(locked); + } + + @Override + public void readData(DataInputStream data) throws IOException { + matrix = new InventoryAdapter(new InventoryCrafting(DUMMY_CONTAINER, 3, 3)); + matrix.readData(data); + sanitizeMatrix(); + lastUsed = data.readLong(); + locked = data.readBoolean(); + } + /** * This is a nasty hack to hide a bug where the recipe stacks somehow * get the stackSize set to -1. Which makes the recipe no longer work, @@ -275,4 +300,15 @@ public class RecipeMemory implements INBTTagable { nbttagcompound.setTag("RecipeMemory", nbttaglist); } + @Override + public void writeData(DataOutputStream data) throws IOException { + PacketHelper.writeStreamables(recipes, data); + } + + @Override + public void readData(DataInputStream data) throws IOException { + recipes.clear(); + List newRecipes = PacketHelper.readStreamables(Recipe.class, data); + recipes.addAll(newRecipes); + } } diff --git a/src/main/java/forestry/farming/GuiHandlerFarming.java b/src/main/java/forestry/farming/GuiHandlerFarming.java index 79665dff2..25a5535ea 100755 --- a/src/main/java/forestry/farming/GuiHandlerFarming.java +++ b/src/main/java/forestry/farming/GuiHandlerFarming.java @@ -15,7 +15,7 @@ import net.minecraft.world.World; import forestry.core.GuiHandlerBase; import forestry.core.network.GuiId; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.network.PacketSocketUpdate; import forestry.core.proxy.Proxies; import forestry.farming.gadgets.TileFarmPlain; @@ -34,7 +34,7 @@ public class GuiHandlerFarming extends GuiHandlerBase { switch (GuiId.values()[id]) { case MultiFarmGUI: TileFarmPlain tile = (TileFarmPlain) getTileForestry(world, x, y, z); - Proxies.net.sendToPlayer(new PacketSocketUpdate(PacketIds.SOCKET_UPDATE, x, y, z, tile), player); + Proxies.net.sendToPlayer(new PacketSocketUpdate(PacketId.SOCKET_UPDATE, tile), player); return new ContainerFarm(player.inventory, tile); default: return null; diff --git a/src/main/java/forestry/farming/gadgets/TileFarm.java b/src/main/java/forestry/farming/gadgets/TileFarm.java index f35b4c0f3..a9de54fde 100755 --- a/src/main/java/forestry/farming/gadgets/TileFarm.java +++ b/src/main/java/forestry/farming/gadgets/TileFarm.java @@ -10,6 +10,10 @@ ******************************************************************************/ package forestry.farming.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -27,8 +31,6 @@ import forestry.api.farming.IFarmListener; import forestry.core.gadgets.TileForestry; import forestry.core.inventory.IInventoryAdapter; import forestry.core.network.GuiId; -import forestry.core.network.PacketPayload; -import forestry.core.proxy.Proxies; public abstract class TileFarm extends TileForestry implements IFarmComponent { @@ -129,28 +131,28 @@ public abstract class TileFarm extends TileForestry implements IFarmComponent { } /* TILEFORESTRY */ - @Override - public PacketPayload getPacketPayload() { - PacketPayload payload = new PacketPayload(0, 3); - payload.shortPayload[0] = (short) farmBlock.ordinal(); - payload.shortPayload[1] = (short) (isMaster() ? 1 : 0); + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeShort(farmBlock.ordinal()); + data.writeBoolean(isMaster()); // so the client can know if it is part of an integrated structure - payload.shortPayload[2] = (short) masterY; - - return payload; + data.writeShort(masterY); } @Override - public void fromPacketPayload(PacketPayload payload) { - EnumFarmBlock farmType = EnumFarmBlock.values()[payload.shortPayload[0]]; - if (payload.shortPayload[1] > 0 && !isMaster()) { + public void readData(DataInputStream data) throws IOException { + super.readData(data); + short farmBlockOrdinal = data.readShort(); + EnumFarmBlock farmType = EnumFarmBlock.values()[farmBlockOrdinal]; + if (data.readBoolean() && !isMaster()) { makeMaster(); } // so the client can know if it is part of an integrated structure - this.masterY = payload.shortPayload[2]; + this.masterY = data.readShort(); if (this.farmBlock != farmType) { this.farmBlock = farmType; diff --git a/src/main/java/forestry/farming/gadgets/TileFarmPlain.java b/src/main/java/forestry/farming/gadgets/TileFarmPlain.java index f18d01049..d6b2b6f04 100755 --- a/src/main/java/forestry/farming/gadgets/TileFarmPlain.java +++ b/src/main/java/forestry/farming/gadgets/TileFarmPlain.java @@ -62,7 +62,6 @@ import forestry.core.proxy.Proxies; import forestry.core.utils.DelayTimer; import forestry.core.utils.GuiUtil; import forestry.core.utils.StackUtils; -import forestry.core.utils.Utils; import forestry.core.vect.Vect; import forestry.core.vect.VectUtil; import forestry.farming.FarmHelper; @@ -776,7 +775,7 @@ public class TileFarmPlain extends TileFarm implements IFarmHousing, ISocketable @Override public void removeResources(ItemStack[] resources) { - EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwnerProfile()); + EntityPlayer player = Proxies.common.getPlayer(worldObj, getOwner()); InvTools.removeSets(getInternalInventory(), 1, resources, SLOT_RESOURCES_1, SLOT_RESOURCES_COUNT, player, false, true, true); } @@ -805,7 +804,7 @@ public class TileFarmPlain extends TileFarm implements IFarmHousing, ISocketable continue; } - EntityPlayer player = Proxies.common.getPlayer(world, getOwnerProfile()); + EntityPlayer player = Proxies.common.getPlayer(world, getOwner()); if (!germling.plantSaplingAt(player, inventory.getStackInSlot(i), world, x, y, z)) { continue; } @@ -992,12 +991,6 @@ public class TileFarmPlain extends TileFarm implements IFarmHousing, ISocketable return Config.hints.get("farm"); } - /* IOWNABLE */ - @Override - public boolean isOwnable() { - return true; - } - private static class FarmPlainInventoryAdapter extends TileInventoryAdapter { public FarmPlainInventoryAdapter(TileFarmPlain tile) { super(tile, TileFarmPlain.SLOT_COUNT, "Items"); diff --git a/src/main/java/forestry/mail/MailAddress.java b/src/main/java/forestry/mail/MailAddress.java index e45803617..e4873e3c3 100644 --- a/src/main/java/forestry/mail/MailAddress.java +++ b/src/main/java/forestry/mail/MailAddress.java @@ -121,7 +121,7 @@ public class MailAddress implements INBTTagable, IMailAddress { public String toString() { String name = getName().toLowerCase(Locale.ENGLISH); if (isPlayer()) { - return type + "-" + name + "-" + gameProfile.getId(); + return type + "-" + name + '-' + gameProfile.getId(); } else { return type + "-" + name; } diff --git a/src/main/java/forestry/mail/PacketHandlerMail.java b/src/main/java/forestry/mail/PacketHandlerMail.java index ea5140853..2712c5701 100755 --- a/src/main/java/forestry/mail/PacketHandlerMail.java +++ b/src/main/java/forestry/mail/PacketHandlerMail.java @@ -16,9 +16,9 @@ import java.io.IOException; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; -import forestry.core.interfaces.IPacketHandler; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketUpdate; +import forestry.core.network.IPacketHandler; +import forestry.core.network.PacketId; +import forestry.core.network.PacketString; import forestry.core.proxy.Proxies; import forestry.mail.gui.ContainerCatalogue; import forestry.mail.gui.ContainerLetter; @@ -26,48 +26,49 @@ import forestry.mail.gui.ContainerTradeName; import forestry.mail.gui.GuiMailboxInfo; import forestry.mail.network.PacketLetterInfo; import forestry.mail.network.PacketPOBoxInfo; +import forestry.mail.network.PacketRequestLetterInfo; public class PacketHandlerMail implements IPacketHandler { @Override - public boolean onPacketData(int packetID, DataInputStream data, EntityPlayer player) throws IOException { + public boolean onPacketData(PacketId packetID, DataInputStream data, EntityPlayer player) throws IOException { - PacketUpdate packet; switch (packetID) { - case PacketIds.LETTER_INFO: - PacketLetterInfo packetT = new PacketLetterInfo(); - packetT.readData(data); - onLetterInfo(packetT); + case LETTER_INFO: { + PacketLetterInfo packet = new PacketLetterInfo(data); + onLetterInfo(packet); return true; - case PacketIds.POBOX_INFO: - PacketPOBoxInfo packetP = new PacketPOBoxInfo(); - packetP.readData(data); - onPOBoxInfo(packetP); + } + case POBOX_INFO: { + PacketPOBoxInfo packet = new PacketPOBoxInfo(data); + onPOBoxInfo(packet); return true; - case PacketIds.LETTER_RECIPIENT: - packet = new PacketUpdate(); - packet.readData(data); - onLetterRecipient(player, packet); + } + case LETTER_REQUEST_INFO: { + PacketRequestLetterInfo packet = new PacketRequestLetterInfo(data); + onLetterRequestInfo(player, packet); return true; - case PacketIds.LETTER_TEXT: - packet = new PacketUpdate(); - packet.readData(data); + } + case LETTER_TEXT: { + PacketString packet = new PacketString(data); onLetterText(player, packet); return true; - case PacketIds.TRADING_ADDRESS_SET: - packet = new PacketUpdate(); - packet.readData(data); + } + case TRADING_ADDRESS_SET: { + PacketString packet = new PacketString(data); onAddressSet(player, packet); return true; - case PacketIds.POBOX_INFO_REQUEST: + } + case POBOX_INFO_REQUEST: { onPOBoxInfoRequest(player); return true; + } } return false; } - private void onLetterInfo(PacketLetterInfo packet) { + private static void onLetterInfo(PacketLetterInfo packet) { Container container = Proxies.common.getClientInstance().thePlayer.openContainer; if (container instanceof ContainerLetter) { @@ -78,11 +79,11 @@ public class PacketHandlerMail implements IPacketHandler { } - private void onPOBoxInfo(PacketPOBoxInfo packet) { + private static void onPOBoxInfo(PacketPOBoxInfo packet) { GuiMailboxInfo.instance.setPOBoxInfo(packet.poboxInfo); } - private void onAddressSet(EntityPlayer player, PacketUpdate packet) { + private static void onAddressSet(EntityPlayer player, PacketString packet) { if (!(player.openContainer instanceof ContainerTradeName)) { return; } @@ -90,7 +91,7 @@ public class PacketHandlerMail implements IPacketHandler { ((ContainerTradeName) player.openContainer).handleSetAddress(packet); } - private void onLetterText(EntityPlayer player, PacketUpdate packet) { + private static void onLetterText(EntityPlayer player, PacketString packet) { if (!(player.openContainer instanceof ContainerLetter)) { return; } @@ -98,22 +99,22 @@ public class PacketHandlerMail implements IPacketHandler { ((ContainerLetter) player.openContainer).handleSetText(packet); } - private void onLetterRecipient(EntityPlayer player, PacketUpdate packet) { + private static void onLetterRequestInfo(EntityPlayer player, PacketRequestLetterInfo packet) { if (!(player.openContainer instanceof ContainerLetter)) { return; } - ((ContainerLetter) player.openContainer).handleSetRecipient(player, packet); + ((ContainerLetter) player.openContainer).handleRequestLetterInfo(player, packet); } - private void onPOBoxInfoRequest(EntityPlayer player) { + private static void onPOBoxInfoRequest(EntityPlayer player) { MailAddress address = new MailAddress(player.getGameProfile()); POBox pobox = PostRegistry.getPOBox(player.worldObj, address); if (pobox == null) { return; } - Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketIds.POBOX_INFO, pobox.getPOBoxInfo()), player); + Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketId.POBOX_INFO, pobox.getPOBoxInfo()), player); } } diff --git a/src/main/java/forestry/mail/PostRegistry.java b/src/main/java/forestry/mail/PostRegistry.java index 6fc66c454..a0d77c01d 100755 --- a/src/main/java/forestry/mail/PostRegistry.java +++ b/src/main/java/forestry/mail/PostRegistry.java @@ -30,7 +30,7 @@ import forestry.api.mail.IPostalCarrier; import forestry.api.mail.ITradeStation; import forestry.api.mail.PostManager; import forestry.core.config.ForestryItem; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.mail.items.ItemLetter; import forestry.mail.network.PacketPOBoxInfo; @@ -75,7 +75,7 @@ public class PostRegistry implements IPostRegistry { EntityPlayer player = Proxies.common.getPlayer(world, address.getPlayerProfile()); if (player != null) { - Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketIds.POBOX_INFO, pobox.getPOBoxInfo()), player); + Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketId.POBOX_INFO, pobox.getPOBoxInfo()), player); } } diff --git a/src/main/java/forestry/mail/PostalCarrier.java b/src/main/java/forestry/mail/PostalCarrier.java index 19a3848e8..deb20e760 100755 --- a/src/main/java/forestry/mail/PostalCarrier.java +++ b/src/main/java/forestry/mail/PostalCarrier.java @@ -25,7 +25,7 @@ import forestry.api.mail.IPostalCarrier; import forestry.api.mail.IPostalState; import forestry.api.mail.ITradeStation; import forestry.api.mail.PostManager; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.core.render.TextureManager; import forestry.core.utils.StringUtil; @@ -87,7 +87,7 @@ public class PostalCarrier implements IPostalCarrier { } else { EntityPlayer player = Proxies.common.getPlayer(world, recipient.getPlayerProfile()); if (player != null) { - Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketIds.POBOX_INFO, pobox.getPOBoxInfo()), player); + Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketId.POBOX_INFO, pobox.getPOBoxInfo()), player); } } diff --git a/src/main/java/forestry/mail/TickHandlerMailClient.java b/src/main/java/forestry/mail/TickHandlerMailClient.java index 0eb5f5779..ea6c6c0b8 100755 --- a/src/main/java/forestry/mail/TickHandlerMailClient.java +++ b/src/main/java/forestry/mail/TickHandlerMailClient.java @@ -16,7 +16,7 @@ import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.common.gameevent.TickEvent.RenderTickEvent; import forestry.core.network.ForestryPacket; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.mail.gui.GuiMailboxInfo; @@ -34,7 +34,7 @@ public class TickHandlerMailClient { if (GuiMailboxInfo.instance.hasPOBoxInfo()) { GuiMailboxInfo.instance.render(0, 0); } else { - Proxies.net.sendToServer(new ForestryPacket(PacketIds.POBOX_INFO_REQUEST)); + Proxies.net.sendToServer(new ForestryPacket(PacketId.POBOX_INFO_REQUEST)); } } } diff --git a/src/main/java/forestry/mail/gadgets/MachineMailbox.java b/src/main/java/forestry/mail/gadgets/MachineMailbox.java index 1ca06e44d..ee0ace082 100755 --- a/src/main/java/forestry/mail/gadgets/MachineMailbox.java +++ b/src/main/java/forestry/mail/gadgets/MachineMailbox.java @@ -80,7 +80,7 @@ public class MachineMailbox extends TileBase implements IMailContainer { @Override public void updateServerSide() { if (!isLinked) { - getOrCreateMailInventory(worldObj, getOwnerProfile()); + getOrCreateMailInventory(worldObj, getOwner()); isLinked = true; } } @@ -112,7 +112,7 @@ public class MachineMailbox extends TileBase implements IMailContainer { @Override public boolean hasMail() { - IInventory mailInventory = getOrCreateMailInventory(worldObj, getOwnerProfile()); + IInventory mailInventory = getOrCreateMailInventory(worldObj, getOwner()); for (int i = 0; i < mailInventory.getSizeInventory(); i++) { if (mailInventory.getStackInSlot(i) != null) { return true; diff --git a/src/main/java/forestry/mail/gadgets/MachineTrader.java b/src/main/java/forestry/mail/gadgets/MachineTrader.java index 7bfeb8848..ad5d6e108 100755 --- a/src/main/java/forestry/mail/gadgets/MachineTrader.java +++ b/src/main/java/forestry/mail/gadgets/MachineTrader.java @@ -10,6 +10,9 @@ ******************************************************************************/ package forestry.mail.gadgets; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.Collection; import java.util.LinkedList; @@ -32,7 +35,6 @@ import forestry.core.EnumErrorCode; import forestry.core.gadgets.TileBase; import forestry.core.inventory.IInventoryAdapter; import forestry.core.network.GuiId; -import forestry.core.network.PacketPayload; import forestry.core.proxy.Proxies; import forestry.core.utils.StackUtils; import forestry.mail.MailAddress; @@ -87,26 +89,19 @@ public class MachineTrader extends TileBase { } } - @Override - public PacketPayload getPacketPayload() { - if (address == null || address.getName() == null) { - return null; - } + /* NETWORK */ - PacketPayload payload = new PacketPayload(0, 0, 1); - payload.stringPayload[0] = address.getName(); - return payload; + @Override + public void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeUTF(address.getName()); } @Override - public void fromPacketPayload(PacketPayload payload) { - if (payload.isEmpty()) { - address = null; - return; - } - - String addressName = payload.stringPayload[0]; - address = PostManager.postRegistry.getMailAddress(addressName); + public void readData(DataInputStream data) throws IOException { + super.readData(data); + String address = data.readUTF(); + this.address = PostManager.postRegistry.getMailAddress(address); } /* UPDATING */ @@ -263,7 +258,7 @@ public class MachineTrader extends TileBase { if (hasValidTradeAddress & hasUniqueTradeAddress) { this.address = address; - PostManager.postRegistry.getOrCreateTradeStation(worldObj, getOwnerProfile(), address); + PostManager.postRegistry.getOrCreateTradeStation(worldObj, getOwner(), address); } } } @@ -275,7 +270,7 @@ public class MachineTrader extends TileBase { return super.getInternalInventory(); } - return (TradeStation) PostManager.postRegistry.getOrCreateTradeStation(worldObj, getOwnerProfile(), address); + return (TradeStation) PostManager.postRegistry.getOrCreateTradeStation(worldObj, getOwner(), address); } /* ITRIGGERPROVIDER */ diff --git a/src/main/java/forestry/mail/gui/ContainerCatalogue.java b/src/main/java/forestry/mail/gui/ContainerCatalogue.java index e6e4418e8..a0c4be0ac 100755 --- a/src/main/java/forestry/mail/gui/ContainerCatalogue.java +++ b/src/main/java/forestry/mail/gui/ContainerCatalogue.java @@ -29,9 +29,8 @@ import forestry.api.mail.ITradeStation; import forestry.api.mail.PostManager; import forestry.api.mail.TradeStationInfo; import forestry.core.gui.IGuiSelectable; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketGuiSelect; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.mail.EnumStationState; import forestry.mail.network.PacketLetterInfo; @@ -132,9 +131,7 @@ public class ContainerCatalogue extends Container implements IGuiSelectable { public void cycleFilter() { if (!Proxies.common.isSimulating(player.worldObj)) { - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = 2; - PacketUpdate packet = new PacketUpdate(PacketIds.GUI_SELECTION_CHANGE, payload); + PacketGuiSelect packet = new PacketGuiSelect(PacketId.GUI_SELECTION_CHANGE, 2, 0); Proxies.net.sendToServer(packet); return; } @@ -144,10 +141,9 @@ public class ContainerCatalogue extends Container implements IGuiSelectable { rebuildStationsList(); } - private void sendSelection(boolean advance) { - PacketPayload payload = new PacketPayload(1, 0, 0); - payload.intPayload[0] = advance ? 0 : 1; - PacketUpdate packet = new PacketUpdate(PacketIds.GUI_SELECTION_CHANGE, payload); + private static void sendSelection(boolean advance) { + int value = advance ? 0 : 1; + PacketGuiSelect packet = new PacketGuiSelect(PacketId.GUI_SELECTION_CHANGE, value, 0); Proxies.net.sendToServer(packet); } @@ -191,7 +187,7 @@ public class ContainerCatalogue extends Container implements IGuiSelectable { crafter.sendProgressBarUpdate(this, 2, currentFilter); } - Proxies.net.sendToPlayer(new PacketLetterInfo(PacketIds.LETTER_INFO, EnumAddressee.TRADER, currentTrade, null), player); + Proxies.net.sendToPlayer(new PacketLetterInfo(PacketId.LETTER_INFO, EnumAddressee.TRADER, currentTrade, null), player); needsSynch = false; } } @@ -217,21 +213,24 @@ public class ContainerCatalogue extends Container implements IGuiSelectable { } @Override - public void handleSelectionChange(EntityPlayer player, PacketUpdate packet) { + public void handleSelectionChange(EntityPlayer player, PacketGuiSelect packet) { - if (packet.payload.intPayload[0] == 0) { - nextPage(); - } else if (packet.payload.intPayload[0] == 1) { - previousPage(); - } else if (packet.payload.intPayload[0] == 2) { - cycleFilter(); + switch (packet.getPrimaryIndex()) { + case 0: + nextPage(); + break; + case 1: + previousPage(); + break; + case 2: + cycleFilter(); + break; } needsSynch = true; } @Override - public void setSelection(PacketUpdate packet) { + public void setSelection(PacketGuiSelect packet) { } - } diff --git a/src/main/java/forestry/mail/gui/ContainerLetter.java b/src/main/java/forestry/mail/gui/ContainerLetter.java index b09624935..412def11c 100755 --- a/src/main/java/forestry/mail/gui/ContainerLetter.java +++ b/src/main/java/forestry/mail/gui/ContainerLetter.java @@ -13,8 +13,6 @@ package forestry.mail.gui; import java.util.Iterator; import java.util.UUID; -import org.apache.commons.lang3.StringUtils; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.server.MinecraftServer; import net.minecraft.world.World; @@ -30,13 +28,13 @@ import forestry.api.mail.PostManager; import forestry.api.mail.TradeStationInfo; import forestry.core.gui.ContainerItemInventory; import forestry.core.gui.slots.SlotFiltered; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketId; +import forestry.core.network.PacketString; import forestry.core.proxy.Proxies; import forestry.mail.Letter; import forestry.mail.items.ItemLetter.LetterInventory; import forestry.mail.network.PacketLetterInfo; +import forestry.mail.network.PacketRequestLetterInfo; public class ContainerLetter extends ContainerItemInventory { @@ -123,37 +121,11 @@ public class ContainerLetter extends ContainerItemInventory { setCarrierType(postal.getType()); } - public static void setRecipient(String recipientName, EnumAddressee type) { - if (StringUtils.isBlank(recipientName) || type == null) { - return; - } + public void handleRequestLetterInfo(EntityPlayer player, PacketRequestLetterInfo packet) { + String recipientName = packet.getRecipientName(); + EnumAddressee type = packet.getAddressType(); - // / Send to server - PacketPayload payload = new PacketPayload(0, 0, 2); - payload.stringPayload[0] = recipientName; - payload.stringPayload[1] = type.toString(); - - PacketUpdate packet = new PacketUpdate(PacketIds.LETTER_RECIPIENT, payload); - Proxies.net.sendToServer(packet); - } - - public void handleSetRecipient(EntityPlayer player, PacketUpdate packet) { - String recipientName = packet.payload.stringPayload[0]; - String typeName = packet.payload.stringPayload[1]; - - EnumAddressee type = EnumAddressee.fromString(typeName); - IMailAddress recipient; - if (type == EnumAddressee.PLAYER) { - GameProfile gameProfile = MinecraftServer.getServer().func_152358_ax().func_152655_a(recipientName); - if (gameProfile == null) { - gameProfile = new GameProfile(new UUID(0, 0), recipientName); - } - recipient = PostManager.postRegistry.getMailAddress(gameProfile); - } else if (type == EnumAddressee.TRADER) { - recipient = PostManager.postRegistry.getMailAddress(recipientName); - } else { - return; - } + IMailAddress recipient = getRecipient(recipientName, type); getLetter().setRecipient(recipient); @@ -163,7 +135,24 @@ public class ContainerLetter extends ContainerItemInventory { } // Update info on client - Proxies.net.sendToPlayer(new PacketLetterInfo(PacketIds.LETTER_INFO, type, tradeInfo, recipient), player); + Proxies.net.sendToPlayer(new PacketLetterInfo(PacketId.LETTER_INFO, type, tradeInfo, recipient), player); + } + + public static IMailAddress getRecipient(String recipientName, EnumAddressee type) { + switch (type) { + case PLAYER: { + GameProfile gameProfile = MinecraftServer.getServer().func_152358_ax().func_152655_a(recipientName); + if (gameProfile == null) { + gameProfile = new GameProfile(new UUID(0, 0), recipientName); + } + return PostManager.postRegistry.getMailAddress(gameProfile); + } + case TRADER: { + return PostManager.postRegistry.getMailAddress(recipientName); + } + default: + return null; + } } public IMailAddress getRecipient() { @@ -181,17 +170,13 @@ public class ContainerLetter extends ContainerItemInventory { public void setText(String text) { getLetter().setText(text); - // / Send to server - PacketPayload payload = new PacketPayload(0, 0, 1); - payload.stringPayload[0] = text; - - PacketUpdate packet = new PacketUpdate(PacketIds.LETTER_TEXT, payload); + PacketString packet = new PacketString(PacketId.LETTER_TEXT, text); Proxies.net.sendToServer(packet); } - public void handleSetText(PacketUpdate packet) { - String text = packet.payload.stringPayload[0]; + public void handleSetText(PacketString packet) { + String text = packet.getString(); getLetter().setText(text); } diff --git a/src/main/java/forestry/mail/gui/ContainerMailbox.java b/src/main/java/forestry/mail/gui/ContainerMailbox.java index 397b971c6..a954b7988 100755 --- a/src/main/java/forestry/mail/gui/ContainerMailbox.java +++ b/src/main/java/forestry/mail/gui/ContainerMailbox.java @@ -17,7 +17,7 @@ import net.minecraft.item.ItemStack; import forestry.core.gui.ContainerTile; import forestry.core.gui.slots.SlotOutput; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.proxy.Proxies; import forestry.core.utils.GuiUtil; import forestry.mail.POBox; @@ -56,7 +56,7 @@ public class ContainerMailbox extends ContainerTile { if (GuiUtil.isIndexInRange(slotIndex, SLOT_LETTERS, SLOT_LETTERS_COUNT)) { if (Proxies.common.isSimulating(player.worldObj) && mailInventory != null) { POBoxInfo info = mailInventory.getPOBoxInfo(); - Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketIds.POBOX_INFO, info), player); + Proxies.net.sendToPlayer(new PacketPOBoxInfo(PacketId.POBOX_INFO, info), player); } } diff --git a/src/main/java/forestry/mail/gui/ContainerTradeName.java b/src/main/java/forestry/mail/gui/ContainerTradeName.java index d3d16efe0..6d2348de4 100755 --- a/src/main/java/forestry/mail/gui/ContainerTradeName.java +++ b/src/main/java/forestry/mail/gui/ContainerTradeName.java @@ -18,9 +18,8 @@ import net.minecraft.inventory.Container; import forestry.api.mail.IMailAddress; import forestry.api.mail.PostManager; -import forestry.core.network.PacketIds; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; +import forestry.core.network.PacketId; +import forestry.core.network.PacketString; import forestry.core.proxy.Proxies; import forestry.mail.gadgets.MachineTrader; @@ -42,18 +41,15 @@ public class ContainerTradeName extends Container { return; } - PacketPayload payload = new PacketPayload(0, 0, 1); - payload.stringPayload[0] = addressName; - - PacketUpdate packet = new PacketUpdate(PacketIds.TRADING_ADDRESS_SET, payload); + PacketString packet = new PacketString(PacketId.TRADING_ADDRESS_SET, addressName); Proxies.net.sendToServer(packet); IMailAddress address = PostManager.postRegistry.getMailAddress(addressName); machine.setAddress(address); } - public void handleSetAddress(PacketUpdate packet) { - String addressName = packet.payload.stringPayload[0]; + public void handleSetAddress(PacketString packet) { + String addressName = packet.getString(); IMailAddress address = PostManager.postRegistry.getMailAddress(addressName); machine.setAddress(address); } diff --git a/src/main/java/forestry/mail/gui/GuiLetter.java b/src/main/java/forestry/mail/gui/GuiLetter.java index 9e3951dea..8d43588fb 100755 --- a/src/main/java/forestry/mail/gui/GuiLetter.java +++ b/src/main/java/forestry/mail/gui/GuiLetter.java @@ -32,6 +32,7 @@ import forestry.core.gui.widgets.Widget; import forestry.core.proxy.Proxies; import forestry.core.utils.StringUtil; import forestry.mail.items.ItemLetter.LetterInventory; +import forestry.mail.network.PacketRequestLetterInfo; public class GuiLetter extends GuiForestry { @@ -271,7 +272,8 @@ public class GuiLetter extends GuiForestry { return; } - container.setRecipient(recipientName, type); + PacketRequestLetterInfo packet = new PacketRequestLetterInfo(recipientName, type); + Proxies.net.sendToServer(packet); } private void setText() { diff --git a/src/main/java/forestry/mail/network/PacketLetterInfo.java b/src/main/java/forestry/mail/network/PacketLetterInfo.java index 8ce0d18c4..908892ba4 100644 --- a/src/main/java/forestry/mail/network/PacketLetterInfo.java +++ b/src/main/java/forestry/mail/network/PacketLetterInfo.java @@ -24,6 +24,8 @@ import forestry.api.mail.IMailAddress; import forestry.api.mail.PostManager; import forestry.api.mail.TradeStationInfo; import forestry.core.network.ForestryPacket; +import forestry.core.network.PacketHelper; +import forestry.core.network.PacketId; import forestry.mail.EnumStationState; public class PacketLetterInfo extends ForestryPacket { @@ -32,10 +34,11 @@ public class PacketLetterInfo extends ForestryPacket { public TradeStationInfo tradeInfo; public IMailAddress address; - public PacketLetterInfo() { + public PacketLetterInfo(DataInputStream data) throws IOException { + super(data); } - public PacketLetterInfo(int id, EnumAddressee type, TradeStationInfo info, IMailAddress address) { + public PacketLetterInfo(PacketId id, EnumAddressee type, TradeStationInfo info, IMailAddress address) { super(id); this.type = type; if (type == EnumAddressee.TRADER) { @@ -85,11 +88,9 @@ public class PacketLetterInfo extends ForestryPacket { data.writeLong(tradeInfo.owner.getId().getLeastSignificantBits()); data.writeUTF(tradeInfo.owner.getName()); - writeItemStack(tradeInfo.tradegood, data); - data.writeShort(tradeInfo.required.length); - for (int i = 0; i < tradeInfo.required.length; i++) { - writeItemStack(tradeInfo.required[i], data); - } + PacketHelper.writeItemStack(tradeInfo.tradegood, data); + PacketHelper.writeItemStacks(tradeInfo.required, data); + data.writeShort(tradeInfo.state.ordinal()); } } @@ -119,11 +120,8 @@ public class PacketLetterInfo extends ForestryPacket { ItemStack tradegood; ItemStack[] required; - tradegood = readItemStack(data); - required = new ItemStack[data.readShort()]; - for (int i = 0; i < required.length; i++) { - required[i] = readItemStack(data); - } + tradegood = PacketHelper.readItemStack(data); + required = PacketHelper.readItemStacks(data); this.tradeInfo = new TradeStationInfo(address, owner, tradegood, required, EnumStationState.values()[data.readShort()]); } diff --git a/src/main/java/forestry/mail/network/PacketPOBoxInfo.java b/src/main/java/forestry/mail/network/PacketPOBoxInfo.java index b85a78e97..36d5409b9 100755 --- a/src/main/java/forestry/mail/network/PacketPOBoxInfo.java +++ b/src/main/java/forestry/mail/network/PacketPOBoxInfo.java @@ -15,22 +15,24 @@ import java.io.DataOutputStream; import java.io.IOException; import forestry.core.network.ForestryPacket; +import forestry.core.network.PacketId; import forestry.mail.POBoxInfo; public class PacketPOBoxInfo extends ForestryPacket { public POBoxInfo poboxInfo; - public PacketPOBoxInfo() { + public PacketPOBoxInfo(DataInputStream data) throws IOException { + super(data); } - public PacketPOBoxInfo(int id, POBoxInfo info) { + public PacketPOBoxInfo(PacketId id, POBoxInfo info) { super(id); this.poboxInfo = info; } @Override - public void writeData(DataOutputStream data) throws IOException { + protected void writeData(DataOutputStream data) throws IOException { if (poboxInfo == null) { data.writeShort(-1); return; @@ -42,7 +44,7 @@ public class PacketPOBoxInfo extends ForestryPacket { } @Override - public void readData(DataInputStream data) throws IOException { + protected void readData(DataInputStream data) throws IOException { short isNotNull = data.readShort(); if (isNotNull < 0) { diff --git a/src/main/java/forestry/mail/network/PacketRequestLetterInfo.java b/src/main/java/forestry/mail/network/PacketRequestLetterInfo.java new file mode 100644 index 000000000..2523ef239 --- /dev/null +++ b/src/main/java/forestry/mail/network/PacketRequestLetterInfo.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.mail.network; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import forestry.api.mail.EnumAddressee; +import forestry.core.network.ForestryPacket; +import forestry.core.network.PacketId; + +public class PacketRequestLetterInfo extends ForestryPacket { + + private String recipientName; + private short addressType; + + public PacketRequestLetterInfo(DataInputStream data) throws IOException { + super(data); + } + + public PacketRequestLetterInfo(String recipientName, EnumAddressee addressType) { + super(PacketId.LETTER_REQUEST_INFO); + this.recipientName = recipientName; + this.addressType = (short) addressType.ordinal(); + } + + @Override + protected void writeData(DataOutputStream data) throws IOException { + super.writeData(data); + data.writeUTF(recipientName); + data.writeShort(addressType); + } + + @Override + protected void readData(DataInputStream data) throws IOException { + super.readData(data); + recipientName = data.readUTF(); + addressType = data.readShort(); + } + + public String getRecipientName() { + return recipientName; + } + + public EnumAddressee getAddressType() { + return EnumAddressee.values()[addressType]; + } +} diff --git a/src/main/java/forestry/pipes/PipeItemsPropolis.java b/src/main/java/forestry/pipes/PipeItemsPropolis.java index 3767284d7..9e4d5c6cd 100755 --- a/src/main/java/forestry/pipes/PipeItemsPropolis.java +++ b/src/main/java/forestry/pipes/PipeItemsPropolis.java @@ -140,7 +140,7 @@ public class PipeItemsPropolis extends Pipe { // Bees need to match one of the genome filters for (IAllele[] pattern : filters) { - if (pipeLogic.matchAllele(pattern[0], bee.getIdent()) && pipeLogic.matchAllele(pattern[1], bee.getGenome().getSecondary().getUID())) { + if (matchAllele(pattern[0], bee.getIdent()) && matchAllele(pattern[1], bee.getGenome().getSecondary().getUID())) { filteredOrientations.add(dir); } } @@ -157,6 +157,14 @@ public class PipeItemsPropolis extends Pipe { } } + private static boolean matchAllele(IAllele filter, String ident) { + if (filter == null) { + return true; + } else { + return filter.getUID().equals(ident); + } + } + public void eventHandler(PipeEventItem.Entered event) { // A bit of speed to perhaps prevent bees from popping out of the pipe. try { diff --git a/src/main/java/forestry/pipes/PipeLogicPropolis.java b/src/main/java/forestry/pipes/PipeLogicPropolis.java index 39efb2186..bef850a76 100755 --- a/src/main/java/forestry/pipes/PipeLogicPropolis.java +++ b/src/main/java/forestry/pipes/PipeLogicPropolis.java @@ -20,24 +20,23 @@ import forestry.api.genetics.AlleleManager; import forestry.api.genetics.IAllele; import forestry.api.genetics.IAlleleSpecies; import forestry.core.network.PacketCoordinates; -import forestry.core.network.PacketIds; +import forestry.core.network.PacketId; import forestry.core.network.PacketNBT; -import forestry.core.network.PacketPayload; -import forestry.core.network.PacketUpdate; import forestry.core.proxy.Proxies; +import forestry.pipes.network.PacketGenomeFilterChange; +import forestry.pipes.network.PacketTypeFilterChange; import buildcraft.transport.Pipe; +import buildcraft.transport.PipeTransportItems; public class PipeLogicPropolis { - @SuppressWarnings("rawtypes") - private final Pipe pipe; + private final Pipe pipe; private final EnumFilterType[] typeFilter = new EnumFilterType[6]; private final IAllele[][][] genomeFilter = new IAllele[6][3][2]; - @SuppressWarnings("rawtypes") - public PipeLogicPropolis(Pipe pipe) { + public PipeLogicPropolis(Pipe pipe) { this.pipe = pipe; for (int i = 0; i < typeFilter.length; i++) { typeFilter[i] = EnumFilterType.CLOSED; @@ -51,11 +50,11 @@ public class PipeLogicPropolis { for (int i = 0; i < 6; i++) { for (int j = 0; j < 3; j++) { - if (nbttagcompound.hasKey("GenomeFilterS" + i + "-" + j + "-" + 0)) { - genomeFilter[i][j][0] = AlleleManager.alleleRegistry.getAllele(nbttagcompound.getString("GenomeFilterS" + i + "-" + j + "-" + 0)); + if (nbttagcompound.hasKey("GenomeFilterS" + i + '-' + j + '-' + 0)) { + genomeFilter[i][j][0] = AlleleManager.alleleRegistry.getAllele(nbttagcompound.getString("GenomeFilterS" + i + '-' + j + '-' + 0)); } - if (nbttagcompound.hasKey("GenomeFilterS" + i + "-" + j + "-" + 1)) { - genomeFilter[i][j][1] = AlleleManager.alleleRegistry.getAllele(nbttagcompound.getString("GenomeFilterS" + i + "-" + j + "-" + 1)); + if (nbttagcompound.hasKey("GenomeFilterS" + i + '-' + j + '-' + 1)) { + genomeFilter[i][j][1] = AlleleManager.alleleRegistry.getAllele(nbttagcompound.getString("GenomeFilterS" + i + '-' + j + '-' + 1)); } } } @@ -69,10 +68,10 @@ public class PipeLogicPropolis { for (int i = 0; i < 6; i++) { for (int j = 0; j < 3; j++) { if (genomeFilter[i][j][0] != null) { - nbttagcompound.setString("GenomeFilterS" + i + "-" + j + "-" + 0, genomeFilter[i][j][0].getUID()); + nbttagcompound.setString("GenomeFilterS" + i + '-' + j + '-' + 0, genomeFilter[i][j][0].getUID()); } if (genomeFilter[i][j][1] != null) { - nbttagcompound.setString("GenomeFilterS" + i + "-" + j + "-" + 1, genomeFilter[i][j][1].getUID()); + nbttagcompound.setString("GenomeFilterS" + i + '-' + j + '-' + 1, genomeFilter[i][j][1].getUID()); } } } @@ -89,7 +88,7 @@ public class PipeLogicPropolis { public boolean matchType(ForgeDirection orientation, EnumFilterType type, IBee bee) { EnumFilterType filter = typeFilter[orientation.ordinal()]; if (filter == EnumFilterType.BEE) { - return type != EnumFilterType.ITEM && type != EnumFilterType.CLOSED && type != EnumFilterType.CLOSED; + return type != EnumFilterType.ITEM && type != EnumFilterType.CLOSED; } // Special bee filtering @@ -124,14 +123,6 @@ public class PipeLogicPropolis { return filter == type; } - public boolean matchAllele(IAllele filter, String ident) { - if (filter == null) { - return true; - } else { - return filter.getUID().equals(ident); - } - } - public ArrayList getGenomeFilters(ForgeDirection orientation) { ArrayList filters = new ArrayList(); @@ -184,53 +175,43 @@ public class PipeLogicPropolis { } } - // Server side + /* NETWORK */ public void sendFilterSet(EntityPlayer player) { NBTTagCompound nbttagcompound = new NBTTagCompound(); this.writeToNBT(nbttagcompound); - Proxies.net.sendToPlayer(new PacketNBT(PacketIds.PROP_SEND_FILTER_SET, nbttagcompound), player); - } - - public void handleTypeFilterChange(PacketPayload payload) { - typeFilter[payload.intPayload[0]] = EnumFilterType.values()[payload.intPayload[1]]; - } - - public void handleGenomeFilterChange(PacketPayload payload) { - if (!payload.stringPayload[0].equals("NULL")) { - genomeFilter[payload.intPayload[0]][payload.intPayload[1]][payload.intPayload[2]] = AlleleManager.alleleRegistry - .getAllele(payload.stringPayload[0]); - } else { - genomeFilter[payload.intPayload[0]][payload.intPayload[1]][payload.intPayload[2]] = null; - } - } - - // Client side - public void requestFilterSet() { - Proxies.net.sendToServer(new PacketCoordinates(PacketIds.PROP_REQUEST_FILTER_SET, pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord)); - } - - public void sendTypeFilterChange(ForgeDirection orientation, EnumFilterType filter) { - PacketPayload payload = new PacketPayload(2, 0, 0); - payload.intPayload[0] = orientation.ordinal(); - payload.intPayload[1] = filter.ordinal(); - Proxies.net.sendToServer(new PacketUpdate(PacketIds.PROP_SEND_FILTER_CHANGE_TYPE, pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord, payload)); - } - - public void sendGenomeFilterChange(ForgeDirection orientation, int pattern, int allele, IAllele species) { - PacketPayload payload = new PacketPayload(3, 0, 1); - payload.intPayload[0] = orientation.ordinal(); - payload.intPayload[1] = pattern; - payload.intPayload[2] = allele; - if (species != null) { - payload.stringPayload[0] = species.getUID(); - } else { - payload.stringPayload[0] = "NULL"; - } - - Proxies.net.sendToServer(new PacketUpdate(PacketIds.PROP_SEND_FILTER_CHANGE_GENOME, pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord, payload)); + Proxies.net.sendToPlayer(new PacketNBT(PacketId.PROP_SEND_FILTER_SET, nbttagcompound), player); } public void handleFilterSet(PacketNBT packet) { this.readFromNBT(packet.getTagCompound()); } + + public void requestFilterSet() { + Proxies.net.sendToServer(new PacketCoordinates(PacketId.PROP_REQUEST_FILTER_SET, pipe.container)); + } + + public void sendTypeFilterChange(ForgeDirection orientation, EnumFilterType filter) { + PacketTypeFilterChange packet = new PacketTypeFilterChange(pipe.container, orientation, filter); + Proxies.net.sendToServer(packet); + } + + public void handleTypeFilterChange(PacketTypeFilterChange packet) { + int orientation = packet.getOrientation(); + int filterOrdinal = packet.getFilter(); + typeFilter[orientation] = EnumFilterType.values()[filterOrdinal]; + } + + public void sendGenomeFilterChange(ForgeDirection orientation, int pattern, int allele, IAllele species) { + PacketGenomeFilterChange packet = new PacketGenomeFilterChange(pipe.container, orientation, pattern, allele, species); + Proxies.net.sendToServer(packet); + } + + public void handleGenomeFilterChange(PacketGenomeFilterChange packet) { + IAllele species = packet.getSpecies(); + int orientation = packet.getOrientation(); + int pattern = packet.getPattern(); + int allele = packet.getAllele(); + + genomeFilter[orientation][pattern][allele] = species; + } } diff --git a/src/main/java/forestry/pipes/gui/GuiPropolisPipe.java b/src/main/java/forestry/pipes/gui/GuiPropolisPipe.java index 9d7095733..68eb4a6cc 100755 --- a/src/main/java/forestry/pipes/gui/GuiPropolisPipe.java +++ b/src/main/java/forestry/pipes/gui/GuiPropolisPipe.java @@ -235,9 +235,11 @@ public class GuiPropolisPipe extends GuiForestry implements IGuiSelectable { @@ -32,11 +32,11 @@ public class ContainerNaturalistBackpack extends ContainerItemInventory