From 60abdbac3c6786a186410bc1a2f298ac83ad57b6 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 19 Apr 2020 11:17:40 +0200 Subject: [PATCH] Fix NullPointerException with invalid scoreboard teams --- .../java/scoreboard/JavaTeamTranslator.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/scoreboard/JavaTeamTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/scoreboard/JavaTeamTranslator.java index c9d1ccfe2..d3bc6b4e7 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/scoreboard/JavaTeamTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/scoreboard/JavaTeamTranslator.java @@ -33,6 +33,7 @@ import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.scoreboard.Scoreboard; +import org.geysermc.connector.scoreboard.Team; import org.geysermc.connector.scoreboard.UpdateType; import org.geysermc.connector.utils.MessageUtils; @@ -45,9 +46,10 @@ public class JavaTeamTranslator extends PacketTranslator { @Override public void translate(ServerTeamPacket packet, GeyserSession session) { - GeyserConnector.getInstance().getLogger().debug("Team packet " + packet.getTeamName() + " " + packet.getAction()+" "+ Arrays.toString(packet.getPlayers())); + GeyserConnector.getInstance().getLogger().debug("Team packet " + packet.getTeamName() + " " + packet.getAction() + " " + Arrays.toString(packet.getPlayers())); Scoreboard scoreboard = session.getScoreboardCache().getScoreboard(); + Team team = scoreboard.getTeam(packet.getTeamName()); switch (packet.getAction()) { case CREATE: scoreboard.registerNewTeam(packet.getTeamName(), toPlayerSet(packet.getPlayers())) @@ -57,22 +59,33 @@ public class JavaTeamTranslator extends PacketTranslator { .setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix())); break; case UPDATE: - scoreboard.getTeam(packet.getTeamName()) - .setName(MessageUtils.getBedrockMessage(packet.getDisplayName())) - .setColor(packet.getColor()) - .setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix())) - .setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix())) - .setUpdateType(UpdateType.UPDATE); + if (team != null) { + team.setName(MessageUtils.getBedrockMessage(packet.getDisplayName())) + .setColor(packet.getColor()) + .setPrefix(MessageUtils.getBedrockMessage(packet.getPrefix())) + .setSuffix(MessageUtils.getBedrockMessage(packet.getSuffix())) + .setUpdateType(UpdateType.UPDATE); + } else { + GeyserConnector.getInstance().getLogger().error("Error while translating Team Packet " + packet.getAction() + "! Scoreboard Team " + packet.getTeamName() + " is not registered."); + } break; case ADD_PLAYER: - scoreboard.getTeam(packet.getTeamName()).addEntities(packet.getPlayers()); + if(team != null){ + team.addEntities(packet.getPlayers()); + } else { + GeyserConnector.getInstance().getLogger().error("Error while translating Team Packet " + packet.getAction() + "! Scoreboard Team " + packet.getTeamName() + " is not registered."); + } break; case REMOVE_PLAYER: - scoreboard.getTeam(packet.getTeamName()).removeEntities(packet.getPlayers()); + if(team != null){ + team.removeEntities(packet.getPlayers()); + } else { + GeyserConnector.getInstance().getLogger().error("Error while translating Team Packet " + packet.getAction() + "! Scoreboard Team " + packet.getTeamName() + " is not registered."); + } break; case REMOVE: - scoreboard.removeTeam(packet.getTeamName()); - break; + scoreboard.removeTeam(packet.getTeamName()); + break; } scoreboard.onUpdate(); }