SteamWar/BauSystem2.0
Archiviert
12
0

Add a few RegionLib getters
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Add storage saving and loading
Dieser Commit ist enthalten in:
yoyosource 2023-11-17 18:14:16 +01:00
Ursprung 4ee93b59dd
Commit bdeb7da4c6
6 geänderte Dateien mit 224 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -192,7 +192,7 @@ public class SteamWarLuaPlugin extends TwoArgFunction {
class Print extends VarArgFunction {
@Override
public Varargs invoke(Varargs args) {
player.sendMessage(varArgsToString(args));
player.sendMessage(varArgsToString(args).replaceAll("&([a-z0-9klmnor])", "§\1"));
return LuaValue.NIL;
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.features.script.lua.libs;
import de.steamwar.bausystem.region.Point;
import lombok.AllArgsConstructor;
import org.bukkit.entity.Player;
import org.luaj.vm2.*;
@ -69,6 +70,14 @@ public interface LuaLib {
return de.steamwar.bausystem.features.script.lua.SteamWarLuaPlugin.varArgsToString(varargs);
}
default LuaTable toPos(Point point) {
return LuaValue.tableOf(new LuaValue[] {
LuaValue.valueOf("x"), LuaValue.valueOf(point.getX()),
LuaValue.valueOf("y"), LuaValue.valueOf(point.getY()),
LuaValue.valueOf("z"), LuaValue.valueOf(point.getZ())
});
}
default Class<? extends LuaLib> parent() {
return null;
}

Datei anzeigen

@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.script.lua.libs;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.luaj.vm2.LuaTable;
@ -93,7 +94,7 @@ public class PlayerLib implements LuaLib {
@Override
public Varargs invoke(Varargs args) {
player.sendMessage(varArgsToString(args));
player.sendMessage(varArgsToString(args).replaceAll("&([a-z0-9klmnor])", "§\1"));
return LuaValue.NIL;
}
}
@ -107,7 +108,7 @@ public class PlayerLib implements LuaLib {
@Override
public Varargs invoke(Varargs args) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(varArgsToString(args)));
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(varArgsToString(args).replaceAll("&([a-z0-9klmnor])", "§\1")));
return LuaValue.NIL;
}
}

Datei anzeigen

@ -81,6 +81,17 @@ public class RegionLib implements LuaLib {
Loader loader = Loader.getLoader(player);
table.set("loader", getter(() -> loader == null ? "OFF" : loader.getStage().name()));
table.set("copyPoint", getter(() -> toPos(region.get().getCopyPoint())));
table.set("minPointBuild", getter(() -> toPos(region.get().getMinPointBuild())));
table.set("maxPointBuild", getter(() -> toPos(region.get().getMaxPointBuild())));
table.set("minPointBuildExtension", getter(() -> toPos(region.get().getMinPointBuildExtension())));
table.set("maxPointBuildExtension", getter(() -> toPos(region.get().getMaxPointBuildExtension())));
table.set("testblockPoint", getter(() -> toPos(region.get().getTestBlockPoint())));
table.set("minPointTestblock", getter(() -> toPos(region.get().getMinPointTestblock())));
table.set("maxPointTestblock", getter(() -> toPos(region.get().getMaxPointTestblock())));
table.set("minPointTestblockExtension", getter(() -> toPos(region.get().getMinPointTestblockExtension())));
table.set("maxPointTestblockExtension", getter(() -> toPos(region.get().getMaxPointTestblockExtension())));
return table;
}

Datei anzeigen

