diff --git a/src/main/java/crazypants/enderio/EnderIO.java b/src/main/java/crazypants/enderio/EnderIO.java index c5c0b5c8..e6f92e10 100644 --- a/src/main/java/crazypants/enderio/EnderIO.java +++ b/src/main/java/crazypants/enderio/EnderIO.java @@ -61,7 +61,7 @@ import crazypants.enderio.material.ItemMaterial; import crazypants.enderio.material.ItemPowderIngot; import crazypants.enderio.material.MaterialRecipes; import crazypants.enderio.network.PacketPipeline; -import crazypants.enderio.network.PacketTileEntity; +import crazypants.enderio.network.PacketTileEntityNbt; import crazypants.enderio.teleport.BlockTravelAnchor; import crazypants.enderio.teleport.ItemTravelStaff; import crazypants.enderio.teleport.TeleportRecipes; @@ -202,7 +202,7 @@ public class EnderIO { instance = this; packetPipeline.initalise(); - packetPipeline.registerPacket(PacketTileEntity.class); + packetPipeline.registerPacket(PacketTileEntityNbt.class); packetPipeline.registerPacket(PacketRedstoneMode.class); NetworkRegistry.INSTANCE.registerGuiHandler(this, guiHandler); diff --git a/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java b/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java index ca264245..7c1d7709 100644 --- a/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java +++ b/src/main/java/crazypants/enderio/conduit/BlockConduitBundle.java @@ -34,6 +34,7 @@ import crazypants.enderio.conduit.geom.CollidableComponent; import crazypants.enderio.conduit.geom.ConduitConnectorType; import crazypants.enderio.conduit.gui.ExternalConnectionContainer; import crazypants.enderio.conduit.gui.GuiExternalConnection; +import crazypants.enderio.conduit.liquid.PacketFluidLevel; import crazypants.enderio.conduit.redstone.IRedstoneConduit; import crazypants.enderio.enderface.BlockEio; import crazypants.enderio.machine.painter.PainterUtil; @@ -51,6 +52,8 @@ public class BlockConduitBundle extends BlockEio implements IGuiHandler { MinecraftForge.EVENT_BUS.register(ConduitNetworkTickHandler.instance); FMLCommonHandler.instance().bus().register(ConduitNetworkTickHandler.instance); + EnderIO.packetPipeline.registerPacket(PacketFluidLevel.class); + BlockConduitBundle result = new BlockConduitBundle(); result.init(); return result; diff --git a/src/main/java/crazypants/enderio/conduit/ConduitPacketHandler.java b/src/main/java/crazypants/enderio/conduit/ConduitPacketHandler.java index 34f82951..32374165 100644 --- a/src/main/java/crazypants/enderio/conduit/ConduitPacketHandler.java +++ b/src/main/java/crazypants/enderio/conduit/ConduitPacketHandler.java @@ -63,47 +63,6 @@ public class ConduitPacketHandler { // } // } // - // public static Packet createFluidConduitLevelPacket(ILiquidConduit liquidConduit) { - // ByteArrayOutputStream bos = new ByteArrayOutputStream(); - // DataOutputStream dos = new DataOutputStream(bos); - // IConduitBundle bundle = liquidConduit.getBundle(); - // try { - // dos.writeInt(PacketHandler.ID_CONDUIT_FLUID_LEVEL); - // dos.writeInt(bundle.getEntity().xCoord); - // dos.writeInt(bundle.getEntity().yCoord); - // dos.writeInt(bundle.getEntity().zCoord); - // NBTTagCompound tc = new NBTTagCompound(); - // liquidConduit.writeToNBT(tc); - // PacketUtil.writeNBTTagCompound(tc, dos); - // - // } catch (IOException e) { - // // never thrown - // } - // Packet250CustomPayload pkt = new Packet250CustomPayload(); - // pkt.channel = PacketHandler.CHANNEL; - // pkt.data = bos.toByteArray(); - // pkt.length = bos.size(); - // pkt.isChunkDataPacket = true; - // return pkt; - // } - // - // private void processFluidConduitLevelPacket(DataInputStream data, Player player) throws IOException { - // World world = getWorld(player); - // if(world == null) { - // return; - // } - // IConduitBundle conBun = getConduitBundle(data, world); - // if(conBun == null) { - // return; - // } - // ILiquidConduit con = conBun.getConduit(ILiquidConduit.class); - // if(con == null) { - // //Log.warn("processFluidConduitLevelPacket: no fluid conduit exists in bundle when recieving packet."); - // return; - // } - // NBTTagCompound tc = PacketUtil.readNBTTagCompound(data); - // con.readFromNBT(tc, TileConduitBundle.NBT_VERSION); - // } // // public static Packet createItemChannelPacket(IItemConduit itemConduit, ForgeDirection dir, DyeColor col, boolean input) { // ByteArrayOutputStream bos = new ByteArrayOutputStream(); diff --git a/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java b/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java index 49fe58cf..a92ef6bf 100644 --- a/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java +++ b/src/main/java/crazypants/enderio/conduit/TileConduitBundle.java @@ -38,7 +38,7 @@ import crazypants.util.BlockCoord; public class TileConduitBundle extends TileEntityEio implements IConduitBundle { - static final short NBT_VERSION = 1; + public static final short NBT_VERSION = 1; private final List conduits = new ArrayList(); diff --git a/src/main/java/crazypants/enderio/conduit/liquid/LiquidConduit.java b/src/main/java/crazypants/enderio/conduit/liquid/LiquidConduit.java index c3b73b62..5d99329a 100644 --- a/src/main/java/crazypants/enderio/conduit/liquid/LiquidConduit.java +++ b/src/main/java/crazypants/enderio/conduit/liquid/LiquidConduit.java @@ -16,6 +16,7 @@ import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTankInfo; import net.minecraftforge.fluids.IFluidHandler; +import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import crazypants.enderio.Config; @@ -106,10 +107,7 @@ public class LiquidConduit extends AbstractTankConduit { //need to send a custom packet as we don't want want to trigger a full chunk update, just //need to get the required values to the entity renderer BlockCoord loc = getLocation(); - //TODO:1.7 - // Packet packet = ConduitPacketHandler.createFluidConduitLevelPacket(this); - // PacketDispatcher.sendPacketToAllAround(loc.x, loc.y, loc.z, 64, world.provider.dimensionId, packet); - + EnderIO.packetPipeline.sendToAllAround(new PacketFluidLevel(this), new TargetPoint(world.provider.dimensionId, loc.x, loc.y, loc.z, 64)); lastSyncRatio = tank.getFilledRatio(); } } diff --git a/src/main/java/crazypants/enderio/conduit/liquid/PacketFluidLevel.java b/src/main/java/crazypants/enderio/conduit/liquid/PacketFluidLevel.java new file mode 100644 index 00000000..38730d51 --- /dev/null +++ b/src/main/java/crazypants/enderio/conduit/liquid/PacketFluidLevel.java @@ -0,0 +1,95 @@ +package crazypants.enderio.conduit.liquid; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import cpw.mods.fml.common.network.ByteBufUtils; +import crazypants.enderio.conduit.IConduitBundle; +import crazypants.enderio.conduit.TileConduitBundle; +import crazypants.enderio.network.AbstractPacketTileEntity; + +public class PacketFluidLevel extends AbstractPacketTileEntity { + + NBTTagCompound tc; + + public PacketFluidLevel() { + } + + public PacketFluidLevel(ILiquidConduit conduit) { + super(conduit.getBundle().getEntity()); + tc = new NBTTagCompound(); + conduit.writeToNBT(tc); + } + + @Override + public void encode(ChannelHandlerContext ctx, ByteBuf buf) { + super.encode(ctx, buf); + ByteBufUtils.writeTag(buf, tc); + } + + @Override + public void decode(ChannelHandlerContext ctx, ByteBuf buf) { + super.decode(ctx, buf); + tc = ByteBufUtils.readTag(buf); + + } + + @Override + protected void handleClientSide(EntityPlayer player, World worldObj, TileEntity tile) { + if(tc == null || !(tile instanceof IConduitBundle)) { + return; + } + IConduitBundle bundle = (IConduitBundle) tile; + ILiquidConduit con = bundle.getConduit(ILiquidConduit.class); + if(con == null) { + return; + } + con.readFromNBT(tc, TileConduitBundle.NBT_VERSION); + } + + // public static Packet createFluidConduitLevelPacket(ILiquidConduit liquidConduit) { + // ByteArrayOutputStream bos = new ByteArrayOutputStream(); + // DataOutputStream dos = new DataOutputStream(bos); + // IConduitBundle bundle = liquidConduit.getBundle(); + // try { + // dos.writeInt(PacketHandler.ID_CONDUIT_FLUID_LEVEL); + // dos.writeInt(bundle.getEntity().xCoord); + // dos.writeInt(bundle.getEntity().yCoord); + // dos.writeInt(bundle.getEntity().zCoord); + // NBTTagCompound tc = new NBTTagCompound(); + // liquidConduit.writeToNBT(tc); + // PacketUtil.writeNBTTagCompound(tc, dos); + // + // } catch (IOException e) { + // // never thrown + // } + // Packet250CustomPayload pkt = new Packet250CustomPayload(); + // pkt.channel = PacketHandler.CHANNEL; + // pkt.data = bos.toByteArray(); + // pkt.length = bos.size(); + // pkt.isChunkDataPacket = true; + // return pkt; + // } + // + // private void processFluidConduitLevelPacket(DataInputStream data, Player player) throws IOException { + // World world = getWorld(player); + // if(world == null) { + // return; + // } + // IConduitBundle conBun = getConduitBundle(data, world); + // if(conBun == null) { + // return; + // } + // ILiquidConduit con = conBun.getConduit(ILiquidConduit.class); + // if(con == null) { + // //Log.warn("processFluidConduitLevelPacket: no fluid conduit exists in bundle when recieving packet."); + // return; + // } + // NBTTagCompound tc = PacketUtil.readNBTTagCompound(data); + // con.readFromNBT(tc, TileConduitBundle.NBT_VERSION); + // } + +} diff --git a/src/main/java/crazypants/enderio/material/ItemFusedQuartzFrame.java b/src/main/java/crazypants/enderio/material/ItemFusedQuartzFrame.java index b54e5ce9..2d7d2c54 100644 --- a/src/main/java/crazypants/enderio/material/ItemFusedQuartzFrame.java +++ b/src/main/java/crazypants/enderio/material/ItemFusedQuartzFrame.java @@ -37,7 +37,7 @@ public class ItemFusedQuartzFrame extends Item { protected void init() { GameRegistry.registerItem(this, ModObject.itemFusedQuartzFrame.unlocalisedName); - MachineRecipeRegistry.instance.registerRecipe(ModObject.blockPainter.unlocalisedName, new FramePainterRecipe()); + MachineRecipeRegistry.instance.registerRecipe(ModObject.blockPainter.unlocalisedName, new FramePainterRecipe(this)); } @Override @@ -83,17 +83,20 @@ public class ItemFusedQuartzFrame extends Item { public static final class FramePainterRecipe extends BasicPainterTemplate { - public FramePainterRecipe() { + private ItemFusedQuartzFrame i; + + public FramePainterRecipe(ItemFusedQuartzFrame itemFusedQuartzFrame) { + i = itemFusedQuartzFrame; } @Override public boolean isValidTarget(ItemStack target) { - return target != null && target.getItem() == EnderIO.itemFusedQuartzFrame; + return target != null && target.getItem() == i; } @Override public List getAllRecipes() { - ItemStack is = new ItemStack(EnderIO.itemFusedQuartzFrame, 1, 0); + ItemStack is = new ItemStack(i, 1, 0); IEnderIoRecipe recipe = new EnderIoRecipe(IEnderIoRecipe.PAINTER_ID, DEFAULT_ENERGY_PER_TASK, is, is); return Collections.singletonList(recipe); } diff --git a/src/main/java/crazypants/enderio/network/AbstractPacketTileEntity.java b/src/main/java/crazypants/enderio/network/AbstractPacketTileEntity.java new file mode 100644 index 00000000..305d4da4 --- /dev/null +++ b/src/main/java/crazypants/enderio/network/AbstractPacketTileEntity.java @@ -0,0 +1,93 @@ +package crazypants.enderio.network; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import cpw.mods.fml.common.network.ByteBufUtils; +import crazypants.enderio.Log; + +public class AbstractPacketTileEntity implements IPacketEio { + + protected int x; + protected int y; + protected int z; + + private Class tileClass; + + protected AbstractPacketTileEntity() { + } + + protected AbstractPacketTileEntity(T tile) { + tileClass = tile.getClass(); + x = tile.xCoord; + y = tile.yCoord; + z = tile.zCoord; + } + + @Override + public void encode(ChannelHandlerContext ctx, ByteBuf buf) { + buf.writeInt(x); + buf.writeInt(y); + buf.writeInt(z); + ByteBufUtils.writeUTF8String(buf, tileClass.getName()); + + } + + @Override + public void decode(ChannelHandlerContext ctx, ByteBuf buf) { + x = buf.readInt(); + y = buf.readInt(); + z = buf.readInt(); + String str = ByteBufUtils.readUTF8String(buf); + try { + tileClass = (Class) Class.forName(str); + } catch (Exception e) { + Log.error("AbstractPacketTileEntity could not load tile entity class: " + str); + } + } + + @Override + public void handleClientSide(EntityPlayer player) { + T tile = getTileEntity(player.worldObj); + if(tile != null) { + handleClientSide(player, player.worldObj, tile); + } + } + + protected void handleClientSide(EntityPlayer player, World worldObj, T tile) { + handle(player, worldObj, tile); + } + + @Override + public void handleServerSide(EntityPlayer player) { + T tile = getTileEntity(player.worldObj); + if(tile != null) { + handleServerSide(player, player.worldObj, tile); + } + } + + protected void handleServerSide(EntityPlayer player, World worldObj, T tile) { + handle(player, worldObj, tile); + } + + protected void handle(EntityPlayer player, World worldObj, T tile) { + + } + + protected T getTileEntity(World worldObj) { + if(worldObj == null) { + return null; + } + TileEntity te = worldObj.getTileEntity(x, y, z); + if(te == null) { + return null; + } + if(tileClass.isAssignableFrom(te.getClass())) { + return (T) te; + } + return null; + } + +} diff --git a/src/main/java/crazypants/enderio/network/PacketTileEntity.java b/src/main/java/crazypants/enderio/network/PacketTileEntityNbt.java similarity index 93% rename from src/main/java/crazypants/enderio/network/PacketTileEntity.java rename to src/main/java/crazypants/enderio/network/PacketTileEntityNbt.java index ed1d7a44..630e29da 100644 --- a/src/main/java/crazypants/enderio/network/PacketTileEntity.java +++ b/src/main/java/crazypants/enderio/network/PacketTileEntityNbt.java @@ -11,7 +11,7 @@ import net.minecraft.world.World; /** * Created by CrazyPants on 27/02/14. */ -public class PacketTileEntity implements IPacketEio { +public class PacketTileEntityNbt implements IPacketEio { TileEntity te; @@ -22,11 +22,11 @@ public class PacketTileEntity implements IPacketEio { boolean renderOnUpdate = false; - public PacketTileEntity() { + public PacketTileEntityNbt() { } - public PacketTileEntity(TileEntity te) { + public PacketTileEntityNbt(TileEntity te) { this.te = te; x = te.xCoord; y = te.yCoord; diff --git a/src/main/java/crazypants/enderio/teleport/GuiTravelAuth.java b/src/main/java/crazypants/enderio/teleport/GuiTravelAuth.java index 2a168caf..938db9bc 100644 --- a/src/main/java/crazypants/enderio/teleport/GuiTravelAuth.java +++ b/src/main/java/crazypants/enderio/teleport/GuiTravelAuth.java @@ -11,7 +11,7 @@ import net.minecraft.world.World; import org.lwjgl.opengl.GL11; import crazypants.enderio.EnderIO; -import crazypants.enderio.network.PacketTileEntity; +import crazypants.enderio.network.PacketTileEntityNbt; import crazypants.gui.GuiContainerBase; import crazypants.render.ColorUtil; import crazypants.render.RenderUtil; @@ -50,7 +50,7 @@ public class GuiTravelAuth extends GuiContainerBase { ContainerTravelAuth poo = (ContainerTravelAuth) inventorySlots; if(ta.authoriseUser(player, poo.enteredPassword)) { TileEntity te = ((TileEntity) ta); - EnderIO.packetPipeline.sendToServer(new PacketTileEntity(te)); + EnderIO.packetPipeline.sendToServer(new PacketTileEntityNbt(te)); this.mc.displayGuiScreen((GuiScreen) null); this.mc.setIngameFocus();