SteamWar/BauSystem2.0
Archiviert
12
0

CustomCommandListener on steroids

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-07-10 14:26:33 +02:00
Ursprung 649a25166f
Commit 78ad3cf7a3
3 geänderte Dateien mit 53 neuen und 9 gelöschten Zeilen

Datei anzeigen

@ -19,9 +19,12 @@
package de.steamwar.bausystem.features.script;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import de.steamwar.core.VersionedCallable;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -32,13 +35,44 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
@Linked(LinkageType.LISTENER)
public class CustomCommandListener implements Listener {
private Map<Player, Map<String, BookMeta>> playerMap = new HashMap<>();
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
private static class CustomCommand {
private final BookMeta bookMeta;
private final String[] args;
public boolean execute(PlayerCommandPreprocessEvent e) {
String[] command = e.getMessage().split(" ");
if (args.length != command.length) {
return false;
}
if (!args[0].equals(command[0])) {
return false;
}
Map<String, Value> arguments = new HashMap<>();
for (int i = 1; i < args.length; i++) {
String current = args[i];
if (current.startsWith("<") && current.endsWith(">")) {
arguments.put(current.substring(1, current.length() - 1), new Value.StringValue(command[i]));
} else {
if (!current.equals(command[i])) {
return false;
}
}
}
e.setCancelled(true);
new ScriptExecutor(bookMeta, e.getPlayer(), arguments);
return true;
}
}
private Map<Player, List<CustomCommand>> playerMap = new HashMap<>();
private void updateInventory(Player p) {
playerMap.remove(p);
@ -55,10 +89,10 @@ public class CustomCommandListener implements Listener {
continue;
}
String s = bookMeta.getPage(1).split("\n")[0];
if (!s.startsWith("#!CMD ")) {
if (!s.startsWith("#!CMD /")) {
continue;
}
playerMap.computeIfAbsent(p, player -> new HashMap<>()).put(s.substring(6), bookMeta);
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomCommand(bookMeta, s.substring(6).split(" ")));
}
}
@ -86,10 +120,16 @@ public class CustomCommandListener implements Listener {
return;
}
Map<String, BookMeta> bookMetaMap = playerMap.get(e.getPlayer());
if (bookMetaMap == null) {
List<CustomCommand> customCommands = playerMap.get(e.getPlayer());
if (customCommands == null) {
return;
}
customCommands.stream().map(customCommand -> customCommand.execute(e)).filter(b -> !b).findFirst();
/*String[] command = e.getMessage().split(" ");
BookMeta bookMeta = bookMetaMap.get(e.getMessage());
if (bookMeta == null) {
return;
@ -99,7 +139,7 @@ public class CustomCommandListener implements Listener {
new ScriptExecutor(bookMeta, e.getPlayer());
} catch (StackOverflowError exception) {
// Ignored
}
}*/
}
private boolean isNoBook(ItemStack item) {

Datei anzeigen

@ -39,6 +39,10 @@ public final class ScriptExecutor {
private int index = 0;
public ScriptExecutor(BookMeta bookMeta, Player player) {
this(bookMeta, player, new HashMap<>());
}
public ScriptExecutor(BookMeta bookMeta, Player player, Map<String, Value> localVariables) {
this.player = player;
globalVariables = ScriptListener.getGlobalContext(player);
@ -62,6 +66,7 @@ public final class ScriptExecutor {
}
}
if (commands.isEmpty()) return;
localVariables.forEach(this.localVariables::putValue);
resume();
}

Datei anzeigen

@ -35,7 +35,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.BoundingBox;