2019-07-30 02:47:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-08-04 03:24:53 +00:00
|
|
|
package org.geysermc.connector.scoreboard;
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreboardPosition;
|
2019-07-30 02:47:49 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ScoreInfo;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.RemoveObjectivePacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.SetDisplayObjectivePacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.SetScorePacket;
|
|
|
|
import lombok.Getter;
|
2019-09-25 21:52:28 +00:00
|
|
|
import org.geysermc.api.Geyser;
|
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-07-30 02:47:49 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
import static org.geysermc.connector.scoreboard.UpdateType.*;
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
@Getter
|
|
|
|
public class Scoreboard {
|
2019-07-30 02:47:49 +00:00
|
|
|
private GeyserSession session;
|
2019-09-25 21:52:28 +00:00
|
|
|
private AtomicLong nextId = new AtomicLong(0);
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
private Map<String, Objective> objectives = new HashMap<>();
|
|
|
|
private Map<String, Team> teams = new HashMap<>();
|
2019-07-30 02:47:49 +00:00
|
|
|
|
|
|
|
public Scoreboard(GeyserSession session) {
|
|
|
|
this.session = session;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Objective registerNewObjective(String objectiveId, ScoreboardPosition displaySlot) {
|
|
|
|
Objective objective = new Objective(this, objectiveId, displaySlot, "unknown", 0);
|
|
|
|
if (objectives.containsKey(objectiveId)) despawnObjective(objectives.get(objectiveId));
|
|
|
|
objectives.put(objectiveId, objective);
|
|
|
|
return objective;
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Team registerNewTeam(String teamName, Set<String> players) {
|
|
|
|
if (teams.containsKey(teamName)) {
|
|
|
|
Geyser.getLogger().info("Ignoring team " + teamName + ". It overrides without removing old team.");
|
|
|
|
return getTeam(teamName);
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
Team team = new Team(this, teamName).setEntities(players);
|
|
|
|
teams.put(teamName, team);
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
for (Objective objective : objectives.values()) {
|
|
|
|
for (Score score : objective.getScores().values()) {
|
|
|
|
if (players.contains(score.getName())) {
|
|
|
|
score.setTeam(team).setUpdateType(ADD);
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
return team;
|
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Objective getObjective(String objectiveName) {
|
|
|
|
return objectives.get(objectiveName);
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Team getTeam(String teamName) {
|
|
|
|
return teams.get(teamName);
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void unregisterObjective(String objectiveName) {
|
2019-09-25 21:52:28 +00:00
|
|
|
Objective objective = getObjective(objectiveName);
|
|
|
|
if (objective != null) objective.setUpdateType(REMOVE);
|
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public void removeTeam(String teamName) {
|
|
|
|
if (teams.remove(teamName) != null) {
|
|
|
|
for (Objective objective : objectives.values()) {
|
|
|
|
for (Score score : objective.getScores().values()) {
|
|
|
|
if (score.getName().equals(teamName)) {
|
|
|
|
score.setTeam(null).setUpdateType(ADD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onUpdate() {
|
2019-09-25 21:52:28 +00:00
|
|
|
Set<Objective> changedObjectives = new HashSet<>();
|
|
|
|
List<ScoreInfo> addScores = new ArrayList<>();
|
|
|
|
List<ScoreInfo> removeScores = new ArrayList<>();
|
|
|
|
|
|
|
|
for (String objectiveId : new ArrayList<>(objectives.keySet())) {
|
|
|
|
Objective objective = objectives.get(objectiveId);
|
|
|
|
if (objective.getUpdateType() != NOTHING) changedObjectives.add(objective);
|
|
|
|
|
|
|
|
for (String identifier : new HashSet<>(objective.getScores().keySet())) {
|
|
|
|
Score score = objective.getScores().get(identifier);
|
|
|
|
|
|
|
|
boolean add = (objective.getUpdateType() != NOTHING && objective.getUpdateType() != REMOVE) && score.getUpdateType() != REMOVE || score.getUpdateType() == ADD;
|
|
|
|
boolean remove = (add && score.getUpdateType() != ADD && objective.getUpdateType() != ADD) || objective.getUpdateType() == REMOVE || score.getUpdateType() == REMOVE;
|
|
|
|
|
|
|
|
ScoreInfo info = new ScoreInfo(score.getId(), score.getObjective().getObjectiveName(), score.getScore(), score.getDisplayName());
|
|
|
|
if (add || (score.getTeam() != null && (score.getTeam().getUpdateType() == ADD || score.getTeam().getUpdateType() == UPDATE))) addScores.add(info);
|
|
|
|
if (remove || (score.getTeam() != null && score.getTeam().getUpdateType() != NOTHING)) removeScores.add(info);
|
|
|
|
|
|
|
|
if (score.getUpdateType() == REMOVE) {
|
|
|
|
objective.removeScore(score.getName());
|
|
|
|
}
|
2019-08-04 03:24:53 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
if (addScores.contains(info) || removeScores.contains(info)) changedObjectives.add(objective);
|
|
|
|
score.setUpdateType(NOTHING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Objective objective : changedObjectives) {
|
|
|
|
boolean update = objective.getUpdateType() == NOTHING || objective.getUpdateType() == UPDATE;
|
|
|
|
if (objective.getUpdateType() == REMOVE || update) {
|
|
|
|
RemoveObjectivePacket removeObjectivePacket = new RemoveObjectivePacket();
|
|
|
|
removeObjectivePacket.setObjectiveId(objective.getObjectiveName());
|
|
|
|
session.getUpstream().sendPacket(removeObjectivePacket);
|
|
|
|
if (objective.getUpdateType() == REMOVE) {
|
|
|
|
objectives.remove(objective.getObjectiveName()); // now we can deregister
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (objective.getUpdateType() == ADD || update) {
|
|
|
|
SetDisplayObjectivePacket displayObjectivePacket = new SetDisplayObjectivePacket();
|
|
|
|
displayObjectivePacket.setObjectiveId(objective.getObjectiveName());
|
|
|
|
displayObjectivePacket.setDisplayName(objective.getDisplayName());
|
|
|
|
displayObjectivePacket.setCriteria("dummy");
|
|
|
|
displayObjectivePacket.setDisplaySlot(objective.getDisplaySlot());
|
|
|
|
displayObjectivePacket.setSortOrder(1); // ??
|
|
|
|
session.getUpstream().sendPacket(displayObjectivePacket);
|
|
|
|
}
|
|
|
|
objective.setUpdateType(NOTHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!removeScores.isEmpty()) {
|
|
|
|
SetScorePacket setScorePacket = new SetScorePacket();
|
|
|
|
setScorePacket.setAction(SetScorePacket.Action.REMOVE);
|
|
|
|
setScorePacket.setInfos(removeScores);
|
|
|
|
session.getUpstream().sendPacket(setScorePacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!addScores.isEmpty()) {
|
|
|
|
SetScorePacket setScorePacket = new SetScorePacket();
|
|
|
|
setScorePacket.setAction(SetScorePacket.Action.SET);
|
|
|
|
setScorePacket.setInfos(addScores);
|
|
|
|
session.getUpstream().sendPacket(setScorePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void despawnObjective(Objective objective) {
|
2019-07-30 02:47:49 +00:00
|
|
|
RemoveObjectivePacket removeObjectivePacket = new RemoveObjectivePacket();
|
|
|
|
removeObjectivePacket.setObjectiveId(objective.getObjectiveName());
|
|
|
|
session.getUpstream().sendPacket(removeObjectivePacket);
|
2019-09-25 21:52:28 +00:00
|
|
|
objectives.remove(objective.getDisplayName());
|
|
|
|
|
|
|
|
List<ScoreInfo> toRemove = new ArrayList<>();
|
|
|
|
for (String identifier : objective.getScores().keySet()) {
|
|
|
|
Score score = objective.getScores().get(identifier);
|
|
|
|
toRemove.add(new ScoreInfo(
|
|
|
|
score.getId(), score.getObjective().getObjectiveName(),
|
|
|
|
0, ""
|
|
|
|
));
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
if (!toRemove.isEmpty()) {
|
2019-07-30 02:47:49 +00:00
|
|
|
SetScorePacket setScorePacket = new SetScorePacket();
|
2019-09-25 21:52:28 +00:00
|
|
|
setScorePacket.setAction(SetScorePacket.Action.REMOVE);
|
|
|
|
setScorePacket.setInfos(toRemove);
|
2019-07-30 02:47:49 +00:00
|
|
|
session.getUpstream().sendPacket(setScorePacket);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Team getTeamFor(String entity) {
|
|
|
|
for (Team team : teams.values()) {
|
|
|
|
if (team.getEntities().contains(entity)) {
|
|
|
|
return team;
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
return null;
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
|
|
|
}
|