simplify scoreboard patch to be 100% packet based (#2129)

* simplify scoreboard patch to be 100% packet based

which fixes edge case client errors

* fix edge cases of respawning and multinpc boards
This commit is contained in:
Alex "mcmonkey" Goodwin 2020-05-01 02:49:25 -07:00 committed by GitHub
parent 16c418415b
commit 2c24671ae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 80 additions and 84 deletions

View File

@ -50,7 +50,6 @@ 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;
@ -288,16 +287,6 @@ public class EventListen implements Listener {
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() {
@ -482,7 +471,7 @@ public class EventListen implements Listener {
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);
}
}

View File

@ -397,7 +397,7 @@ public class CitizensNPC extends AbstractNPC {
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()) {

View File

@ -3,7 +3,6 @@ package net.citizensnpcs.util;
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;
@ -16,12 +15,12 @@ import org.bukkit.entity.LivingEntity;
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;
@ -86,6 +85,10 @@ public class Util {
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();
}
@ -152,16 +155,6 @@ public class Util {
}
}
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();
}
@ -242,14 +235,7 @@ public class Util {
*/
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);
}
}
@ -308,7 +294,7 @@ public class Util {
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);
@ -316,7 +302,7 @@ public class Util {
}
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();
}

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);

View File

@ -67,12 +67,14 @@ public class HumanController extends AbstractEntityController {
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);
@ -84,7 +86,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, 0);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
@ -105,15 +107,16 @@ public class HumanController extends AbstractEntityController {
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);