From afe18379e0915cb32596d773992d52014ad4640d Mon Sep 17 00:00:00 2001 From: HugoDaBosss Date: Fri, 11 Mar 2016 11:48:42 +0100 Subject: [PATCH] Optimize packet searching --- .../myles/ViaVersion/packets/PacketType.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/packets/PacketType.java b/src/main/java/us/myles/ViaVersion/packets/PacketType.java index a9122df8d..1291a508b 100644 --- a/src/main/java/us/myles/ViaVersion/packets/PacketType.java +++ b/src/main/java/us/myles/ViaVersion/packets/PacketType.java @@ -172,11 +172,11 @@ public enum PacketType { } public static PacketType findNewPacket(State state, Direction direction, int id) { - return newids.get(toLong((short)direction.ordinal(), (short) state.ordinal(), id)); + return newids.get(toShort((short) id, (short)direction.ordinal(), (short) state.ordinal())); } public static PacketType findOldPacket(State state, Direction direction, int id) { - return oldids.get(toLong((short)direction.ordinal(), (short) state.ordinal(), id)); + return oldids.get(toShort((short) id, (short)direction.ordinal(), (short) state.ordinal())); } public static PacketType getIncomingPacket(State state, int id) { @@ -187,17 +187,16 @@ public enum PacketType { return findOldPacket(state, Direction.OUTGOING, id); } - private static long toLong(short a, short b, int c) { - int d = (a << 16) | (b & 0xFFFF); - return (long) c << 32 | d & 0xFFFFFFFFL; + private static short toShort(short id, short direction, short state) { + return (short) ((id & 0x00FF) | (direction<<8) & 0x0F00 | (state << 12) & 0xF000); } - - private static HashMap oldids = new HashMap(); - private static HashMap newids = new HashMap(); + + private static HashMap oldids = new HashMap(); + private static HashMap newids = new HashMap(); static { for(PacketType pt : PacketType.values()) { - oldids.put(toLong((short)pt.getDirection().ordinal(), (short)pt.getState().ordinal(), pt.getPacketID()), pt); - newids.put(toLong((short)pt.getDirection().ordinal(), (short)pt.getState().ordinal(), pt.getNewPacketID()), pt); + oldids.put(toShort((short) pt.getPacketID(), (short)pt.getDirection().ordinal(), (short)pt.getState().ordinal()), pt); + newids.put(toShort((short) pt.getNewPacketID(), (short)pt.getDirection().ordinal(), (short)pt.getState().ordinal()), pt); } }