Skip to content

Commit

Permalink
patch the majority of potential scoreboard packet errors (#2128)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 30, 2020
1 parent 27f4171 commit 981ade8
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 55 deletions.
14 changes: 2 additions & 12 deletions main/src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,8 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
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;
Expand Down Expand Up @@ -92,9 +90,7 @@ public void apply(Team team, boolean nameVisibility) {
}
}
}
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team);
}
Util.sendTeamPacketToAll(team, 2);
}

public void removeTag(String tag) {
Expand Down
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ public static void sendTabListRemove(Player recipient, Player listPlayer) {
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) {
Expand Down
2 changes: 1 addition & 1 deletion main/src/main/java/net/citizensnpcs/util/NMSBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public static void registerPlayer(org.bukkit.entity.Entity entity) {
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>();
Expand Down
46 changes: 46 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 All @@ -15,15 +16,18 @@
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
Expand Down Expand Up @@ -270,6 +274,48 @@ public static String[] splitPlayerName(String coloredName) {
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_].*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ public void sendTabListRemove(Player recipient, Collection<? extends SkinnableEn
}

@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);

Expand All @@ -871,7 +871,7 @@ public void sendTeamPacket(Player recipient, Team team) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ public void sendTabListRemove(Player recipient, Player listPlayer) {
}

@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);

Expand All @@ -938,7 +938,7 @@ public void sendTeamPacket(Player recipient, Team team) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ public void sendTabListRemove(Player recipient, Collection<? extends SkinnableEn
}

@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);

Expand All @@ -935,7 +935,7 @@ public void sendTeamPacket(Player recipient, Team team) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ public void sendTabListRemove(Player recipient, Collection<? extends SkinnableEn
}

@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);

Expand All @@ -970,7 +970,7 @@ public void sendTeamPacket(Player recipient, Team team) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ public void sendTabListRemove(Player recipient, Player listPlayer) {
}

@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);

Expand All @@ -1033,7 +1033,7 @@ public void sendTeamPacket(Player recipient, Team team) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ public void sendTabListRemove(Player recipient, Player listPlayer) {
}

@Override
public void sendTeamPacket(Player recipient, Team team) {
public void sendTeamPacket(Player recipient, Team team, int mode) {
Preconditions.checkNotNull(recipient);
Preconditions.checkNotNull(team);

Expand All @@ -1036,7 +1036,7 @@ public void sendTeamPacket(Player recipient, Team team) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void run() {

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);
Expand Down Expand Up @@ -115,6 +113,7 @@ public void remove() {
team.setSuffix("");
}
team.removePlayer(entity);
Util.sendTeamPacketToAll(team, 1);
}
}
NMS.removeFromWorld(entity);
Expand Down
Loading

0 comments on commit 981ade8

Please sign in to comment.