Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 261 lines 10 kB view raw
1package net.minecraft.server; 2 3import com.legacyminecraft.poseidon.PoseidonConfig; 4import com.legacyminecraft.poseidon.packets.ArtificialPacket53BlockChange; 5 6import uk.betacraft.uberbukkit.packet.Packet62Sound; 7import uk.betacraft.uberbukkit.packet.Packet63Digging; 8 9import java.io.DataInputStream; 10import java.io.DataOutputStream; 11import java.io.EOFException; 12import java.io.IOException; 13import java.util.HashMap; 14import java.util.HashSet; 15import java.util.Map; 16import java.util.Set; 17 18public abstract class Packet { 19 20 private static Map packetIdToClassMap = new HashMap(); 21 private static Map packetClassToIdMap = new HashMap(); 22 private static Set clientPacketIdList = new HashSet(); 23 private static Set serverPacketIdList = new HashSet(); 24 public final long timestamp = System.currentTimeMillis(); 25 public boolean k = false; 26 private static HashMap e; 27 private static int f; 28 29 protected int pvn; // uberbukkit 30 31 public Packet() { 32 } 33 34 /** 35 * Register a packet 36 * 37 * @param id 38 * @param clientSide 39 * @param serverSide 40 * @param oclass 41 * @author moderator_Man 42 */ 43 public static void registerPacket(int id, boolean clientSide, boolean serverSide, Class oclass) { 44 if (packetIdToClassMap.containsKey(Integer.valueOf(id))) 45 throw new IllegalArgumentException("Duplicate packet id:" + id); 46 else if (packetClassToIdMap.containsKey(oclass)) 47 throw new IllegalArgumentException("Duplicate packet class:" + oclass); 48 else { 49 packetIdToClassMap.put(Integer.valueOf(id), oclass); 50 packetClassToIdMap.put(oclass, Integer.valueOf(id)); 51 if (clientSide) clientPacketIdList.add(Integer.valueOf(id)); 52 if (serverSide) serverPacketIdList.add(Integer.valueOf(id)); 53 } 54 } 55 56 static void a(int i, boolean flag, boolean flag1, Class oclass) { 57 if (packetIdToClassMap.containsKey(Integer.valueOf(i))) { 58 throw new IllegalArgumentException("Duplicate packet id:" + i); 59 } else if (packetClassToIdMap.containsKey(oclass)) { 60 throw new IllegalArgumentException("Duplicate packet class:" + oclass); 61 } else { 62 packetIdToClassMap.put(Integer.valueOf(i), oclass); 63 packetClassToIdMap.put(oclass, Integer.valueOf(i)); 64 if (flag) { 65 clientPacketIdList.add(Integer.valueOf(i)); 66 } 67 68 if (flag1) { 69 serverPacketIdList.add(Integer.valueOf(i)); 70 } 71 } 72 } 73 74 public static Packet a(int i) { 75 try { 76 Class oclass = (Class) packetIdToClassMap.get(Integer.valueOf(i)); 77 78 return oclass == null ? null : (Packet) oclass.newInstance(); 79 } catch (Exception exception) { 80 exception.printStackTrace(); 81 System.out.println("Skipping packet with id " + i); 82 return null; 83 } 84 } 85 86 public final int b() { 87 return ((Integer) packetClassToIdMap.get(this.getClass())).intValue(); 88 } 89 90 // CraftBukkit - throws IOException 91 public static Packet a(DataInputStream datainputstream, boolean flag, int pvn) throws IOException { 92 boolean flag1 = false; 93 Packet packet = null; 94 95 int i; 96 97 try { 98 i = datainputstream.read(); 99 if (i == -1) { 100 return null; 101 } 102 103 if (flag && !serverPacketIdList.contains(Integer.valueOf(i)) || !flag && !clientPacketIdList.contains(Integer.valueOf(i))) { 104 System.out.println("Bad packet id: " + i); //Project Poseidon 105 return null; //Project Poseidon 106 //throw new IOException("Bad packet id " + i); //Project Poseidon - Comment Out 107 } 108 109 packet = a(i); 110 if (packet == null) { 111 throw new IOException("Bad packet id " + i); 112 } 113 114 packet.pvn = pvn; 115 packet.a(datainputstream); 116 } catch (EOFException eofexception) { 117 System.out.println("Reached end of stream"); 118 return null; 119 } 120 121 // CraftBukkit start 122 catch (java.net.SocketTimeoutException exception) { 123 System.out.println("Read timed out"); 124 return null; 125 } catch (java.net.SocketException exception) { 126 if (!(boolean) PoseidonConfig.getInstance().getConfigOption("settings.remove-join-leave-debug", true)) { 127 System.out.println("Connection reset"); 128 } 129 return null; 130 } 131 // CraftBukkit end 132 133 PacketCounter packetcounter = (PacketCounter) e.get(Integer.valueOf(i)); 134 135 if (packetcounter == null) { 136 packetcounter = new PacketCounter((EmptyClass1) null); 137 e.put(Integer.valueOf(i), packetcounter); 138 } 139 140 packetcounter.a(packet.a()); 141 ++f; 142 if (f % 1000 == 0) { 143 ; 144 } 145 146 return packet; 147 } 148 149 // CraftBukkit - throws IOException 150 public static void a(Packet packet, DataOutputStream dataoutputstream) throws IOException { 151 dataoutputstream.write(packet.b()); 152 packet.a(dataoutputstream); 153 } 154 155 // CraftBukkit - throws IOException 156 public static void a(String s, DataOutputStream dataoutputstream) throws IOException { 157 if (s.length() > 32767) { 158 throw new IOException("String too big"); 159 } else { 160 dataoutputstream.writeShort(s.length()); 161 dataoutputstream.writeChars(s); 162 } 163 } 164 165 // CraftBukkit - throws IOException 166 public static String a(DataInputStream datainputstream, int i) throws IOException { 167 short short1 = datainputstream.readShort(); 168 169 if (short1 > i) { 170 throw new IOException("Received string length longer than maximum allowed (" + short1 + " > " + i + ")"); 171 } else if (short1 < 0) { 172 throw new IOException("Received string length is less than zero! Weird string!"); 173 } else { 174 StringBuilder stringbuilder = new StringBuilder(); 175 176 for (int j = 0; j < short1; ++j) { 177 stringbuilder.append(datainputstream.readChar()); 178 } 179 180 return stringbuilder.toString(); 181 } 182 } 183 184 public abstract void a(DataInputStream datainputstream) throws IOException; // CraftBukkit 185 186 public abstract void a(DataOutputStream dataoutputstream) throws IOException; // CraftBukkit 187 188 public abstract void a(NetHandler nethandler); 189 190 public abstract int a(); 191 192 // uberbukkit - cloning assures that a packet queued for everyone is sent according to each player's PVN 193 public Packet clone() { 194 return this; 195 } 196 197 static { 198 a(0, true, true, Packet0KeepAlive.class); 199 a(1, true, true, Packet1Login.class); 200 a(2, true, true, Packet2Handshake.class); 201 a(3, true, true, Packet3Chat.class); 202 a(4, true, false, Packet4UpdateTime.class); 203 a(5, true, true, Packet5EntityEquipment.class); 204 a(6, true, false, Packet6SpawnPosition.class); 205 a(7, false, true, Packet7UseEntity.class); 206 a(8, true, false, Packet8UpdateHealth.class); 207 a(9, true, true, Packet9Respawn.class); 208 a(10, true, true, Packet10Flying.class); 209 a(11, true, true, Packet11PlayerPosition.class); 210 a(12, true, true, Packet12PlayerLook.class); 211 a(13, true, true, Packet13PlayerLookMove.class); 212 a(14, false, true, Packet14BlockDig.class); 213 a(15, false, true, Packet15Place.class); 214 a(16, false, true, Packet16BlockItemSwitch.class); 215 a(17, true, false, Packet17.class); 216 a(18, true, true, Packet18ArmAnimation.class); 217 a(19, false, true, Packet19EntityAction.class); 218 a(20, true, false, Packet20NamedEntitySpawn.class); 219 a(21, true, true, Packet21PickupSpawn.class); 220 a(22, true, false, Packet22Collect.class); 221 a(23, true, false, Packet23VehicleSpawn.class); 222 a(24, true, false, Packet24MobSpawn.class); 223 a(25, true, false, Packet25EntityPainting.class); 224 a(27, false, false, Packet27.class); // CraftBukkit - true -> false; disabled unused packet. TODO -- check if needed 225 a(28, true, false, Packet28EntityVelocity.class); 226 a(29, true, false, Packet29DestroyEntity.class); 227 a(30, true, false, Packet30Entity.class); 228 a(31, true, false, Packet31RelEntityMove.class); 229 a(32, true, false, Packet32EntityLook.class); 230 a(33, true, false, Packet33RelEntityMoveLook.class); 231 a(34, true, false, Packet34EntityTeleport.class); 232 a(38, true, false, Packet38EntityStatus.class); 233 a(39, true, false, Packet39AttachEntity.class); 234 a(40, true, false, Packet40EntityMetadata.class); 235 a(50, true, false, Packet50PreChunk.class); 236 a(51, true, false, Packet51MapChunk.class); 237 a(52, true, false, Packet52MultiBlockChange.class); 238 a(53, true, false, Packet53BlockChange.class); 239 a(54, true, false, Packet54PlayNoteBlock.class); 240 a(60, true, false, Packet60Explosion.class); 241 a(61, true, false, Packet61.class); 242 a(62, true, false, Packet62Sound.class); // uberbukkit - protocol extension 243 a(63, true, false, Packet63Digging.class); // uberbukkit - protocol extension 244 a(70, true, false, Packet70Bed.class); 245 a(71, true, false, Packet71Weather.class); 246 a(100, true, false, Packet100OpenWindow.class); 247 a(101, true, true, Packet101CloseWindow.class); 248 a(102, false, true, Packet102WindowClick.class); 249 a(103, true, false, Packet103SetSlot.class); 250 a(104, true, false, Packet104WindowItems.class); 251 a(105, true, false, Packet105CraftProgressBar.class); 252 a(106, true, true, Packet106Transaction.class); 253 a(130, true, true, Packet130UpdateSign.class); 254 a(131, true, false, Packet131.class); 255 a(200, true, false, Packet200Statistic.class); 256 a(255, true, true, Packet255KickDisconnect.class); 257 packetClassToIdMap.put(ArtificialPacket53BlockChange.class, 53); //Poseidon - Artificial Block Change Packet 258 e = new HashMap(); 259 f = 0; 260 } 261}