2019-09-25 21:52:28 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.scoreboard;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreboardPosition;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
public class Objective {
|
|
|
|
private Scoreboard scoreboard;
|
|
|
|
private long id;
|
2019-10-04 17:25:08 +00:00
|
|
|
private boolean temp;
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
@Setter
|
|
|
|
private UpdateType updateType = UpdateType.ADD;
|
|
|
|
private String objectiveName;
|
|
|
|
private String displaySlot;
|
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
|
|
|
|
|
|
|
private Map<String, Score> scores = new HashMap<>();
|
|
|
|
|
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-04-22 06:03:46 +00:00
|
|
|
* @param scoreboard the scoreboard
|
|
|
|
* @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;
|
|
|
|
this.temp = true;
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
public Objective(Scoreboard scoreboard, String objectiveName, ScoreboardPosition displaySlot, String displayName, int type) {
|
|
|
|
this(scoreboard, objectiveName, displaySlot.name().toLowerCase(), displayName, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Objective(Scoreboard scoreboard, String objectiveName, String 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;
|
|
|
|
this.displaySlot = displaySlot;
|
|
|
|
this.displayName = displayName;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerScore(String id, int score) {
|
|
|
|
if (!scores.containsKey(id)) {
|
2019-10-04 17:25:08 +00:00
|
|
|
Score score1 = new Score(this, id)
|
|
|
|
.setScore(score)
|
|
|
|
.setTeam(scoreboard.getTeamFor(id));
|
2019-09-25 21:52:28 +00:00
|
|
|
scores.put(id, score1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setScore(String id, int score) {
|
|
|
|
if (scores.containsKey(id)) {
|
|
|
|
scores.get(id).setScore(score).setUpdateType(UpdateType.ADD);
|
|
|
|
} else {
|
|
|
|
registerScore(id, score);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setScoreText(String oldText, String newText) {
|
|
|
|
if (!scores.containsKey(oldText) || oldText.equals(newText)) return;
|
|
|
|
Score oldScore = scores.get(oldText);
|
|
|
|
|
2019-10-04 17:25:08 +00:00
|
|
|
Score newScore = new Score(this, newText)
|
|
|
|
.setScore(oldScore.getScore())
|
|
|
|
.setTeam(scoreboard.getTeamFor(newText));
|
2019-09-25 21:52:28 +00:00
|
|
|
|
|
|
|
scores.put(newText, newScore);
|
|
|
|
oldScore.setUpdateType(UpdateType.REMOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getScore(String id) {
|
|
|
|
if (scores.containsKey(id)) {
|
|
|
|
return scores.get(id).getScore();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Score getScore(int line) {
|
|
|
|
for (Score score : scores.values()) {
|
|
|
|
if (score.getScore() == line) return score;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void resetScore(String id) {
|
|
|
|
if (scores.containsKey(id)) {
|
|
|
|
scores.get(id).setUpdateType(UpdateType.REMOVE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeScore(String id) {
|
|
|
|
scores.remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Objective setDisplayName(String displayName) {
|
|
|
|
this.displayName = displayName;
|
|
|
|
if (updateType == UpdateType.NOTHING) updateType = UpdateType.UPDATE;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Objective setType(int type) {
|
|
|
|
this.type = type;
|
|
|
|
if (updateType == UpdateType.NOTHING) updateType = UpdateType.UPDATE;
|
|
|
|
return this;
|
|
|
|
}
|
2019-10-04 17:25:08 +00:00
|
|
|
|
|
|
|
public void removeTemp(ScoreboardPosition displaySlot) {
|
|
|
|
if (temp) {
|
|
|
|
temp = false;
|
|
|
|
this.displaySlot = displaySlot.name().toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|