Use scoreboard for display names

This commit is contained in:
libraryaddict 2017-08-06 12:23:47 +12:00
parent 833ea9531e
commit d5913354e4
6 changed files with 35 additions and 30 deletions

View File

@ -8,7 +8,10 @@ import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction;
import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.PlayerInfoData;
import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile; 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.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseParser; import me.libraryaddict.disguise.utilities.DisguiseParser;
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguiseParseException; 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.LibsMsg;
import me.libraryaddict.disguise.utilities.UpdateChecker; import me.libraryaddict.disguise.utilities.UpdateChecker;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -37,6 +39,7 @@ import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.bukkit.scoreboard.Team;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
@ -450,7 +453,11 @@ public class DisguiseListener implements Listener {
} else { } else {
if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) {
if (disguise.getWatcher() instanceof LivingWatcher) { 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()) { if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) {
disguise.getWatcher().setCustomNameVisible(true); disguise.getWatcher().setCustomNameVisible(true);

View File

@ -6,11 +6,15 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import me.libraryaddict.disguise.utilities.DisguiseParser; import me.libraryaddict.disguise.utilities.DisguiseParser;
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm; import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
/** /**
* @author libraryaddict * @author libraryaddict
@ -36,6 +40,12 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
return new ArrayList<>(new HashSet<>(list)); 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<String> getAllowedDisguises( protected ArrayList<String> getAllowedDisguises(
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> hashMap) { HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> hashMap) {
ArrayList<String> allowedDisguises = new ArrayList<>(); ArrayList<String> allowedDisguises = new ArrayList<>();

View File

@ -61,7 +61,7 @@ public class DisguiseCommand extends DisguiseBaseCommand implements TabCompleter
if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) {
if (disguise.getWatcher() instanceof LivingWatcher) { if (disguise.getWatcher() instanceof LivingWatcher) {
disguise.getWatcher().setCustomName(((Player) sender).getDisplayName()); disguise.getWatcher().setCustomName(getDisplayName(sender));
if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) {
disguise.getWatcher().setCustomNameVisible(true); disguise.getWatcher().setCustomNameVisible(true);

View File

@ -86,7 +86,7 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom
if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) {
if (disguise.getWatcher() instanceof LivingWatcher) { if (disguise.getWatcher() instanceof LivingWatcher) {
disguise.getWatcher().setCustomName(player.getDisplayName()); disguise.getWatcher().setCustomName(getDisplayName(sender));
if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) {
disguise.getWatcher().setCustomNameVisible(true); disguise.getWatcher().setCustomNameVisible(true);

View File

@ -172,7 +172,7 @@ public class DisguiseRadiusCommand extends DisguiseBaseCommand implements TabCom
if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) {
if (disguise.getWatcher() instanceof LivingWatcher) { if (disguise.getWatcher() instanceof LivingWatcher) {
disguise.getWatcher().setCustomName(((Player) entity).getDisplayName()); disguise.getWatcher().setCustomName(getDisplayName(entity));
if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) {
disguise.getWatcher().setCustomNameVisible(true); disguise.getWatcher().setCustomNameVisible(true);
} }

View File

@ -23,22 +23,17 @@ import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.SheepWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.WolfWatcher;
public class PacketListenerClientInteract extends PacketAdapter public class PacketListenerClientInteract extends PacketAdapter {
{ public PacketListenerClientInteract(LibsDisguises plugin) {
public PacketListenerClientInteract(LibsDisguises plugin)
{
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY); super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.USE_ENTITY);
} }
@Override @Override
public void onPacketReceiving(PacketEvent event) public void onPacketReceiving(PacketEvent event) {
{
if (event.isCancelled()) if (event.isCancelled())
return; return;
try try {
{
Player observer = event.getPlayer(); Player observer = event.getPlayer();
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
@ -48,42 +43,35 @@ public class PacketListenerClientInteract extends PacketAdapter
Entity entity = entityModifer.read(0); 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); event.setCancelled(true);
} }
for (ItemStack item : new ItemStack[] for (ItemStack item : new ItemStack[]{observer.getInventory().getItemInMainHand(),
{ observer.getInventory().getItemInOffHand()}) {
observer.getInventory().getItemInMainHand(), observer.getInventory().getItemInOffHand()
})
{
if (item == null || item.getType() != Material.INK_SACK) if (item == null || item.getType() != Material.INK_SACK)
continue; continue;
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); 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; continue;
AnimalColor color = AnimalColor.getColor(item.getDurability()); AnimalColor color = AnimalColor.getColor(item.getDurability());
if (disguise.getType() == DisguiseType.SHEEP) if (disguise.getType() == DisguiseType.SHEEP) {
{
SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); SheepWatcher watcher = (SheepWatcher) disguise.getWatcher();
watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor());
} } else {
else
{
WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); WolfWatcher watcher = (WolfWatcher) disguise.getWatcher();
watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor());
} }
} }
} }
catch (Exception e) catch (Exception e) {
{
e.printStackTrace(); e.printStackTrace();
} }
} }