Add 1.21 Scoreboard Support

This commit is contained in:
2024-11-15 22:06:10 +01:00
parent a78974b903
commit 35ca8f3877
6 changed files with 231 additions and 81 deletions
+33
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)
}
}
}
@@ -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.ISWScoreboard {
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());
}
}, 5, 10);
}
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);
}
}