From fd2926241cdd10d7cb22551aedda47e0222efb80 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 5 Feb 2020 14:26:55 +1300 Subject: [PATCH] Add isDynamicName for player disguises with mobs custom names that change --- .../disguise/disguisetypes/Disguise.java | 23 +- .../disguisetypes/PlayerDisguise.java | 247 +++++++++--------- .../parser/params/ParamInfoManager.java | 1 + 3 files changed, 146 insertions(+), 125 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java index cc8162a3..214969f8 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -23,6 +23,7 @@ import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.translations.LibsMsg; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.ComponentBuilder; +import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Entity; @@ -174,12 +175,26 @@ public abstract class Disguise { @Override public void run() { - if (DisguiseConfig.isNotifyPlayerDisguised() && getEntity() instanceof Player && - ++actionBarTicks % 15 == 0 && DisguiseAPI.getDisguise(getEntity()) == Disguise.this) { + if (++actionBarTicks % 15 == 0) { actionBarTicks = 0; - ((Player) getEntity()).spigot().sendMessage(ChatMessageType.ACTION_BAR, - new ComponentBuilder(LibsMsg.ACTION_BAR_MESSAGE.get(getType().toReadable())).create()); + if (DisguiseConfig.isNotifyPlayerDisguised() && getEntity() instanceof Player && + DisguiseAPI.getDisguise(getEntity()) == Disguise.this) { + ((Player) getEntity()).spigot().sendMessage(ChatMessageType.ACTION_BAR, + new ComponentBuilder(LibsMsg.ACTION_BAR_MESSAGE.get(getType().toReadable())).create()); + } + + if (Disguise.this instanceof PlayerDisguise && ((PlayerDisguise) Disguise.this).isDynamicName()) { + String name = getEntity().getCustomName(); + + if (name == null) { + name = ""; + } + + if (!((PlayerDisguise) Disguise.this).getName().equals(name)) { + ((PlayerDisguise) Disguise.this).setName(name); + } + } } // If entity is no longer valid. Remove it. diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java index 46accb40..333cc03a 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java @@ -8,6 +8,8 @@ import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction; import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedGameProfile; +import lombok.Getter; +import lombok.Setter; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; @@ -35,6 +37,9 @@ public class PlayerDisguise extends TargetedDisguise { private String skinToUse; private boolean nameVisible = true; private UUID uuid = UUID.randomUUID(); + @Getter + @Setter + private boolean dynamicName; private PlayerDisguise() { super(DisguiseType.PLAYER); @@ -181,79 +186,12 @@ public class PlayerDisguise extends TargetedDisguise { return gameProfile; } - public String getName() { - return playerName; - } - - public String getSkin() { - return skinToUse; - } - - @Override - public PlayerWatcher getWatcher() { - return (PlayerWatcher) super.getWatcher(); - } - - public boolean isDisplayedInTab() { - return getWatcher().isDisplayedInTab(); - } - - @Override - public boolean isPlayerDisguise() { - return true; - } - - @Override - public PlayerDisguise removePlayer(Player player) { - return (PlayerDisguise) super.removePlayer(player); - } - - @Override - public PlayerDisguise removePlayer(String playername) { - return (PlayerDisguise) super.removePlayer(playername); - } - - @Override - public PlayerDisguise setDisguiseTarget(TargetType newTargetType) { - return (PlayerDisguise) super.setDisguiseTarget(newTargetType); - } - - public void setDisplayedInTab(boolean showPlayerInTab) { - getWatcher().setDisplayedInTab(showPlayerInTab); - } - - @Override - public PlayerDisguise setEntity(Entity entity) { - return (PlayerDisguise) super.setEntity(entity); - } - public void setGameProfile(WrappedGameProfile gameProfile) { this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, gameProfile.getName(), gameProfile); } - @Override - public PlayerDisguise setHearSelfDisguise(boolean hearSelfDisguise) { - return (PlayerDisguise) super.setHearSelfDisguise(hearSelfDisguise); - } - - @Override - public PlayerDisguise setHideArmorFromSelf(boolean hideArmor) { - return (PlayerDisguise) super.setHideArmorFromSelf(hideArmor); - } - - @Override - public PlayerDisguise setHideHeldItemFromSelf(boolean hideHeldItem) { - return (PlayerDisguise) super.setHideHeldItemFromSelf(hideHeldItem); - } - - @Override - public PlayerDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) { - return (PlayerDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise); - } - - @Override - public PlayerDisguise setModifyBoundingBox(boolean modifyBox) { - return (PlayerDisguise) super.setModifyBoundingBox(modifyBox); + public String getName() { + return playerName; } public void setName(String name) { @@ -295,53 +233,8 @@ public class PlayerDisguise extends TargetedDisguise { } } - @Override - public PlayerDisguise setReplaceSounds(boolean areSoundsReplaced) { - return (PlayerDisguise) super.setReplaceSounds(areSoundsReplaced); - } - - @Override - public boolean startDisguise() { - if (!isDisguiseInUse()) { - if (skinToUse != null && gameProfile == null) { - currentLookup = new LibsProfileLookup() { - @Override - public void onLookup(WrappedGameProfile gameProfile) { - if (currentLookup != this || gameProfile == null || gameProfile.getProperties().isEmpty()) - return; - - setSkin(gameProfile); - - currentLookup = null; - } - }; - - WrappedGameProfile gameProfile = DisguiseUtilities.getProfileFromMojang(this.skinToUse, currentLookup, - LibsDisguises.getInstance().getConfig().getBoolean("ContactMojangServers", true)); - - if (gameProfile != null) { - setSkin(gameProfile); - } - } - - if (getName().equals("") && getEntity() != null) { - String name = getEntity().getCustomName(); - - if (name == null || name.isEmpty()) { - name = getEntity().getType().name(); - } - - setName(name); - } - } - - boolean result = super.startDisguise(); - - if (result && hasExtendedName()) { - DisguiseUtilities.registerExtendedName(getName()); - } - - return result; + public String getSkin() { + return skinToUse; } public PlayerDisguise setSkin(String newSkin) { @@ -423,6 +316,123 @@ public class PlayerDisguise extends TargetedDisguise { return this; } + @Override + public PlayerWatcher getWatcher() { + return (PlayerWatcher) super.getWatcher(); + } + + @Override + public PlayerDisguise setWatcher(FlagWatcher newWatcher) { + return (PlayerDisguise) super.setWatcher(newWatcher); + } + + public boolean isDisplayedInTab() { + return getWatcher().isDisplayedInTab(); + } + + public void setDisplayedInTab(boolean showPlayerInTab) { + getWatcher().setDisplayedInTab(showPlayerInTab); + } + + @Override + public boolean isPlayerDisguise() { + return true; + } + + @Override + public PlayerDisguise removePlayer(Player player) { + return (PlayerDisguise) super.removePlayer(player); + } + + @Override + public PlayerDisguise removePlayer(String playername) { + return (PlayerDisguise) super.removePlayer(playername); + } + + @Override + public PlayerDisguise setDisguiseTarget(TargetType newTargetType) { + return (PlayerDisguise) super.setDisguiseTarget(newTargetType); + } + + @Override + public PlayerDisguise setEntity(Entity entity) { + return (PlayerDisguise) super.setEntity(entity); + } + + @Override + public PlayerDisguise setHearSelfDisguise(boolean hearSelfDisguise) { + return (PlayerDisguise) super.setHearSelfDisguise(hearSelfDisguise); + } + + @Override + public PlayerDisguise setHideArmorFromSelf(boolean hideArmor) { + return (PlayerDisguise) super.setHideArmorFromSelf(hideArmor); + } + + @Override + public PlayerDisguise setHideHeldItemFromSelf(boolean hideHeldItem) { + return (PlayerDisguise) super.setHideHeldItemFromSelf(hideHeldItem); + } + + @Override + public PlayerDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) { + return (PlayerDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise); + } + + @Override + public PlayerDisguise setModifyBoundingBox(boolean modifyBox) { + return (PlayerDisguise) super.setModifyBoundingBox(modifyBox); + } + + @Override + public PlayerDisguise setReplaceSounds(boolean areSoundsReplaced) { + return (PlayerDisguise) super.setReplaceSounds(areSoundsReplaced); + } + + @Override + public boolean startDisguise() { + if (!isDisguiseInUse()) { + if (skinToUse != null && gameProfile == null) { + currentLookup = new LibsProfileLookup() { + @Override + public void onLookup(WrappedGameProfile gameProfile) { + if (currentLookup != this || gameProfile == null || gameProfile.getProperties().isEmpty()) + return; + + setSkin(gameProfile); + + currentLookup = null; + } + }; + + WrappedGameProfile gameProfile = DisguiseUtilities.getProfileFromMojang(this.skinToUse, currentLookup, + LibsDisguises.getInstance().getConfig().getBoolean("ContactMojangServers", true)); + + if (gameProfile != null) { + setSkin(gameProfile); + } + } + + if (getName().equals("") && getEntity() != null) { + String name = getEntity().getCustomName(); + + if (name == null || name.isEmpty()) { + name = getEntity().getType().name(); + } + + setName(name); + } + } + + boolean result = super.startDisguise(); + + if (result && hasExtendedName()) { + DisguiseUtilities.registerExtendedName(getName()); + } + + return result; + } + @Override public PlayerDisguise setVelocitySent(boolean sendVelocity) { return (PlayerDisguise) super.setVelocitySent(sendVelocity); @@ -433,11 +443,6 @@ public class PlayerDisguise extends TargetedDisguise { return (PlayerDisguise) super.setViewSelfDisguise(viewSelfDisguise); } - @Override - public PlayerDisguise setWatcher(FlagWatcher newWatcher) { - return (PlayerDisguise) super.setWatcher(newWatcher); - } - @Override public PlayerDisguise silentlyAddPlayer(String playername) { return (PlayerDisguise) super.silentlyAddPlayer(playername); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java index d6e0420f..2a471592 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java @@ -135,6 +135,7 @@ public class ParamInfoManager { if (watcherClass == PlayerWatcher.class) { try { methods.add(PlayerDisguise.class.getMethod("setNameVisible", boolean.class)); + methods.add(PlayerDisguise.class.getMethod("setDynamicName", boolean.class)); } catch (NoSuchMethodException e) { e.printStackTrace();