From a92c8fb55e62ea542dda0002eb1446ca488484d5 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 26 Sep 2020 12:00:44 +1200 Subject: [PATCH] Add ability for player disguises to copy existing players display name --- .../disguise/DisguiseConfig.java | 4 +++ .../disguisetypes/PlayerDisguise.java | 27 ++++++++++------ .../disguise/utilities/DisguiseUtilities.java | 32 +++++++++++++++++++ .../listeners/PlayerSkinHandler.java | 1 - src/main/resources/config.yml | 3 ++ 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java index c3f6a387..100982ec 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java @@ -258,6 +258,9 @@ public class DisguiseConfig { @Getter @Setter private static long lastUpdateRequest; + @Getter + @Setter + private static boolean copyPlayerTeamInfo; public static boolean isArmorstandsName() { return getPlayerNameType() == PlayerNameType.ARMORSTANDS; @@ -678,6 +681,7 @@ public class DisguiseConfig { setVelocitySent(config.getBoolean("SendVelocity")); setViewDisguises(config.getBoolean("ViewSelfDisguises")); setWarnScoreboardConflict(config.getBoolean("Scoreboard.WarnConflict")); + setCopyPlayerTeamInfo(config.getBoolean("Scoreboard.CopyPlayerTeamInfo")); setWitherSkullPacketsEnabled(config.getBoolean("PacketsEnabled.WitherSkull")); setWolfDyeable(config.getBoolean("DyeableWolf")); setTablistRemoveDelay(config.getInt("TablistRemoveDelay")); diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java index cefad386..cd60464f 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/PlayerDisguise.java @@ -272,6 +272,12 @@ public class PlayerDisguise extends TargetedDisguise { } } + if (DisguiseConfig.isCopyPlayerTeamInfo() && + (DisguiseConfig.getPlayerNameType() == DisguiseConfig.PlayerNameType.TEAMS || + DisguiseConfig.getPlayerNameType() == DisguiseConfig.PlayerNameType.ARMORSTANDS)) { + name = DisguiseUtilities.getDisplayName(name); + } + if (name.equals(playerName)) { return; } @@ -337,13 +343,13 @@ public class PlayerDisguise extends TargetedDisguise { try { for (Player player : Bukkit.getOnlinePlayers()) { - if (!canSee(player)) + if (!canSee(player)) { continue; + } ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab); } - } - catch (InvocationTargetException e) { + } catch (InvocationTargetException e) { e.printStackTrace(); } } @@ -436,8 +442,9 @@ public class PlayerDisguise extends TargetedDisguise { currentLookup = new LibsProfileLookup() { @Override public void onLookup(WrappedGameProfile gameProfile) { - if (currentLookup != this || gameProfile == null || gameProfile.getProperties().isEmpty()) + if (currentLookup != this || gameProfile == null || gameProfile.getProperties().isEmpty()) { return; + } setSkin(gameProfile); @@ -485,8 +492,7 @@ public class PlayerDisguise extends TargetedDisguise { string.contains(",\"name\":")) { try { return DisguiseUtilities.getGson().fromJson(string, WrappedGameProfile.class); - } - catch (Exception ex) { + } catch (Exception ex) { throw new IllegalStateException( "Tried to parse " + string + " to a GameProfile, but it has been formatted incorrectly!"); } @@ -505,14 +511,14 @@ public class PlayerDisguise extends TargetedDisguise { try { for (Player player : Bukkit.getOnlinePlayers()) { - if (!canSee(player)) + if (!canSee(player)) { continue; + } ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab); ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab); } - } - catch (InvocationTargetException e) { + } catch (InvocationTargetException e) { e.printStackTrace(); } } @@ -604,8 +610,9 @@ public class PlayerDisguise extends TargetedDisguise { currentLookup = new LibsProfileLookup() { @Override public void onLookup(WrappedGameProfile gameProfile) { - if (currentLookup != this || gameProfile == null || gameProfile.getProperties().isEmpty()) + if (currentLookup != this || gameProfile == null || gameProfile.getProperties().isEmpty()) { return; + } setSkin(gameProfile); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index e2fd71b6..dd9233f2 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -235,6 +235,38 @@ public class DisguiseUtilities { return team.getPrefix() + team.getColor() + player.getName() + team.getSuffix(); } + public static String getDisplayName(String playerName) { + if (StringUtils.isEmpty(playerName)) { + return playerName; + } + + Team team = Bukkit.getScoreboardManager().getMainScoreboard().getEntryTeam(playerName); + + if (team != null && (!StringUtils.isEmpty(team.getPrefix()) || !StringUtils.isEmpty(team.getSuffix()))) { + return team.getPrefix() + team.getColor() + playerName + team.getSuffix(); + } + + Player player = Bukkit.getPlayer(playerName); + + if (player == null) { + return playerName; + } + + team = Bukkit.getScoreboardManager().getMainScoreboard().getEntryTeam(player.getUniqueId().toString()); + + if (team == null || (StringUtils.isEmpty(team.getPrefix()) && StringUtils.isEmpty(team.getSuffix()))) { + String name = player.getDisplayName(); + + if (name.equals(playerName)) { + return player.getPlayerListName(); + } + + return name; + } + + return team.getPrefix() + team.getColor() + player.getName() + team.getSuffix(); + } + public static void saveViewPreferances() { if (!DisguiseConfig.isSaveUserPreferences()) { return; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/listeners/PlayerSkinHandler.java b/src/main/java/me/libraryaddict/disguise/utilities/listeners/PlayerSkinHandler.java index 8d105d29..319b86fe 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/listeners/PlayerSkinHandler.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/listeners/PlayerSkinHandler.java @@ -158,7 +158,6 @@ public class PlayerSkinHandler implements Listener { } getCache().invalidate(player); - } @EventHandler diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0cef962c..668e8c90 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -109,6 +109,9 @@ Scoreboard: # Should the scoreboard warn you if it detects a potential conflict? # If self disguises are disabled, or the scoreboard is using IGNORE_SCOREBOARD then this does nothing. WarnConflict: true + # When disguising as a player, should the prefix/suffix of the player disguise name copy the team info? + # Only takes effect if using PlayerNames TEAMS or ARMORSTANDS + CopyPlayerTeamInfo: true # Shall I notify those with the correct permission when there's a LibsDisguises update? # Disabling this will also disable notifications when the plugin updated