From 1da5b65460774c1918559af501331857e4bd1841 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 5 Mar 2024 11:06:31 +0100 Subject: [PATCH] Add ScoreboardLib and TpsLib --- .../types/ScoreboardElement_GENERIC.java | 2 +- BauSystem_Main/src/BauSystem.properties | 1 - BauSystem_Main/src/BauSystem_de.properties | 1 - .../script/lua/libs/ScoreboardLib.java | 71 +++++++++++++++++++ .../features/script/lua/libs/ServerLib.java | 13 ---- .../features/script/lua/libs/TpsLib.java | 53 ++++++++++++++ .../features/world/BauScoreboard.java | 47 +++++++++--- SCRIPT.md | 28 ++++++++ sw.def.lua | 22 +++++- 9 files changed, 210 insertions(+), 28 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ScoreboardLib.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TpsLib.java diff --git a/BauSystem_Linkage/src/de/steamwar/linkage/types/ScoreboardElement_GENERIC.java b/BauSystem_Linkage/src/de/steamwar/linkage/types/ScoreboardElement_GENERIC.java index 01e29833..ffd6382f 100644 --- a/BauSystem_Linkage/src/de/steamwar/linkage/types/ScoreboardElement_GENERIC.java +++ b/BauSystem_Linkage/src/de/steamwar/linkage/types/ScoreboardElement_GENERIC.java @@ -35,6 +35,6 @@ public class ScoreboardElement_GENERIC implements LinkageType { @Override public void generateCode(BuildPlan buildPlan, MethodBuilder methodBuilder, String s, TypeElement typeElement) { buildPlan.addImport("de.steamwar.bausystem.features.world.BauScoreboard"); - methodBuilder.addLine("BauScoreboard.ELEMENTS.add(" + s + ");"); + methodBuilder.addLine("BauScoreboard.addElement(" + s + ");"); } } diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index c2210163..887439f7 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -566,7 +566,6 @@ LOADER_PAUSED=§7The Loader is now paused. LOADER_SMALL_TIME=§cThe wait time is too small LOADER_NEW_TIME=§7The wait time is now: {0} LOADER_NEW_LOAD_TIME=§7The action wait time is now: {0} -LOADER_PERMS=§cYou are not allowed to use the Loader here LOADER_NOTHING_RECORDED=§cYou have not recorded anything yet! LOADER_GUI_TITLE=Loader GUI diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 43a2ba56..46b30880 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -534,7 +534,6 @@ LOADER_PAUSED=§7Der Loader ist nun pausiert. LOADER_SMALL_TIME=§cDie Wartezeit ist zu klein LOADER_NEW_TIME=§7Die Schusswartezeit ist nun: {0} LOADER_NEW_LOAD_TIME=§7Die Setzwartezeit ist nun: {0} -LOADER_PERMS=§cDu darfst hier nicht den Detonator nutzen LOADER_NOTHING_RECORDED=§cEs wurden keine Elemente aufgenommen! LOADER_GUI_TITLE=Loader Einstellungen diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ScoreboardLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ScoreboardLib.java new file mode 100644 index 00000000..af061ac3 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ScoreboardLib.java @@ -0,0 +1,71 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script.lua.libs; + +import de.steamwar.bausystem.features.world.BauScoreboard; +import de.steamwar.bausystem.utils.ScoreboardElement; +import de.steamwar.linkage.Linked; +import org.bukkit.entity.Player; +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.Varargs; +import org.luaj.vm2.lib.TwoArgFunction; +import org.luaj.vm2.lib.VarArgFunction; + +@Linked +public class ScoreboardLib implements LuaLib { + + @Override + public String name() { + return "scoreboard"; + } + + @Override + public LuaTable get(Player player) { + LuaTable luaTable = new LuaTable(); + + LuaTable groups = new LuaTable(); + for (ScoreboardElement.ScoreboardGroup group : ScoreboardElement.ScoreboardGroup.values()) { + groups.set(group.name(), group.ordinal()); + } + luaTable.set("group", groups); + + luaTable.set("element", new TwoArgFunction() { + @Override + public LuaValue call(LuaValue key, LuaValue group) { + String elementKey = key.checkjstring(); + ScoreboardElement.ScoreboardGroup elementGroup = ScoreboardElement.ScoreboardGroup.values()[group.checkint()]; + + return new VarArgFunction() { + @Override + public Varargs invoke(Varargs args) { + if (args.narg() == 0) { + BauScoreboard.setAdditionalElement(player, elementKey, elementGroup, null); + } else { + BauScoreboard.setAdditionalElement(player, elementKey, elementGroup, args.arg1().checkjstring()); + } + return NIL; + } + }; + } + }); + return luaTable; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ServerLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ServerLib.java index 8e800329..02ccd975 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ServerLib.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/ServerLib.java @@ -21,9 +21,7 @@ package de.steamwar.bausystem.features.script.lua.libs; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.features.tpslimit.TPSSystem; import de.steamwar.bausystem.features.tpslimit.TPSUtils; -import de.steamwar.core.TPSWatcher; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; import org.bukkit.Material; @@ -75,17 +73,6 @@ public class ServerLib implements LuaLib { return NIL; } }); - - LuaValue tpsLib = LuaValue.tableOf(); - tpsLib.set("oneSecond", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.ONE_SECOND))); - tpsLib.set("tenSecond", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.TEN_SECONDS))); - tpsLib.set("oneMinute", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.ONE_MINUTE))); - tpsLib.set("fiveMinute", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.FIVE_MINUTES))); - tpsLib.set("tenMinute", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.TEN_MINUTES))); - tpsLib.set("current", getter(TPSWatcher::getTPS)); - tpsLib.set("limit", getter(TPSSystem.getInstance()::getCurrentTPSLimit)); - - serverLib.set("tps", tpsLib); return serverLib; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TpsLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TpsLib.java new file mode 100644 index 00000000..3b0b430d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TpsLib.java @@ -0,0 +1,53 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script.lua.libs; + +import de.steamwar.bausystem.features.tpslimit.TPSSystem; +import de.steamwar.core.TPSWatcher; +import de.steamwar.linkage.Linked; +import org.bukkit.entity.Player; +import org.luaj.vm2.LuaTable; + +@Linked +public class TpsLib implements LuaLib { + + @Override + public Class parent() { + return ServerLib.class; + } + + @Override + public String name() { + return "tps"; + } + + @Override + public LuaTable get(Player player) { + LuaTable tpsLib = new LuaTable(); + tpsLib.set("oneSecond", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.ONE_SECOND))); + tpsLib.set("tenSecond", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.TEN_SECONDS))); + tpsLib.set("oneMinute", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.ONE_MINUTE))); + tpsLib.set("fiveMinute", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.FIVE_MINUTES))); + tpsLib.set("tenMinute", getter(() -> TPSWatcher.getTPS(TPSWatcher.TPSType.TEN_MINUTES))); + tpsLib.set("current", getter(TPSWatcher::getTPS)); + tpsLib.set("limit", getter(TPSSystem.getInstance()::getCurrentTPSLimit)); + return tpsLib; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/BauScoreboard.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/BauScoreboard.java index 6efda09f..cce4ea38 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/BauScoreboard.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/BauScoreboard.java @@ -4,6 +4,7 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.region.GlobalRegion; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.shared.Pair; import de.steamwar.bausystem.utils.ScoreboardElement; import de.steamwar.linkage.Linked; import de.steamwar.scoreboard.SWScoreboard; @@ -12,26 +13,37 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.util.*; @Linked public class BauScoreboard implements Listener { - public static final List ELEMENTS = new ArrayList<>(); + private static final Map> ELEMENTS = new HashMap<>(); + private static final Map>> ADDITIONAL_SCOREBOARD_LINES = new HashMap<>(); + + public static void addElement(ScoreboardElement scoreboardElement) { + List elements = ELEMENTS.computeIfAbsent(scoreboardElement.getGroup(), scoreboardGroup -> new ArrayList<>()); + elements.add(scoreboardElement); + elements.sort(Comparator.comparingInt(ScoreboardElement::order)); + } + + public static void setAdditionalElement(Player player, String key, ScoreboardElement.ScoreboardGroup group, String value) { + Map> playerElements = ADDITIONAL_SCOREBOARD_LINES.computeIfAbsent(player, player1 -> new HashMap<>()); + if (value == null || value.isBlank()) { + playerElements.remove(key); + return; + } + Pair element = playerElements.computeIfAbsent(key, s -> new Pair<>(null, null)); + element.setKey(group); + element.setValue(value); + } @EventHandler public void handlePlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - Map> map = new HashMap<>(); - for (ScoreboardElement element : ELEMENTS) { - map.computeIfAbsent(element.getGroup(), scoreboardGroup -> new ArrayList<>()).add(element); - } - map.forEach((scoreboardGroup, scoreboardElements) -> { - scoreboardElements.sort(Comparator.comparingInt(ScoreboardElement::order)); - }); - SWScoreboard.createScoreboard(player, new ScoreboardCallback() { @Override public HashMap getData() { @@ -52,9 +64,9 @@ public class BauScoreboard implements Listener { } private void calcGroup(List elements, String separator, Region region, ScoreboardElement.ScoreboardGroup group) { - if (map.containsKey(group)) { + if (ELEMENTS.containsKey(group)) { List groupElements = new ArrayList<>(); - for (ScoreboardElement element : map.get(group)) { + for (ScoreboardElement element : ELEMENTS.get(group)) { groupElements.add(element.get(region, player)); } groupElements.removeIf(Objects::isNull); @@ -63,6 +75,14 @@ public class BauScoreboard implements Listener { elements.addAll(groupElements); } } + if (ADDITIONAL_SCOREBOARD_LINES.containsKey(player)) { + ADDITIONAL_SCOREBOARD_LINES.get(player).values().forEach(scoreboardGroupStringPair -> { + if (scoreboardGroupStringPair.getKey() != group) { + return; + } + elements.add(scoreboardGroupStringPair.getValue()); + }); + } } @Override @@ -74,4 +94,9 @@ public class BauScoreboard implements Listener { } }); } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + ADDITIONAL_SCOREBOARD_LINES.remove(event.getPlayer()); + } } diff --git a/SCRIPT.md b/SCRIPT.md index ae9cb181..3ac69cb7 100644 --- a/SCRIPT.md +++ b/SCRIPT.md @@ -69,6 +69,7 @@ In den Scripten gibt es dazu noch folgende globale Variablen: - [`server`](#server) - [`storage`](#storage) - [`inventory`](#inventory) +- [`scoreboard`](#scoreboard) - `_worldedit` Ohne eine Kategorie sind folgende Funktionen verfügbar, die nicht allgemein sind: @@ -255,6 +256,33 @@ Wenn eine Barrier statt des richtigen Items angezeigt wird, dann ist das angegeb ⚠️⚠️⚠️ ``` +## scoreboard +Das `scoreboard`-Modul stellt Funktionen zur Verfügung, um Zeilen im Scoreboard hinzuzufügen. +Es gibt folgende Funktionen: + +| Name | Signature | Beschreibung | +|-----------|-------------------------------------------|----------------------------------------------------------------------| +| `group` | | Siehe: [Scoreboardgroups](#scoreboardgroups) | +| `element` | element(String, Group): ScoreboardElement | Erstellt ein ScoreboardElement, welche man nachfolgend befüllen kann | + +Ein ScoreboardElement ist ein Objekt, womit du direkt auf einen Wert zugreifen kannst und es ändern kannst. +Es geht wie folgt: +```lua +key = scoreboard.element("key", scoreboard.group.OTHER) + +key("Hello World") -- Setzt im Bereich Other den Text "Hello World" unter allem anderen +key() -- Removed im Bereich Other den vorherigen Text "Hello World" +``` + +## Scoreboardgroups + +| Name | +|----------| +| `Header` | +| `Region` | +| `Other` | +| `Footer` | + # SteamWar.de-Global-Api Mit `/script` kann man Script-Bücher global abspeichern. Diese haben dann zugrif auf die `global`-Api. Die `global`-Api stellt Funktionen zur Verfügung um auf Events, Commands und Hotkeys mit einem Script zu reagieren. diff --git a/sw.def.lua b/sw.def.lua index 613437a1..e4faf43c 100644 --- a/sw.def.lua +++ b/sw.def.lua @@ -18,9 +18,29 @@ --- --- This file contains the definitions for the SteamWar.de script API. --- It is used by the IDE to provide code completion and type checking. ---- Created by Chaoscaot +--- Created by Chaoscaot and YoyoNow --- +scoreboard = {} + +---@param key string +---@param group ScoreboardGroup +---@return ScoreboardElement +function scoreboard.element(key, group) return nil end + +---@class ScoreboardElement +---@overload fun(): void +---@overload fun(value: string): void + +---@class ScoreboardGroup +---@class group +---@field HEADER ScoreboardGroup +---@field REGION ScoreboardGroup +---@field OTHER ScoreboardGroup +---@field FOOTER ScoreboardGroup +local group = {} +scoreboard.group = group + inventory = {} ---@param title string