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

@ -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
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 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?
*/

View File

@ -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())) {

View File

@ -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);
}

View File

@ -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<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() {
return selfDisguisesIds;
}

View File

@ -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);