From 7a3f764581f185a6fc29df2ef75b316b7ff47c87 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 12 Aug 2020 19:28:41 +1200 Subject: [PATCH] Fix a performance issue, fixes #503 --- .../libraryaddict/disguise/DisguiseAPI.java | 4 +- .../disguise/DisguiseConfig.java | 3 + .../disguise/disguisetypes/Disguise.java | 2 +- .../disguise/utilities/DisguiseUtilities.java | 58 ++++++++----------- 4 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java b/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java index cb75001b..52870b92 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java @@ -363,7 +363,7 @@ public class DisguiseAPI { return null; } - return DisguiseUtilities.getMainDisguise(disguised.getUniqueId()); + return DisguiseUtilities.getMainDisguise(disguised.getEntityId()); } public static String parseToString(Disguise disguise, boolean outputSkin) { @@ -400,7 +400,7 @@ public class DisguiseAPI { return null; } - return DisguiseUtilities.getDisguises(disguised.getUniqueId()); + return DisguiseUtilities.getDisguises(disguised.getEntityId()); } public static int getSelfDisguiseId() { diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java index e655a636..fb1fd94c 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java @@ -21,6 +21,7 @@ import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.boss.BarColor; import org.bukkit.boss.BarStyle; @@ -29,6 +30,7 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.scheduler.BukkitTask; @@ -298,6 +300,7 @@ public class DisguiseConfig { for (Set disguises : DisguiseUtilities.getDisguises().values()) { for (Disguise disguise : disguises) { disguise.getWatcher().setSprinting(true); + disguise.getWatcher().setHelmet(new ItemStack(Material.LEATHER_HELMET)); } } } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java index 934254fa..86e9c410 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -1144,7 +1144,7 @@ public abstract class Disguise { } // Stick the disguise in the disguises bin - DisguiseUtilities.addDisguise(entity.getUniqueId(), (TargetedDisguise) this); + DisguiseUtilities.addDisguise(entity.getEntityId(), (TargetedDisguise) this); if (isSelfDisguiseVisible() && getEntity() instanceof Player) { DisguiseUtilities.removeSelfDisguise(this); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 827fb7f0..c74db3c3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -129,7 +129,7 @@ public class DisguiseUtilities { * A hashmap of the uuid's of entitys, alive and dead. And their disguises in use */ @Getter - private static final Map> disguises = new HashMap<>(); + private static final Map> disguises = new HashMap<>(); /** * Disguises which are stored ready for a entity to be seen by a player Preferably, disguises in this should only * stay in for @@ -546,9 +546,9 @@ public class DisguiseUtilities { return false; } - public static void addDisguise(UUID entityUUID, TargetedDisguise disguise) { - if (!getDisguises().containsKey(entityUUID)) { - getDisguises().put(entityUUID, new HashSet<>()); + public static void addDisguise(Integer entityId, TargetedDisguise disguise) { + if (!getDisguises().containsKey(entityId)) { + getDisguises().put(entityId, new HashSet<>()); synchronized (isNoInteract) { Entity entity = disguise.getEntity(); @@ -586,7 +586,7 @@ public class DisguiseUtilities { } } - getDisguises().get(entityUUID).add(disguise); + getDisguises().get(entityId).add(disguise); checkConflicts(disguise, null); @@ -601,10 +601,8 @@ public class DisguiseUtilities { return; } - UUID uniqueId = entity.getUniqueId(); - for (TargetedDisguise disguise : getFutureDisguises().remove(entity.getEntityId())) { - addDisguise(uniqueId, disguise); + addDisguise(entity.getEntityId(), disguise); } } @@ -673,7 +671,7 @@ public class DisguiseUtilities { return; } - Iterator disguiseItel = getDisguises().get(disguise.getEntity().getUniqueId()).iterator(); + Iterator disguiseItel = getDisguises().get(disguise.getEntity().getEntityId()).iterator(); // Iterate through the disguises while (disguiseItel.hasNext()) { @@ -832,11 +830,11 @@ public class DisguiseUtilities { } public static TargetedDisguise getDisguise(Player observer, Entity entity) { - UUID entityId = entity.getUniqueId(); + int entityId = entity.getEntityId(); - if (futureDisguises.containsKey(entity.getEntityId())) { - for (TargetedDisguise disguise : futureDisguises.remove(entity.getEntityId())) { - addDisguise(entityId, disguise); + if (futureDisguises.containsKey(entityId)) { + for (TargetedDisguise disguise : futureDisguises.remove(entityId)) { + addDisguise(entity.getEntityId(), disguise); } } @@ -853,7 +851,7 @@ public class DisguiseUtilities { return null; } - public static TargetedDisguise[] getDisguises(UUID entityId) { + public static TargetedDisguise[] getDisguises(Integer entityId) { if (getDisguises().containsKey(entityId)) { Set disguises = getDisguises().get(entityId); @@ -899,7 +897,7 @@ public class DisguiseUtilities { return null; } - public static TargetedDisguise getMainDisguise(UUID entityId) { + public static TargetedDisguise getMainDisguise(Integer entityId) { TargetedDisguise toReturn = null; if (getDisguises().containsKey(entityId)) { @@ -1195,8 +1193,8 @@ public class DisguiseUtilities { } public static boolean isDisguiseInUse(Disguise disguise) { - return disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getUniqueId()) && - getDisguises().get(disguise.getEntity().getUniqueId()).contains(disguise); + return disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getEntityId()) && + getDisguises().get(disguise.getEntity().getEntityId()).contains(disguise); } /** @@ -1412,7 +1410,7 @@ public class DisguiseUtilities { } public static boolean removeDisguise(TargetedDisguise disguise) { - UUID entityId = disguise.getEntity().getUniqueId(); + int entityId = disguise.getEntity().getEntityId(); if (getDisguises().containsKey(entityId) && getDisguises().get(entityId).remove(disguise)) { if (getDisguises().get(entityId).isEmpty()) { @@ -2484,8 +2482,8 @@ public class DisguiseUtilities { Entity e = disguise.getEntity(); // If the disguises entity is null, or the disguised entity isn't a player; return - if (!(e instanceof Player) || !getDisguises().containsKey(e.getUniqueId()) || - !getDisguises().get(e.getUniqueId()).contains(disguise)) { + if (!(e instanceof Player) || !getDisguises().containsKey(e.getEntityId()) || + !getDisguises().get(e.getEntityId()).contains(disguise)) { return; } @@ -2816,31 +2814,23 @@ public class DisguiseUtilities { } public static Disguise getDisguise(Player observer, int entityId) { - Entity entity = null; - // If the entity ID is the same as self disguises id, then it needs to be set to the observers id if (entityId == DisguiseAPI.getSelfDisguiseId()) { - entity = observer; - } else { + entityId = observer.getEntityId(); + } + + if (getFutureDisguises().containsKey(entityId)) { for (Entity e : observer.getWorld().getEntities()) { if (e.getEntityId() != entityId) { continue; } - entity = e; - break; + onFutureDisguise(e); } - if (entity == null) { - return null; - } } - if (getFutureDisguises().containsKey(entityId)) { - onFutureDisguise(entity); - } - - TargetedDisguise[] disguises = getDisguises(entity.getUniqueId()); + TargetedDisguise[] disguises = getDisguises(entityId); if (disguises == null) { return null;