packets packets every where.

This commit is contained in:
cp committed 2014-03-04 12:52:10 +08:00
1 parent c6670e0060
commit 02d3805656
10 files changed
+208 -57

No files matched your search

@@ -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);
@@ -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;
@@ -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();
@@ -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<IConduit> conduits = new ArrayList<IConduit>();
@@ -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();
}
}
@@ -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<TileEntity> {
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);
// }
}
@@ -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<IEnderIoRecipe> 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);
}
@@ -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<T extends TileEntity> implements IPacketEio {
protected int x;
protected int y;
protected int z;
private Class<? extends TileEntity> 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<TileEntity>) 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;
}
}
@@ -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;
@@ -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();