patch the majority of potential scoreboard packet errors (#2128)

This commit is contained in:
Alex "mcmonkey" Goodwin 2020-04-30 10:39:07 -07:00 committed by GitHub
parent 27f417113a
commit 981ade8974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 84 additions and 55 deletions

View File

@ -482,18 +482,8 @@ public class EventListen implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) {
skinUpdateTracker.updatePlayer(event.getPlayer(), 6 * 20, true);
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
for (NPC npc : getAllNPCs()) {
if (!(npc.getEntity() instanceof Player)) {
continue;
}
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)
continue;
NMS.sendTeamPacket(event.getPlayer(), team);
}
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean() && !Util.isPlayerMainScoreboard(event.getPlayer())) {
Util.sendAllNpcTeamsTo(event.getPlayer(), 0);
}
}

View File

@ -4,9 +4,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus;
@ -92,9 +90,7 @@ public class ScoreboardTrait extends Trait {
}
}
}
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 2);
}
public void removeTag(String tag) {

View File

@ -366,8 +366,8 @@ public class NMS {
BRIDGE.sendTabListRemove(recipient, listPlayer);
}
public static void sendTeamPacket(Player recipient, Team team) {
BRIDGE.sendTeamPacket(recipient, team);
public static void sendTeamPacket(Player recipient, Team team, int mode) {
BRIDGE.sendTeamPacket(recipient, team, mode);
}
public static void setBodyYaw(Entity entity, float yaw) {

View File

@ -119,7 +119,7 @@ public interface NMSBridge {
public void sendTabListRemove(Player recipient, Player listPlayer);
public void sendTeamPacket(Player recipient, Team team);
public void sendTeamPacket(Player recipient, Team team, int mode);
public void setBodyYaw(Entity entity, float yaw);

View File

@ -64,6 +64,10 @@ public class PlayerUpdateTask extends BukkitRunnable {
PLAYERS.put(entity.getUniqueId(), (Player) entity);
}
public static Iterable<Player> getRegisteredPlayerNPCs() {
return PLAYERS.values();
}
private static Map<UUID, org.bukkit.entity.Player> PLAYERS = new HashMap<UUID, org.bukkit.entity.Player>();
private static Map<UUID, org.bukkit.entity.Entity> TICKERS = new HashMap<UUID, org.bukkit.entity.Entity>();
private static List<org.bukkit.entity.Entity> TICKERS_PENDING_ADD = new ArrayList<org.bukkit.entity.Entity>();

View File

@ -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;
@ -15,15 +16,18 @@ 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.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;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.npc.ai.NPCHolder;
public class Util {
// Static class for small (emphasis small) utility methods
@ -270,6 +274,48 @@ public class Util {
return new String[] { name, prefix, suffix };
}
public static boolean isPlayerMainScoreboard(Player player) {
boolean isOnMain = player.getScoreboard().equals(Bukkit.getScoreboardManager().getMainScoreboard());
if (isOnMain) {
playersOnMainScoreboard.add(player.getUniqueId());
}
else {
playersOnMainScoreboard.remove(player.getUniqueId());
}
return isOnMain;
}
public static void sendAllNpcTeamsTo(Player player, int mode) {
for (Player npcPlayer : PlayerUpdateTask.getRegisteredPlayerNPCs()) {
NPC npc = ((NPCHolder) npcPlayer).getNPC();
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)
continue;
NMS.sendTeamPacket(player, team, mode);
}
}
/**
* @param mode 0 for create, 1 for remove, 2 for update
*/
public static void sendTeamPacketToAll(Team team, int mode) {
for (Player player : Bukkit.getOnlinePlayers()) {
boolean wasOnMain = playersOnMainScoreboard.contains(player.getUniqueId());
if (!isPlayerMainScoreboard(player)) {
if (wasOnMain) {
sendAllNpcTeamsTo(player, 0);
}
else {
NMS.sendTeamPacket(player, team, mode);
}
}
}
}
private static final Set<UUID> playersOnMainScoreboard = Sets.newHashSet();
private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static String MINECRAFT_REVISION;
private static final Pattern NON_ALPHABET_MATCHER = Pattern.compile(".*[^A-Za-z0-9_].*");

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -861,7 +861,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -871,7 +871,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.invoke(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -928,7 +928,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -938,7 +938,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.invoke(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -925,7 +925,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -935,7 +935,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.invoke(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -960,7 +960,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -970,7 +970,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.invoke(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -1023,7 +1023,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -1033,7 +1033,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.invoke(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -1026,7 +1026,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -1036,7 +1036,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.invoke(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -84,9 +84,7 @@ public class HumanController extends AbstractEntityController {
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 0);
}
}
}, 20);
@ -115,6 +113,7 @@ public class HumanController extends AbstractEntityController {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);

View File

@ -788,7 +788,7 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);
@ -798,7 +798,7 @@ public class NMSImpl implements NMSBridge {
try {
ScoreboardTeam nmsTeam = (ScoreboardTeam) TEAM_FIELD.get(team);
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, 0));
sendPacket(recipient, new PacketPlayOutScoreboardTeam(nmsTeam, mode));
} catch (Throwable e) {
e.printStackTrace();
}