Add storage saving and loading
Dieser Commit ist enthalten in:
Ursprung
4ee93b59dd
Commit
bdeb7da4c6
@ -192,7 +192,7 @@ public class SteamWarLuaPlugin extends TwoArgFunction {
|
|||||||
class Print extends VarArgFunction {
|
class Print extends VarArgFunction {
|
||||||
@Override
|
@Override
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
player.sendMessage(varArgsToString(args));
|
player.sendMessage(varArgsToString(args).replaceAll("&([a-z0-9klmnor])", "§\1"));
|
||||||
return LuaValue.NIL;
|
return LuaValue.NIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem.features.script.lua.libs;
|
package de.steamwar.bausystem.features.script.lua.libs;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.region.Point;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.luaj.vm2.*;
|
import org.luaj.vm2.*;
|
||||||
@ -69,6 +70,14 @@ public interface LuaLib {
|
|||||||
return de.steamwar.bausystem.features.script.lua.SteamWarLuaPlugin.varArgsToString(varargs);
|
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() {
|
default Class<? extends LuaLib> parent() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.script.lua.libs;
|
|||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import net.md_5.bungee.api.ChatMessageType;
|
import net.md_5.bungee.api.ChatMessageType;
|
||||||
import net.md_5.bungee.api.chat.TextComponent;
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
import org.bukkit.Color;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.luaj.vm2.LuaTable;
|
import org.luaj.vm2.LuaTable;
|
||||||
@ -93,7 +94,7 @@ public class PlayerLib implements LuaLib {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
player.sendMessage(varArgsToString(args));
|
player.sendMessage(varArgsToString(args).replaceAll("&([a-z0-9klmnor])", "§\1"));
|
||||||
return LuaValue.NIL;
|
return LuaValue.NIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,7 +108,7 @@ public class PlayerLib implements LuaLib {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Varargs invoke(Varargs args) {
|
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;
|
return LuaValue.NIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,6 +82,17 @@ public class RegionLib implements LuaLib {
|
|||||||
Loader loader = Loader.getLoader(player);
|
Loader loader = Loader.getLoader(player);
|
||||||
table.set("loader", getter(() -> loader == null ? "OFF" : loader.getStage().name()));
|
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;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,13 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem.features.script.lua.libs;
|
package de.steamwar.bausystem.features.script.lua.libs;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
import de.steamwar.bausystem.region.Region;
|
import de.steamwar.bausystem.region.Region;
|
||||||
import de.steamwar.linkage.Linked;
|
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.bukkit.entity.Player;
|
||||||
import org.luaj.vm2.LuaTable;
|
import org.luaj.vm2.LuaTable;
|
||||||
import org.luaj.vm2.LuaValue;
|
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.TwoArgFunction;
|
||||||
import org.luaj.vm2.lib.VarArgFunction;
|
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.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Linked
|
@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<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<>();
|
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
|
@Override
|
||||||
public String name() {
|
public String name() {
|
||||||
return "storage";
|
return "storage";
|
||||||
@ -92,7 +253,7 @@ public class StorageLib implements LuaLib {
|
|||||||
storageLib.set("global", global);
|
storageLib.set("global", global);
|
||||||
|
|
||||||
LuaTable playerStorage = new LuaTable();
|
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() {
|
playerStorage.set("get", new OneArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
@ -137,34 +298,38 @@ public class StorageLib implements LuaLib {
|
|||||||
storageLib.set("player", playerStorage);
|
storageLib.set("player", playerStorage);
|
||||||
|
|
||||||
LuaTable regionStorage = new LuaTable();
|
LuaTable regionStorage = new LuaTable();
|
||||||
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
|
|
||||||
regionStorage.set("get", new OneArgFunction() {
|
regionStorage.set("get", new OneArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call(LuaValue arg) {
|
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);
|
return regionStorageMap.getOrDefault(arg.checkjstring(), NIL);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
regionStorage.set("set", new TwoArgFunction() {
|
regionStorage.set("set", new TwoArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
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);
|
return regionStorageMap.put(arg1.checkjstring(), arg2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
regionStorage.set("has", new OneArgFunction() {
|
regionStorage.set("has", new OneArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call(LuaValue arg) {
|
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()));
|
return valueOf(regionStorageMap.containsKey(arg.checkjstring()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
regionStorage.set("remove", new OneArgFunction() {
|
regionStorage.set("remove", new OneArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
|
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
|
||||||
return regionStorageMap.remove(arg.checkjstring());
|
return regionStorageMap.remove(arg.checkjstring());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
regionStorage.set("accessor", new OneArgFunction() {
|
regionStorage.set("accessor", new OneArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
|
HashMap<String, LuaValue> regionStorageMap = REGION_STORAGE.computeIfAbsent(Region.getRegion(player.getLocation()), k -> new HashMap<>());
|
||||||
String key = arg.checkjstring();
|
String key = arg.checkjstring();
|
||||||
return new VarArgFunction() {
|
return new VarArgFunction() {
|
||||||
@Override
|
@Override
|
||||||
|
31
sw.def.lua
31
sw.def.lua
@ -153,6 +153,36 @@ function iregion.protect() return nil end
|
|||||||
---@return string
|
---@return string
|
||||||
function iregion.loader() return nil end
|
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'
|
---@alias TNTMode 'ALLOW' | 'DENY' | 'ONLY_TB'
|
||||||
|
|
||||||
---@class tnt
|
---@class tnt
|
||||||
@ -247,6 +277,7 @@ function tps.limit() return nil end
|
|||||||
storage = {}
|
storage = {}
|
||||||
|
|
||||||
---@class storageLib
|
---@class storageLib
|
||||||
|
---Any Primitive, Array or Table will be saved across restarts, everything else will be discarded
|
||||||
local storageLib = {}
|
local storageLib = {}
|
||||||
|
|
||||||
---@param key string
|
---@param key string
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren