Skip to content

Commit

Permalink
Reduce potential for errors with packet teams (#2138)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mcmonkey4eva authored May 5, 2020
1 parent 92d3605 commit 30f2dee
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 105 deletions.
17 changes: 17 additions & 0 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
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;
Expand Down Expand Up @@ -416,6 +417,22 @@ public void onNPCDespawn(NPCDespawnEvent event) {
+ 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)
Expand Down
5 changes: 5 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -104,6 +105,10 @@ public static String getMinecraftRevision() {
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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() {

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;
Expand Down Expand Up @@ -105,20 +105,6 @@ public Player getBukkitEntity() {
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();
Expand Down

0 comments on commit 30f2dee

Please sign in to comment.