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