2019-09-25 21:52:28 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2019-09-25 21:52:28 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.scoreboard;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreboardPosition;
|
2021-09-29 18:36:27 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
|
2019-09-25 21:52:28 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
|
|
|
|
|
|
|
import java.util.Map;
|
2020-08-28 15:47:52 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
@Getter
|
2020-11-09 09:34:27 +00:00
|
|
|
public final class Objective {
|
2020-08-28 15:47:52 +00:00
|
|
|
private final Scoreboard scoreboard;
|
|
|
|
private final long id;
|
|
|
|
private boolean active = true;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
@Setter
|
|
|
|
private UpdateType updateType = UpdateType.ADD;
|
2020-11-09 09:34:27 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
private String objectiveName;
|
2020-11-09 09:34:27 +00:00
|
|
|
private ScoreboardPosition displaySlot;
|
2020-08-28 15:47:52 +00:00
|
|
|
private String displaySlotName;
|
2019-10-04 17:25:08 +00:00
|
|
|
private String displayName = "unknown";
|
|
|
|
private int type = 0; // 0 = integer, 1 = heart
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
private Map<String, Score> scores = new ConcurrentHashMap<>();
|
2019-09-25 21:52:28 +00:00
|
|
|
|
2019-10-04 17:25:08 +00:00
|
|
|
private Objective(Scoreboard scoreboard) {
|
|
|
|
this.id = scoreboard.getNextId().getAndIncrement();
|
|
|
|
this.scoreboard = scoreboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* /!\ This method is made for temporary objectives until the real objective is received
|
2020-08-28 15:47:52 +00:00
|
|
|
*
|
|
|
|
* @param scoreboard the scoreboard
|
2020-04-22 06:03:46 +00:00
|
|
|
* @param objectiveName the name of the objective
|
2019-10-04 17:25:08 +00:00
|
|
|
*/
|
|
|
|
public Objective(Scoreboard scoreboard, String objectiveName) {
|
|
|
|
this(scoreboard);
|
|
|
|
this.objectiveName = objectiveName;
|
2020-08-28 15:47:52 +00:00
|
|
|
this.active = false;
|
2019-10-04 17:25:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Objective(Scoreboard scoreboard, String objectiveName, ScoreboardPosition displaySlot, String displayName, int type) {
|
2019-10-04 17:25:08 +00:00
|
|
|
this(scoreboard);
|
2019-09-25 21:52:28 +00:00
|
|
|
this.objectiveName = objectiveName;
|
2021-09-29 18:36:27 +00:00
|
|
|
this.displaySlot = displaySlot;
|
2020-08-28 15:47:52 +00:00
|
|
|
this.displaySlotName = translateDisplaySlot(displaySlot);
|
2019-09-25 21:52:28 +00:00
|
|
|
this.displayName = displayName;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:34:27 +00:00
|
|
|
private static String translateDisplaySlot(ScoreboardPosition displaySlot) {
|
2021-09-10 18:10:56 +00:00
|
|
|
return switch (displaySlot) {
|
|
|
|
case BELOW_NAME -> "belowname";
|
|
|
|
case PLAYER_LIST -> "list";
|
|
|
|
default -> "sidebar";
|
|
|
|
};
|
2020-11-09 09:34:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public void registerScore(String id, int score) {
|
|
|
|
if (!scores.containsKey(id)) {
|
2020-11-09 09:34:27 +00:00
|
|
|
long scoreId = scoreboard.getNextId().getAndIncrement();
|
|
|
|
Score scoreObject = new Score(scoreId, id)
|
2019-10-04 17:25:08 +00:00
|
|
|
.setScore(score)
|
2020-10-08 23:30:05 +00:00
|
|
|
.setTeam(scoreboard.getTeamFor(id))
|
|
|
|
.setUpdateType(UpdateType.ADD);
|
2020-11-09 09:34:27 +00:00
|
|
|
scores.put(id, scoreObject);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setScore(String id, int score) {
|
2020-11-09 09:34:27 +00:00
|
|
|
Score stored = scores.get(id);
|
|
|
|
if (stored != null) {
|
|
|
|
stored.setScore(score)
|
|
|
|
.setUpdateType(UpdateType.UPDATE);
|
2020-08-28 15:47:52 +00:00
|
|
|
return;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
2020-08-28 15:47:52 +00:00
|
|
|
registerScore(id, score);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
public void removeScore(String id) {
|
2020-11-09 09:34:27 +00:00
|
|
|
Score stored = scores.get(id);
|
|
|
|
if (stored != null) {
|
|
|
|
stored.setUpdateType(UpdateType.REMOVE);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
/**
|
|
|
|
* Used internally to remove a score from the score map
|
|
|
|
*/
|
|
|
|
public void removeScore0(String id) {
|
2019-09-25 21:52:28 +00:00
|
|
|
scores.remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Objective setDisplayName(String displayName) {
|
|
|
|
this.displayName = displayName;
|
2020-08-28 15:47:52 +00:00
|
|
|
if (updateType == UpdateType.NOTHING) {
|
|
|
|
updateType = UpdateType.UPDATE;
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Objective setType(int type) {
|
|
|
|
this.type = type;
|
2020-08-28 15:47:52 +00:00
|
|
|
if (updateType == UpdateType.NOTHING) {
|
|
|
|
updateType = UpdateType.UPDATE;
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
return this;
|
|
|
|
}
|
2019-10-04 17:25:08 +00:00
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
public void setActive(ScoreboardPosition displaySlot) {
|
|
|
|
if (!active) {
|
|
|
|
active = true;
|
2021-09-29 18:36:27 +00:00
|
|
|
this.displaySlot = displaySlot;
|
2020-08-28 15:47:52 +00:00
|
|
|
displaySlotName = translateDisplaySlot(displaySlot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:36:27 +00:00
|
|
|
/**
|
|
|
|
* The objective will be removed on the next update
|
|
|
|
*/
|
|
|
|
public void pendingRemove() {
|
|
|
|
updateType = UpdateType.REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TeamColor getTeamColor() {
|
|
|
|
return switch (displaySlot) {
|
|
|
|
case SIDEBAR_TEAM_RED -> TeamColor.RED;
|
|
|
|
case SIDEBAR_TEAM_AQUA -> TeamColor.AQUA;
|
|
|
|
case SIDEBAR_TEAM_BLUE -> TeamColor.BLUE;
|
|
|
|
case SIDEBAR_TEAM_GOLD -> TeamColor.GOLD;
|
|
|
|
case SIDEBAR_TEAM_GRAY -> TeamColor.GRAY;
|
|
|
|
case SIDEBAR_TEAM_BLACK -> TeamColor.BLACK;
|
|
|
|
case SIDEBAR_TEAM_GREEN -> TeamColor.GREEN;
|
|
|
|
case SIDEBAR_TEAM_WHITE -> TeamColor.WHITE;
|
|
|
|
case SIDEBAR_TEAM_YELLOW -> TeamColor.YELLOW;
|
|
|
|
case SIDEBAR_TEAM_DARK_RED -> TeamColor.DARK_RED;
|
|
|
|
case SIDEBAR_TEAM_DARK_AQUA -> TeamColor.DARK_AQUA;
|
|
|
|
case SIDEBAR_TEAM_DARK_BLUE -> TeamColor.DARK_BLUE;
|
|
|
|
case SIDEBAR_TEAM_DARK_GRAY -> TeamColor.DARK_GRAY;
|
|
|
|
case SIDEBAR_TEAM_DARK_GREEN -> TeamColor.DARK_GREEN;
|
|
|
|
case SIDEBAR_TEAM_DARK_PURPLE -> TeamColor.DARK_PURPLE;
|
|
|
|
case SIDEBAR_TEAM_LIGHT_PURPLE -> TeamColor.LIGHT_PURPLE;
|
|
|
|
default -> null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
public void removed() {
|
2021-09-29 18:36:27 +00:00
|
|
|
active = false;
|
|
|
|
updateType = UpdateType.REMOVE;
|
2020-08-28 15:47:52 +00:00
|
|
|
scores = null;
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|