2011-04-23 02:06:48 +02:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.EOFException;
|
|
|
|
import java.io.IOException;
|
2012-10-25 05:53:23 +02:00
|
|
|
import java.net.Socket;
|
2011-04-23 02:06:48 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public abstract class Packet {
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static IntHashMap l = new IntHashMap();
|
2011-11-20 09:01:14 +01:00
|
|
|
private static Map a = new HashMap();
|
|
|
|
private static Set b = new HashSet();
|
2012-02-29 22:31:04 +01:00
|
|
|
private static Set c = new HashSet();
|
2011-04-23 02:06:48 +02:00
|
|
|
public final long timestamp = System.currentTimeMillis();
|
2012-03-01 11:49:23 +01:00
|
|
|
public static long n;
|
|
|
|
public static long o;
|
2012-07-29 09:33:13 +02:00
|
|
|
public static long p;
|
|
|
|
public static long q;
|
2012-01-14 21:03:48 +01:00
|
|
|
public boolean lowPriority = false;
|
2012-08-12 06:14:23 +02:00
|
|
|
// CraftBukkit start - calculate packet ID once - used a bunch of times
|
|
|
|
private int packetID;
|
2012-09-14 07:03:47 +02:00
|
|
|
|
2012-08-12 06:14:23 +02:00
|
|
|
public Packet() {
|
|
|
|
packetID = ((Integer) a.get(this.getClass())).intValue();
|
2012-09-14 07:03:47 +02:00
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2011-04-23 02:06:48 +02:00
|
|
|
|
|
|
|
static void a(int i, boolean flag, boolean flag1, Class oclass) {
|
2012-07-29 09:33:13 +02:00
|
|
|
if (l.b(i)) {
|
2011-04-23 02:06:48 +02:00
|
|
|
throw new IllegalArgumentException("Duplicate packet id:" + i);
|
2011-11-20 09:01:14 +01:00
|
|
|
} else if (a.containsKey(oclass)) {
|
2011-04-23 02:06:48 +02:00
|
|
|
throw new IllegalArgumentException("Duplicate packet class:" + oclass);
|
|
|
|
} else {
|
2012-07-29 09:33:13 +02:00
|
|
|
l.a(i, oclass);
|
2011-11-20 09:01:14 +01:00
|
|
|
a.put(oclass, Integer.valueOf(i));
|
2011-04-23 02:06:48 +02:00
|
|
|
if (flag) {
|
2011-11-20 09:01:14 +01:00
|
|
|
b.add(Integer.valueOf(i));
|
2011-04-23 02:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flag1) {
|
2012-02-29 22:31:04 +01:00
|
|
|
c.add(Integer.valueOf(i));
|
2011-04-23 02:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static Packet d(int i) {
|
2011-04-23 02:06:48 +02:00
|
|
|
try {
|
2012-07-29 09:33:13 +02:00
|
|
|
Class oclass = (Class) l.get(i);
|
2011-04-23 02:06:48 +02:00
|
|
|
|
|
|
|
return oclass == null ? null : (Packet) oclass.newInstance();
|
|
|
|
} catch (Exception exception) {
|
|
|
|
exception.printStackTrace();
|
|
|
|
System.out.println("Skipping packet with id " + i);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static void a(DataOutputStream dataoutputstream, byte[] abyte) throws IOException { // CraftBukkit - throws IOException
|
|
|
|
dataoutputstream.writeShort(abyte.length);
|
|
|
|
dataoutputstream.write(abyte);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] b(DataInputStream datainputstream) throws IOException { // CraftBukkit - throws IOException
|
|
|
|
short short1 = datainputstream.readShort();
|
|
|
|
|
|
|
|
if (short1 < 0) {
|
|
|
|
throw new IOException("Key was smaller than nothing! Weird key!");
|
|
|
|
} else {
|
|
|
|
byte[] abyte = new byte[short1];
|
|
|
|
|
|
|
|
datainputstream.read(abyte);
|
|
|
|
return abyte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int k() {
|
2012-08-12 06:14:23 +02:00
|
|
|
return packetID; // ((Integer) a.get(this.getClass())).intValue(); // CraftBukkit
|
2011-04-23 02:06:48 +02:00
|
|
|
}
|
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
public static Packet a(DataInputStream datainputstream, boolean flag, Socket socket) throws IOException { // CraftBukkit - throws IOException
|
2011-04-23 02:06:48 +02:00
|
|
|
boolean flag1 = false;
|
|
|
|
Packet packet = null;
|
2012-10-25 05:53:23 +02:00
|
|
|
int i = socket.getSoTimeout();
|
2011-04-23 02:06:48 +02:00
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
int j;
|
2011-04-23 02:06:48 +02:00
|
|
|
|
|
|
|
try {
|
2012-10-25 05:53:23 +02:00
|
|
|
j = datainputstream.read();
|
|
|
|
if (j == -1) {
|
2011-04-23 02:06:48 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
if (flag && !c.contains(Integer.valueOf(j)) || !flag && !b.contains(Integer.valueOf(j))) {
|
|
|
|
throw new IOException("Bad packet id " + j);
|
2011-04-23 02:06:48 +02:00
|
|
|
}
|
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
packet = d(j);
|
2011-04-23 02:06:48 +02:00
|
|
|
if (packet == null) {
|
2012-10-25 05:53:23 +02:00
|
|
|
throw new IOException("Bad packet id " + j);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet instanceof Packet254GetInfo) {
|
|
|
|
socket.setSoTimeout(1500);
|
2011-04-23 02:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
packet.a(datainputstream);
|
2012-07-29 09:33:13 +02:00
|
|
|
++n;
|
|
|
|
o += (long) packet.a();
|
2011-04-23 02:06:48 +02:00
|
|
|
} catch (EOFException eofexception) {
|
|
|
|
System.out.println("Reached end of stream");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-04-23 10:47:38 +02:00
|
|
|
// CraftBukkit start
|
2011-06-27 00:25:01 +02:00
|
|
|
catch (java.net.SocketTimeoutException exception) {
|
2011-04-23 10:47:38 +02:00
|
|
|
System.out.println("Read timed out");
|
|
|
|
return null;
|
2011-06-27 00:25:01 +02:00
|
|
|
} catch (java.net.SocketException exception) {
|
2011-04-23 10:47:38 +02:00
|
|
|
System.out.println("Connection reset");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
PacketCounter.a(j, (long) packet.a());
|
2012-07-29 09:33:13 +02:00
|
|
|
++n;
|
|
|
|
o += (long) packet.a();
|
2012-10-25 05:53:23 +02:00
|
|
|
socket.setSoTimeout(i);
|
2011-04-23 02:06:48 +02:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static void a(Packet packet, DataOutputStream dataoutputstream) throws IOException { // CraftBukkit - throws IOException
|
|
|
|
dataoutputstream.write(packet.k());
|
2011-04-23 02:06:48 +02:00
|
|
|
packet.a(dataoutputstream);
|
2012-07-29 09:33:13 +02:00
|
|
|
++p;
|
|
|
|
q += (long) packet.a();
|
2011-04-23 02:06:48 +02:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static void a(String s, DataOutputStream dataoutputstream) throws IOException { // CraftBukkit - throws IOException
|
2011-04-23 02:06:48 +02:00
|
|
|
if (s.length() > 32767) {
|
|
|
|
throw new IOException("String too big");
|
|
|
|
} else {
|
|
|
|
dataoutputstream.writeShort(s.length());
|
|
|
|
dataoutputstream.writeChars(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static String a(DataInputStream datainputstream, int i) throws IOException { // CraftBukkit - throws IOException
|
2011-04-23 02:06:48 +02:00
|
|
|
short short1 = datainputstream.readShort();
|
|
|
|
|
|
|
|
if (short1 > i) {
|
|
|
|
throw new IOException("Received string length longer than maximum allowed (" + short1 + " > " + i + ")");
|
|
|
|
} else if (short1 < 0) {
|
|
|
|
throw new IOException("Received string length is less than zero! Weird string!");
|
|
|
|
} else {
|
|
|
|
StringBuilder stringbuilder = new StringBuilder();
|
|
|
|
|
|
|
|
for (int j = 0; j < short1; ++j) {
|
|
|
|
stringbuilder.append(datainputstream.readChar());
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringbuilder.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public abstract void a(DataInputStream datainputstream) throws IOException; // CraftBukkit - throws IOException
|
2011-04-23 02:06:48 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public abstract void a(DataOutputStream dataoutputstream) throws IOException; // CraftBukkit - throws IOException
|
2011-04-23 02:06:48 +02:00
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
public abstract void handle(NetHandler nethandler);
|
2011-04-23 02:06:48 +02:00
|
|
|
|
|
|
|
public abstract int a();
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public boolean e() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean a(Packet packet) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean a_() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
String s = this.getClass().getSimpleName();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack c(DataInputStream datainputstream) throws IOException { // CraftBukkit - throws IOException
|
2011-11-20 09:01:14 +01:00
|
|
|
ItemStack itemstack = null;
|
|
|
|
short short1 = datainputstream.readShort();
|
|
|
|
|
|
|
|
if (short1 >= 0) {
|
|
|
|
byte b0 = datainputstream.readByte();
|
|
|
|
short short2 = datainputstream.readShort();
|
|
|
|
|
|
|
|
itemstack = new ItemStack(short1, b0, short2);
|
2012-07-29 09:33:13 +02:00
|
|
|
itemstack.tag = d(datainputstream);
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return itemstack;
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static void a(ItemStack itemstack, DataOutputStream dataoutputstream) throws IOException { // CraftBukkit - throws IOException
|
|
|
|
if (itemstack == null) {
|
2011-11-20 09:01:14 +01:00
|
|
|
dataoutputstream.writeShort(-1);
|
|
|
|
} else {
|
|
|
|
dataoutputstream.writeShort(itemstack.id);
|
|
|
|
dataoutputstream.writeByte(itemstack.count);
|
|
|
|
dataoutputstream.writeShort(itemstack.getData());
|
2012-07-29 09:33:13 +02:00
|
|
|
NBTTagCompound nbttagcompound = null;
|
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
if (itemstack.getItem().n() || itemstack.getItem().q()) {
|
2012-07-29 09:33:13 +02:00
|
|
|
nbttagcompound = itemstack.tag;
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
|
|
|
|
a(nbttagcompound, dataoutputstream);
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static NBTTagCompound d(DataInputStream datainputstream) throws IOException { // CraftBukkit - throws IOException
|
2011-11-20 09:01:14 +01:00
|
|
|
short short1 = datainputstream.readShort();
|
|
|
|
|
|
|
|
if (short1 < 0) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
byte[] abyte = new byte[short1];
|
|
|
|
|
|
|
|
datainputstream.readFully(abyte);
|
2011-11-30 00:17:43 +01:00
|
|
|
return NBTCompressedStreamTools.a(abyte);
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
protected static void a(NBTTagCompound nbttagcompound, DataOutputStream dataoutputstream) throws IOException { // CraftBukkit - throws IOException
|
2011-11-20 09:01:14 +01:00
|
|
|
if (nbttagcompound == null) {
|
|
|
|
dataoutputstream.writeShort(-1);
|
|
|
|
} else {
|
2011-11-30 00:17:43 +01:00
|
|
|
byte[] abyte = NBTCompressedStreamTools.a(nbttagcompound);
|
2011-11-20 09:01:14 +01:00
|
|
|
|
|
|
|
dataoutputstream.writeShort((short) abyte.length);
|
|
|
|
dataoutputstream.write(abyte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 02:06:48 +02:00
|
|
|
static {
|
|
|
|
a(0, true, true, Packet0KeepAlive.class);
|
|
|
|
a(1, true, true, Packet1Login.class);
|
2012-07-29 09:33:13 +02:00
|
|
|
a(2, false, true, Packet2Handshake.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(3, true, true, Packet3Chat.class);
|
|
|
|
a(4, true, false, Packet4UpdateTime.class);
|
|
|
|
a(5, true, false, Packet5EntityEquipment.class);
|
|
|
|
a(6, true, false, Packet6SpawnPosition.class);
|
|
|
|
a(7, false, true, Packet7UseEntity.class);
|
|
|
|
a(8, true, false, Packet8UpdateHealth.class);
|
|
|
|
a(9, true, true, Packet9Respawn.class);
|
|
|
|
a(10, true, true, Packet10Flying.class);
|
|
|
|
a(11, true, true, Packet11PlayerPosition.class);
|
|
|
|
a(12, true, true, Packet12PlayerLook.class);
|
|
|
|
a(13, true, true, Packet13PlayerLookMove.class);
|
|
|
|
a(14, false, true, Packet14BlockDig.class);
|
|
|
|
a(15, false, true, Packet15Place.class);
|
|
|
|
a(16, false, true, Packet16BlockItemSwitch.class);
|
2011-11-30 00:17:43 +01:00
|
|
|
a(17, true, false, Packet17EntityLocationAction.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(18, true, true, Packet18ArmAnimation.class);
|
|
|
|
a(19, false, true, Packet19EntityAction.class);
|
|
|
|
a(20, true, false, Packet20NamedEntitySpawn.class);
|
|
|
|
a(21, true, false, Packet21PickupSpawn.class);
|
|
|
|
a(22, true, false, Packet22Collect.class);
|
|
|
|
a(23, true, false, Packet23VehicleSpawn.class);
|
|
|
|
a(24, true, false, Packet24MobSpawn.class);
|
|
|
|
a(25, true, false, Packet25EntityPainting.class);
|
2011-09-15 02:23:52 +02:00
|
|
|
a(26, true, false, Packet26AddExpOrb.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(28, true, false, Packet28EntityVelocity.class);
|
|
|
|
a(29, true, false, Packet29DestroyEntity.class);
|
|
|
|
a(30, true, false, Packet30Entity.class);
|
|
|
|
a(31, true, false, Packet31RelEntityMove.class);
|
|
|
|
a(32, true, false, Packet32EntityLook.class);
|
|
|
|
a(33, true, false, Packet33RelEntityMoveLook.class);
|
|
|
|
a(34, true, false, Packet34EntityTeleport.class);
|
2012-03-01 11:49:23 +01:00
|
|
|
a(35, true, false, Packet35EntityHeadRotation.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(38, true, false, Packet38EntityStatus.class);
|
|
|
|
a(39, true, false, Packet39AttachEntity.class);
|
|
|
|
a(40, true, false, Packet40EntityMetadata.class);
|
2011-09-15 02:23:52 +02:00
|
|
|
a(41, true, false, Packet41MobEffect.class);
|
|
|
|
a(42, true, false, Packet42RemoveMobEffect.class);
|
|
|
|
a(43, true, false, Packet43SetExperience.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(51, true, false, Packet51MapChunk.class);
|
|
|
|
a(52, true, false, Packet52MultiBlockChange.class);
|
|
|
|
a(53, true, false, Packet53BlockChange.class);
|
|
|
|
a(54, true, false, Packet54PlayNoteBlock.class);
|
2012-07-29 09:33:13 +02:00
|
|
|
a(55, true, false, Packet55BlockBreakAnimation.class);
|
|
|
|
a(56, true, false, Packet56MapChunkBulk.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(60, true, false, Packet60Explosion.class);
|
2011-11-30 00:17:43 +01:00
|
|
|
a(61, true, false, Packet61WorldEvent.class);
|
2012-07-29 09:33:13 +02:00
|
|
|
a(62, true, false, Packet62NamedSoundEffect.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(70, true, false, Packet70Bed.class);
|
|
|
|
a(71, true, false, Packet71Weather.class);
|
|
|
|
a(100, true, false, Packet100OpenWindow.class);
|
|
|
|
a(101, true, true, Packet101CloseWindow.class);
|
|
|
|
a(102, false, true, Packet102WindowClick.class);
|
|
|
|
a(103, true, false, Packet103SetSlot.class);
|
|
|
|
a(104, true, false, Packet104WindowItems.class);
|
|
|
|
a(105, true, false, Packet105CraftProgressBar.class);
|
|
|
|
a(106, true, true, Packet106Transaction.class);
|
2011-09-15 02:23:52 +02:00
|
|
|
a(107, true, true, Packet107SetCreativeSlot.class);
|
2011-11-30 00:17:43 +01:00
|
|
|
a(108, false, true, Packet108ButtonClick.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(130, true, true, Packet130UpdateSign.class);
|
2011-11-30 00:17:43 +01:00
|
|
|
a(131, true, false, Packet131ItemData.class);
|
2012-03-01 11:49:23 +01:00
|
|
|
a(132, true, false, Packet132TileEntityData.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(200, true, false, Packet200Statistic.class);
|
2011-09-15 02:23:52 +02:00
|
|
|
a(201, true, false, Packet201PlayerInfo.class);
|
2012-03-22 21:39:39 +01:00
|
|
|
a(202, true, true, Packet202Abilities.class);
|
2012-07-29 09:33:13 +02:00
|
|
|
a(203, true, true, Packet203TabComplete.class);
|
|
|
|
a(204, false, true, Packet204LocaleAndViewDistance.class);
|
|
|
|
a(205, false, true, Packet205ClientCommand.class);
|
2012-01-12 23:10:13 +01:00
|
|
|
a(250, true, true, Packet250CustomPayload.class);
|
2012-07-29 09:33:13 +02:00
|
|
|
a(252, true, true, Packet252KeyResponse.class);
|
|
|
|
a(253, true, false, Packet253KeyRequest.class);
|
2011-09-15 02:23:52 +02:00
|
|
|
a(254, false, true, Packet254GetInfo.class);
|
2011-04-23 02:06:48 +02:00
|
|
|
a(255, true, true, Packet255KickDisconnect.class);
|
|
|
|
}
|
|
|
|
}
|