Skip to content

Commit

Permalink
simplify scoreboard patch to be 100% packet based (#2129)
Browse files Browse the repository at this point in the history
* simplify scoreboard patch to be 100% packet based

which fixes edge case client errors

* fix edge cases of respawning and multinpc boards
  • Loading branch information
mcmonkey4eva committed May 1, 2020
1 parent 16c4184 commit 2c24671
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 84 deletions.
13 changes: 1 addition & 12 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Expand Up @@ -50,7 +50,6 @@
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 @@ -288,16 +287,6 @@ public void onEntityDeath(EntityDeathEvent event) {
Bukkit.getPluginManager().callEvent(new NPCDeathEvent(npc, event));
npc.despawn(DespawnReason.DEATH);

if (npc.data().has(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA)) {
String teamName = npc.data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA);
Team team = Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName);
if (team != null) {
team.unregister();
}

npc.data().remove(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA);
}

if (npc.data().get(NPC.RESPAWN_DELAY_METADATA, -1) >= 0) {
int delay = npc.data().get(NPC.RESPAWN_DELAY_METADATA, -1);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
Expand Down Expand Up @@ -482,7 +471,7 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
public void onPlayerJoin(PlayerJoinEvent event) {
skinUpdateTracker.updatePlayer(event.getPlayer(), 6 * 20, true);

if (Setting.USE_SCOREBOARD_TEAMS.asBoolean() && !Util.isPlayerMainScoreboard(event.getPlayer())) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Util.updateNPCTeams(event.getPlayer(), 0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -397,7 +397,7 @@ private void updateCustomName() {
String teamName = data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, "");
Team team = null;
if (!(getEntity() instanceof Player) || teamName.length() == 0
|| (team = Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName)) == null)
|| (team = Util.getDummyScoreboard().getTeam(teamName)) == null)
return;

if (!Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Expand Down
30 changes: 8 additions & 22 deletions main/src/main/java/net/citizensnpcs/util/Util.java
Expand Up @@ -3,7 +3,6 @@
import java.util.EnumSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
Expand All @@ -16,12 +15,12 @@
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;

import net.citizensnpcs.api.event.NPCCollisionEvent;
import net.citizensnpcs.api.event.NPCPushEvent;
Expand Down Expand Up @@ -86,6 +85,10 @@ public static void faceLocation(Entity entity, Location to, boolean headOnly, bo
NMS.look(entity, to, headOnly, immediate);
}

public static Scoreboard getDummyScoreboard() {
return DUMMY_SCOREBOARD;
}

public static Location getEyeLocation(Entity entity) {
return entity instanceof LivingEntity ? ((LivingEntity) entity).getEyeLocation() : entity.getLocation();
}
Expand Down Expand Up @@ -152,16 +155,6 @@ public static boolean isOffHand(PlayerInteractEvent event) {
}
}

public static boolean isPlayerMainScoreboard(Player player) {
boolean isOnMain = player.getScoreboard().equals(Bukkit.getScoreboardManager().getMainScoreboard());
if (isOnMain) {
PLAYERS_ON_MAIN_SCOREBOARD.add(player.getUniqueId());
} else {
PLAYERS_ON_MAIN_SCOREBOARD.remove(player.getUniqueId());
}
return isOnMain;
}

public static String listValuesPretty(Enum<?>[] values) {
return "<e>" + Joiner.on("<a>, <e>").join(values).toLowerCase();
}
Expand Down Expand Up @@ -242,14 +235,7 @@ public static String prettyPrintLocation(Location to) {
*/
public static void sendTeamPacketToOnlinePlayers(Team team, int mode) {
for (Player player : Bukkit.getOnlinePlayers()) {
boolean wasOnMain = PLAYERS_ON_MAIN_SCOREBOARD.contains(player.getUniqueId());
if (isPlayerMainScoreboard(player))
continue;
if (wasOnMain) {
updateNPCTeams(player, 0);
} else {
NMS.sendTeamPacket(player, team, mode);
}
NMS.sendTeamPacket(player, team, mode);
}
}

Expand Down Expand Up @@ -308,15 +294,15 @@ public static void updateNPCTeams(Player toUpdate, int mode) {
String teamName = npc.data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, "");
Team team = null;
if (teamName.length() == 0
|| (team = Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName)) == null)
|| (team = Util.getDummyScoreboard().getTeam(teamName)) == null)
continue;

NMS.sendTeamPacket(toUpdate, team, mode);
}
}

