From 6e5241aec3aff925cfececbd36103f3876ebea80 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 5 Dec 2013 21:05:58 +1300 Subject: [PATCH] Add new config option to remove unseen disguises for targeted disguises. --- config.yml | 6 +++- .../libraryaddict/disguise/DisguiseAPI.java | 9 ++++++ .../disguise/DisguiseListener.java | 11 +++++++ .../libraryaddict/disguise/LibsDisguises.java | 1 + .../disguise/utilities/DisguiseUtilities.java | 31 ++++++++++++++++--- .../disguise/utilities/ReflectionManager.java | 20 ++++++------ 6 files changed, 63 insertions(+), 15 deletions(-) diff --git a/config.yml b/config.yml index c801f202..32293f0d 100644 --- a/config.yml +++ b/config.yml @@ -27,4 +27,8 @@ RemoveHeldItem: true # If you set a disguise to burning, it will no longer be able to be shown as sneaking or invisible. # Set this to true if you want the disguise to get the animations of the disguised entity. Such as invisible, on fire, sprinting, sneaking, blocking # This is only valid if you set a animation on the disguise itself. Because the entitys animations are applied otherwise. -AddEntityAnimations: true \ No newline at end of file +AddEntityAnimations: true +# If all players who can see a targeted disguise (Only advalible to plugins) have quit. +# Does the plugin remove that disguise from valid disguises? If your plugin handles this. Then thats good. +# Else its a memory leak. This loops through all disguise to see if anyone else is online who can see that disguise. +RemoveUnusedDisguises: true \ No newline at end of file diff --git a/src/me/libraryaddict/disguise/DisguiseAPI.java b/src/me/libraryaddict/disguise/DisguiseAPI.java index 1bd1aa5b..34637388 100644 --- a/src/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/me/libraryaddict/disguise/DisguiseAPI.java @@ -22,6 +22,7 @@ public class DisguiseAPI { private static boolean hidingArmor; private static boolean hidingHeldItem; private static boolean isEntityAnimationsAdded; + private static boolean removeUnseenDisguises; private static boolean sendVelocity; @Deprecated @@ -203,6 +204,10 @@ public class DisguiseAPI { return PacketsManager.isHearDisguisesEnabled(); } + public static boolean isUnusedDisguisesRemoved() { + return removeUnseenDisguises; + } + /** * Is the velocity packets sent */ @@ -261,6 +266,10 @@ public class DisguiseAPI { PacketsManager.setHearDisguisesListener(isSoundsEnabled); } + public static void setUnusedDisguisesRemoved(boolean remove) { + removeUnseenDisguises = remove; + } + /** * Disable velocity packets being sent for w/e reason. Maybe you want every ounce of performance you can get? */ diff --git a/src/me/libraryaddict/disguise/DisguiseListener.java b/src/me/libraryaddict/disguise/DisguiseListener.java index 13d4d2bf..ba27fbeb 100644 --- a/src/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/me/libraryaddict/disguise/DisguiseListener.java @@ -3,6 +3,7 @@ package me.libraryaddict.disguise; import java.util.HashMap; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.UpdateChecker; @@ -14,6 +15,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.vehicle.VehicleEnterEvent; import org.bukkit.event.vehicle.VehicleExitEvent; import org.bukkit.scheduler.BukkitRunnable; @@ -66,6 +68,15 @@ public class DisguiseListener implements Listener { p.sendMessage(String.format(updateMessage, currentVersion, latestVersion)); } + @EventHandler + public void onQuit(PlayerQuitEvent event) { + if (DisguiseAPI.isUnusedDisguisesRemoved()) { + for (TargetedDisguise disguise : DisguiseUtilities.getSeenDisguises(event.getPlayer().getName())) { + disguise.removeDisguise(); + } + } + } + @EventHandler public void onRightClick(PlayerInteractEntityEvent event) { if (disguiseSlap.containsKey(event.getPlayer().getName())) { diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 60820715..5a3cdda0 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -64,6 +64,7 @@ public class LibsDisguises extends JavaPlugin { DisguiseAPI.setHideArmorFromSelf(getConfig().getBoolean("RemoveArmor")); DisguiseAPI.setHideHeldItemFromSelf(getConfig().getBoolean("RemoveHeldItem")); DisguiseAPI.setAddEntityAnimations(getConfig().getBoolean("AddEntityAnimations")); + DisguiseAPI.setUnusedDisguisesRemoved(getConfig().getBoolean("RemoveUnusedDisguises")); if (DisguiseAPI.isHidingArmorFromSelf() || DisguiseAPI.isHidingHeldItemFromSelf()) { DisguiseAPI.setInventoryListenerEnabled(true); } diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 910dc43c..66cced71 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -2,9 +2,11 @@ package me.libraryaddict.disguise.utilities; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.LibsDisguises; @@ -101,10 +103,10 @@ public class DisguiseUtilities { // If fed a name. I can do this. // But the rest of the time.. Its going to conflict. // The below is debug output. Most people wouldn't care for it. - - // System.out.print("Cannot set more than one " + TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS - // + " on a entity. Removed the old disguise."); - + + // System.out.print("Cannot set more than one " + TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS + // + " on a entity. Removed the old disguise."); + disguiseItel.remove(); /* if (name != null) { if (!disguise.getObservers().contains(name)) { @@ -163,6 +165,27 @@ public class DisguiseUtilities { return new TargetedDisguise[0]; } + public static List getSeenDisguises(String viewer) { + List dis = new ArrayList(); + for (HashSet disguises : getDisguises().values()) { + for (TargetedDisguise disguise : disguises) { + if (disguise.getDisguiseTarget() == TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS) { + if (disguise.canSee(viewer)) { + boolean add = false; + for (String observer : disguise.getObservers()) { + if (!observer.equals(viewer) && Bukkit.getPlayerExact(observer) != null) { + add = true; + break; + } + } + if (add) + dis.add(disguise); + } + } + } + }return dis; + } + public static HashMap getSelfDisguisesIds() { return selfDisguisesIds; } diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index cbfa3260..8a1cd1a9 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -84,16 +84,6 @@ public class ReflectionManager { return null; } - public static Object getGameProfile(String playerName) { - try { - return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile").getConstructor(String.class, String.class) - .newInstance(playerName, playerName); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - public static Entity getBukkitEntity(Object nmsEntity) { try { Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity); @@ -147,6 +137,16 @@ public class ReflectionManager { return null; } + public static Object getGameProfile(String playerName) { + try { + return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile").getConstructor(String.class, String.class) + .newInstance(playerName, playerName); + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + public static Class getNmsClass(String className) { try { return Class.forName("net.minecraft.server." + bukkitVersion + "." + className);