From 93615b2ec633e98e96640228d010a40417068204 Mon Sep 17 00:00:00 2001 From: DarkGaurdsman Date: Thu, 2 Jan 2014 12:28:49 -0500 Subject: [PATCH] Added a simple packet loader interface for simple object packets --- .../minecraft/network/PacketHandler.java | 50 ++++++++++++++++++- .../minecraft/save/IPacketLoad.java | 15 ++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/com/builtbroken/minecraft/save/IPacketLoad.java diff --git a/src/com/builtbroken/minecraft/network/PacketHandler.java b/src/com/builtbroken/minecraft/network/PacketHandler.java index 98e8cbf4b..d67fbba15 100644 --- a/src/com/builtbroken/minecraft/network/PacketHandler.java +++ b/src/com/builtbroken/minecraft/network/PacketHandler.java @@ -15,6 +15,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import universalelectricity.api.vector.Vector3; +import com.builtbroken.minecraft.save.IPacketLoad; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; @@ -171,9 +172,54 @@ public class PacketHandler implements IPacketHandler return getPacketWithID(channelName, -1, sendData); } - /** Gets a packet for the tile entity. + /** Generates a packet for a tile entity using a loader instance. This is mainly used to pass + * data from an object inside a tile entity and have it go write back to that object on the + * other side of the network. Your tile entity will still need to grab the data in the same way + * as getTilePacket. Then just pass it back to the object that wrote the data. * - * @return */ + * @param channelName - channel to send the packet threw + * @param packetID - id to be used with the tile entity receive method + * @param sender - tile entity that send the packet, and will get the packet + * @param load - object that will be loading the data + * @return new Packet250CustomPayLoad */ + public Packet getPacketFromLoader(String channelName, String packetID, TileEntity sender, IPacketLoad load) + { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + DataOutputStream data = new DataOutputStream(bytes); + + try + { + data.writeInt(PacketHandler.tile.getID()); + + data.writeInt(sender.xCoord); + data.writeInt(sender.yCoord); + data.writeInt(sender.zCoord); + data.writeUTF(packetID); + load.loadPacket(data); + + Packet250CustomPayload packet = new Packet250CustomPayload(); + packet.channel = channelName; + packet.data = bytes.toByteArray(); + packet.length = packet.data.length; + + return packet; + } + catch (IOException e) + { + System.out.println("Failed to create packet."); + e.printStackTrace(); + } + + return null; + } + + /** Generates a packet for a tile entity using the data given + * + * @param channelName - channel to send the packet threw + * @param packetID - id to be used with the tile entity receive method + * @param sender - tile entity that send the packet, and will get the packet + * @param sendData - data to be sent + * @return new Packet250CustomPayLoad */ @SuppressWarnings("resource") public Packet getTilePacket(String channelName, String packetID, TileEntity sender, Object... sendData) { diff --git a/src/com/builtbroken/minecraft/save/IPacketLoad.java b/src/com/builtbroken/minecraft/save/IPacketLoad.java new file mode 100644 index 000000000..59739a9bf --- /dev/null +++ b/src/com/builtbroken/minecraft/save/IPacketLoad.java @@ -0,0 +1,15 @@ +package com.builtbroken.minecraft.save; + +import java.io.DataOutputStream; + +import com.google.common.io.ByteArrayDataInput; + +/** Used for object that only have one set of data to send and receive + * + * @author DarkGuardsman */ +public interface IPacketLoad +{ + public void readPacket(ByteArrayDataInput data); + + public void loadPacket(DataOutputStream data); +}