Added support for Packets for ItemStacks

This commit is contained in:
DarkGuardsman 2013-12-31 12:36:09 -05:00
parent e81b4354a6
commit 02a0000be1
4 changed files with 110 additions and 1 deletions

View file

@ -0,0 +1,11 @@
package com.builtbroken.minecraft.network;
import com.google.common.io.ByteArrayDataInput;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
public interface ISimpleItemPacketReceiver
{
public boolean simplePacket(EntityPlayer player, ItemStack stack, String id, ByteArrayDataInput data);
}

View file

@ -16,5 +16,6 @@ public interface ISimplePacketReceiver
* @param data - data from the packet, after location has been read
* @param player - player that the packet was sent to or came from
* @return true if the packet was used */
public boolean simplePacket(String id, ByteArrayDataInput data, Player player);
public boolean simplePacket(String id, ByteArrayDataInput data, Player player);
}

View file

@ -44,6 +44,7 @@ public class PacketHandler implements IPacketHandler
registerManager(new PacketManagerTile());
registerManager(PacketManagerKeyEvent.instance());
registerManager(new PacketManagerEntity());
registerManager(new PacketManagerItem());
}
public static void registerManager(IPacketManager manager)

View file

@ -0,0 +1,96 @@
package com.builtbroken.minecraft.network;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.network.Player;
/** Helps update the server or client with general information about an item. Should mostly be used
* for GUIs or render effects.
*
* @author DarkGuardsman */
public class PacketManagerItem implements IPacketManager
{
static int packetID = 0;
@Override
public int getID()
{
return packetID;
}
@Override
public void setID(int maxID)
{
packetID = maxID;
}
@Override
public void handlePacket(INetworkManager network, Packet250CustomPayload packet, Player player, ByteArrayDataInput data)
{
try
{
EntityPlayer entityPlayer = (EntityPlayer) player;
String id = data.readUTF();
int slot = data.readInt();
ItemStack stack = null;
if (slot >= 0)
{
stack = entityPlayer.inventory.getStackInSlot(slot);
}
else if (slot == -1)
{
stack = entityPlayer.getHeldItem();
}
if (stack != null & stack.getItem() != null)
{
if (stack.getItem() instanceof ISimpleItemPacketReceiver)
{
((ISimpleItemPacketReceiver) stack.getItem()).simplePacket(entityPlayer, stack, id, data);
}
}
}
catch (Exception e)
{
System.out.println("[CoreLibrary] Error reading packet for item data");
e.printStackTrace();
}
}
@SuppressWarnings("resource")
public static Packet getPacket(EntityPlayer player, String channelName, String id, int slot, Object... dataToSend)
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream data = new DataOutputStream(bytes);
try
{
data.writeInt(packetID);
data.writeUTF(id);
data.writeInt(slot);
data = PacketHandler.instance.encodeDataStream(data, dataToSend);
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;
}
}