Update StorageLib and add some utility functions
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
13ec64a4fa
Commit
9ca83f02d9
@ -24,8 +24,10 @@ 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.OneArgFunction;
|
||||
import org.luaj.vm2.lib.TwoArgFunction;
|
||||
import org.luaj.vm2.lib.VarArgFunction;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@ -58,6 +60,34 @@ public class StorageLib implements LuaLib {
|
||||
return GLOBAL_STORAGE.put(arg1.checkjstring(), arg2);
|
||||
}
|
||||
});
|
||||
global.set("has", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return valueOf(GLOBAL_STORAGE.containsKey(arg.checkjstring()));
|
||||
}
|
||||
});
|
||||
global.set("remove", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return GLOBAL_STORAGE.remove(arg.checkjstring());
|
||||
}
|
||||
});
|
||||
global.set("accessor", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
String key = arg.checkjstring();
|
||||
return new VarArgFunction() {
|
||||
@Override
|
||||
public Varargs invoke(Varargs args) {
|
||||
if (args.narg() == 0) {
|
||||
return GLOBAL_STORAGE.getOrDefault(key, NIL);
|
||||
} else {
|
||||
return GLOBAL_STORAGE.put(key, args.arg(1));
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
||||
storageLib.set("global", global);
|
||||
|
||||
LuaTable playerStorage = new LuaTable();
|
||||
@ -74,6 +104,34 @@ public class StorageLib implements LuaLib {
|
||||
return playerStorageMap.put(arg1.checkjstring(), arg2);
|
||||
}
|
||||
});
|
||||
playerStorage.set("has", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return valueOf(playerStorageMap.containsKey(arg.checkjstring()));
|
||||
}
|
||||
});
|
||||
playerStorage.set("remove", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return playerStorageMap.remove(arg.checkjstring());
|
||||
}
|
||||
});
|
||||
playerStorage.set("accessor", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
String key = arg.checkjstring();
|
||||
return new VarArgFunction() {
|
||||
@Override
|
||||
public Varargs invoke(Varargs args) {
|
||||
if (args.narg() == 0) {
|
||||
return playerStorageMap.getOrDefault(key, NIL);
|
||||
} else {
|
||||
return playerStorageMap.put(key, args.arg(1));
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
||||
storageLib.set("player", playerStorage);
|
||||
|
||||
LuaTable regionStorage = new LuaTable();
|
||||
@ -90,6 +148,34 @@ public class StorageLib implements LuaLib {
|
||||
return regionStorageMap.put(arg1.checkjstring(), arg2);
|
||||
}
|
||||
});
|
||||
regionStorage.set("has", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return valueOf(regionStorageMap.containsKey(arg.checkjstring()));
|
||||
}
|
||||
});
|
||||
regionStorage.set("remove", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return regionStorageMap.remove(arg.checkjstring());
|
||||
}
|
||||
});
|
||||
regionStorage.set("accessor", new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg) {
|
||||
String key = arg.checkjstring();
|
||||
return new VarArgFunction() {
|
||||
@Override
|
||||
public Varargs invoke(Varargs args) {
|
||||
if (args.narg() == 0) {
|
||||
return regionStorageMap.getOrDefault(key, NIL);
|
||||
} else {
|
||||
return regionStorageMap.put(key, args.arg(1));
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
||||
storageLib.set("region", regionStorage);
|
||||
|
||||
return storageLib;
|
||||
|
20
SCRIPT.md
20
SCRIPT.md
@ -206,10 +206,22 @@ Es gibt folgende Module:
|
||||
|
||||
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 |
|
||||
| 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 |
|
||||
| `has` | set(String): Boolean | Prüft ob ein Wert vorhanden ist |
|
||||
| `remove` | remove(String) | Löscht den Schlüssel |
|
||||
| `accessor` | accessor(String): Accessor | Gibt einen Accessor zurück |
|
||||
|
||||
Ein Accessor ist ein Objekt, womit du direkt auf einen Wert zugreifen kannst und es ändern kannst.
|
||||
Es geht wie folgt:
|
||||
```lua
|
||||
keyAccessor = storage.player.accessor("key")
|
||||
|
||||
keyAccessor("Hello World") -- Setzt den Wert auf "Hello World"
|
||||
print(keyAccessor()) -- Gibt den Wert zurück
|
||||
```
|
||||
|
||||
## inventory
|
||||
Das `inventory`-Modul stellt Funktionen zur Verfügung, um ein Inventar zu öffnen.
|
||||
|
16
sw.def.lua
16
sw.def.lua
@ -255,6 +255,22 @@ function storageLib.get(key) return nil end
|
||||
---@return void
|
||||
function storageLib.set(key, value) end
|
||||
|
||||
---@param key string
|
||||
---@return boolean
|
||||
function storageLib.has(key) return nil end
|
||||
|
||||
---@param key string
|
||||
---@return void
|
||||
function storageLib.remove(key) end
|
||||
|
||||
---@param key string
|
||||
---@return Accessor
|
||||
function storageLib.accessor(key) return nil end
|
||||
|
||||
---@class Accessor
|
||||
---@overload fun(): any
|
||||
---@overload fun(value: any)
|
||||
|
||||
---@class Selection
|
||||
---@field max Position
|
||||
---@field min Position
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren