Merge pull request 'Add 1.21 Scoreboard Support' (#54) from SpigotCore/1.21 into main

Reviewed-on: https://steamwar.de/devlabs/SteamWar/SteamWar/pulls/54
Reviewed-by: Lixfel <lixfel@steamwar.de>
This commit is contained in:
Lixfel
2024-11-24 20:13:27 +01:00
11 changed files with 238 additions and 100 deletions

View File

@@ -56,7 +56,7 @@ public class BauScoreboard implements Listener {
public void handlePlayerJoin(PlayerJoinEvent event) { public void handlePlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
SWScoreboard.createScoreboard(player, new ScoreboardCallback() { SWScoreboard.impl.createScoreboard(player, new ScoreboardCallback() {
@Override @Override
public HashMap<String, Integer> getData() { public HashMap<String, Integer> getData() {
Region region = Region.getRegion(player.getLocation()); Region region = Region.getRegion(player.getLocation());

View File

@@ -60,17 +60,17 @@ public class FightScoreboard implements Listener, ScoreboardCallback {
private FightScoreboard(){ private FightScoreboard(){
new StateDependentListener(ArenaMode.Replay, FightState.All, this); new StateDependentListener(ArenaMode.Replay, FightState.All, this);
Bukkit.getOnlinePlayers().forEach(player -> SWScoreboard.createScoreboard(player, this)); Bukkit.getOnlinePlayers().forEach(player -> SWScoreboard.impl.createScoreboard(player, this));
} }
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
SWScoreboard.createScoreboard(event.getPlayer(), this); SWScoreboard.impl.createScoreboard(event.getPlayer(), this);
} }
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
SWScoreboard.removeScoreboard(event.getPlayer()); SWScoreboard.impl.removeScoreboard(event.getPlayer());
} }
public void setTitle(String t) { public void setTitle(String t) {

View File

@@ -53,7 +53,7 @@ class FightScoreboard implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
SWScoreboard.createScoreboard(event.getPlayer(), new ScoreboardCallback() { SWScoreboard.impl.createScoreboard(event.getPlayer(), new ScoreboardCallback() {
@Override @Override
public String getTitle() { public String getTitle() {
return "§eMissileWars"; return "§eMissileWars";
@@ -81,7 +81,7 @@ class FightScoreboard implements Listener {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
SWScoreboard.removeScoreboard(event.getPlayer()); SWScoreboard.impl.removeScoreboard(event.getPlayer());
} }
static Scoreboard getScoreboard() { static Scoreboard getScoreboard() {

View File

@@ -0,0 +1,33 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
plugins {
steamwar.java
}
dependencies {
compileOnly(project(":SpigotCore:SpigotCore_Main", "default"))
compileOnly(libs.paperapi21) {
attributes {
// Very Hacky, but it works
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 21)
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.scoreboard;
import de.steamwar.core.Core;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import java.util.HashMap;
import java.util.Map;
public class SWScoreboard21 implements SWScoreboard {
private static final HashMap<Player, ScoreboardCallback> playerBoards = new HashMap<>();
private static final String SIDEBAR = "sw-sidebar";
static {
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> {
for(Map.Entry<Player, ScoreboardCallback> scoreboard : playerBoards.entrySet()) {
render(scoreboard.getKey(), scoreboard.getValue());
}
}, 10, 5);
}
private static void render(Player player, ScoreboardCallback callback) {
if (player.getScoreboard().getObjective(SIDEBAR) != null) {
player.getScoreboard().getObjective(SIDEBAR).unregister();
}
Objective objective = player.getScoreboard().registerNewObjective(SIDEBAR, "dummy", Component.text(callback.getTitle()));
objective.setAutoUpdateDisplay(true);
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
callback.getData().forEach((text, score) -> objective.getScore(text).setScore(score));
}
@Override
public boolean createScoreboard(Player player, ScoreboardCallback callback) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
player.setScoreboard(scoreboard);
render(player, callback);
playerBoards.put(player, callback);
return true;
}
@Override
public void removeScoreboard(Player player) {
player.getScoreboard().getObjective(SIDEBAR).unregister();
playerBoards.remove(player);
}
}

View File

@@ -0,0 +1,115 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.scoreboard;
import com.comphenix.tinyprotocol.Reflection;
import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.core.Core;
import de.steamwar.core.FlatteningWrapper;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
public class SWScoreboard8 implements SWScoreboard {
private static final Reflection.FieldAccessor<String> scoreboardName = Reflection.getField(FlatteningWrapper.scoreboardObjective, String.class, 0);
private static final Reflection.FieldAccessor<Integer> scoreboardAction = Reflection.getField(FlatteningWrapper.scoreboardObjective, int.class, Core.getVersion() > 15 ? 3 : 0);
private static final Class<?> scoreboardDisplayEnum = Reflection.getClass("{nms.world.scores.criteria}.IScoreboardCriteria$EnumScoreboardHealthDisplay");
private static final Reflection.FieldAccessor<?> scoreboardDisplayType = Reflection.getField(FlatteningWrapper.scoreboardObjective, scoreboardDisplayEnum, 0);
private static final Object displayTypeIntegers = scoreboardDisplayEnum.getEnumConstants()[0];
private static final Reflection.FieldAccessor<String> scoreName = Reflection.getField(FlatteningWrapper.scoreboardScore, String.class, 0);
private static final Reflection.FieldAccessor<String> scoreScoreboardName = Reflection.getField(FlatteningWrapper.scoreboardScore, String.class, 1);
private static final Reflection.FieldAccessor<Integer> scoreValue = Reflection.getField(FlatteningWrapper.scoreboardScore, int.class, 0);
private static final HashMap<Player, ScoreboardCallback> playerBoards = new HashMap<>(); //Object -> Scoreboard | Alle Versionen in der Map!
private static int toggle = 0; // Scoreboard 0 updates while scoreboard 1 is presenting. toggle marks the current active scoreboard
private static final String SIDEBAR = "Sidebar";
private static final Object[] DELETE_SCOREBOARD = new Object[2];
private static final Object[] DISPLAY_SIDEBAR = new Object[2];
static {
Class<?> scoreboardDisplayObjective = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutScoreboardDisplayObjective");
Reflection.FieldAccessor<String> scoreboardDisplayName = Reflection.getField(scoreboardDisplayObjective, String.class, 0);
Reflection.FieldAccessor<Integer> scoreboardDisplaySlot = Reflection.getField(scoreboardDisplayObjective, int.class, 0);
for(int id = 0; id < 2; id++) {
DELETE_SCOREBOARD[id] = Reflection.newInstance(FlatteningWrapper.scoreboardObjective);
scoreboardName.set(DELETE_SCOREBOARD[id], SIDEBAR + id);
scoreboardAction.set(DELETE_SCOREBOARD[id], 1); //1 to remove
DISPLAY_SIDEBAR[id] = Reflection.newInstance(scoreboardDisplayObjective);
scoreboardDisplayName.set(DISPLAY_SIDEBAR[id], SIDEBAR + id);
scoreboardDisplaySlot.set(DISPLAY_SIDEBAR[id], 1); // 1 = Sidebar
}
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> {
toggle ^= 1; // Toggle between 0 and 1
for(Map.Entry<Player, ScoreboardCallback> scoreboard : playerBoards.entrySet()) {
Player player = scoreboard.getKey();
ScoreboardCallback callback = scoreboard.getValue();
TinyProtocol.instance.sendPacket(player, DELETE_SCOREBOARD[toggle]);
TinyProtocol.instance.sendPacket(player, createSidebarPacket(callback.getTitle()));
for(Map.Entry<String, Integer> score : callback.getData().entrySet()){
TinyProtocol.instance.sendPacket(player, createScorePacket(score.getKey(), score.getValue()));
}
Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> {
if(!player.isOnline())
return;
TinyProtocol.instance.sendPacket(player, DISPLAY_SIDEBAR[toggle]);
}, 2);
}
}, 10, 5);
}
public boolean createScoreboard(Player player, ScoreboardCallback callback) {
playerBoards.put(player, callback);
return true;
}
public void removeScoreboard(Player player) {
if(playerBoards.remove(player) == null || !player.isOnline())
return;
TinyProtocol.instance.sendPacket(player, DELETE_SCOREBOARD[toggle]);
}
private static Object createSidebarPacket(String name){
Object packet = Reflection.newInstance(FlatteningWrapper.scoreboardObjective);
scoreboardName.set(packet, SIDEBAR + toggle);
scoreboardAction.set(packet, 0); //0 to create
FlatteningWrapper.impl.setScoreboardTitle(packet, name);
scoreboardDisplayType.set(packet, displayTypeIntegers);
return packet;
}
private static Object createScorePacket(String name, int value){
Object packet = Reflection.newInstance(FlatteningWrapper.scoreboardScore);
scoreName.set(packet, name);
scoreScoreboardName.set(packet, SIDEBAR + toggle);
scoreValue.set(packet, value);
FlatteningWrapper.impl.setScoreAction(packet);
return packet;
}
}

View File

@@ -19,99 +19,13 @@
package de.steamwar.scoreboard; package de.steamwar.scoreboard;
import com.comphenix.tinyprotocol.Reflection;
import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import de.steamwar.core.FlatteningWrapper; import de.steamwar.core.VersionDependent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; public interface SWScoreboard {
import java.util.Map; public static final SWScoreboard impl = VersionDependent.getVersionImpl(Core.getInstance());
public class SWScoreboard { boolean createScoreboard(Player player, ScoreboardCallback callback);
private SWScoreboard() {} void removeScoreboard(Player player);
private static final Reflection.FieldAccessor<String> scoreboardName = Reflection.getField(FlatteningWrapper.scoreboardObjective, String.class, 0);
private static final Reflection.FieldAccessor<Integer> scoreboardAction = Reflection.getField(FlatteningWrapper.scoreboardObjective, int.class, Core.getVersion() > 15 ? 3 : 0);
private static final Class<?> scoreboardDisplayEnum = Reflection.getClass("{nms.world.scores.criteria}.IScoreboardCriteria$EnumScoreboardHealthDisplay");
private static final Reflection.FieldAccessor<?> scoreboardDisplayType = Reflection.getField(FlatteningWrapper.scoreboardObjective, scoreboardDisplayEnum, 0);
private static final Object displayTypeIntegers = scoreboardDisplayEnum.getEnumConstants()[0];
private static final Reflection.FieldAccessor<String> scoreName = Reflection.getField(FlatteningWrapper.scoreboardScore, String.class, 0);
private static final Reflection.FieldAccessor<String> scoreScoreboardName = Reflection.getField(FlatteningWrapper.scoreboardScore, String.class, 1);
private static final Reflection.FieldAccessor<Integer> scoreValue = Reflection.getField(FlatteningWrapper.scoreboardScore, int.class, 0);
private static final HashMap<Player, ScoreboardCallback> playerBoards = new HashMap<>(); //Object -> Scoreboard | Alle Versionen in der Map!
private static int toggle = 0; // Scoreboard 0 updates while scoreboard 1 is presenting. toggle marks the current active scoreboard
private static final String SIDEBAR = "Sidebar";
private static final Object[] DELETE_SCOREBOARD = new Object[2];
private static final Object[] DISPLAY_SIDEBAR = new Object[2];
static {
Class<?> scoreboardDisplayObjective = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutScoreboardDisplayObjective");
Reflection.FieldAccessor<String> scoreboardDisplayName = Reflection.getField(scoreboardDisplayObjective, String.class, 0);
Reflection.FieldAccessor<Integer> scoreboardDisplaySlot = Reflection.getField(scoreboardDisplayObjective, int.class, 0);
for(int id = 0; id < 2; id++) {
DELETE_SCOREBOARD[id] = Reflection.newInstance(FlatteningWrapper.scoreboardObjective);
scoreboardName.set(DELETE_SCOREBOARD[id], SIDEBAR + id);
scoreboardAction.set(DELETE_SCOREBOARD[id], 1); //1 to remove
DISPLAY_SIDEBAR[id] = Reflection.newInstance(scoreboardDisplayObjective);
scoreboardDisplayName.set(DISPLAY_SIDEBAR[id], SIDEBAR + id);
scoreboardDisplaySlot.set(DISPLAY_SIDEBAR[id], 1); // 1 = Sidebar
}
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> {
toggle ^= 1; // Toggle between 0 and 1
for(Map.Entry<Player, ScoreboardCallback> scoreboard : playerBoards.entrySet()) {
Player player = scoreboard.getKey();
ScoreboardCallback callback = scoreboard.getValue();
TinyProtocol.instance.sendPacket(player, DELETE_SCOREBOARD[toggle]);
TinyProtocol.instance.sendPacket(player, createSidebarPacket(callback.getTitle()));
for(Map.Entry<String, Integer> score : callback.getData().entrySet()){
TinyProtocol.instance.sendPacket(player, createScorePacket(score.getKey(), score.getValue()));
}
Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> {
if(!player.isOnline())
return;
TinyProtocol.instance.sendPacket(player, DISPLAY_SIDEBAR[toggle]);
}, 2);
}
}, 10, 5);
}
public static boolean createScoreboard(Player player, ScoreboardCallback callback) {
playerBoards.put(player, callback);
return true;
}
public static void removeScoreboard(Player player) {
if(playerBoards.remove(player) == null || !player.isOnline())
return;
TinyProtocol.instance.sendPacket(player, DELETE_SCOREBOARD[toggle]);
}
private static Object createSidebarPacket(String name){
Object packet = Reflection.newInstance(FlatteningWrapper.scoreboardObjective);
scoreboardName.set(packet, SIDEBAR + toggle);
scoreboardAction.set(packet, 0); //0 to create
FlatteningWrapper.impl.setScoreboardTitle(packet, name);
scoreboardDisplayType.set(packet, displayTypeIntegers);
return packet;
}
private static Object createScorePacket(String name, int value){
Object packet = Reflection.newInstance(FlatteningWrapper.scoreboardScore);
scoreName.set(packet, name);
scoreScoreboardName.set(packet, SIDEBAR + toggle);
scoreValue.set(packet, value);
FlatteningWrapper.impl.setScoreAction(packet);
return packet;
}
} }

View File

@@ -40,4 +40,5 @@ dependencies {
implementation(project(":SpigotCore:SpigotCore_18")) implementation(project(":SpigotCore:SpigotCore_18"))
implementation(project(":SpigotCore:SpigotCore_19")) implementation(project(":SpigotCore:SpigotCore_19"))
implementation(project(":SpigotCore:SpigotCore_20")) implementation(project(":SpigotCore:SpigotCore_20"))
implementation(project(":SpigotCore:SpigotCore_21"))
} }

View File

@@ -43,7 +43,7 @@ object IngameListener: Listener {
@EventHandler @EventHandler
fun onJoin(e: PlayerJoinEvent) { fun onJoin(e: PlayerJoinEvent) {
SWScoreboard.createScoreboard(e.player, TNTLeagueScoreboard(e.player)) SWScoreboard.impl.createScoreboard(e.player, TNTLeagueScoreboard(e.player))
} }
@EventHandler @EventHandler

View File

@@ -57,7 +57,7 @@ object TNTLeagueGame {
state = GameState.RUNNING state = GameState.RUNNING
plugin.server.onlinePlayers.forEach { SWScoreboard.createScoreboard(it, TNTLeagueScoreboard(it)) } plugin.server.onlinePlayers.forEach { SWScoreboard.impl.createScoreboard(it, TNTLeagueScoreboard(it)) }
blueTeam.start() blueTeam.start()
redTeam.start() redTeam.start()
@@ -101,7 +101,7 @@ object TNTLeagueGame {
plugin.server.onlinePlayers.forEach { plugin.server.onlinePlayers.forEach {
it.gameMode = GameMode.SPECTATOR it.gameMode = GameMode.SPECTATOR
SWScoreboard.removeScoreboard(it) SWScoreboard.impl.removeScoreboard(it)
it.playSound(Sound.sound(org.bukkit.Sound.ENTITY_ENDER_DRAGON_DEATH.key, Sound.Source.MASTER, 1f, 1f)) it.playSound(Sound.sound(org.bukkit.Sound.ENTITY_ENDER_DRAGON_DEATH.key, Sound.Source.MASTER, 1f, 1f))
} }

View File

@@ -226,6 +226,7 @@ include(
"SpigotCore:SpigotCore_18", "SpigotCore:SpigotCore_18",
"SpigotCore:SpigotCore_19", "SpigotCore:SpigotCore_19",
"SpigotCore:SpigotCore_20", "SpigotCore:SpigotCore_20",
"SpigotCore:SpigotCore_21",
"SpigotCore:SpigotCore_Main" "SpigotCore:SpigotCore_Main"
) )