SteamWar/BauSystem2.0
Archiviert
12
0

Implement new logic for CustomScript

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-09-20 22:01:37 +02:00
Ursprung 966ed270db
Commit 1b22f9b895
3 geänderte Dateien mit 68 neuen und 167 gelöschten Zeilen

Datei anzeigen

@ -26,7 +26,6 @@ import lombok.experimental.UtilityClass;
import org.bukkit.Material;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONMap;
@ -43,7 +42,7 @@ public class CustomScript {
public interface MenuScript {
void toYAPION(YAPIONMap yapionMap);
ItemStack toItem();
SWItem toItem();
}
public interface CustomEvent extends Script {
@ -53,8 +52,8 @@ public class CustomScript {
@RequiredArgsConstructor
public class InventoryEvent implements CustomEvent {
private final BookMeta bookMeta;
private final String eventName;
public final BookMeta bookMeta;
public final String eventName;
@Override
public String eventName() {
@ -70,8 +69,8 @@ public class CustomScript {
@RequiredArgsConstructor
public class MenuEvent implements CustomEvent, MenuScript {
private final List<String> pages;
private final String eventName;
public final List<String> pages;
public final String eventName;
@Override
public String eventName() {
@ -92,12 +91,12 @@ public class CustomScript {
}
@Override
public ItemStack toItem() {
public SWItem toItem() {
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: §e" + eventName);
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem.getItemStack();
return swItem;
}
}
@ -161,8 +160,8 @@ public class CustomScript {
@RequiredArgsConstructor
public class InventoryCommand implements CustomCommand {
private final BookMeta bookMeta;
private final String[] args;
public final BookMeta bookMeta;
public final String[] args;
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
Map<String, Value> arguments = check(args, command);
@ -178,8 +177,8 @@ public class CustomScript {
@RequiredArgsConstructor
public class MenuCommand implements CustomCommand, MenuScript {
private final List<String> pages;
private final String[] args;
public final List<String> pages;
public final String[] args;
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
Map<String, Value> arguments = check(args, command);
@ -200,12 +199,12 @@ public class CustomScript {
}
@Override
public ItemStack toItem() {
public SWItem toItem() {
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§8/§e" + String.join(" ", args));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem.getItemStack();
return swItem;
}
}
}

Datei anzeigen

@ -20,15 +20,12 @@
package de.steamwar.bausystem.features.script;
import de.steamwar.bausystem.SWUtils;
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 de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.sql.UserConfig;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -53,117 +50,11 @@ import java.util.stream.Collectors;
@Linked(LinkageType.LISTENER)
public class CustomScriptListener implements Listener {
private interface CustomCommand {
default Map<String, Value> check(String[] args, String[] command) {
if (args.length < command.length) {
return null;
}
if (!args[0].equals(command[0])) {
return null;
}
Map<String, Value> arguments = new HashMap<>();
if (!check(arguments, args, command, 0, 0)) {
return null;
}
return arguments;
}
default boolean check(Map<String, Value> arguments, String[] args, String[] command, int argsIndex, int commandIndex) {
if (command.length <= commandIndex) {
for (int i = argsIndex; i < args.length; i++) {
if (!(args[i].startsWith("(") && args[i].endsWith(")"))) {
return false;
}
}
return true;
}
if (args.length <= argsIndex) return true;
String currentArg = args[argsIndex];
String currentCommand = command[commandIndex];
if (currentArg.startsWith("<") && currentArg.endsWith(">")) {
arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand));
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
} else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) {
arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand));
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
} else if (currentArg.startsWith("(") && currentArg.endsWith(")")) {
if (!trim(currentArg, 1).equals(currentCommand)) {
arguments.put(trim(currentArg, 1), new Value.BooleanValue(false));
return check(arguments, args, command, argsIndex + 1, commandIndex);
} else {
arguments.put(trim(currentArg, 1), new Value.BooleanValue(true));
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
}
} else {
if (!currentArg.equals(currentCommand)) return false;
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
}
}
default String trim(String s, int count) {
return s.substring(count, s.length() - count);
}
boolean execute(String[] command, PlayerCommandPreprocessEvent e);
}
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
private static class InventoryCommand implements CustomCommand {
private final BookMeta bookMeta;
private final String[] args;
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
Map<String, Value> arguments = check(args, command);
if (arguments == null) {
return false;
}
e.setCancelled(true);
new ScriptExecutor(bookMeta, e.getPlayer(), arguments);
return true;
}
}
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
private static class MenuCommand implements CustomCommand {
private final List<String> pages;
private final String[] args;
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
Map<String, Value> arguments = check(args, command);
if (arguments == null) {
return false;
}
e.setCancelled(true);
new ScriptExecutor(pages, e.getPlayer(), arguments);
return true;
}
public void toYAPION(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
public ItemStack toItem() {
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, String.join(" ", args));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem.getItemStack();
}
}
private Map<Player, List<CustomCommand>> playerMap = new HashMap<>();
private Map<Player, List<CustomScript.Script>> playerMap = new HashMap<>();
private void updateInventory(Player p) {
playerMap.computeIfPresent(p, (player, customCommands) -> {
customCommands.removeIf(InventoryCommand.class::isInstance);
customCommands.removeIf(CustomScript.InventoryCommand.class::isInstance);
return customCommands;
});
for (ItemStack item : p.getInventory().getContents()) {
@ -179,10 +70,11 @@ public class CustomScriptListener implements Listener {
continue;
}
String s = bookMeta.getPage(1).split("\n")[0];
if (!s.startsWith("#!CMD /")) {
continue;
if (s.startsWith("#!CMD /")) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" ")));
} else if (s.startsWith("#!EVENT ")) {
// Event System
}
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryCommand(bookMeta, s.substring(6).split(" ")));
}
}
@ -210,7 +102,13 @@ public class CustomScriptListener implements Listener {
yapionObject.getYAPIONMapOrSetDefault("commands", new YAPIONMap()).forEach((key, value) -> {
String[] command = ((YAPIONValue<String>) key).get().split(" ");
List<String> pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList());
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuCommand(pages, command));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuCommand(pages, command));
});
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> {
String eventName = ((YAPIONValue<String>) key).get();
List<String> pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList());
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuEvent(pages, eventName));
});
}
@ -223,10 +121,15 @@ public class CustomScriptListener implements Listener {
private YAPIONObject output(Player p) {
if (!playerMap.containsKey(p)) return new YAPIONObject();
YAPIONObject yapionObject = new YAPIONObject();
YAPIONMap yapionMap = new YAPIONMap();
yapionObject.add("commands", yapionMap);
playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> {
menuCommand.toYAPION(yapionMap);
YAPIONMap commandsMap = new YAPIONMap();
yapionObject.add("commands", commandsMap);
playerMap.get(p).stream().filter(CustomScript.MenuCommand.class::isInstance).map(CustomScript.MenuCommand.class::cast).forEach(menuCommand -> {
menuCommand.toYAPION(commandsMap);
});
YAPIONMap eventsMap = new YAPIONMap();
yapionObject.add("events", eventsMap);
playerMap.get(p).stream().filter(CustomScript.MenuEvent.class::isInstance).map(CustomScript.MenuEvent.class::cast).forEach(menuCommand -> {
menuCommand.toYAPION(commandsMap);
});
return yapionObject;
}
@ -258,13 +161,9 @@ public class CustomScriptListener implements Listener {
return;
}
List<CustomCommand> inventoryCommands = playerMap.get(e.getPlayer());
if (inventoryCommands == null) {
return;
}
List<CustomScript.CustomCommand> customCommands = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList());
String[] command = e.getMessage().split(" ");
for (CustomCommand customCommand : inventoryCommands) {
for (CustomScript.CustomCommand customCommand : customCommands) {
if (customCommand.execute(command, e)) {
return;
}
@ -276,13 +175,14 @@ public class CustomScriptListener implements Listener {
}
public void openCommandsMenu(Player p) {
List<SWListInv.SWListEntry<MenuCommand>> menuCommands = new ArrayList<>();
playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> {
String command = "§e" + String.join(" ", menuCommand.args);
List<SWListInv.SWListEntry<CustomScript.MenuScript>> menuCommands = new ArrayList<>();
playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> {
SWItem swItem = menuItem.toItem();
swItem.setLore(Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"));
menuCommands.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, command, Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"), false, clickType -> {
}), menuCommand));
menuCommands.add(new SWListInv.SWListEntry<>(swItem, menuItem));
});
int length = (int) output(p).toYAPION(new LengthOutput()).getLength();
StringBuilder menuName = new StringBuilder();
menuName.append("§eScript Commands ");
@ -295,11 +195,12 @@ public class CustomScriptListener implements Listener {
menuName.append("§a");
}
menuName.append(percentage).append("§7%");
SWListInv<MenuCommand> menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> {
SWListInv<CustomScript.MenuScript> menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> {
if (!clickType.isCreativeAction()) {
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
}
SWUtils.giveItemToPlayer(p, menuCommand.toItem());
SWUtils.giveItemToPlayer(p, menuCommand.toItem().getItemStack());
p.closeInventory();
save(p);
});
@ -320,30 +221,31 @@ public class CustomScriptListener implements Listener {
return;
}
String s = bookMeta.getPage(1).split("\n")[0];
if (!s.startsWith("#!CMD /")) {
return;
}
MenuCommand menuCommand = new MenuCommand(bookMeta.getPages(), s.substring(6).split(" "));
for (CustomCommand customCommand : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) {
if (!(customCommand instanceof MenuCommand)) {
continue;
if (s.startsWith("#!CMD /")) {
CustomScript.MenuCommand menuCommand = new CustomScript.MenuCommand(bookMeta.getPages(), s.substring(6).split(" "));
for (CustomScript.Script script : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) {
if (!(script instanceof CustomScript.MenuCommand)) {
continue;
}
if (Arrays.equals(((CustomScript.MenuCommand) script).args, menuCommand.args)) {
p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert");
return;
}
}
if (Arrays.equals(((MenuCommand) customCommand).args, menuCommand.args)) {
p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert");
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand);
if (!save(p)) {
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
p.closeInventory();
SWUtils.giveItemToPlayer(p, p.getItemOnCursor());
save(p);
p.sendMessage("§cScript-Buch Limit erreicht");
return;
}
p.setItemOnCursor(null);
openCommandsMenu(p);
} else if (s.startsWith("#!EVENT ")) {
// Event stuff
}
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand);
if (!save(p)) {
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
p.closeInventory();
SWUtils.giveItemToPlayer(p, p.getItemOnCursor());
save(p);
p.sendMessage("§cScript-Buch Limit erreicht");
return;
}
p.setItemOnCursor(null);
openCommandsMenu(p);
}));
menuCommandSWListInv.open();
}

Datei anzeigen

@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand {
}
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht (<>) wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> {
}), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§8-§7 FF"), false, clickType -> {
}), null));
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> {
}), null));