forked from SteamWar/SteamWar
Improve ScriptRunner
This commit is contained in:
+26
-13
@@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.features.script.lua.CommandRegister;
|
||||
import de.steamwar.bausystem.features.script.lua.SteamWarGlobalLuaPlugin;
|
||||
import de.steamwar.bausystem.features.script.lua.SteamWarPlatform;
|
||||
import de.steamwar.core.SWPlayer;
|
||||
import de.steamwar.sql.Script;
|
||||
import de.steamwar.sql.SteamwarUser;
|
||||
import lombok.experimental.UtilityClass;
|
||||
@@ -44,12 +45,14 @@ public class ScriptRunner {
|
||||
// Key -> bau-script-<BUCH NAME>
|
||||
// Value -> <LUA Script>
|
||||
|
||||
private static final Map<Player, Map<SteamWarGlobalLuaPlugin.EventType, List<LuaFunction>>> EVENT_MAP = new HashMap<>();
|
||||
private static final Map<Player, Map<Hotkey, List<LuaFunction>>> HOTKEY_MAP = new HashMap<>();
|
||||
private static final Map<Player, Map<String, CommandRegister>> COMMAND_MAP = new HashMap<>();
|
||||
public class ScriptData implements SWPlayer.Component {
|
||||
private Map<SteamWarGlobalLuaPlugin.EventType, List<LuaFunction>> events = new EnumMap<>(SteamWarGlobalLuaPlugin.EventType.class);
|
||||
private Map<Hotkey, List<LuaFunction>> hotkeys = new HashMap<>();
|
||||
private Map<String, CommandRegister> commands = new HashMap<>();
|
||||
}
|
||||
|
||||
public Set<String> getCommandsOfPlayer(Player player) {
|
||||
return COMMAND_MAP.getOrDefault(player, new HashMap<>()).keySet();
|
||||
return SWPlayer.of(player).getComponentOrDefault(ScriptData.class, ScriptData::new).commands.keySet();
|
||||
}
|
||||
|
||||
public static void runScript(String script, Player player) {
|
||||
@@ -64,10 +67,12 @@ public class ScriptRunner {
|
||||
|
||||
public static void createGlobalScript(List<String> scripts, Player player) {
|
||||
remove(player);
|
||||
SWPlayer swPlayer = SWPlayer.of(player);
|
||||
ScriptData scriptData = swPlayer.getComponentOrDefault(ScriptData.class, ScriptData::new);
|
||||
Globals globals = SteamWarPlatform.createGlobalGlobals(player,
|
||||
(s, luaFunction) -> EVENT_MAP.computeIfAbsent(player, player1 -> new EnumMap<>(SteamWarGlobalLuaPlugin.EventType.class)).computeIfAbsent(s, s1 -> new ArrayList<>()).add(luaFunction),
|
||||
(s, luaFunction) -> HOTKEY_MAP.computeIfAbsent(player, player1 -> new HashMap<>()).computeIfAbsent(Hotkey.fromString(s), s1 -> new ArrayList<>()).add(luaFunction),
|
||||
commandRegister -> COMMAND_MAP.computeIfAbsent(player, player1 -> new HashMap<>()).put(commandRegister.getName(), commandRegister));
|
||||
(s, luaFunction) -> scriptData.events.computeIfAbsent(s, s1 -> new ArrayList<>()).add(luaFunction),
|
||||
(s, luaFunction) -> scriptData.hotkeys.computeIfAbsent(Hotkey.fromString(s), s1 -> new ArrayList<>()).add(luaFunction),
|
||||
commandRegister -> scriptData.commands.put(commandRegister.getName(), commandRegister));
|
||||
|
||||
for (String script : scripts) {
|
||||
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> globals.load(script).call());
|
||||
@@ -75,13 +80,14 @@ public class ScriptRunner {
|
||||
}
|
||||
|
||||
public static void remove(Player player) {
|
||||
EVENT_MAP.remove(player);
|
||||
COMMAND_MAP.remove(player);
|
||||
HOTKEY_MAP.remove(player);
|
||||
SWPlayer.of(player).removeComponent(ScriptData.class);
|
||||
}
|
||||
|
||||
public static void callEvent(Player player, SteamWarGlobalLuaPlugin.EventType event, LuaValue eventValue, Event wrappedEvent) {
|
||||
List<LuaFunction> luaFunctions = EVENT_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(event, Collections.emptyList());
|
||||
List<LuaFunction> luaFunctions = SWPlayer.of(player).getComponent(ScriptData.class)
|
||||
.map(scriptData -> scriptData.events)
|
||||
.orElse(Collections.emptyMap())
|
||||
.getOrDefault(event, Collections.emptyList());
|
||||
if (luaFunctions.isEmpty()) {
|
||||
if(event == SteamWarGlobalLuaPlugin.EventType.DoubleSwap) {
|
||||
player.performCommand("gui");
|
||||
@@ -124,7 +130,10 @@ public class ScriptRunner {
|
||||
}
|
||||
|
||||
public static boolean callCommand(Player player, String command, String[] argsArray) {
|
||||
CommandRegister commandRegister = COMMAND_MAP.getOrDefault(player, Collections.emptyMap()).get(command);
|
||||
CommandRegister commandRegister = SWPlayer.of(player).getComponent(ScriptData.class)
|
||||
.map(scriptData -> scriptData.commands)
|
||||
.orElse(Collections.emptyMap())
|
||||
.get(command);
|
||||
if (commandRegister == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -188,7 +197,11 @@ public class ScriptRunner {
|
||||
|
||||
public static void callHotkey(int mods, int key, Player player, boolean pressed) {
|
||||
Hotkey hotkey = Hotkey.fromChar(key, mods);
|
||||
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> HOTKEY_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(hotkey, Collections.emptyList()).forEach(luaFunction -> luaFunction.call(LuaValue.valueOf(pressed))));
|
||||
List<LuaFunction> hotkeys = SWPlayer.of(player).getComponent(ScriptData.class)
|
||||
.map(scriptData -> scriptData.hotkeys)
|
||||
.orElse(Collections.emptyMap())
|
||||
.getOrDefault(hotkey, Collections.emptyList());
|
||||
catchScript("SCRIPT_ERROR_GLOBAL", player, () -> hotkeys.forEach(luaFunction -> luaFunction.call(LuaValue.valueOf(pressed))));
|
||||
}
|
||||
|
||||
public static void catchScript(String errorMsg, Player player, Runnable run) {
|
||||
|
||||
Reference in New Issue
Block a user