diff --git a/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index 2f0dba7cc..3b7362d72 100644 --- a/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -198,6 +198,7 @@ public class PacketWrapper { /** * Send this packet to the associated user. * Be careful not to send packets twice. + * (Sends it after current) * * @throws Exception if it fails to write */ diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java index 7aecc9032..abcce506b 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java @@ -18,6 +18,8 @@ public class ItemRewriter { private static final Map POTION_NAME_TO_ID = new HashMap<>(); private static final Map POTION_ID_TO_NAME = new HashMap<>(); + private static final Map POTION_INDEX = new HashMap<>(); + static { /* Entities */ registerEntity(1, "Item"); @@ -84,6 +86,7 @@ public class ItemRewriter { registerEntity(200, "EnderCrystal"); /* Potions */ + registerPotion(-1, "empty"); registerPotion(0, "water"); registerPotion(64, "mundane"); registerPotion(32, "thick"); @@ -242,12 +245,20 @@ public class ItemRewriter { } } + public static int getNewEffectID(int oldID) { + if (oldID >= 16384) { + oldID -= 8192; + } + return POTION_INDEX.containsKey(oldID) ? POTION_INDEX.get(oldID) : 0; + } + private static void registerEntity(Integer id, String name) { ENTTIY_ID_TO_NAME.put(id, name); ENTTIY_NAME_TO_ID.put(name, id); } private static void registerPotion(Integer id, String name) { + POTION_INDEX.put(id, POTION_ID_TO_NAME.size()); POTION_ID_TO_NAME.put(id, name); POTION_NAME_TO_ID.put(name, id); } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index 5261423e9..04887c96a 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -1,5 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.packets; +import org.bukkit.Material; import org.bukkit.entity.EntityType; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -11,11 +12,14 @@ import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.NewType; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; import us.myles.ViaVersion.util.EntityUtil; +import java.util.ArrayList; import java.util.List; public class SpawnPackets { @@ -81,6 +85,34 @@ public class SpawnPackets { wrapper.write(Type.SHORT, vZ); } }); + + // Handle potions + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + final int entityID = wrapper.get(Type.VAR_INT, 0); + final int data = wrapper.get(Type.INT, 0); // Data + + int typeID = wrapper.get(Type.BYTE, 0); + if (EntityUtil.getTypeFromID(typeID, true) == EntityType.SPLASH_POTION) { + // Convert this to meta data, woo! + PacketWrapper metaPacket = wrapper.create(0x39, new ValueCreator() { + @Override + public void write(PacketWrapper wrapper) throws Exception { + wrapper.write(Type.VAR_INT, entityID); + List meta = new ArrayList<>(); + Item item = new Item((short) Material.POTION.getId(), (byte) 1, (short) data, null); + ItemRewriter.toClient(item); // Rewrite so that it gets the right nbt + + Metadata potion = new Metadata(5, NewType.Slot.getTypeID(), Type.ITEM, item); + meta.add(potion); + wrapper.write(Protocol1_9TO1_8.METADATA_LIST, meta); + } + }); + metaPacket.send(); + } + } + }); } }); diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index d8463f977..8c6a0523f 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -15,6 +15,7 @@ import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ArmorType; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.sounds.SoundEffect; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; @@ -40,7 +41,9 @@ public class WorldPackets { @Override public void registerMap() { map(Type.INT); // 0 - Effect ID - // Everything else get's written through + map(Type.POSITION); // 1 - Position + map(Type.INT); // 2 - Data + map(Type.BOOLEAN); // 3 - Disable relative volume handler(new PacketHandler() { @Override @@ -55,6 +58,18 @@ public class WorldPackets { wrapper.set(Type.INT, 0, id); } }); + // Rewrite potion effect as it changed to use a dynamic registry + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int id = wrapper.get(Type.INT, 0); + if (id == 2002) { + int data = wrapper.get(Type.INT, 1); + int newData = ItemRewriter.getNewEffectID(data); + wrapper.set(Type.INT, 1, newData); + } + } + }); } });