From 1576c895b5a4f88a0dd48eb6b579943c39a3e18c Mon Sep 17 00:00:00 2001 From: NavidK0 Date: Fri, 4 Mar 2016 22:52:52 -0500 Subject: [PATCH] Switch to ProtocolLib MinecraftKeys wrapper Add in helper method to quickly create DataWatcher.Item objects Typo in last commit, meant to say, disguises will NOT show up. Sorry to those who got excited. :( --- .../disguise/disguisetypes/FlagWatcher.java | 26 ++++++-------- .../disguise/utilities/DisguiseSound.java | 7 ++-- .../disguise/utilities/DisguiseUtilities.java | 36 ------------------- .../disguise/utilities/PacketsManager.java | 12 +++---- .../disguise/utilities/ReflectionManager.java | 26 +++++++++++++- 5 files changed, 42 insertions(+), 65 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index ad19a785..81e120d4 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -109,15 +109,13 @@ public class FlagWatcher { value = this.addEntityAnimations((byte) value, (byte) watch.getValue()); } boolean isDirty = watch.getDirtyState(); - watch = new WrappedWatchableObject(id); - watch.setValue(value); + watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(id, value)); if (!isDirty) { watch.setDirtyState(false); } } else { boolean isDirty = watch.getDirtyState(); - watch = new WrappedWatchableObject(id); - watch.setValue(watch.getValue()); + watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(id, watch.getValue())); if (!isDirty) { watch.setDirtyState(false); } @@ -126,16 +124,15 @@ public class FlagWatcher { } if (sendAllCustom) { // Its sending the entire meta data. Better add the custom meta - for (int value : entityValues.keySet()) { - if (sentValues.contains(value)) { + for (int id : entityValues.keySet()) { + if (sentValues.contains(id)) { continue; } - Object obj = entityValues.get(value); - if (obj == null) { + Object value = entityValues.get(id); + if (value == null) { continue; } - WrappedWatchableObject watch = new WrappedWatchableObject(value); - watch.setValue(obj); + WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(id, value)); newList.add(watch); } } @@ -246,11 +243,9 @@ public class FlagWatcher { for (int i = 0; i <= 31; i++) { WrappedWatchableObject watchable = null; if (this.entityValues.containsKey(i) && this.entityValues.get(i) != null) { - watchable = new WrappedWatchableObject(i); - watchable.setValue(entityValues.get(i)); + watchable = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(i, entityValues.get(i))); } else if (this.backupEntityValues.containsKey(i) && this.backupEntityValues.get(i) != null) { - watchable = new WrappedWatchableObject(i); - watchable.setValue(backupEntityValues.get(i)); + watchable = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(i, entityValues.get(i))); } if (watchable != null) { watchableObjects.add(watchable); @@ -271,8 +266,7 @@ public class FlagWatcher { if (isEntityAnimationsAdded() && DisguiseConfig.isMetadataPacketsEnabled() && data == 0) { value = addEntityAnimations((byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0)); } - WrappedWatchableObject watch = new WrappedWatchableObject(data); - watch.setValue(value); + WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(data, value)); list.add(watch); } if (!list.isEmpty()) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java index c3af2622..2114c20e 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java @@ -1,5 +1,6 @@ package me.libraryaddict.disguise.utilities; +import com.comphenix.protocol.wrappers.MinecraftKey; import org.bukkit.Sound; import java.lang.reflect.Field; @@ -132,10 +133,8 @@ public enum DisguiseSound { try { Field f_getMinecraftKey = ReflectionManager.getNmsField("SoundEffect", "b"); f_getMinecraftKey.setAccessible(true); - Object minecraftKey = f_getMinecraftKey.get(soundEffect); - Field f_getValue = ReflectionManager.getNmsField("MinecraftKey", "a"); - String sound = (String) f_getValue.get(soundEffect); //Our prize! - return sound; + MinecraftKey key = MinecraftKey.fromHandle(f_getMinecraftKey.get(soundEffect)); + return key.getKey(); } catch (IllegalAccessException e) { e.printStackTrace(); } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 0b257d2a..6c582cb1 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -7,13 +7,11 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedGameProfile; -import com.comphenix.protocol.wrappers.WrappedWatchableObject; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.DisguiseType; -import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; @@ -593,40 +591,6 @@ public class DisguiseUtilities { return ReflectionManager.getSkullBlob(ReflectionManager.grabProfileAddUUID(playerName)); } - /** - * Please note that in the future when 'DualInt' and the like are removed. This should break.. However, that should be negated in the future as I'd be able to set the watcher index's as per the spigot version. Instead of checking on the player's version every single packet.. - */ - public static List rebuildForVersion(Player player, FlagWatcher watcher, - List list) { - if (true) // Use for future protocol compatibility - { - return list; - } - ArrayList rebuiltList = new ArrayList<>(); - ArrayList backups = new ArrayList<>(); - for (WrappedWatchableObject obj : list) { - if (obj.getValue().getClass().getName().startsWith("org.")) { - backups.add(obj); - continue; - } - switch (obj.getIndex()) { - // TODO: Future version support - } - } - Iterator itel = backups.iterator(); - while (itel.hasNext()) { - int index = itel.next().getIndex(); - for (WrappedWatchableObject obj2 : rebuiltList) { - if (index == obj2.getIndex()) { - itel.remove(); - break; - } - } - } - rebuiltList.addAll(backups); - return rebuiltList; - } - /** * Resends the entity to this specific player */ diff --git a/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java index 4b126e17..38d8a76c 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -364,7 +364,6 @@ public class PacketsManager { try { List list = DisguiseConfig.isMetadataPacketsEnabled() ? flagWatcher.convert(watcher .getWatchableObjects()) : flagWatcher.getWatchableObjects(); - list = DisguiseUtilities.rebuildForVersion(player, flagWatcher, list); for (WrappedWatchableObject watchableObject : list) { newWatcher.setObject(watchableObject.getIndex(), watchableObject.getValue()); } @@ -818,11 +817,10 @@ public class PacketsManager { StructureModifier mods = packet.getModifier(); mods.write(0, observer.getEntityId()); List watchableList = new ArrayList<>(); - byte b = (byte) 1 << 5; + Byte b = 1 << 5; if (observer.isSprinting()) b = (byte) (b | 1 << 3); - WrappedWatchableObject watch = new WrappedWatchableObject(0); - watch.setValue(b); + WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, b)); watchableList.add(watch); packet.getWatchableCollectionModifier().write(0, watchableList); try { @@ -1313,7 +1311,6 @@ public class PacketsManager { if (DisguiseConfig.isMetadataPacketsEnabled()) { List watchableObjects = disguise.getWatcher().convert( packets[0].getWatchableCollectionModifier().read(0)); - watchableObjects = DisguiseUtilities.rebuildForVersion(observer, disguise.getWatcher(), watchableObjects); packets[0] = new PacketContainer(sentPacket.getType()); StructureModifier newMods = packets[0].getModifier(); newMods.write(0, entity.getEntityId()); @@ -1422,8 +1419,8 @@ public class PacketsManager { // Convert the datawatcher List list = new ArrayList<>(); if (DisguiseConfig.isMetadataPacketsEnabled()) { - WrappedWatchableObject watch = new WrappedWatchableObject(0); - watch.setValue(WrappedDataWatcher.getEntityWatcher(entity).getByte(0)); + WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, + WrappedDataWatcher.getEntityWatcher(entity).getByte(0))); list.add(watch); list = disguise.getWatcher().convert(list); } else { @@ -1434,7 +1431,6 @@ public class PacketsManager { } } } - list = DisguiseUtilities.rebuildForVersion(observer, disguise.getWatcher(), list); // Construct the packets to return PacketContainer packetBlock = new PacketContainer(Server.ENTITY_METADATA); packetBlock.getModifier().write(0, entity.getEntityId()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java index 8e057ec7..3fc83573 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -1,5 +1,8 @@ package me.libraryaddict.disguise.utilities; +import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; +import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; +import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.google.common.collect.ImmutableMap; import org.bukkit.Art; @@ -14,6 +17,7 @@ import org.bukkit.potion.PotionEffect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Map; @@ -457,9 +461,10 @@ public class ReflectionManager { } public static void removePlayer(Player player) { - + //Some future remove code if needed } + public static void setAllowSleep(Player player) { try { //TODO: Fix this! @@ -471,6 +476,25 @@ public class ReflectionManager { } } + /** + * This creates a DataWatcherItem usable with WrappedWatchableObject + * @param id + * @param value + * @return + */ + public static Object createDataWatcherItem(int id, Object value) { + if (value == null) return null; + Serializer serializer = Registry.get(value.getClass()); + WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer); + Constructor construct = ReflectionManager.getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class); + try { + return construct.newInstance(watcherObject.getHandle(), value); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) { try { Location loc = entity.getLocation();