@ -19,8 +19,13 @@
package de.steamwar.bausystem.features.script.lua.libs;
import com.google.gson.*;
import de.steamwar.bausystem.region.Region;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.api.Disable;
import de.steamwar.linkage.api.Enable;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
@ -29,15 +34,171 @@ import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Linked
public class StorageLib implements LuaLib {
public class StorageLib implements LuaLib, Enable, Disable {
private final Gson gson = new Gson();
private final File storageDirectory = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "script_storage");
private static final HashMap<String, LuaValue> GLOBAL_STORAGE = new HashMap<>();
private static final HashMap<Player, HashMap<String, LuaValue>> PLAYER_STORAGE = new HashMap<>();
private static final HashMap<UUID, HashMap<String, LuaValue>> PLAYER_STORAGE = new HashMap<>();
private static final HashMap<Region, HashMap<String, LuaValue>> REGION_STORAGE = new HashMap<>();
@Override
public void enable() {
if (!storageDirectory.exists()) storageDirectory.mkdirs();
try {
JsonObject jsonObject = JsonParser.parseReader(new FileReader(new File(storageDirectory, "global.json"))).getAsJsonObject();
jsonObject.keySet().forEach(key -> {
GLOBAL_STORAGE.put(key, fromJson(jsonObject.get(key)));
});
} catch (Exception e) {}
File regionStorageDirectory = new File(storageDirectory, "region");
regionStorageDirectory.mkdirs();
for (File regionStorage : regionStorageDirectory.listFiles()) {
try {
JsonObject jsonObject = JsonParser.parseReader(new FileReader(regionStorage)).getAsJsonObject();
HashMap<String, LuaValue> map = new HashMap<>();
jsonObject.keySet().forEach(key -> {
map.put(key, fromJson(jsonObject.get(key)));
});
Region region = Region.getREGION_MAP().get(regionStorage.getName().substring(0, regionStorage.getName().length() - ".json".length()));
REGION_STORAGE.put(region, map);
} catch (Exception e) {}
}
File playerStorageDirectory = new File(storageDirectory, "player");
playerStorageDirectory.mkdirs();
for (File playerStorage : playerStorageDirectory.listFiles()) {
try {
JsonObject jsonObject = JsonParser.parseReader(new FileReader(playerStorage)).getAsJsonObject();
HashMap<String, LuaValue> map = new HashMap<>();
jsonObject.keySet().forEach(key -> {
map.put(key, fromJson(jsonObject.get(key)));
});
SteamwarUser steamwarUser = SteamwarUser.get(Integer.parseInt(playerStorage.getName().substring(0, playerStorage.getName().length() - ".json".length())));
PLAYER_STORAGE.put(steamwarUser.getUUID(), map);
} catch (Exception e) {}
}
}
private LuaValue fromJson(JsonElement jsonElement) {
if (jsonElement.isJsonNull()) {
return LuaValue.NIL;
}
if (jsonElement.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
return LuaValue.valueOf(jsonPrimitive.getAsBoolean());
}
if (jsonPrimitive.isNumber()) {
try {
return LuaValue.valueOf(jsonPrimitive.getAsInt());
} catch (NumberFormatException e) {}
try {
return LuaValue.valueOf(jsonPrimitive.getAsDouble());
} catch (NumberFormatException e) {}
}
if (jsonPrimitive.isString()) {
return LuaValue.valueOf(jsonPrimitive.getAsString());
}
return null;
}
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
LuaTable luaTable = new LuaTable();
jsonObject.keySet().forEach(string -> {
LuaValue value = fromJson(jsonObject.get(string));
if (value == null) return;
luaTable.set(string, value);
});
return luaTable;
}
return null;
}
@Override
public void disable() {
if (!storageDirectory.exists()) storageDirectory.mkdirs();
try {
FileWriter fileWriter = new FileWriter(new File(storageDirectory, "global.json"));
gson.toJson(toJson(GLOBAL_STORAGE), fileWriter);
fileWriter.close();
} catch (IOException e) {}
File regionStorageDirectory = new File(storageDirectory, "region");
regionStorageDirectory.mkdirs();
for (Map.Entry<Region, HashMap<String, LuaValue>> entry : REGION_STORAGE.entrySet()) {
try {
FileWriter fileWriter = new FileWriter(new File(regionStorageDirectory, entry.getKey().getName() + ".json"));
gson.toJson(toJson(entry.getValue()), fileWriter);
fileWriter.close();
} catch (IOException e) {}
}
File playerStorageDirectory = new File(storageDirectory, "player");
playerStorageDirectory.mkdirs();
for (Map.Entry<UUID, HashMap<String, LuaValue>> entry : PLAYER_STORAGE.entrySet()) {
try {
FileWriter fileWriter = new FileWriter(new File(playerStorageDirectory, SteamwarUser.get(entry.getKey()).getId() + ".json"));
gson.toJson(toJson(entry.getValue()), fileWriter);
fileWriter.close();
} catch (IOException e) {}
}
}
private JsonObject toJson(HashMap<String, LuaValue> valueMap) {
JsonObject jsonObject = new JsonObject();
valueMap.forEach((string, luaValue) -> {
JsonElement value = toJson(luaValue);
if (value == null) return;
jsonObject.add(string, value);
});
return jsonObject;
}
private JsonElement toJson(LuaValue luaValue) {
if (luaValue.isnil()) {
return JsonNull.INSTANCE;
}
try {
return new JsonPrimitive(luaValue.checkboolean());
} catch (Exception e) {}
try {
return new JsonPrimitive(luaValue.checkint());
} catch (Exception e) {}
try {
return new JsonPrimitive(luaValue.checkdouble());
} catch (Exception e) {}
if (luaValue.isstring()) {
return new JsonPrimitive(luaValue.tojstring());
}
if (luaValue.istable()) {
LuaTable luaTable = luaValue.checktable();
JsonObject jsonObject = new JsonObject();
for (LuaValue key : luaTable.keys()) {
JsonElement value = toJson(luaTable.get(key));
if (value == null) continue;
try {
jsonObject.add(key.checkjstring(), value);
} catch (Exception e) {}
}
return jsonObject;
}
return null;
}
@Override
public String name() {
return "storage";
@ -92,7 +253,7 @@ public class StorageLib implements LuaLib {
storageLib.set("global", global);
LuaTable playerStorage = new LuaTable();
HashMap<String, LuaValue> playerStorageMap = PLAYER_STORAGE.computeIfAbsent(player, k -> new HashMap<>());
HashMap<String, LuaValue> playerStorageMap = PLAYER_STORAGE.computeIfAbsent(player.getUniqueId(), k -> new HashMap<>());
playerStorage.set("get", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
@ -137,34 +298,38 @@ public class StorageLib implements LuaLib {
storageLib.set("player", playerStorage);
LuaTable regionStorage = new LuaTable();
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
regionStorage.set("get", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
return regionStorageMap.getOrDefault(arg.checkjstring(), NIL);
}
});
regionStorage.set("set", new TwoArgFunction() {
@Override
public LuaValue call(LuaValue arg1, LuaValue arg2) {
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
return regionStorageMap.put(arg1.checkjstring(), arg2);
}
});
regionStorage.set("has", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
return valueOf(regionStorageMap.containsKey(arg.checkjstring()));
}
});
regionStorage.set("remove", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
return regionStorageMap.remove(arg.checkjstring());
}
});
regionStorage.set("accessor", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
String key = arg.checkjstring();
return new VarArgFunction() {
@Override

Datei anzeigen

@ -153,6 +153,36 @@ function iregion.protect() return nil end
---@return string
function iregion.loader() return nil end
---@return Position
function iregion.copyPoint() return nil end
---@return Position
function iregion.minPointBuild() return nil end
---@return Position
function iregion.maxPointBuild() return nil end
---@return Position
function iregion.minPointBuildExtension() return nil end
---@return Position
function iregion.maxPointBuildExtension() return nil end
---@return Position
function iregion.testblockPoint() return nil end
---@return Position
function iregion.minPointTestblock() return nil end
---@return Position
function iregion.maxPointTestblock() return nil end
---@return Position
function iregion.minPointTestblockExtension() return nil end
---@return Position
function iregion.maxPointTestblockExtension() return nil end
---@alias TNTMode 'ALLOW' | 'DENY' | 'ONLY_TB'
---@class tnt
@ -247,6 +277,7 @@ function tps.limit() return nil end
storage = {}
---@class storageLib
---Any Primitive, Array or Table will be saved across restarts, everything else will be discarded
local storageLib = {}
---@param key string