From 30f2dee4d7ba34b89f643f85404f0ea51dae1e6c Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Tue, 5 May 2020 03:20:56 -0700 Subject: [PATCH] Reduce potential for errors with packet teams (#2138) * Reduce potential for errors with packet teams Move scoreboard removal out of NMS to event method, and make more unique names * remove unneeded isCancelled check --- .../main/java/net/citizensnpcs/EventListen.java | 17 +++++++++++++++++ .../main/java/net/citizensnpcs/util/Util.java | 5 +++++ .../nms/v1_10_R1/entity/HumanController.java | 16 +--------------- .../nms/v1_11_R1/entity/HumanController.java | 16 +--------------- .../nms/v1_12_R1/entity/HumanController.java | 16 +--------------- .../nms/v1_13_R2/entity/HumanController.java | 16 +--------------- .../nms/v1_14_R1/entity/HumanController.java | 16 +--------------- .../nms/v1_15_R1/entity/HumanController.java | 16 +--------------- .../nms/v1_8_R3/entity/HumanController.java | 16 +--------------- 9 files changed, 29 insertions(+), 105 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/EventListen.java b/main/src/main/java/net/citizensnpcs/EventListen.java index 13ba4a037..19fccacdc 100644 --- a/main/src/main/java/net/citizensnpcs/EventListen.java +++ b/main/src/main/java/net/citizensnpcs/EventListen.java @@ -50,6 +50,7 @@ import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scoreboard.Team; import com.google.common.base.Predicates; import com.google.common.collect.ArrayListMultimap; @@ -416,6 +417,22 @@ public class EventListen implements Listener { + event.getReason().name()); } skinUpdateTracker.onNPCDespawn(event.getNPC()); + if (event.getNPC().getEntity() instanceof Player && Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { + String teamName = event.getNPC().data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, ""); + if (teamName.length() > 0) { + Player player = (Player) event.getNPC().getEntity(); + Team team = Util.getDummyScoreboard().getTeam(teamName); + if (team != null && team.hasPlayer(player)) { + if (team.getSize() == 1) { + Util.sendTeamPacketToOnlinePlayers(team, 1); + team.unregister(); + } + else { + team.removePlayer(player); + } + } + } + } } @EventHandler(ignoreCancelled = true) diff --git a/main/src/main/java/net/citizensnpcs/util/Util.java b/main/src/main/java/net/citizensnpcs/util/Util.java index 6065c1bed..067b33ff0 100644 --- a/main/src/main/java/net/citizensnpcs/util/Util.java +++ b/main/src/main/java/net/citizensnpcs/util/Util.java @@ -4,6 +4,7 @@ import java.util.EnumSet; import java.util.Random; import java.util.Set; import java.util.regex.Pattern; +import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -104,6 +105,10 @@ public class Util { return MINECRAFT_REVISION.substring(MINECRAFT_REVISION.lastIndexOf('.') + 2); } + public static String getTeamName(UUID id) { + return "CIT-" + id.toString().replace("-", "").substring(0, 12); + } + public static boolean isAlwaysFlyable(EntityType type) { if (type.name().toLowerCase().equals("vex") || type.name().toLowerCase().equals("parrot") || type.name().toLowerCase().equals("bee") || type.name().toLowerCase().equals("phantom")) diff --git a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/HumanController.java b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/HumanController.java index 6c714ecde..3ff1fd2c8 100644 --- a/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/HumanController.java +++ b/v1_10_R1/src/main/java/net/citizensnpcs/nms/v1_10_R1/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC(); diff --git a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/HumanController.java b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/HumanController.java index 7a47fa54e..6ab84aa34 100644 --- a/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/HumanController.java +++ b/v1_11_R1/src/main/java/net/citizensnpcs/nms/v1_11_R1/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC(); diff --git a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/HumanController.java b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/HumanController.java index a0a8771e6..4a1550f61 100644 --- a/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/HumanController.java +++ b/v1_12_R1/src/main/java/net/citizensnpcs/nms/v1_12_R1/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC(); diff --git a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HumanController.java b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HumanController.java index fd6a397ed..47a92f6e5 100644 --- a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HumanController.java +++ b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC(); diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HumanController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HumanController.java index 29dd8bd5d..d5d8a9775 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HumanController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC(); diff --git a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/HumanController.java b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/HumanController.java index 726fa983b..d519715c6 100644 --- a/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/HumanController.java +++ b/v1_15_R1/src/main/java/net/citizensnpcs/nms/v1_15_R1/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC(); diff --git a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/HumanController.java b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/HumanController.java index ad6395fb0..1d7339ba4 100644 --- a/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/HumanController.java +++ b/v1_8_R3/src/main/java/net/citizensnpcs/nms/v1_8_R3/entity/HumanController.java @@ -68,7 +68,7 @@ public class HumanController extends AbstractEntityController { if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { Scoreboard scoreboard = Util.getDummyScoreboard(); - String teamName = profile.getId().toString().substring(0, 16); + String teamName = Util.getTeamName(profile.getId()); Team team = scoreboard.getTeam(teamName); int mode = 2; @@ -105,20 +105,6 @@ public class HumanController extends AbstractEntityController { public void remove() { Player entity = getBukkitEntity(); if (entity != null) { - if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) { - String teamName = entity.getUniqueId().toString().substring(0, 16); - Scoreboard scoreboard = Util.getDummyScoreboard(); - Team team = scoreboard.getTeam(teamName); - if (team != null && team.hasPlayer(entity)) { - if (team.getSize() == 1) { - Util.sendTeamPacketToOnlinePlayers(team, 1); - team.unregister(); - } - else { - team.removePlayer(entity); - } - } - } NMS.removeFromWorld(entity); SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null; npc.getSkinTracker().onRemoveNPC();