private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard();
private static String MINECRAFT_REVISION;
private static final Pattern NON_ALPHABET_MATCHER = Pattern.compile(".*[^A-Za-z0-9_].*");
private static final Set<UUID> PLAYERS_ON_MAIN_SCOREBOARD = Sets.newHashSet();
}
Expand Up @@ -67,12 +67,14 @@ public void run() {
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);

if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
String teamName = profile.getId().toString().substring(0, 16);

Team team = scoreboard.getTeam(teamName);
int mode = 2;
if (team == null) {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
Expand All @@ -84,7 +86,7 @@ public void run() {

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);

Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
Expand All @@ -105,15 +107,16 @@ public void remove() {
if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
String teamName = entity.getUniqueId().toString().substring(0, 16);
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
Team team = scoreboard.getTeam(teamName);
if (team != null && team.hasPlayer(entity)) {
if (team.getSize() == 1) {
team.setPrefix("");
team.setSuffix("");
Util.sendTeamPacketToOnlinePlayers(team, 1);
team.unregister();
}
else {
team.removePlayer(entity);
}
team.removePlayer(entity);
Util.sendTeamPacketToOnlinePlayers(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Expand Up @@ -67,12 +67,14 @@ public void run() {
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);

if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
String teamName = profile.getId().toString().substring(0, 16);

Team team = scoreboard.getTeam(teamName);
int mode = 2;
if (team == null) {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
Expand All @@ -84,7 +86,7 @@ public void run() {

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);

Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
Expand All @@ -105,15 +107,16 @@ public void remove() {
if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
String teamName = entity.getUniqueId().toString().substring(0, 16);
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
Team team = scoreboard.getTeam(teamName);
if (team != null && team.hasPlayer(entity)) {
if (team.getSize() == 1) {
team.setPrefix("");
team.setSuffix("");
Util.sendTeamPacketToOnlinePlayers(team, 1);
team.unregister();
}
else {
team.removePlayer(entity);
}
team.removePlayer(entity);
Util.sendTeamPacketToOnlinePlayers(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Expand Up @@ -67,12 +67,14 @@ public void run() {
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);

if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
String teamName = profile.getId().toString().substring(0, 16);

Team team = scoreboard.getTeam(teamName);
int mode = 2;
if (team == null) {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
Expand All @@ -84,7 +86,7 @@ public void run() {

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);

Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
Expand All @@ -105,15 +107,16 @@ public void remove() {
if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
String teamName = entity.getUniqueId().toString().substring(0, 16);
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
Team team = scoreboard.getTeam(teamName);
if (team != null && team.hasPlayer(entity)) {
if (team.getSize() == 1) {
team.setPrefix("");
team.setSuffix("");
Util.sendTeamPacketToOnlinePlayers(team, 1);
team.unregister();
}
else {
team.removePlayer(entity);
}
team.removePlayer(entity);
Util.sendTeamPacketToOnlinePlayers(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Expand Up @@ -67,12 +67,14 @@ public void run() {
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);

if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
String teamName = profile.getId().toString().substring(0, 16);

Team team = scoreboard.getTeam(teamName);
int mode = 2;
if (team == null) {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
Expand All @@ -84,7 +86,7 @@ public void run() {

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);

Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
Expand All @@ -105,15 +107,16 @@ public void remove() {
if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
String teamName = entity.getUniqueId().toString().substring(0, 16);
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
Team team = scoreboard.getTeam(teamName);
if (team != null && team.hasPlayer(entity)) {
if (team.getSize() == 1) {
team.setPrefix("");
team.setSuffix("");
Util.sendTeamPacketToOnlinePlayers(team, 1);
team.unregister();
}
else {
team.removePlayer(entity);
}
team.removePlayer(entity);
Util.sendTeamPacketToOnlinePlayers(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Expand Up @@ -67,12 +67,14 @@ public void run() {
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);

if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
String teamName = profile.getId().toString().substring(0, 16);

Team team = scoreboard.getTeam(teamName);
int mode = 2;
if (team == null) {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
Expand All @@ -84,7 +86,7 @@ public void run() {

handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);

Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
Expand All @@ -105,15 +107,16 @@ public void remove() {
if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
String teamName = entity.getUniqueId().toString().substring(0, 16);
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Scoreboard scoreboard = Util.getDummyScoreboard();
Team team = scoreboard.getTeam(teamName);
if (team != null && team.hasPlayer(entity)) {
if (team.getSize() == 1) {
team.setPrefix("");
team.setSuffix("");
Util.sendTeamPacketToOnlinePlayers(team, 1);
team.unregister();
}
else {
team.removePlayer(entity);
}
team.removePlayer(entity);
Util.sendTeamPacketToOnlinePlayers(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down

0 comments on commit 2c24671

Please sign in to comment.