From ae06e527be04fc788bc6d157f5ffdf8fa42753e8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 23 May 2014 13:58:21 +1200 Subject: [PATCH] Added disable metadata packet - Aka cached --- config.yml | 8 +++++- .../disguise/DisguiseConfig.java | 9 +++++++ .../libraryaddict/disguise/LibsDisguises.java | 1 + .../disguise/disguisetypes/FlagWatcher.java | 26 +++++++++++++++++++ .../disguise/utilities/PacketsManager.java | 26 ++++++++++++------- 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/config.yml b/config.yml index 3219c0cc..cb30366a 100644 --- a/config.yml +++ b/config.yml @@ -86,9 +86,15 @@ PacketsEnabled: # Entity enquipment is the packets that are sent to ensure that a disguise has or doesn't have armor, and their held item. # Disabling this means that any disguises which can wear armor or hold items will show the armor/held item that the disguised is wearing. Enquipment: true + # This doesn't actually disable the packet. It would introduce problems. Instead it does the next best thing and caches the data. + # This means that entity metadata will not change, and will only be sent in the spawn packet. + # This is good if performance is extremely in need. + # This is bad to disable unless you are ONLY going to use the disguises for decorations. + # To be honest. This is basically "Disable entity animations". That option is called 'AddEntityAnimations' in the config but unlike that, this is always in effect. + Metadata: true # Movement packets are the biggest cpu hit. These are majorly used to ensure that the disguises facing direction isn't bugged up Movement: true # Disable this if you don't mind crashing everytime you see someone riding something disguised as a non-living entity Riding: true # When disguised as a wither skull, it sends a look packet every tick so that the wither skull is facing the right way. - WitherSkull: true + WitherSkull: true \ No newline at end of file diff --git a/src/me/libraryaddict/disguise/DisguiseConfig.java b/src/me/libraryaddict/disguise/DisguiseConfig.java index aa84deea..82760737 100644 --- a/src/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/me/libraryaddict/disguise/DisguiseConfig.java @@ -22,6 +22,7 @@ public class DisguiseConfig { private static boolean movementEnabled; private static boolean removeUnseenDisguises; private static boolean ridingEnabled; + private static boolean sendsEntityMetadata; private static boolean sendVelocity; private static boolean showNameAboveHead; private static boolean showNameAboveHeadAlwaysVisible; @@ -86,6 +87,10 @@ public class DisguiseConfig { return keepDisguisePlayerLogout; } + public static boolean isMetadataPacketsEnabled() { + return sendsEntityMetadata; + } + public static boolean isMiscDisguisesForLivingEnabled() { return miscDisguisesForLivingEnabled; } @@ -235,6 +240,10 @@ public class DisguiseConfig { keepDisguisePlayerLogout = keepDisguise; } + public static void setMetadataPacketsEnabled(boolean enabled) { + sendsEntityMetadata = enabled; + } + public static void setMiscDisguisesForLivingEnabled(boolean enabled) { if (enabled != isMiscDisguisesForLivingEnabled()) { miscDisguisesForLivingEnabled = enabled; diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 1a34e41c..075fcb33 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -106,6 +106,7 @@ public class LibsDisguises extends JavaPlugin { DisguiseConfig.setRidingPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Riding")); DisguiseConfig.setEntityStatusPacketsEnabled(getConfig().getBoolean("PacketsEnabled.EntityStatus")); DisguiseConfig.setCollectPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Collect")); + DisguiseConfig.setMetadataPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Metadata")); try { // Here I use reflection to set the plugin for Disguise.. // Kind of stupid but I don't want open API calls for a commonly used object. diff --git a/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index bea4e746..adce204c 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -48,6 +48,7 @@ public class FlagWatcher { private boolean hasDied; private org.bukkit.inventory.ItemStack[] items = new org.bukkit.inventory.ItemStack[5]; private HashSet modifiedEntityAnimations = new HashSet(); + private List watchableObjects; public FlagWatcher(Disguise disguise) { this.disguise = (TargetedDisguise) disguise; @@ -179,6 +180,13 @@ public class FlagWatcher { return backup; } + public List getWatchableObjects() { + if (watchableObjects == null) { + rebuildWatchableObjects(); + } + return watchableObjects; + } + protected boolean hasValue(int no) { return entityValues.containsKey(no); } @@ -207,6 +215,21 @@ public class FlagWatcher { return getFlag(3); } + public void rebuildWatchableObjects() { + watchableObjects = new ArrayList(); + for (int i = 0; i <= 31; i++) { + WrappedWatchableObject watchable = null; + if (this.entityValues.containsKey(i) && this.entityValues.get(i) != null) { + watchable = new WrappedWatchableObject(i, entityValues.get(i)); + } else if (this.backupEntityValues.containsKey(i) && this.backupEntityValues.get(i) != null) { + watchable = new WrappedWatchableObject(i, backupEntityValues.get(i)); + } + if (watchable != null) { + watchableObjects.add(watchable); + } + } + } + protected void sendData(int data) { if (getDisguise().getWatcher() == null || !DisguiseAPI.isDisguiseInUse(getDisguise())) return; @@ -330,6 +353,9 @@ public class FlagWatcher { protected void setValue(int no, Object value) { entityValues.put(no, value); + if (!DisguiseConfig.isMetadataPacketsEnabled()) { + this.rebuildWatchableObjects(); + } } } diff --git a/src/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/me/libraryaddict/disguise/utilities/PacketsManager.java index 963e6dc3..9ba52d19 100644 --- a/src/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -52,6 +52,7 @@ public class PacketsManager { * "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard" */ private static boolean cancelSound; + private static PacketListener clientInteractEntityListener; private static PacketListener inventoryListenerClient; private static PacketListener inventoryListenerServer; private static boolean inventoryModifierEnabled; @@ -59,7 +60,6 @@ public class PacketsManager { private static PacketListener mainListener; private static PacketListener soundsListener; private static boolean soundsListenerEnabled; - private static PacketListener clientInteractEntityListener; private static PacketListener viewDisguisesListener; private static boolean viewDisguisesListenerEnabled; @@ -304,8 +304,9 @@ public class PacketsManager { private static WrappedDataWatcher createDataWatcher(WrappedDataWatcher watcher, FlagWatcher flagWatcher) { WrappedDataWatcher newWatcher = new WrappedDataWatcher(); try { - List list = watcher.getWatchableObjects(); - for (WrappedWatchableObject watchableObject : flagWatcher.convert(list)) { + List list = DisguiseConfig.isMetadataPacketsEnabled() ? flagWatcher.convert(watcher + .getWatchableObjects()) : flagWatcher.getWatchableObjects(); + for (WrappedWatchableObject watchableObject : list) { newWatcher.setObject(watchableObject.getIndex(), watchableObject.getValue()); } } catch (Exception ex) { @@ -1186,12 +1187,16 @@ public class PacketsManager { // Else if the packet is sending entity metadata else if (sentPacket.getType() == PacketType.Play.Server.ENTITY_METADATA) { - List watchableObjects = disguise.getWatcher().convert( - packets[0].getWatchableCollectionModifier().read(0)); - packets[0] = new PacketContainer(sentPacket.getType()); - StructureModifier newMods = packets[0].getModifier(); - newMods.write(0, entity.getEntityId()); - packets[0].getWatchableCollectionModifier().write(0, watchableObjects); + if (DisguiseConfig.isMetadataPacketsEnabled()) { + List watchableObjects = disguise.getWatcher().convert( + packets[0].getWatchableCollectionModifier().read(0)); + packets[0] = new PacketContainer(sentPacket.getType()); + StructureModifier newMods = packets[0].getModifier(); + newMods.write(0, entity.getEntityId()); + packets[0].getWatchableCollectionModifier().write(0, watchableObjects); + } else { + packets = new PacketContainer[0]; + } } // Else if the packet is spawning.. @@ -1269,9 +1274,10 @@ public class PacketsManager { for (WrappedWatchableObject value : dataWatcher.getWatchableObjects()) { if (value.getIndex() == 0) { list.add(value); + break; } } - list = disguise.getWatcher().convert(list); + list = DisguiseConfig.isMetadataPacketsEnabled() ? disguise.getWatcher().convert(list) : list; // Construct the packets to return PacketContainer packetBlock = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); packetBlock.getModifier().write(0, entity.getEntityId());