Add new config option to remove unseen disguises for targeted disguises.

This commit is contained in:
libraryaddict 2013-12-05 21:05:58 +13:00
parent d415f6340e
commit 6e5241aec3
6 changed files with 63 additions and 15 deletions

View File

@ -28,3 +28,7 @@ RemoveHeldItem: true
# 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 # 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. # This is only valid if you set a animation on the disguise itself. Because the entitys animations are applied otherwise.
AddEntityAnimations: true 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

View File

@ -22,6 +22,7 @@ public class DisguiseAPI {
private static boolean hidingArmor; private static boolean hidingArmor;
private static boolean hidingHeldItem; private static boolean hidingHeldItem;
private static boolean isEntityAnimationsAdded; private static boolean isEntityAnimationsAdded;
private static boolean removeUnseenDisguises;
private static boolean sendVelocity; private static boolean sendVelocity;
@Deprecated @Deprecated
@ -203,6 +204,10 @@ public class DisguiseAPI {
return PacketsManager.isHearDisguisesEnabled(); return PacketsManager.isHearDisguisesEnabled();
} }
public static boolean isUnusedDisguisesRemoved() {
return removeUnseenDisguises;
}
/** /**
* Is the velocity packets sent * Is the velocity packets sent
*/ */
@ -261,6 +266,10 @@ public class DisguiseAPI {
PacketsManager.setHearDisguisesListener(isSoundsEnabled); 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? * Disable velocity packets being sent for w/e reason. Maybe you want every ounce of performance you can get?
*/ */

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise;
import java.util.HashMap; import java.util.HashMap;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.UpdateChecker; import me.libraryaddict.disguise.utilities.UpdateChecker;
@ -14,6 +15,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent; import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.event.vehicle.VehicleExitEvent; import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -66,6 +68,15 @@ public class DisguiseListener implements Listener {
p.sendMessage(String.format(updateMessage, currentVersion, latestVersion)); 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 @EventHandler
public void onRightClick(PlayerInteractEntityEvent event) { public void onRightClick(PlayerInteractEntityEvent event) {
if (disguiseSlap.containsKey(event.getPlayer().getName())) { if (disguiseSlap.containsKey(event.getPlayer().getName())) {

View File

@ -64,6 +64,7 @@ public class LibsDisguises extends JavaPlugin {
DisguiseAPI.setHideArmorFromSelf(getConfig().getBoolean("RemoveArmor")); DisguiseAPI.setHideArmorFromSelf(getConfig().getBoolean("RemoveArmor"));
DisguiseAPI.setHideHeldItemFromSelf(getConfig().getBoolean("RemoveHeldItem")); DisguiseAPI.setHideHeldItemFromSelf(getConfig().getBoolean("RemoveHeldItem"));
DisguiseAPI.setAddEntityAnimations(getConfig().getBoolean("AddEntityAnimations")); DisguiseAPI.setAddEntityAnimations(getConfig().getBoolean("AddEntityAnimations"));
DisguiseAPI.setUnusedDisguisesRemoved(getConfig().getBoolean("RemoveUnusedDisguises"));
if (DisguiseAPI.isHidingArmorFromSelf() || DisguiseAPI.isHidingHeldItemFromSelf()) { if (DisguiseAPI.isHidingArmorFromSelf() || DisguiseAPI.isHidingHeldItemFromSelf()) {
DisguiseAPI.setInventoryListenerEnabled(true); DisguiseAPI.setInventoryListenerEnabled(true);
} }

View File

@ -2,9 +2,11 @@ package me.libraryaddict.disguise.utilities;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
@ -102,8 +104,8 @@ public class DisguiseUtilities {
// But the rest of the time.. Its going to conflict. // But the rest of the time.. Its going to conflict.
// The below is debug output. Most people wouldn't care for it. // 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 // System.out.print("Cannot set more than one " + TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS
// + " on a entity. Removed the old disguise."); // + " on a entity. Removed the old disguise.");
disguiseItel.remove(); disguiseItel.remove();
/* if (name != null) { /* if (name != null) {
@ -163,6 +165,27 @@ public class DisguiseUtilities {
return new TargetedDisguise[0]; return new TargetedDisguise[0];
} }
public static List<TargetedDisguise> getSeenDisguises(String viewer) {
List<TargetedDisguise> dis = new ArrayList<TargetedDisguise>();
for (HashSet<TargetedDisguise> 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<Integer, Integer> getSelfDisguisesIds() { public static HashMap<Integer, Integer> getSelfDisguisesIds() {
return selfDisguisesIds; return selfDisguisesIds;
} }

View File

@ -84,16 +84,6 @@ public class ReflectionManager {
return null; 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) { public static Entity getBukkitEntity(Object nmsEntity) {
try { try {
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity); Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity);
@ -147,6 +137,16 @@ public class ReflectionManager {
return null; 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) { public static Class getNmsClass(String className) {
try { try {
return Class.forName("net.minecraft.server." + bukkitVersion + "." + className); return Class.forName("net.minecraft.server." + bukkitVersion + "." + className);