Start work on scoreboard translation

This commit is contained in:
RednedEpic 2019-07-22 01:52:56 -05:00
parent a2633ea76c
commit 00a2b4fc0c
6 changed files with 513 additions and 0 deletions

View file

@ -41,6 +41,7 @@ import lombok.Getter;
import org.geysermc.api.command.CommandSender;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.remote.RemoteJavaServer;
import org.geysermc.connector.network.session.cache.ScoreboardCache;
import org.geysermc.connector.network.translators.Registry;
import java.util.UUID;
@ -63,11 +64,16 @@ public class GeyserSession implements PlayerSession, CommandSender {
@Getter
private AuthenticationData authenticationData;
@Getter
private ScoreboardCache scoreboardCache;
private boolean closed;
public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession) {
this.connector = connector;
this.upstream = bedrockServerSession;
this.scoreboardCache = new ScoreboardCache(this);
}
public void connect(RemoteJavaServer remoteServer) {

View file

@ -0,0 +1,51 @@
/*
* 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
*/
package org.geysermc.connector.network.session.cache;
import com.nukkitx.protocol.bedrock.packet.RemoveObjectivePacket;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.scoreboard.Scoreboard;
public class ScoreboardCache {
private GeyserSession session;
public ScoreboardCache(GeyserSession session) {
this.session = session;
}
@Getter
@Setter
private Scoreboard scoreboard;
public void removeScoreboard() {
RemoveObjectivePacket removeObjectivePacket = new RemoveObjectivePacket();
removeObjectivePacket.setObjectiveId(scoreboard.getObjective().getObjectiveName());
session.getUpstream().sendPacket(removeObjectivePacket);
}
}

View file

@ -28,6 +28,7 @@ package org.geysermc.connector.network.translators;
import com.flowpowered.math.vector.Vector2f;
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;
import com.github.steveice10.mc.protocol.data.game.scoreboard.ObjectiveAction;
import com.github.steveice10.mc.protocol.data.message.TranslationMessage;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
@ -36,6 +37,9 @@ import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntit
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityPositionRotationPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityTeleportPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityVelocityPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerDisplayScoreboardPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerScoreboardObjectivePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerUpdateScorePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerUpdateTimePacket;
import com.nukkitx.nbt.CompoundTagBuilder;
import com.nukkitx.nbt.NbtUtils;
@ -44,6 +48,9 @@ import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.protocol.bedrock.data.GamePublishSetting;
import com.nukkitx.protocol.bedrock.data.GameRule;
import com.nukkitx.protocol.bedrock.packet.*;
import org.geysermc.connector.network.session.cache.ScoreboardCache;
import org.geysermc.connector.network.translators.scoreboard.Scoreboard;
import org.geysermc.connector.network.translators.scoreboard.ScoreboardObjective;
import org.geysermc.connector.utils.MessageUtils;
import org.geysermc.connector.utils.Toolbox;
@ -75,6 +82,7 @@ public class TranslatorsInit {
addTitlePackets();
addTimePackets();
addEntityPackets();
addScoreboardPackets();
}
private static void addLoginPackets() {
@ -271,4 +279,132 @@ public class TranslatorsInit {
session.getUpstream().sendPacket(entityMotionPacket);
});
}
public static void addScoreboardPackets() {
Registry.add(ServerDisplayScoreboardPacket.class, (packet, session) -> {
try {
ScoreboardCache cache = session.getScoreboardCache();
Scoreboard scoreboard = new Scoreboard(session);
/*
if (cache.getScoreboard() != null)
cache.setScoreboard(scoreboard);
*/
System.out.println("added scoreboard " + packet.getScoreboardName());
// scoreboard.onUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
});
Registry.add(ServerScoreboardObjectivePacket.class, (packet, session) -> {
try {
ScoreboardCache cache = session.getScoreboardCache();
Scoreboard scoreboard = new Scoreboard(session);
if (cache.getScoreboard() != null)
scoreboard = cache.getScoreboard();
System.out.println("new objective registered with " + packet.getName());
if (packet.getAction() == ObjectiveAction.ADD || packet.getAction() == ObjectiveAction.UPDATE) {
ScoreboardObjective objective = scoreboard.registerNewObjective(packet.getName());
objective.setDisplaySlot(ScoreboardObjective.DisplaySlot.SIDEBAR);
objective.setDisplayName(packet.getDisplayName().getFullText());
} else {
scoreboard.unregisterObjective(packet.getDisplayName().getFullText());
}
scoreboard.onUpdate();
cache.setScoreboard(scoreboard);
} catch (Exception ex) {
ex.printStackTrace();
}
});
Registry.add(ServerUpdateScorePacket.class, (packet, session) -> {
try {
ScoreboardCache cache = session.getScoreboardCache();
Scoreboard scoreboard = new Scoreboard(session);
if (cache.getScoreboard() != null)
scoreboard = cache.getScoreboard();
ScoreboardObjective objective = scoreboard.getObjective(packet.getObjective());
if (objective == null) {
objective = scoreboard.registerNewObjective(packet.getObjective());
}
System.out.println(packet.getEntry() + " <-> objective = " + packet.getObjective() + " val " + packet.getValue());
switch (packet.getAction()) {
case REMOVE:
objective.registerScore(packet.getObjective(), packet.getEntry(), packet.getValue(), SetScorePacket.Action.REMOVE);
break;
case ADD_OR_UPDATE:
objective.registerScore(packet.getObjective(), packet.getEntry(), packet.getValue(), SetScorePacket.Action.SET);
break;
}
cache.setScoreboard(scoreboard);
scoreboard.onUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
});
}
/*
public static void addScoreboardPackets() {
Registry.add(ServerDisplayScoreboardPacket.class, (packet, session) -> {
SetDisplayObjectivePacket objectivePacket = new SetDisplayObjectivePacket();
objectivePacket.setCriteria("dummy");
objectivePacket.setDisplaySlot("sidebar");
objectivePacket.setDisplayName(packet.getScoreboardName());
objectivePacket.setSortOrder(1);
objectivePacket.setObjectiveId(UUID.randomUUID().toString()); // uhh
session.getUpstream().sendPacket(objectivePacket);
System.out.println("added scoreboard " + packet.getScoreboardName());
});
Registry.add(ServerScoreboardObjectivePacket.class, (packet, session) -> {
if (packet.getAction() == ObjectiveAction.REMOVE) {
RemoveObjectivePacket objectivePacket = new RemoveObjectivePacket();
objectivePacket.setObjectiveId(packet.getDisplayName().getFullText());
session.getUpstream().sendPacket(objectivePacket);
} else {
SetDisplayObjectivePacket objectivePacket = new SetDisplayObjectivePacket();
objectivePacket.setCriteria("dummy");
objectivePacket.setDisplaySlot("sidebar");
objectivePacket.setDisplayName(packet.getDisplayName().getFullText());
objectivePacket.setSortOrder(1);
objectivePacket.setObjectiveId(packet.getName()); // uhh
session.getUpstream().sendPacket(objectivePacket);
}
System.out.println("new objective registered with " + packet.getName());
});
Registry.add(ServerUpdateScorePacket.class, (packet, session) -> {
if (packet.getAction() == ScoreboardAction.ADD_OR_UPDATE) {
SetDisplayObjectivePacket objectivePacket = new SetDisplayObjectivePacket();
objectivePacket.setObjectiveId(packet.getObjective());
objectivePacket.setDisplaySlot("sidebar");
objectivePacket.setCriteria("dummy");
objectivePacket.setDisplayName(packet.getEntry());
objectivePacket.setSortOrder(1);
session.getUpstream().sendPacket(objectivePacket);
} else {
RemoveObjectivePacket objectivePacket = new RemoveObjectivePacket();
objectivePacket.setObjectiveId(packet.getObjective());
session.getUpstream().sendPacket(objectivePacket);
}
System.out.println(packet.getEntry() + " <-> objective = " + packet.getObjective() + " val " + packet.getValue());
});
}
*/
}

View file

@ -0,0 +1,67 @@
/*
* 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
*/
package org.geysermc.connector.network.translators.scoreboard;
import com.nukkitx.protocol.bedrock.packet.SetScorePacket;
import lombok.Getter;
import lombok.Setter;
import java.util.Random;
/**
* Adapted from: https://github.com/Ragnok123/GTScoreboard
*/
public class Score {
@Getter
@Setter
private int score;
@Getter
private long scoreboardId;
private ScoreboardObjective objective;
private String fakePlayer;
private long id;
private boolean isFake;
@Getter
@Setter
private SetScorePacket.Action action = SetScorePacket.Action.SET;
private boolean modified = false;
@Getter
@Setter
private String fakeId;
public Score(ScoreboardObjective objective, String fakePlayer) {
scoreboardId = -new Random().nextLong();
objective = objective;
fakePlayer = fakePlayer;
isFake = true;
}
}

View file

@ -0,0 +1,127 @@
/*
* 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
*/
package org.geysermc.connector.network.translators.scoreboard;
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;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Adapted from: https://github.com/Ragnok123/GTScoreboard
*/
public class Scoreboard {
@Getter
private ScoreboardObjective objective;
private GeyserSession session;
private long id;
private Map<String, ScoreboardObjective> objectiveMap = new HashMap<String, ScoreboardObjective>();
public Scoreboard(GeyserSession session) {
this.session = session;
id = new Random().nextLong();
}
public ScoreboardObjective registerNewObjective(String objectiveName) {
ScoreboardObjective objective = new ScoreboardObjective();
objective.setObjectiveName(objectiveName);
this.objective = objective;
if (!objectiveMap.containsKey(objectiveName)) {
objectiveMap.put(objectiveName, objective);
}
return objective;
}
public ScoreboardObjective getObjective(String objectiveName) {
ScoreboardObjective objective = null;
if (objectiveMap.containsKey(objectiveName) && this.objective.getObjectiveName().contains(objectiveName)) {
objective = this.objective;
}
return objective;
}
public void setObjective(String objectiveName) {
if (objectiveMap.containsKey(objectiveName))
objective = objectiveMap.get(objectiveName);
}
public void unregisterObjective(String objectiveName) {
if (!objectiveMap.containsKey(objectiveName))
return;
if (objective.getObjectiveName().equals(objective)) {
objective = null;
}
objectiveMap.remove(objectiveName);
}
public void onUpdate() {
RemoveObjectivePacket removeObjectivePacket = new RemoveObjectivePacket();
removeObjectivePacket.setObjectiveId(objective.getObjectiveName());
session.getUpstream().sendPacket(removeObjectivePacket);
SetDisplayObjectivePacket displayObjectivePacket = new SetDisplayObjectivePacket();
displayObjectivePacket.setObjectiveId(objective.getObjectiveName());
displayObjectivePacket.setDisplayName(objective.getDisplayName());
displayObjectivePacket.setCriteria("dummy");
displayObjectivePacket.setDisplaySlot(ScoreboardObjective.DisplaySlot.SIDEBAR.name());
displayObjectivePacket.setSortOrder(1);
session.getUpstream().sendPacket(displayObjectivePacket);
Map<String, Score> fakeMap = new HashMap<String, Score>();
for (Map.Entry<String, Score> entry : objective.getScores().entrySet()) {
fakeMap.put(entry.getKey(), entry.getValue());
}
for (Score score : fakeMap.values()) {
ScoreInfo scoreInfo = new ScoreInfo(score.getScoreboardId(), objective.getObjectiveName(), score.getScore(), score.getFakeId());
SetScorePacket setScorePacket = new SetScorePacket();
setScorePacket.setAction(score.getAction());
setScorePacket.setInfos(Arrays.asList(scoreInfo));
session.getUpstream().sendPacket(setScorePacket);
if (score.getAction() == SetScorePacket.Action.REMOVE) {
String id = score.getFakeId();
objective.getScores().remove(id);
}
}
}
}

View file

@ -0,0 +1,126 @@
/*
* 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
*/
package org.geysermc.connector.network.translators.scoreboard;
import com.nukkitx.protocol.bedrock.packet.SetScorePacket;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.connector.console.GeyserLogger;
import java.util.HashMap;
import java.util.Map;
/**
* Adapted from: https://github.com/Ragnok123/GTScoreboard
*/
public class ScoreboardObjective {
@Getter
@Setter
private int scoreboardTick = 0;
@Getter
@Setter
private String objectiveName;
@Getter
@Setter
private DisplaySlot displaySlot;
@Getter
@Setter
private String displayName;
@Getter
private Map<String, Score> scores = new HashMap<String, Score>();
public void registerScore(String id, String fake, int value) {
registerScore(id, fake, value, SetScorePacket.Action.SET);
}
public void registerScore(String id, String fake, int value, SetScorePacket.Action action) {
Score score = new Score(this, fake);
score.setScore(value);
score.setFakeId(id);
score.setAction(action);
if (!scores.containsKey(id)) {
scores.put(id, score);
} else {
setScore(id, value);
}
}
public void setScore(String id, int value) {
if (scores.containsKey(id)) {
Score modifiedScore = scores.get(id);
modifiedScore.setScore(value);
scores.remove(id);
scores.put(id, modifiedScore);
}
}
public void setScoreText(String id, String text) {
if (scores.containsKey(id)) {
Score oldScore = scores.get(id);
oldScore.setAction(SetScorePacket.Action.REMOVE);
oldScore.setFakeId(id + "_old_changed");
Score newScore = new Score(this, text);
newScore.setScore(oldScore.getScore());
newScore.setFakeId(id);
scores.remove(id);
scores.put(id, newScore);
scores.put(id + "_old_changed", oldScore);
}
}
public int getScore(String id) {
int i = 0;
if (scores.containsKey(id)) {
Score score = scores.get(id);
i = score.getScore();
}
return i;
}
public void resetScore(String id) {
if (scores.containsKey(id)) {
Score modifiedScore = scores.get(id);
modifiedScore.setAction(SetScorePacket.Action.REMOVE);
scores.remove(id);
scores.put(id, modifiedScore);
}
}
public enum DisplaySlot {
SIDEBAR,
LIST,
BELOWNAME;
}
}