diff --git a/src/main/java/techreborn/api/recipe/RecipeCrafter.java b/src/main/java/techreborn/api/recipe/RecipeCrafter.java index f468d14e3..cf3fe4eba 100644 --- a/src/main/java/techreborn/api/recipe/RecipeCrafter.java +++ b/src/main/java/techreborn/api/recipe/RecipeCrafter.java @@ -181,7 +181,7 @@ public class RecipeCrafter { } public void useAllInputs() { - if (currentRecipe == null) { + if (currentRecipe == null || !parentTile.getWorldObj().isRemote) { return; } for (ItemStack input : currentRecipe.getInputs()) { diff --git a/src/main/java/techreborn/packets/PacketHandler.java b/src/main/java/techreborn/packets/PacketHandler.java index bf4ba4cce..353d61c09 100644 --- a/src/main/java/techreborn/packets/PacketHandler.java +++ b/src/main/java/techreborn/packets/PacketHandler.java @@ -20,9 +20,8 @@ public class PacketHandler extends FMLIndexedMessageToMessageCodec { private static EnumMap channels; - public PacketHandler() - { - + public PacketHandler() { + addDiscriminator(0, PacketSync.class); } public static EnumMap getChannels() diff --git a/src/main/java/techreborn/packets/PacketSync.java b/src/main/java/techreborn/packets/PacketSync.java new file mode 100644 index 000000000..2a61260eb --- /dev/null +++ b/src/main/java/techreborn/packets/PacketSync.java @@ -0,0 +1,69 @@ +package techreborn.packets; + +import io.netty.buffer.ByteBuf; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTSizeTracker; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import techreborn.tiles.TileMachineBase; +import techreborn.util.ClientUtil; +import techreborn.util.LogHelper; + +import java.io.IOException; + + +public class PacketSync extends SimplePacket { + + private NBTTagCompound nbttagcompound; + private int x, y, z; + + public PacketSync() { + } + + public PacketSync(NBTTagCompound nbttagcompound, int x, int y, int z) { + this.nbttagcompound = nbttagcompound; + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public void writeData(ByteBuf data) throws IOException{ + + try { + byte[] compressed = CompressedStreamTools.compress(nbttagcompound); + if (compressed.length > 65535) { + LogHelper.error("NBT data is too large (" + compressed.length + " > 65535)! Please report!"); + } + data.writeShort(compressed.length); + data.writeBytes(compressed); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void readData(ByteBuf data) throws IOException{ + int length = data.readUnsignedShort(); + byte[] compressed = new byte[length]; + data.readBytes(compressed); + + try { + this.nbttagcompound = CompressedStreamTools.func_152457_a(compressed, NBTSizeTracker.field_152451_a); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public NBTTagCompound getTagCompound() { + return this.nbttagcompound; + } + + @Override + public void execute() { + TileEntity tile = ClientUtil.getWorld().getTileEntity(x, y, z); + if(tile instanceof TileMachineBase){ + ((TileMachineBase) tile).readSyncFromNBT(getTagCompound()); + } + } +} diff --git a/src/main/java/techreborn/tiles/TileAlloyFurnace.java b/src/main/java/techreborn/tiles/TileAlloyFurnace.java index 3dcdd81e7..4017586b8 100644 --- a/src/main/java/techreborn/tiles/TileAlloyFurnace.java +++ b/src/main/java/techreborn/tiles/TileAlloyFurnace.java @@ -82,5 +82,4 @@ public class TileAlloyFurnace extends TileMachineBase implements IWrenchable { inventory.writeToNBT(tagCompound); } - } diff --git a/src/main/java/techreborn/tiles/TileAlloySmelter.java b/src/main/java/techreborn/tiles/TileAlloySmelter.java index cfd71ff9d..ac24df43c 100644 --- a/src/main/java/techreborn/tiles/TileAlloySmelter.java +++ b/src/main/java/techreborn/tiles/TileAlloySmelter.java @@ -91,6 +91,20 @@ public class TileAlloySmelter extends TileMachineBase implements IWrenchable, IE crafter.writeToNBT(tagCompound); } + @Override + public void readSyncFromNBT(NBTTagCompound tagCompound) { + super.readSyncFromNBT(tagCompound); + energy.readFromNBT(tagCompound); + crafter.readFromNBT(tagCompound); + } + + @Override + public void writeSyncToNBT(NBTTagCompound tagCompound) { + super.writeSyncToNBT(tagCompound); + energy.writeToNBT(tagCompound); + crafter.writeToNBT(tagCompound); + } + @Override public void invalidate(){ energy.invalidate(); diff --git a/src/main/java/techreborn/tiles/TileMachineBase.java b/src/main/java/techreborn/tiles/TileMachineBase.java index f47c508b1..2296231ef 100644 --- a/src/main/java/techreborn/tiles/TileMachineBase.java +++ b/src/main/java/techreborn/tiles/TileMachineBase.java @@ -8,6 +8,8 @@ import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import techreborn.packets.PacketHandler; +import techreborn.packets.PacketSync; +import techreborn.packets.SimplePacket; import java.util.List; @@ -33,6 +35,7 @@ public class TileMachineBase extends TileEntity { public void syncWithAll() { if (!worldObj.isRemote) { + //PacketHandler.sendPacketToAllPlayers(getSyncPacket()); PacketHandler.sendPacketToAllPlayers(getDescriptionPacket(), worldObj); } else { @@ -42,6 +45,19 @@ public class TileMachineBase extends TileEntity { ticksSinceLastSync = 0; } + public Packet getDescriptionPacket2() { + NBTTagCompound nbtTag = new NBTTagCompound(); + writeSyncToNBT(nbtTag); + return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, + this.zCoord, 1, nbtTag); + } + + public SimplePacket getSyncPacket() { + NBTTagCompound nbtTag = new NBTTagCompound(); + writeSyncToNBT(nbtTag); + return new PacketSync(nbtTag, xCoord, yCoord, zCoord); + } + public Packet getDescriptionPacket() { NBTTagCompound nbtTag = new NBTTagCompound(); writeToNBT(nbtTag); @@ -56,5 +72,13 @@ public class TileMachineBase extends TileEntity { readFromNBT(packet.func_148857_g()); } + public void readSyncFromNBT(NBTTagCompound tagCompound) { + + } + + public void writeSyncToNBT(NBTTagCompound tagCompound) { + + } + } diff --git a/src/main/java/techreborn/util/ClientUtil.java b/src/main/java/techreborn/util/ClientUtil.java new file mode 100644 index 000000000..97604368c --- /dev/null +++ b/src/main/java/techreborn/util/ClientUtil.java @@ -0,0 +1,15 @@ +package techreborn.util; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.Minecraft; +import net.minecraft.world.World; + +public class ClientUtil { + + @SideOnly(Side.CLIENT) + public static World getWorld(){ + return Minecraft.getMinecraft().theWorld; + } + +}