2019-07-30 02:47:49 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2019-07-30 02:47:49 +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-07-30 02:47:49 +00:00
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ScoreInfo;
|
2019-07-30 02:47:49 +00:00
|
|
|
import lombok.Getter;
|
2019-09-25 21:52:28 +00:00
|
|
|
import lombok.experimental.Accessors;
|
2019-07-30 02:47:49 +00:00
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
@Getter
|
2019-09-25 21:52:28 +00:00
|
|
|
@Accessors(chain = true)
|
2020-11-09 09:34:27 +00:00
|
|
|
public final class Score {
|
2020-08-28 15:47:52 +00:00
|
|
|
private final long id;
|
|
|
|
private final String name;
|
2020-11-09 09:34:27 +00:00
|
|
|
private ScoreInfo cachedInfo;
|
|
|
|
|
2020-12-14 23:22:31 +00:00
|
|
|
/**
|
|
|
|
* Changes that have been made since the last cached data.
|
|
|
|
*/
|
2021-09-29 18:36:27 +00:00
|
|
|
private final Score.ScoreData currentData;
|
2020-12-14 23:22:31 +00:00
|
|
|
/**
|
|
|
|
* The data that is currently displayed to the Bedrock client.
|
|
|
|
*/
|
|
|
|
private Score.ScoreData cachedData;
|
2020-11-09 09:34:27 +00:00
|
|
|
|
|
|
|
public Score(long id, String name) {
|
|
|
|
this.id = id;
|
2019-09-25 21:52:28 +00:00
|
|
|
this.name = name;
|
2020-11-09 09:34:27 +00:00
|
|
|
this.currentData = new ScoreData();
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getDisplayName() {
|
2020-11-09 09:34:27 +00:00
|
|
|
Team team = cachedData.team;
|
2020-08-28 15:47:52 +00:00
|
|
|
if (team != null) {
|
2020-11-09 09:34:27 +00:00
|
|
|
return team.getDisplayName(name);
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
return name;
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|
2019-10-04 17:25:08 +00:00
|
|
|
|
2021-10-04 18:46:38 +00:00
|
|
|
public int getScore() {
|
|
|
|
return currentData.getScore();
|
|
|
|
}
|
|
|
|
|
2019-10-04 17:25:08 +00:00
|
|
|
public Score setScore(int score) {
|
2020-11-09 09:34:27 +00:00
|
|
|
currentData.score = score;
|
2020-08-28 15:47:52 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:34:27 +00:00
|
|
|
public Team getTeam() {
|
|
|
|
return currentData.team;
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:47:52 +00:00
|
|
|
public Score setTeam(Team team) {
|
2020-11-09 09:34:27 +00:00
|
|
|
if (currentData.team != null && team != null) {
|
|
|
|
if (!currentData.team.equals(team)) {
|
|
|
|
currentData.team = team;
|
2021-09-29 18:36:27 +00:00
|
|
|
setUpdateType(UpdateType.UPDATE);
|
2020-08-28 15:47:52 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// simplified from (this.team != null && team == null) || (this.team == null && team != null)
|
2020-11-09 09:34:27 +00:00
|
|
|
if (currentData.team != null || team != null) {
|
|
|
|
currentData.team = team;
|
2021-09-29 18:36:27 +00:00
|
|
|
setUpdateType(UpdateType.UPDATE);
|
2020-11-09 09:34:27 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public UpdateType getUpdateType() {
|
2020-12-14 23:22:31 +00:00
|
|
|
return currentData.updateType;
|
2020-11-09 09:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Score setUpdateType(UpdateType updateType) {
|
|
|
|
if (updateType != UpdateType.NOTHING) {
|
2021-10-04 18:46:38 +00:00
|
|
|
currentData.changed = true;
|
2020-08-28 15:47:52 +00:00
|
|
|
}
|
2020-11-09 09:34:27 +00:00
|
|
|
currentData.updateType = updateType;
|
2019-10-04 17:25:08 +00:00
|
|
|
return this;
|
|
|
|
}
|
2020-08-28 15:47:52 +00:00
|
|
|
|
2020-11-09 09:34:27 +00:00
|
|
|
public boolean shouldUpdate() {
|
2021-10-04 18:46:38 +00:00
|
|
|
return cachedData == null || currentData.changed ||
|
2020-11-09 09:34:27 +00:00
|
|
|
(currentData.team != null && currentData.team.shouldUpdate());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void update(String objectiveName) {
|
|
|
|
if (cachedData == null) {
|
|
|
|
cachedData = new ScoreData();
|
|
|
|
cachedData.updateType = UpdateType.ADD;
|
|
|
|
if (currentData.updateType == UpdateType.REMOVE) {
|
|
|
|
cachedData.updateType = UpdateType.REMOVE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cachedData.updateType = currentData.updateType;
|
|
|
|
}
|
|
|
|
|
2021-10-04 18:46:38 +00:00
|
|
|
currentData.changed = false;
|
2020-11-09 09:34:27 +00:00
|
|
|
cachedData.team = currentData.team;
|
|
|
|
cachedData.score = currentData.score;
|
|
|
|
|
|
|
|
String name = this.name;
|
|
|
|
if (cachedData.team != null) {
|
|
|
|
cachedData.team.prepareUpdate();
|
|
|
|
name = cachedData.team.getDisplayName(name);
|
|
|
|
}
|
|
|
|
cachedInfo = new ScoreInfo(id, objectiveName, cachedData.score, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
public static final class ScoreData {
|
2021-09-29 18:36:27 +00:00
|
|
|
private UpdateType updateType;
|
2021-10-04 18:46:38 +00:00
|
|
|
private boolean changed;
|
2020-11-09 09:34:27 +00:00
|
|
|
|
|
|
|
private Team team;
|
|
|
|
private int score;
|
|
|
|
|
2021-09-29 18:36:27 +00:00
|
|
|
private ScoreData() {
|
2020-11-09 09:34:27 +00:00
|
|
|
updateType = UpdateType.ADD;
|
|
|
|
}
|
2020-08-28 15:47:52 +00:00
|
|
|
}
|
2019-07-30 02:47:49 +00:00
|
|
|
}
|