From df9cdfa5486e1548d146262cb13f5bf206112755 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 14 May 2021 16:31:22 +0200 Subject: [PATCH] refactor some scoreboard packets --- .../data/scoreboard/ScoreboardManager.java | 45 ------- .../data/scoreboard/ScoreboardManager.kt | 18 +++ .../data/scoreboard/ScoreboardObjective.java | 63 --------- ...boardScore.java => ScoreboardObjective.kt} | 40 ++---- .../data/scoreboard/ScoreboardScore.kt | 15 +++ .../s2c/play/PacketScoreboardObjective.java | 126 ------------------ .../s2c/play/PacketScoreboardUpdateScore.java | 97 -------------- ...BorderS2CFactory.kt => WorldBorderS2CF.kt} | 2 +- .../PacketScoreboardDisplayScoreboard.java | 4 +- .../play/{ => scoreboard}/PacketTeams.java | 14 +- .../CreateScoreboardObjectiveS2CP.kt | 71 ++++++++++ .../RemoveScoreboardObjectiveS2CP.kt | 33 +++++ .../objective/ScoreboardObjectiveS2CF.kt | 50 +++++++ .../UpdateScoreboardObjectiveS2CP.kt | 59 ++++++++ .../score/PutScoreboardScoreS2CP.kt | 41 ++++++ .../score/RemoveScoreboardScoreS2CP.kt | 41 ++++++ .../scoreboard/score/ScoreboardScoreS2CF.kt | 41 ++++++ .../minosoft/protocol/protocol/PacketTypes.kt | 10 +- 18 files changed, 395 insertions(+), 375 deletions(-) delete mode 100644 src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.java create mode 100644 src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.kt delete mode 100644 src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.java rename src/main/java/de/bixilon/minosoft/data/scoreboard/{ScoreboardScore.java => ScoreboardObjective.kt} (52%) create mode 100644 src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.kt delete mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardObjective.java delete mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardUpdateScore.java rename src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/{WorldBorderS2CFactory.kt => WorldBorderS2CF.kt} (98%) rename src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/{ => scoreboard}/PacketScoreboardDisplayScoreboard.java (95%) rename src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/{ => scoreboard}/PacketTeams.java (91%) create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/CreateScoreboardObjectiveS2CP.kt create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/RemoveScoreboardObjectiveS2CP.kt create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/ScoreboardObjectiveS2CF.kt create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/UpdateScoreboardObjectiveS2CP.kt create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/PutScoreboardScoreS2CP.kt create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/RemoveScoreboardScoreS2CP.kt create mode 100644 src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/ScoreboardScoreS2CF.kt diff --git a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.java b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.java deleted file mode 100644 index 29a6c689e..000000000 --- a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.data.scoreboard; - -import java.util.HashMap; - -public class ScoreboardManager { - private final HashMap teams = new HashMap<>(); - private final HashMap objectives = new HashMap<>(); - - public void addTeam(Team team) { - this.teams.put(team.getName(), team); - } - - public Team getTeam(String name) { - return this.teams.get(name); - } - - public void removeTeam(String name) { - this.teams.remove(name); - } - - public void addObjective(ScoreboardObjective objective) { - this.objectives.put(objective.getObjectiveName(), objective); - } - - public void removeObjective(String name) { - this.objectives.remove(name); - } - - public ScoreboardObjective getObjective(String objective) { - return this.objectives.get(objective); - } -} diff --git a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.kt b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.kt new file mode 100644 index 000000000..8a791cfaa --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardManager.kt @@ -0,0 +1,18 @@ +/* + * Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.scoreboard + +class ScoreboardManager { + val teams: MutableMap = mutableMapOf() + val objectives: MutableMap = mutableMapOf() +} diff --git a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.java b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.java deleted file mode 100644 index e8fe3bbcc..000000000 --- a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.data.scoreboard; - -import de.bixilon.minosoft.data.text.ChatComponent; - -import java.util.HashMap; - -public class ScoreboardObjective { - private final String objectiveName; - private final HashMap scores = new HashMap<>(); - ChatComponent objectiveValue; - - public ScoreboardObjective(String objectiveName, ChatComponent objectiveValue) { - this.objectiveName = objectiveName; - this.objectiveValue = objectiveValue; - } - - public String getObjectiveName() { - return this.objectiveName; - } - - public ChatComponent getObjectiveValue() { - return this.objectiveValue; - } - - public HashMap getScores() { - return this.scores; - } - - public void addScore(ScoreboardScore score) { - if (this.scores.containsKey(score.getItemName())) { - // update - this.scores.get(score.getItemName()).setScoreName(score.getScoreName()); - this.scores.get(score.getItemName()).setScore(score.getScore()); - return; - } - this.scores.put(score.getItemName(), score); - } - - public void removeScore(String itemName) { - this.scores.remove(itemName); - } - - public ScoreboardScore getScore(String itemName) { - return this.scores.get(itemName); - } - - public void setValue(ChatComponent value) { - this.objectiveValue = value; - } -} diff --git a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.java b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt similarity index 52% rename from src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.java rename to src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt index 9be50ea29..a2f657aa2 100644 --- a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.java +++ b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt @@ -10,37 +10,15 @@ * * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ +package de.bixilon.minosoft.data.scoreboard -package de.bixilon.minosoft.data.scoreboard; +import de.bixilon.minosoft.data.text.ChatComponent +import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective.CreateScoreboardObjectiveS2CP -public class ScoreboardScore { - private final String itemName; - String scoreName; - int score; - - public ScoreboardScore(String itemName, String scoreName, int score) { - this.itemName = itemName; - this.scoreName = scoreName; - this.score = score; - } - - public String getItemName() { - return this.itemName; - } - - public String getScoreName() { - return this.scoreName; - } - - public void setScoreName(String scoreName) { - this.scoreName = scoreName; - } - - public int getScore() { - return this.score; - } - - public void setScore(int score) { - this.score = score; - } +class ScoreboardObjective( + val name: String, + var displayName: ChatComponent, + var unit: CreateScoreboardObjectiveS2CP.ObjectiveUnits, +) { + val scores: MutableMap = mutableMapOf() } diff --git a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.kt b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.kt new file mode 100644 index 000000000..f2a2d48c5 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardScore.kt @@ -0,0 +1,15 @@ +/* + * Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.scoreboard + +class ScoreboardScore(val entity: String, var objective: String, var score: Int) diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardObjective.java b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardObjective.java deleted file mode 100644 index 189681521..000000000 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardObjective.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.protocol.packets.s2c.play; - -import de.bixilon.minosoft.data.scoreboard.ScoreboardObjective; -import de.bixilon.minosoft.data.text.ChatComponent; -import de.bixilon.minosoft.protocol.network.connection.PlayConnection; -import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket; -import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer; -import de.bixilon.minosoft.util.logging.Log; - -import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.*; - -public class PacketScoreboardObjective extends PlayS2CPacket { - private final String name; - private final ScoreboardObjectiveActions action; - private ChatComponent value; - private ScoreboardObjectiveTypes type; - - public PacketScoreboardObjective(PlayInByteBuffer buffer) { - this.name = buffer.readString(); - if (buffer.getVersionId() < V_14W04A) { // ToDo - this.value = buffer.readChatComponent(); - } - this.action = ScoreboardObjectiveActions.byId(buffer.readUnsignedByte()); - if (this.action == ScoreboardObjectiveActions.CREATE || this.action == ScoreboardObjectiveActions.UPDATE) { - if (buffer.getVersionId() >= V_14W04A) { // ToDo - this.value = buffer.readChatComponent(); - } - if (buffer.getVersionId() >= V_14W08A) { - if (buffer.getVersionId() >= V_17W47A && buffer.getVersionId() < V_17W49A) { - // got removed in these 3 versions - return; - } - if (buffer.getVersionId() < V_17W49A) { - this.type = ScoreboardObjectiveTypes.byName(buffer.readString()); - } else { - this.type = ScoreboardObjectiveTypes.byId(buffer.readVarInt()); - } - } - } - return; - } - - @Override - public void handle(PlayConnection connection) { - switch (getAction()) { - case CREATE -> connection.getScoreboardManager().addObjective(new ScoreboardObjective(getName(), getValue())); - case UPDATE -> connection.getScoreboardManager().getObjective(getName()).setValue(getValue()); - case REMOVE -> connection.getScoreboardManager().removeObjective(getName()); - } - } - - @Override - public void log() { - if (this.action == ScoreboardObjectiveActions.CREATE || this.action == ScoreboardObjectiveActions.UPDATE) { - Log.protocol(String.format("[IN] Received scoreboard objective action (action=%s, name=\"%s\", value=\"%s\", type=%s)", this.action, this.name, this.value.getAnsiColoredMessage(), this.type)); - } else { - Log.protocol(String.format("[IN] Received scoreboard objective action (action=%s, name=\"%s\")", this.action, this.name)); - } - } - - public String getName() { - return this.name; - } - - public ChatComponent getValue() { - return this.value; - } - - public ScoreboardObjectiveActions getAction() { - return this.action; - } - - public enum ScoreboardObjectiveActions { - CREATE, - REMOVE, - UPDATE; - - private static final ScoreboardObjectiveActions[] SCOREBOARD_OBJECTIVE_ACTIONS = values(); - - public static ScoreboardObjectiveActions byId(int id) { - return SCOREBOARD_OBJECTIVE_ACTIONS[id]; - } - } - - public enum ScoreboardObjectiveTypes { - INTEGER("integer"), - HEARTS("hearts"); - - private static final ScoreboardObjectiveTypes[] SCOREBOARD_OBJECTIVE_TYPES = values(); - private final String name; - - ScoreboardObjectiveTypes(String name) { - this.name = name; - } - - public static ScoreboardObjectiveTypes byName(String name) { - for (ScoreboardObjectiveTypes type : values()) { - if (type.getName().equals(name)) { - return type; - } - } - return null; - } - - public static ScoreboardObjectiveTypes byId(int id) { - return SCOREBOARD_OBJECTIVE_TYPES[id]; - } - - public String getName() { - return this.name; - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardUpdateScore.java b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardUpdateScore.java deleted file mode 100644 index d72c688c6..000000000 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardUpdateScore.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.protocol.packets.s2c.play; - -import de.bixilon.minosoft.data.scoreboard.ScoreboardObjective; -import de.bixilon.minosoft.data.scoreboard.ScoreboardScore; -import de.bixilon.minosoft.protocol.network.connection.PlayConnection; -import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket; -import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer; -import de.bixilon.minosoft.util.logging.Log; - -import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W04A; - -public class PacketScoreboardUpdateScore extends PlayS2CPacket { - private final String itemName; - private final ScoreboardUpdateScoreActions action; - private String scoreName; - private int scoreValue; - - public PacketScoreboardUpdateScore(PlayInByteBuffer buffer) { - this.itemName = buffer.readString(); - this.action = ScoreboardUpdateScoreActions.byId(buffer.readUnsignedByte()); - if (buffer.getVersionId() < V_14W04A) { // ToDo - if (this.action == ScoreboardUpdateScoreActions.REMOVE) { - return; - } - // not present id action == REMOVE - this.scoreName = buffer.readString(); - this.scoreValue = buffer.readInt(); - return; - } - this.scoreName = buffer.readString(); - - if (this.action == ScoreboardUpdateScoreActions.REMOVE) { - return; - } - // not present id action == REMOVE - this.scoreValue = buffer.readVarInt(); - } - - @Override - public void handle(PlayConnection connection) { - switch (getAction()) { - case CREATE_UPDATE -> connection.getScoreboardManager().getObjective(getScoreName()).addScore(new ScoreboardScore(getItemName(), getScoreName(), getScoreValue())); - case REMOVE -> { - ScoreboardObjective objective = connection.getScoreboardManager().getObjective(getScoreName()); - if (objective != null) { - // thanks mojang - objective.removeScore(getItemName()); - } - } - } - } - - @Override - public void log() { - Log.protocol(String.format("[IN] Received scoreboard score update (itemName=\"%s\", action=%s, scoreName=\"%s\", scoreValue=%d", this.itemName, this.action, this.scoreName, this.scoreValue)); - } - - public String getItemName() { - return this.itemName; - } - - public ScoreboardUpdateScoreActions getAction() { - return this.action; - } - - public String getScoreName() { - return this.scoreName; - } - - public int getScoreValue() { - return this.scoreValue; - } - - public enum ScoreboardUpdateScoreActions { - CREATE_UPDATE, - REMOVE; - - private static final ScoreboardUpdateScoreActions[] SCOREBOARD_UPDATE_SCORE_ACTIONS = values(); - - public static ScoreboardUpdateScoreActions byId(int id) { - return SCOREBOARD_UPDATE_SCORE_ACTIONS[id]; - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/WorldBorderS2CFactory.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/WorldBorderS2CF.kt similarity index 98% rename from src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/WorldBorderS2CFactory.kt rename to src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/WorldBorderS2CF.kt index 7383a3e32..d02ad7d41 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/WorldBorderS2CFactory.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/border/WorldBorderS2CF.kt @@ -18,7 +18,7 @@ import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer import de.bixilon.minosoft.util.KUtil import de.bixilon.minosoft.util.enum.ValuesEnum -object WorldBorderS2CFactory { +object WorldBorderS2CF { // this function was signed by Maximilian Rosenmüller // and that means A LOT! diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardDisplayScoreboard.java b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/PacketScoreboardDisplayScoreboard.java similarity index 95% rename from src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardDisplayScoreboard.java rename to src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/PacketScoreboardDisplayScoreboard.java index 0a2ea025d..c5349fc14 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketScoreboardDisplayScoreboard.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/PacketScoreboardDisplayScoreboard.java @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020 Moritz Zwerger + * Copyright (C) 2021 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -11,7 +11,7 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.protocol.packets.s2c.play; +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard; import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket; import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer; diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketTeams.java b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/PacketTeams.java similarity index 91% rename from src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketTeams.java rename to src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/PacketTeams.java index f29c0eca9..784fd9b70 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PacketTeams.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/PacketTeams.java @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020 Moritz Zwerger + * Copyright (C) 2021 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -11,7 +11,7 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.protocol.packets.s2c.play; +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard; import de.bixilon.minosoft.data.scoreboard.Team; import de.bixilon.minosoft.data.text.ChatCode; @@ -89,11 +89,11 @@ public class PacketTeams extends PlayS2CPacket { @Override public void handle(PlayConnection connection) { switch (getAction()) { - case CREATE -> connection.getScoreboardManager().addTeam(new Team(getName(), getDisplayName(), getPrefix(), getSuffix(), isFriendlyFireEnabled(), isSeeingFriendlyInvisibles(), getPlayerNames())); - case INFORMATION_UPDATE -> connection.getScoreboardManager().getTeam(getName()).updateInformation(getDisplayName(), getPrefix(), getSuffix(), isFriendlyFireEnabled(), isSeeingFriendlyInvisibles()); - case REMOVE -> connection.getScoreboardManager().removeTeam(getName()); - case PLAYER_ADD -> connection.getScoreboardManager().getTeam(getName()).addPlayers(Arrays.asList(getPlayerNames())); - case PLAYER_REMOVE -> connection.getScoreboardManager().getTeam(getName()).removePlayers(Arrays.asList(getPlayerNames())); + case CREATE -> connection.getScoreboardManager().getTeams().put(getName(), new Team(getName(), getDisplayName(), getPrefix(), getSuffix(), isFriendlyFireEnabled(), isSeeingFriendlyInvisibles(), getPlayerNames())); + case INFORMATION_UPDATE -> connection.getScoreboardManager().getTeams().get(getName()).updateInformation(getDisplayName(), getPrefix(), getSuffix(), isFriendlyFireEnabled(), isSeeingFriendlyInvisibles()); + case REMOVE -> connection.getScoreboardManager().getTeams().remove(getName()); + case PLAYER_ADD -> connection.getScoreboardManager().getTeams().get(getName()).addPlayers(Arrays.asList(getPlayerNames())); + case PLAYER_REMOVE -> connection.getScoreboardManager().getTeams().get(getName()).removePlayers(Arrays.asList(getPlayerNames())); } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/CreateScoreboardObjectiveS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/CreateScoreboardObjectiveS2CP.kt new file mode 100644 index 000000000..6c22ea729 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/CreateScoreboardObjectiveS2CP.kt @@ -0,0 +1,71 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective + +import de.bixilon.minosoft.data.scoreboard.ScoreboardObjective +import de.bixilon.minosoft.data.text.ChatComponent +import de.bixilon.minosoft.protocol.network.connection.PlayConnection +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.protocol.protocol.ProtocolVersions +import de.bixilon.minosoft.util.KUtil +import de.bixilon.minosoft.util.enum.ValuesEnum +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType + +class CreateScoreboardObjectiveS2CP(val objective: String, private var _displayName: ChatComponent?, buffer: PlayInByteBuffer) : PlayS2CPacket() { + val displayName: ChatComponent + get() = _displayName!! + var unit: ObjectiveUnits = ObjectiveUnits.INTEGER + private set + + init { + if (buffer.versionId >= ProtocolVersions.V_14W04A) { // ToDo + this._displayName = buffer.readChatComponent() + } + if (buffer.versionId >= ProtocolVersions.V_14W08A) { + when { + buffer.versionId >= ProtocolVersions.V_17W47A && buffer.versionId < ProtocolVersions.V_17W49A -> { + } + buffer.versionId < ProtocolVersions.V_17W49A -> { + unit = ObjectiveUnits[buffer.readString()] + } + else -> { + unit = ObjectiveUnits[buffer.readVarInt()] + } + } + } + } + + override fun log() { + Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Create scoreboard objective (objective=$objective, displayName=$displayName, unit=$unit)" } + } + + override fun handle(connection: PlayConnection) { + connection.scoreboardManager.objectives[objective] = ScoreboardObjective(objective, displayName, unit) + } + + enum class ObjectiveUnits { + INTEGER, + HEARTS, + ; + + companion object : ValuesEnum { + override val VALUES: Array = values() + override val NAME_MAP: Map = KUtil.getEnumValues(VALUES) + } + } + +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/RemoveScoreboardObjectiveS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/RemoveScoreboardObjectiveS2CP.kt new file mode 100644 index 000000000..fdfdd5da6 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/RemoveScoreboardObjectiveS2CP.kt @@ -0,0 +1,33 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective + +import de.bixilon.minosoft.protocol.network.connection.PlayConnection +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType + +class RemoveScoreboardObjectiveS2CP(val objective: String, buffer: PlayInByteBuffer) : PlayS2CPacket() { + + override fun log() { + Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Remove scoreboard objective (objective=$objective)" } + } + + override fun handle(connection: PlayConnection) { + connection.scoreboardManager.objectives.remove(objective) + } + +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/ScoreboardObjectiveS2CF.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/ScoreboardObjectiveS2CF.kt new file mode 100644 index 000000000..1076fe288 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/ScoreboardObjectiveS2CF.kt @@ -0,0 +1,50 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective + +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.score.RemoveScoreboardScoreS2CP +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.protocol.protocol.ProtocolVersions +import de.bixilon.minosoft.util.KUtil +import de.bixilon.minosoft.util.enum.ValuesEnum + +object ScoreboardObjectiveS2CF { + + fun createPacket(buffer: PlayInByteBuffer): PlayS2CPacket { + val objective = buffer.readString() + val displayName = if (buffer.versionId < ProtocolVersions.V_14W04A) { // ToDo + buffer.readChatComponent() + } else { + null + } + return when (ScoreboardScoreActions[buffer.readUnsignedByte()]) { + ScoreboardScoreActions.CREATE -> CreateScoreboardObjectiveS2CP(objective, displayName, buffer) + ScoreboardScoreActions.REMOVE -> RemoveScoreboardScoreS2CP(objective, buffer) + ScoreboardScoreActions.UPDATE -> UpdateScoreboardObjectiveS2CP(objective, displayName, buffer) + } + } + + enum class ScoreboardScoreActions { + CREATE, + REMOVE, + UPDATE, + ; + + companion object : ValuesEnum { + override val VALUES: Array = values() + override val NAME_MAP: Map = KUtil.getEnumValues(VALUES) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/UpdateScoreboardObjectiveS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/UpdateScoreboardObjectiveS2CP.kt new file mode 100644 index 000000000..920da4004 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/objective/UpdateScoreboardObjectiveS2CP.kt @@ -0,0 +1,59 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective + +import de.bixilon.minosoft.data.text.ChatComponent +import de.bixilon.minosoft.protocol.network.connection.PlayConnection +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.protocol.protocol.ProtocolVersions +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType + +class UpdateScoreboardObjectiveS2CP(val objective: String, private var _displayName: ChatComponent?, buffer: PlayInByteBuffer) : PlayS2CPacket() { + val displayName: ChatComponent + get() = _displayName!! + var unit: CreateScoreboardObjectiveS2CP.ObjectiveUnits = CreateScoreboardObjectiveS2CP.ObjectiveUnits.INTEGER + private set + + init { + if (buffer.versionId >= ProtocolVersions.V_14W04A) { // ToDo + this._displayName = buffer.readChatComponent() + } + if (buffer.versionId >= ProtocolVersions.V_14W08A) { + when { + buffer.versionId >= ProtocolVersions.V_17W47A && buffer.versionId < ProtocolVersions.V_17W49A -> { + } + buffer.versionId < ProtocolVersions.V_17W49A -> { + unit = CreateScoreboardObjectiveS2CP.ObjectiveUnits[buffer.readString()] + } + else -> { + unit = CreateScoreboardObjectiveS2CP.ObjectiveUnits[buffer.readVarInt()] + } + } + } + } + + override fun log() { + Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Update scoreboard objective (objective=$objective, displayName=$displayName, unit=$unit)" } + } + + override fun handle(connection: PlayConnection) { + connection.scoreboardManager.objectives[objective]?.let { + it.displayName = displayName + it.unit = unit + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/PutScoreboardScoreS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/PutScoreboardScoreS2CP.kt new file mode 100644 index 000000000..6b7a6ca87 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/PutScoreboardScoreS2CP.kt @@ -0,0 +1,41 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.score + +import de.bixilon.minosoft.data.scoreboard.ScoreboardScore +import de.bixilon.minosoft.protocol.network.connection.PlayConnection +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.protocol.protocol.ProtocolVersions +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType + +class PutScoreboardScoreS2CP(val entity: String, buffer: PlayInByteBuffer) : PlayS2CPacket() { + val objective = buffer.readString() + val score: Int = if (buffer.versionId < ProtocolVersions.V_14W04A) { // ToDo + buffer.readInt() + } else { + buffer.readVarInt() + } + + override fun handle(connection: PlayConnection) { + connection.scoreboardManager.objectives[objective]?.scores?.put(entity, ScoreboardScore(entity, objective, score)) + } + + + override fun log() { + Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Put scoreboard score (entity=$entity, objective=$objective, score=$score)" } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/RemoveScoreboardScoreS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/RemoveScoreboardScoreS2CP.kt new file mode 100644 index 000000000..2fa2ced91 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/RemoveScoreboardScoreS2CP.kt @@ -0,0 +1,41 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.score + +import de.bixilon.minosoft.protocol.network.connection.PlayConnection +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.protocol.protocol.ProtocolVersions +import de.bixilon.minosoft.util.logging.Log +import de.bixilon.minosoft.util.logging.LogLevels +import de.bixilon.minosoft.util.logging.LogMessageType + +class RemoveScoreboardScoreS2CP(val entity: String, buffer: PlayInByteBuffer) : PlayS2CPacket() { + val objective = if (buffer.versionId >= ProtocolVersions.V_14W04A) { // ToDo + buffer.readString() + } else { + null + } + + override fun handle(connection: PlayConnection) { + objective?.let { + connection.scoreboardManager.objectives[objective]?.scores?.remove(entity) + } ?: TODO() + } + + + override fun log() { + Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Remove scoreboard score (entity=$entity, objective=$objective)" } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/ScoreboardScoreS2CF.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/ScoreboardScoreS2CF.kt new file mode 100644 index 000000000..1a326096b --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/scoreboard/score/ScoreboardScoreS2CF.kt @@ -0,0 +1,41 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.score + +import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.util.KUtil +import de.bixilon.minosoft.util.enum.ValuesEnum + +object ScoreboardScoreS2CF { + + fun createPacket(buffer: PlayInByteBuffer): PlayS2CPacket { + val entity = buffer.readString() + return when (ScoreboardScoreActions[buffer.readUnsignedByte()]) { + ScoreboardScoreActions.PUT -> PutScoreboardScoreS2CP(entity, buffer) + ScoreboardScoreActions.REMOVE -> RemoveScoreboardScoreS2CP(entity, buffer) + } + } + + enum class ScoreboardScoreActions { + PUT, + REMOVE, + ; + + companion object : ValuesEnum { + override val VALUES: Array = values() + override val NAME_MAP: Map = KUtil.getEnumValues(VALUES) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketTypes.kt b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketTypes.kt index 5aedb53e6..b4581b3e5 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketTypes.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketTypes.kt @@ -42,6 +42,10 @@ import de.bixilon.minosoft.protocol.packets.s2c.play.combat.CombatEventEndS2CP import de.bixilon.minosoft.protocol.packets.s2c.play.combat.CombatEventEnterS2CP import de.bixilon.minosoft.protocol.packets.s2c.play.combat.CombatEventKillS2CP import de.bixilon.minosoft.protocol.packets.s2c.play.combat.CombatEventS2CF +import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.PacketScoreboardDisplayScoreboard +import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.PacketTeams +import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective.ScoreboardObjectiveS2CF +import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.score.ScoreboardScoreS2CF import de.bixilon.minosoft.protocol.packets.s2c.play.title.* import de.bixilon.minosoft.protocol.packets.s2c.status.PacketStatusResponse import de.bixilon.minosoft.protocol.packets.s2c.status.StatusPongS2CP @@ -216,7 +220,7 @@ class PacketTypes { PLAY_RESPAWN({ RespawnS2CP(it) }, isThreadSafe = false), PLAY_ENTITY_HEAD_ROTATION({ EntityHeadRotationS2CP(it) }), PLAY_SELECT_ADVANCEMENT_TAB({ PacketSelectAdvancementTab(it) }), - PLAY_WORLD_BORDER({ WorldBorderS2CFactory.createPacket(it) }), + PLAY_WORLD_BORDER({ WorldBorderS2CF.createPacket(it) }), PLAY_WORLD_BORDER_INITIALIZE({ InitializeWorldBorderS2CPacket(it) }), PLAY_WORLD_BORDER_SET_CENTER({ SetCenterWorldBorderS2CPacket(it) }), PLAY_WORLD_BORDER_LERP_SIZE({ LerpSizeWorldBorderS2CPacket(it) }), @@ -233,10 +237,10 @@ class PacketTypes { PLAY_ENTITY_EQUIPMENT({ EntityEquipmentS2CP(it) }), PLAY_EXPERIENCE_SET({ ExperienceSetS2CP(it) }), PLAY_HEALTH_SET({ HealthSetS2CP(it) }), - PLAY_SCOREBOARD_OBJECTIVE({ PacketScoreboardObjective(it) }), + PLAY_SCOREBOARD_OBJECTIVE({ ScoreboardObjectiveS2CF.createPacket(it) }), PLAY_ENTITY_PASSENGER_SET({ EntityPassengerSetS2CP(it) }), PLAY_TEAMS({ PacketTeams(it) }), - PLAY_UPDATE_SCORE({ PacketScoreboardUpdateScore(it) }), + PLAY_UPDATE_SCORE({ ScoreboardScoreS2CF.createPacket(it) }), PLAY_SPAWN_POSITION_SET({ SpawnPositionSetS2CP(it) }), PLAY_WORLD_TIME_SET({ WorldTimeSetS2CP(it) }), PLAY_ENTITY_SOUND_EVENT({ EntitySoundEventS2CP(it) }),