ironbarrels/IronChests2/common/cpw/mods/ironchest/PacketHandler.java

87 lines
2.7 KiB
Java
Raw Normal View History

/*******************************************************************************
* Copyright (c) 2012 cpw.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
2012-08-11 07:46:49 +02:00
*
* Contributors:
* cpw - initial API and implementation
******************************************************************************/
package cpw.mods.ironchest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
2012-08-11 07:46:49 +02:00
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
import net.minecraft.src.NetworkManager;
import net.minecraft.src.Packet;
import net.minecraft.src.Packet1Login;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
2012-08-11 07:46:49 +02:00
public class PacketHandler implements IPacketHandler {
@Override
2012-08-11 07:46:49 +02:00
public void onPacketData(NetworkManager network, Packet250CustomPayload packet, Player player) {
ByteArrayDataInput dat = ByteStreams.newDataInput(packet.data);
int x = dat.readInt();
int y = dat.readInt();
int z = dat.readInt();
byte typ = dat.readByte();
boolean hasStacks = dat.readByte() != 0;
int[] items = new int[0];
if (hasStacks) {
items = new int[24];
for (int i = 0; i < items.length; i++) {
items[i] = dat.readInt();
}
}
2012-08-11 07:46:49 +02:00
World world = IronChest.proxy.getClientWorld();
TileEntity te = world.getBlockTileEntity(x, y, z);
if (te instanceof TileEntityIronChest) {
2012-08-11 07:46:49 +02:00
TileEntityIronChest icte = (TileEntityIronChest) te;
icte.handlePacketData(typ, items);
}
}
public static Packet getPacket(TileEntityIronChest tileEntityIronChest) {
2012-08-11 07:46:49 +02:00
ByteArrayOutputStream bos = new ByteArrayOutputStream(140);
DataOutputStream dos = new DataOutputStream(bos);
int x = tileEntityIronChest.xCoord;
int y = tileEntityIronChest.yCoord;
int z = tileEntityIronChest.zCoord;
int typ = tileEntityIronChest.getType().ordinal();
int[] items = tileEntityIronChest.buildIntDataList();
boolean hasStacks = (items != null);
try {
dos.writeInt(x);
dos.writeInt(y);
dos.writeInt(z);
dos.writeByte(typ);
dos.writeByte(hasStacks ? 1 : 0);
if (hasStacks) {
for (int i = 0; i < 24; i++) {
dos.writeInt(items[i]);
}
}
} catch (IOException e) {
// UNPOSSIBLE?
}
Packet250CustomPayload pkt = new Packet250CustomPayload();
pkt.channel = "IronChest";
pkt.data = bos.toByteArray();
pkt.length = bos.size();
pkt.isChunkDataPacket = true;
return pkt;
}
}