From c2edf4584622cdebf285c17f8bfbe678ed48e04a Mon Sep 17 00:00:00 2001 From: Myles Date: Thu, 3 Mar 2016 21:06:36 +0000 Subject: [PATCH] Change package for slot rewriter. --- .../ViaVersion/metadata/MetadataRewriter.java | 4 +- .../{api => }/slot/ItemSlotRewriter.java | 382 +++++++++--------- .../transformers/IncomingTransformer.java | 7 +- .../transformers/OutgoingTransformer.java | 2 +- 4 files changed, 197 insertions(+), 198 deletions(-) rename src/main/java/us/myles/ViaVersion/{api => }/slot/ItemSlotRewriter.java (96%) diff --git a/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java b/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java index a983a41d3..f034bdb16 100644 --- a/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java +++ b/src/main/java/us/myles/ViaVersion/metadata/MetadataRewriter.java @@ -11,8 +11,8 @@ import org.bukkit.util.Vector; import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.slot.ItemSlotRewriter; -import us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack; +import us.myles.ViaVersion.slot.ItemSlotRewriter; +import us.myles.ViaVersion.slot.ItemSlotRewriter.ItemStack; import us.myles.ViaVersion.util.PacketUtil; public class MetadataRewriter { diff --git a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java b/src/main/java/us/myles/ViaVersion/slot/ItemSlotRewriter.java similarity index 96% rename from src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java rename to src/main/java/us/myles/ViaVersion/slot/ItemSlotRewriter.java index fa126972c..0861d9656 100644 --- a/src/main/java/us/myles/ViaVersion/api/slot/ItemSlotRewriter.java +++ b/src/main/java/us/myles/ViaVersion/slot/ItemSlotRewriter.java @@ -1,191 +1,191 @@ -package us.myles.ViaVersion.api.slot; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import org.bukkit.Material; -import org.spacehq.opennbt.tag.builtin.CompoundTag; -import org.spacehq.opennbt.tag.builtin.StringTag; - -import io.netty.buffer.ByteBuf; - -import us.myles.ViaVersion.CancelException; -import us.myles.ViaVersion.util.PacketUtil; - -public class ItemSlotRewriter { - - public static void rewrite1_9To1_8(ByteBuf input, ByteBuf output) throws CancelException { - try { - ItemStack item = readItemStack(input); - fixIdsFrom1_9To1_8(item); - writeItemStack(item, output); - } catch (Exception e) { - System.out.println("Error while rewriting an item slot."); - e.printStackTrace(); - throw new CancelException(); - } - } - - public static void rewrite1_8To1_9(ByteBuf input, ByteBuf output) throws CancelException { - try { - ItemStack item = readItemStack(input); - fixIdsFrom1_8To1_9(item); - writeItemStack(item, output); - } catch (Exception e) { - System.out.println("Error while rewriting an item slot."); - e.printStackTrace(); - throw new CancelException(); - } - } - - public static void fixIdsFrom1_9To1_8(ItemStack item) { - if (item != null) { - if (item.id == Material.MONSTER_EGG.getId() && item.data == 0) { - CompoundTag tag = item.tag; - int data = 0; - if (tag != null && tag.get("EntityTag") instanceof CompoundTag) { - CompoundTag entityTag = tag.get("EntityTag"); - if (entityTag.get("id") instanceof StringTag) { - StringTag id = entityTag.get("id"); - data = ENTTIY_NAME_TO_ID.get(id.getValue()); - } - } - item.tag = null; - item.data = (short) data; - } - } - } - - public static void fixIdsFrom1_8To1_9(ItemStack item) { - if (item != null) { - if (item.id == Material.MONSTER_EGG.getId() && item.data != 0) { - CompoundTag tag = item.tag; - if (tag == null) { - tag = new CompoundTag("tag"); - } - CompoundTag entityTag = new CompoundTag("EntityTag"); - StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get(Integer.valueOf(item.data))); - entityTag.put(id); - tag.put(entityTag); - item.tag = tag; - item.data = 0; - } - } - } - - public static ItemStack readItemStack(ByteBuf input) throws IOException { - short id = input.readShort(); - if (id < 0) { - return null; - } else { - ItemStack item = new ItemStack(); - item.id = id; - item.amount = input.readByte(); - item.data = input.readShort(); - item.tag = PacketUtil.readNBT(input); - return item; - } - } - - public static void writeItemStack(ItemStack item, ByteBuf output) throws IOException { - if (item == null) { - output.writeShort(-1); - } else { - output.writeShort(item.id); - output.writeByte(item.amount); - output.writeShort(item.data); - PacketUtil.writeNBT(output, item.tag); - } - } - - public static class ItemStack { - - private short id; - private byte amount; - private short data; - private CompoundTag tag; - - public static ItemStack fromBukkit(org.bukkit.inventory.ItemStack stack) { - ItemStack item = new ItemStack(); - item.id = (short) stack.getTypeId(); - item.amount = (byte) stack.getAmount(); - item.data = stack.getData().getData(); - // TODO: nbt - return item; - } - } - - private static Map ENTTIY_NAME_TO_ID = new HashMap<>(); - private static Map ENTTIY_ID_TO_NAME = new HashMap<>(); - - static { - register(1, "Item"); - register(2, "XPOrb"); - register(7, "ThrownEgg"); - register(8, "LeashKnot"); - register(9, "Painting"); - register(10, "Arrow"); - register(11, "Snowball"); - register(12, "Fireball"); - register(13, "SmallFireball"); - register(14, "ThrownEnderpearl"); - register(15, "EyeOfEnderSignal"); - register(16, "ThrownPotion"); - register(17, "ThrownExpBottle"); - register(18, "ItemFrame"); - register(19, "WitherSkull"); - register(20, "PrimedTnt"); - register(21, "FallingSand"); - register(22, "FireworksRocketEntity"); - register(30, "ArmorStand"); - register(40, "MinecartCommandBlock"); - register(41, "Boat"); - register(42, "MinecartRideable"); - register(43, "MinecartChest"); - register(44, "MinecartFurnace"); - register(45, "MinecartTNT"); - register(46, "MinecartHopper"); - register(47, "MinecartSpawner"); - register(48, "Mob"); - register(49, "Monster"); - register(50, "Creeper"); - register(51, "Skeleton"); - register(52, "Spider"); - register(53, "Giant"); - register(54, "Zombie"); - register(55, "Slime"); - register(56, "Ghast"); - register(57, "PigZombie"); - register(58, "Enderman"); - register(59, "CaveSpider"); - register(60, "Silverfish"); - register(61, "Blaze"); - register(62, "LavaSlime"); - register(63, "EnderDragon"); - register(64, "WitherBoss"); - register(65, "Bat"); - register(66, "Witch"); - register(67, "Endermite"); - register(68, "Guardian"); - register(90, "Pig"); - register(91, "Sheep"); - register(92, "Cow"); - register(93, "Chicken"); - register(94, "Squid"); - register(95, "Wolf"); - register(96, "MushroomCow"); - register(97, "SnowMan"); - register(98, "Ozelot"); - register(99, "VillagerGolem"); - register(100, "EntityHorse"); - register(101, "Rabbit"); - register(120, "Villager"); - register(200, "EnderCrystal"); - } - - private static void register(Integer id, String name) { - ENTTIY_ID_TO_NAME.put(id, name); - ENTTIY_NAME_TO_ID.put(name, id); - } -} +package us.myles.ViaVersion.slot; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Material; +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import org.spacehq.opennbt.tag.builtin.StringTag; + +import io.netty.buffer.ByteBuf; + +import us.myles.ViaVersion.CancelException; +import us.myles.ViaVersion.util.PacketUtil; + +public class ItemSlotRewriter { + + public static void rewrite1_9To1_8(ByteBuf input, ByteBuf output) throws CancelException { + try { + ItemStack item = readItemStack(input); + fixIdsFrom1_9To1_8(item); + writeItemStack(item, output); + } catch (Exception e) { + System.out.println("Error while rewriting an item slot."); + e.printStackTrace(); + throw new CancelException(); + } + } + + public static void rewrite1_8To1_9(ByteBuf input, ByteBuf output) throws CancelException { + try { + ItemStack item = readItemStack(input); + fixIdsFrom1_8To1_9(item); + writeItemStack(item, output); + } catch (Exception e) { + System.out.println("Error while rewriting an item slot."); + e.printStackTrace(); + throw new CancelException(); + } + } + + public static void fixIdsFrom1_9To1_8(ItemStack item) { + if (item != null) { + if (item.id == Material.MONSTER_EGG.getId() && item.data == 0) { + CompoundTag tag = item.tag; + int data = 0; + if (tag != null && tag.get("EntityTag") instanceof CompoundTag) { + CompoundTag entityTag = tag.get("EntityTag"); + if (entityTag.get("id") instanceof StringTag) { + StringTag id = entityTag.get("id"); + data = ENTTIY_NAME_TO_ID.get(id.getValue()); + } + } + item.tag = null; + item.data = (short) data; + } + } + } + + public static void fixIdsFrom1_8To1_9(ItemStack item) { + if (item != null) { + if (item.id == Material.MONSTER_EGG.getId() && item.data != 0) { + CompoundTag tag = item.tag; + if (tag == null) { + tag = new CompoundTag("tag"); + } + CompoundTag entityTag = new CompoundTag("EntityTag"); + StringTag id = new StringTag("id", ENTTIY_ID_TO_NAME.get(Integer.valueOf(item.data))); + entityTag.put(id); + tag.put(entityTag); + item.tag = tag; + item.data = 0; + } + } + } + + public static ItemStack readItemStack(ByteBuf input) throws IOException { + short id = input.readShort(); + if (id < 0) { + return null; + } else { + ItemStack item = new ItemStack(); + item.id = id; + item.amount = input.readByte(); + item.data = input.readShort(); + item.tag = PacketUtil.readNBT(input); + return item; + } + } + + public static void writeItemStack(ItemStack item, ByteBuf output) throws IOException { + if (item == null) { + output.writeShort(-1); + } else { + output.writeShort(item.id); + output.writeByte(item.amount); + output.writeShort(item.data); + PacketUtil.writeNBT(output, item.tag); + } + } + + public static class ItemStack { + + private short id; + private byte amount; + private short data; + private CompoundTag tag; + + public static ItemStack fromBukkit(org.bukkit.inventory.ItemStack stack) { + ItemStack item = new ItemStack(); + item.id = (short) stack.getTypeId(); + item.amount = (byte) stack.getAmount(); + item.data = stack.getData().getData(); + // TODO: nbt + return item; + } + } + + private static Map ENTTIY_NAME_TO_ID = new HashMap<>(); + private static Map ENTTIY_ID_TO_NAME = new HashMap<>(); + + static { + register(1, "Item"); + register(2, "XPOrb"); + register(7, "ThrownEgg"); + register(8, "LeashKnot"); + register(9, "Painting"); + register(10, "Arrow"); + register(11, "Snowball"); + register(12, "Fireball"); + register(13, "SmallFireball"); + register(14, "ThrownEnderpearl"); + register(15, "EyeOfEnderSignal"); + register(16, "ThrownPotion"); + register(17, "ThrownExpBottle"); + register(18, "ItemFrame"); + register(19, "WitherSkull"); + register(20, "PrimedTnt"); + register(21, "FallingSand"); + register(22, "FireworksRocketEntity"); + register(30, "ArmorStand"); + register(40, "MinecartCommandBlock"); + register(41, "Boat"); + register(42, "MinecartRideable"); + register(43, "MinecartChest"); + register(44, "MinecartFurnace"); + register(45, "MinecartTNT"); + register(46, "MinecartHopper"); + register(47, "MinecartSpawner"); + register(48, "Mob"); + register(49, "Monster"); + register(50, "Creeper"); + register(51, "Skeleton"); + register(52, "Spider"); + register(53, "Giant"); + register(54, "Zombie"); + register(55, "Slime"); + register(56, "Ghast"); + register(57, "PigZombie"); + register(58, "Enderman"); + register(59, "CaveSpider"); + register(60, "Silverfish"); + register(61, "Blaze"); + register(62, "LavaSlime"); + register(63, "EnderDragon"); + register(64, "WitherBoss"); + register(65, "Bat"); + register(66, "Witch"); + register(67, "Endermite"); + register(68, "Guardian"); + register(90, "Pig"); + register(91, "Sheep"); + register(92, "Cow"); + register(93, "Chicken"); + register(94, "Squid"); + register(95, "Wolf"); + register(96, "MushroomCow"); + register(97, "SnowMan"); + register(98, "Ozelot"); + register(99, "VillagerGolem"); + register(100, "EntityHorse"); + register(101, "Rabbit"); + register(120, "Villager"); + register(200, "EnderCrystal"); + } + + private static void register(Integer id, String name) { + ENTTIY_ID_TO_NAME.put(id, name); + ENTTIY_NAME_TO_ID.put(name, id); + } +} diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index bbf0d950a..52179f694 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -5,7 +5,7 @@ import org.bukkit.inventory.ItemStack; import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.ConnectionInfo; import us.myles.ViaVersion.ViaVersionPlugin; -import us.myles.ViaVersion.api.slot.ItemSlotRewriter; +import us.myles.ViaVersion.slot.ItemSlotRewriter; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.util.PacketUtil; @@ -13,7 +13,6 @@ import us.myles.ViaVersion.util.ReflectionUtil; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; public class IncomingTransformer { private final ConnectionInfo info; @@ -181,7 +180,7 @@ public class IncomingTransformer { ItemStack inHand = ViaVersionPlugin.getHandItem(info); try { - us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand); + ItemSlotRewriter.ItemStack item = ItemSlotRewriter.ItemStack.fromBukkit(inHand); ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.writeItemStack(item, output); } catch (Exception e) { @@ -205,7 +204,7 @@ public class IncomingTransformer { // write item in hand ItemStack inHand = ViaVersionPlugin.getHandItem(info); try { - us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.api.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand); + us.myles.ViaVersion.slot.ItemSlotRewriter.ItemStack item = us.myles.ViaVersion.slot.ItemSlotRewriter.ItemStack.fromBukkit(inHand); ItemSlotRewriter.fixIdsFrom1_9To1_8(item); ItemSlotRewriter.writeItemStack(item, output); } catch (Exception e) { diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index 2a7db72c0..38cec131f 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -11,7 +11,7 @@ import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.ConnectionInfo; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.ViaVersion; -import us.myles.ViaVersion.api.slot.ItemSlotRewriter; +import us.myles.ViaVersion.slot.ItemSlotRewriter; import us.myles.ViaVersion.metadata.MetadataRewriter; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State;