From aa0594fa6c3a98078f7eaa23ed13edd71cac339f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 19 Jul 2023 20:42:09 +0200 Subject: [PATCH] Add StorageLib Signed-off-by: Chaoscaot --- .../features/script/event/EventListener.java | 2 + .../features/script/lua/libs/StorageLib.java | 102 ++++++++++++++++++ SCRIPT.md | 20 ++++ 3 files changed, 124 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/StorageLib.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java index 22fd60a3..52fa2354 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.script.event; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.ScriptRunner; import de.steamwar.bausystem.features.script.lua.SteamWarGlobalLuaPlugin; +import de.steamwar.bausystem.features.script.lua.libs.StorageLib; import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.utils.RegionExtensionType; @@ -67,6 +68,7 @@ public class EventListener implements Listener { @EventHandler(priority = EventPriority.HIGH) public void onPlayerQuit(PlayerQuitEvent event) { + StorageLib.removePlayer(event.getPlayer()); ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.SelfLeave, LuaValue.NIL, event); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/StorageLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/StorageLib.java new file mode 100644 index 00000000..5388a696 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/StorageLib.java @@ -0,0 +1,102 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.region.Region; +import de.steamwar.linkage.Linked; +import org.bukkit.entity.Player; +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.lib.OneArgFunction; +import org.luaj.vm2.lib.TwoArgFunction; + +import java.util.HashMap; + +@Linked +public class StorageLib implements LuaLib { + + private static final HashMap GLOBAL_STORAGE = new HashMap<>(); + private static final HashMap> PLAYER_STORAGE = new HashMap<>(); + private static final HashMap> REGION_STORAGE = new HashMap<>(); + + @Override + public String name() { + return "storage"; + } + + @Override + public LuaTable get(Player player) { + LuaTable storageLib = new LuaTable(); + + LuaTable global = new LuaTable(); + global.set("get", new OneArgFunction() { + @Override + public LuaValue call(LuaValue arg) { + return GLOBAL_STORAGE.get(arg.checkjstring()); + } + }); + global.set("set", new TwoArgFunction() { + @Override + public LuaValue call(LuaValue arg1, LuaValue arg2) { + return GLOBAL_STORAGE.put(arg1.checkjstring(), arg2); + } + }); + + storageLib.set("global", global); + + LuaTable playerStorage = new LuaTable(); + HashMap playerStorageMap = PLAYER_STORAGE.computeIfAbsent(player, k -> new HashMap<>()); + playerStorage.set("get", new OneArgFunction() { + @Override + public LuaValue call(LuaValue arg) { + return playerStorageMap.get(arg.checkjstring()); + } + }); + playerStorage.set("set", new TwoArgFunction() { + @Override + public LuaValue call(LuaValue arg1, LuaValue arg2) { + return playerStorageMap.put(arg1.checkjstring(), arg2); + } + }); + storageLib.set("player", playerStorage); + + LuaTable regionStorage = new LuaTable(); + HashMap regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>()); + regionStorage.set("get", new OneArgFunction() { + @Override + public LuaValue call(LuaValue arg) { + return regionStorageMap.get(arg.checkjstring()); + } + }); + regionStorage.set("set", new TwoArgFunction() { + @Override + public LuaValue call(LuaValue arg1, LuaValue arg2) { + return regionStorageMap.put(arg1.checkjstring(), arg2); + } + }); + storageLib.set("region", regionStorage); + + return storageLib; + } + + public static void removePlayer(Player player) { + PLAYER_STORAGE.remove(player); + } +} diff --git a/SCRIPT.md b/SCRIPT.md index 681604e1..0ded3879 100644 --- a/SCRIPT.md +++ b/SCRIPT.md @@ -12,6 +12,7 @@ * [trace](#trace) * [server](#server) * [tps](#tps) + * [storage](#storage) * [SteamWar.de-Global-Api](#steamwarde-global-api) * [Hotkeys](#hotkeys) * [Eventtypen](#eventtypen) @@ -53,6 +54,7 @@ In den Scripten gibt es dazu noch folgende globale Variablen: - [`player`](#player) - [`region`](#region) - [`server`](#server) +- [`storage`](#storage) - `_worldedit` Ohne eine Kategorie sind folgende Funktionen verfügbar, die nicht allgemein sind: @@ -160,6 +162,24 @@ Es gibt folgende Funktionen: | `tenMinute` | tenMinute(): Number | Gibt die durchschnittliche TPS über die letzte 10 Minuten zurück | | `limit` | limit(): Number | Gibt das TPS-Limit zurück | +## storage +Das `storage`-Modul stellt Funktionen zur Verfügung, mit welchen man Werte speichern kann. + +Es gibt folgende Module: + +| Name | Beschreibung | +|----------|---------------------| +| `player` | Spieler abhängig | +| `region` | Region des Spielers | +| `global` | Alle Skripte | + +Alle Module haben folgende Funktionen: + +| Name | Signature | Beschreibung | +|-----------|--------------------|------------------------------------------------------------------------------------------------------------| +| `get` | get(String): Any | Gibt den Wert des Schlüssels zurück | +| `set` | set(String, Any) | Setzt den Wert des Schlüssels auf den angegebenen Wert | + # 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. -- 2.39.2