From d5913354e4e1ce1b5f7071ab54ce5a266b3aaf5b Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 6 Aug 2017 12:23:47 +1200 Subject: [PATCH] Use scoreboard for display names --- .../disguise/DisguiseListener.java | 13 +++++-- .../commands/DisguiseBaseCommand.java | 10 ++++++ .../disguise/commands/DisguiseCommand.java | 2 +- .../commands/DisguisePlayerCommand.java | 2 +- .../commands/DisguiseRadiusCommand.java | 2 +- .../PacketListenerClientInteract.java | 36 +++++++------------ 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/me/libraryaddict/disguise/DisguiseListener.java b/src/me/libraryaddict/disguise/DisguiseListener.java index 34ea1e35..2efc9ee0 100644 --- a/src/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/me/libraryaddict/disguise/DisguiseListener.java @@ -8,7 +8,10 @@ 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 me.libraryaddict.disguise.disguisetypes.*; +import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; +import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; +import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.DisguiseParser; import me.libraryaddict.disguise.utilities.DisguiseParser.DisguiseParseException; @@ -17,7 +20,6 @@ import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsMsg; import me.libraryaddict.disguise.utilities.UpdateChecker; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Entity; @@ -37,6 +39,7 @@ import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; +import org.bukkit.scoreboard.Team; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -450,7 +453,11 @@ public class DisguiseListener implements Listener { } else { if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - disguise.getWatcher().setCustomName(((Player) entity).getDisplayName()); + Team team = ((Player) entity).getScoreboard().getEntryTeam(entity.getName()); + + disguise.getWatcher().setCustomName( + (team == null ? "" : team.getPrefix()) + entity.getName() + (team == null ? "" : + team.getSuffix())); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { disguise.getWatcher().setCustomNameVisible(true); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java index a0c6f75b..655034ae 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseBaseCommand.java @@ -6,11 +6,15 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import org.bukkit.Bukkit; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import me.libraryaddict.disguise.utilities.DisguiseParser; import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm; +import org.bukkit.entity.Player; +import org.bukkit.scoreboard.Scoreboard; +import org.bukkit.scoreboard.Team; /** * @author libraryaddict @@ -36,6 +40,12 @@ public abstract class DisguiseBaseCommand implements CommandExecutor { return new ArrayList<>(new HashSet<>(list)); } + protected String getDisplayName(CommandSender player) { + Team team = ((Player) player).getScoreboard().getEntryTeam(player.getName()); + + return (team == null ? "" : team.getPrefix()) + player.getName() + (team == null ? "" : team.getSuffix()); + } + protected ArrayList getAllowedDisguises( HashMap, Boolean>> hashMap) { ArrayList allowedDisguises = new ArrayList<>(); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java index 5f28ddb7..2d9de915 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java @@ -61,7 +61,7 @@ public class DisguiseCommand extends DisguiseBaseCommand implements TabCompleter if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - disguise.getWatcher().setCustomName(((Player) sender).getDisplayName()); + disguise.getWatcher().setCustomName(getDisplayName(sender)); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { disguise.getWatcher().setCustomNameVisible(true); diff --git a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java index 83207955..e7a2db95 100644 --- a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java @@ -86,7 +86,7 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - disguise.getWatcher().setCustomName(player.getDisplayName()); + disguise.getWatcher().setCustomName(getDisplayName(sender)); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { disguise.getWatcher().setCustomNameVisible(true); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java index 266087ce..44410ebd 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java @@ -172,7 +172,7 @@ public class DisguiseRadiusCommand extends DisguiseBaseCommand implements TabCom if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - disguise.getWatcher().setCustomName(((Player) entity).getDisplayName()); + disguise.getWatcher().setCustomName(getDisplayName(entity)); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { disguise.getWatcher().setCustomNameVisible(true); } diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java index 476fc85a..714edc4d 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java @@ -23,22 +23,17 @@ import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher; -public class PacketListenerClientInteract extends PacketAdapter -{ - public PacketListenerClientInteract(LibsDisguises plugin) - { +public class PacketListenerClientInteract extends PacketAdapter { + public PacketListenerClientInteract(LibsDisguises plugin) { super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY); } @Override - public void onPacketReceiving(PacketEvent event) - { - + public void onPacketReceiving(PacketEvent event) { if (event.isCancelled()) return; - try - { + try { Player observer = event.getPlayer(); if (observer.getName().contains("UNKNOWN[")) // If the player is temporary @@ -48,42 +43,35 @@ public class PacketListenerClientInteract extends PacketAdapter Entity entity = entityModifer.read(0); - if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || entity == observer) - { + if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || entity == observer) { event.setCancelled(true); } - for (ItemStack item : new ItemStack[] - { - observer.getInventory().getItemInMainHand(), observer.getInventory().getItemInOffHand() - }) - { + for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(), + observer.getInventory().getItemInOffHand()}) { if (item == null || item.getType() != Material.INK_SACK) continue; Disguise disguise = DisguiseAPI.getDisguise(observer, entity); - if (disguise == null || (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF)) + if (disguise == null || (disguise.getType() != DisguiseType.SHEEP && disguise + .getType() != DisguiseType.WOLF)) continue; AnimalColor color = AnimalColor.getColor(item.getDurability()); - if (disguise.getType() == DisguiseType.SHEEP) - { + if (disguise.getType() == DisguiseType.SHEEP) { SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); - } - else - { + } else { WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); } } } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } }