SteamWar/BauSystem2.0
Archiviert
12
0

Update script structure
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-12-28 16:09:05 +01:00
Ursprung 162fbbaa0c
Commit 54b34bbfae
20 geänderte Dateien mit 783 neuen und 469 gelöschten Zeilen

Datei anzeigen

@ -20,7 +20,7 @@
package de.steamwar.bausystem.features.region;
import de.steamwar.bausystem.features.script.CustomScriptManager;
import de.steamwar.bausystem.features.script.EventType;
import de.steamwar.bausystem.features.script.custom.event.EventType;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;

Datei anzeigen

@ -1,323 +0,0 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.inventory.SWItem;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.inventory.meta.BookMeta;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@UtilityClass
public class CustomScript {
public interface Script {
}
public interface MenuScript {
void toYAPION(YAPIONMap yapionMap);
SWItem toItem(Player p);
}
public interface Hotkey extends Script {
String hotkey();
boolean execute(Player p, boolean pressed);
}
@RequiredArgsConstructor
public static class InventoryHotkey implements Hotkey {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String hotkey() {
return args[0];
}
@Override
public boolean execute(Player p, boolean pressed) {
Map<String, Value> variables = new HashMap<>();
variables.put("pressed", new Value.BooleanValue(pressed));
variables.put("released", new Value.BooleanValue(!pressed));
new ScriptExecutor(bookMeta, p, variables, null);
return false;
}
}
@RequiredArgsConstructor
public static class MenuHotkey implements Hotkey, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String hotkey() {
return args[0];
}
@Override
public boolean execute(Player p, boolean pressed) {
Map<String, Value> variables = new HashMap<>();
variables.put("pressed", new Value.BooleanValue(pressed));
variables.put("released", new Value.BooleanValue(!pressed));
new ScriptExecutor(pages, p, variables, null);
return false;
}
@Override
public void toYAPION(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
StringBuilder st = new StringBuilder();
for (int i = 1; i < args.length; i++) {
if (i != 1) st.append(" ");
st.append(args[i]);
}
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_HOTKEY_ITEM_NAME", p, args[0], st.toString()));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}
public interface CustomEvent extends Script {
String eventName();
boolean execute(Player p, Map<String, Value> variables, Consumer<String> echoConsumer);
}
@RequiredArgsConstructor
public static class InventoryEvent implements CustomEvent {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String eventName() {
return args[0];
}
@Override
public boolean execute(Player p, Map<String, Value> variables, Consumer<String> echoConsumer) {
new ScriptExecutor(bookMeta, p, variables, echoConsumer);
return true;
}
}
@RequiredArgsConstructor
public static class MenuEvent implements CustomEvent, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String eventName() {
return args[0];
}
@Override
public boolean execute(Player p, Map<String, Value> variables, Consumer<String> echoConsumer) {
new ScriptExecutor(pages, p, variables, echoConsumer);
return false;
}
@Override
public void toYAPION(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
StringBuilder st = new StringBuilder();
for (int i = 1; i < args.length; i++) {
if (i != 1) st.append(" ");
st.append(args[i]);
}
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_EVENT_ITEM_NAME", p, args[0], st.toString()));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}
public interface CustomCommand extends Script {
String[] command();
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
public static class InventoryCommand implements CustomCommand {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String[] command() {
return 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, null);
return true;
}
}
@RequiredArgsConstructor
public static class MenuCommand implements CustomCommand, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String[] command() {
return 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, null);
return true;
}
public boolean equals(MenuCommand menuCommand) {
if (menuCommand.args.length != args.length) {
return false;
}
if (!args[0].equals(menuCommand.args[0])) {
return false;
}
for (int i = 0; i < args.length; i++) {
if (i == 0) continue;
String s1 = args[i];
String s2 = menuCommand.args[i];
if (!s1.equals(s2)) {
return false;
}
if (!(s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">"))) {
return false;
}
if (!(s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)"))) {
return false;
}
}
return true;
}
@Override
public void toYAPION(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_COMMAND_ITEM_NAME", p, String.join(" ", args)));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}
}

Datei anzeigen

@ -21,6 +21,19 @@ package de.steamwar.bausystem.features.script;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.script.custom.MenuScript;
import de.steamwar.bausystem.features.script.custom.Script;
import de.steamwar.bausystem.features.script.custom.command.CustomCommand;
import de.steamwar.bausystem.features.script.custom.command.InventoryCommand;
import de.steamwar.bausystem.features.script.custom.command.MenuCommand;
import de.steamwar.bausystem.features.script.custom.event.CustomEvent;
import de.steamwar.bausystem.features.script.custom.event.EventType;
import de.steamwar.bausystem.features.script.custom.event.InventoryEvent;
import de.steamwar.bausystem.features.script.custom.event.MenuEvent;
import de.steamwar.bausystem.features.script.custom.hotkey.Hotkey;
import de.steamwar.bausystem.features.script.custom.hotkey.Hotkeys;
import de.steamwar.bausystem.features.script.custom.hotkey.InventoryHotkey;
import de.steamwar.bausystem.features.script.custom.hotkey.MenuHotkey;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.inventory.SWItem;
@ -54,11 +67,11 @@ import java.util.stream.Collectors;
@Linked
public class CustomScriptManager implements Listener {
private final Map<Player, List<CustomScript.Script>> playerMap = Collections.synchronizedMap(new HashMap<>()); // new ConcurrentHashMap<>();
public final Map<Player, List<Script>> playerMap = Collections.synchronizedMap(new HashMap<>()); // new ConcurrentHashMap<>();
private void updateInventory(Player p) {
playerMap.computeIfPresent(p, (player, customCommands) -> {
customCommands.removeIf(script -> !(script instanceof CustomScript.MenuScript));
customCommands.removeIf(script -> !(script instanceof MenuScript));
return customCommands;
});
for (ItemStack item : p.getInventory().getContents()) {
@ -75,11 +88,11 @@ public class CustomScriptManager implements Listener {
}
String s = bookMeta.getPage(1).split("\n")[0];
if (s.startsWith("#!CMD /")) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" ")));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryCommand(bookMeta, s.substring(6).split(" ")));
} else if (s.startsWith("#!EVENT ")) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryEvent(bookMeta, s.substring(8).split(" ")));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryEvent(bookMeta, s.substring(8).split(" ")));
} else if (s.startsWith("#!HOTKEY ")) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryHotkey(bookMeta, s.substring(9).split(" ")));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryHotkey(bookMeta, s.substring(9).split(" ")));
}
}
}
@ -113,19 +126,19 @@ public class CustomScriptManager 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 CustomScript.MenuCommand(pages, command));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuCommand(pages, command));
});
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> {
String[] event = ((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 CustomScript.MenuEvent(pages, event));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuEvent(pages, event));
});
yapionObject.getYAPIONMapOrSetDefault("hotkeys", new YAPIONMap()).forEach((key, value) -> {
String[] hotkey = ((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 CustomScript.MenuHotkey(pages, hotkey));
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuHotkey(pages, hotkey));
});
}
@ -141,20 +154,20 @@ public class CustomScriptManager implements Listener {
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);
playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> {
menuCommand.save(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(eventsMap);
playerMap.get(p).stream().filter(MenuEvent.class::isInstance).map(MenuEvent.class::cast).forEach(menuCommand -> {
menuCommand.save(eventsMap);
});
YAPIONMap hotkeysMap = new YAPIONMap();
yapionObject.add("hotkeys", hotkeysMap);
playerMap.get(p).stream().filter(CustomScript.MenuHotkey.class::isInstance).map(CustomScript.MenuHotkey.class::cast).forEach(menuCommand -> {
menuCommand.toYAPION(hotkeysMap);
playerMap.get(p).stream().filter(MenuHotkey.class::isInstance).map(MenuHotkey.class::cast).forEach(menuCommand -> {
menuCommand.save(hotkeysMap);
});
return yapionObject;
}
@ -180,13 +193,13 @@ public class CustomScriptManager implements Listener {
}
public void openCommandsMenu(Player p) {
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 -> {
List<SWListInv.SWListEntry<MenuScript>> menuCommands = new ArrayList<>();
playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(MenuScript.class::isInstance).map(MenuScript.class::cast).forEach(menuItem -> {
SWItem swItem = menuItem.toItem(p);
ItemStack itemStack = swItem.getItemStack();
if (menuItem instanceof CustomScript.MenuHotkey) {
if (menuItem instanceof MenuHotkey) {
itemStack.setType(Material.CHAIN_COMMAND_BLOCK);
} else if (menuItem instanceof CustomScript.MenuEvent) {
} else if (menuItem instanceof MenuEvent) {
itemStack.setType(Material.REPEATING_COMMAND_BLOCK);
} else {
itemStack.setType(Material.COMMAND_BLOCK);
@ -201,7 +214,7 @@ public class CustomScriptManager implements Listener {
double percentage = ((int) ((length / 655336.0) * 1000)) / 10.0;
String menuName = BauSystem.MESSAGE.parse("SCRIPT_MENU_GUI_NAME", p, percentage > 99 ? "§c" : (percentage >= 75 ? "§6" : "§a"), percentage);
SWListInv<CustomScript.MenuScript> menuCommandSWListInv = new SWListInv<>(p, menuName, false, menuCommands, (clickType, menuCommand) -> {
SWListInv<MenuScript> menuCommandSWListInv = new SWListInv<>(p, menuName, false, menuCommands, (clickType, menuCommand) -> {
if (!clickType.isShiftClick()) {
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
}
@ -227,19 +240,19 @@ public class CustomScriptManager implements Listener {
}
String s = bookMeta.getPage(1).split("\n")[0];
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)) {
MenuCommand menuCommand = new MenuCommand(bookMeta.getPages(), s.substring(6).split(" "));
for (Script script : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) {
if (!(script instanceof MenuCommand)) {
continue;
}
if (((CustomScript.MenuCommand) script).equals(menuCommand)) {
if (((MenuCommand) script).equals(menuCommand)) {
BauSystem.MESSAGE.sendPrefixless("SCRIPT_MENU_GUI_DUPLICATE_COMMAND", p, String.join(" ", menuCommand.command()));
return;
}
}
scriptBookLimit(p, menuCommand);
} else if (s.startsWith("#!EVENT ")) {
CustomScript.MenuEvent menuEvent = new CustomScript.MenuEvent(bookMeta.getPages(), s.substring(8).split(" "));
MenuEvent menuEvent = new MenuEvent(bookMeta.getPages(), s.substring(8).split(" "));
try {
EventType.valueOf(menuEvent.eventName());
} catch (Exception e) {
@ -248,13 +261,13 @@ public class CustomScriptManager implements Listener {
}
scriptBookLimit(p, menuEvent);
} else if (s.startsWith("#!HOTKEY ")) {
scriptBookLimit(p, new CustomScript.MenuHotkey(bookMeta.getPages(), s.substring(9).split(" ")));
scriptBookLimit(p, new MenuHotkey(bookMeta.getPages(), s.substring(9).split(" ")));
}
}));
menuCommandSWListInv.open();
}
private void scriptBookLimit(Player p, CustomScript.Script menuScript) {
private void scriptBookLimit(Player p, Script menuScript) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuScript);
if (!save(p)) {
playerMap.get(p).removeIf(script -> script == menuScript);
@ -268,14 +281,25 @@ public class CustomScriptManager implements Listener {
openCommandsMenu(p);
}
public boolean callCommand(Player p, PlayerCommandPreprocessEvent e, String message) {
List<CustomCommand> customCommands = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomCommand.class::isInstance).map(CustomCommand.class::cast).collect(Collectors.toList());
String[] command = message.split(" ");
for (CustomCommand customCommand : customCommands) {
if (customCommand.execute(command, e)) {
return true;
}
}
return false;
}
public boolean callScoreboard(Player p, Map<String, String> variables, Consumer<String> echoConsumer) {
Map<String, Value> valueMap = new HashMap<>();
for (Map.Entry<String, String> entry : variables.entrySet()) {
valueMap.put(entry.getKey(), new Value.StringValue(entry.getValue()));
}
List<CustomScript.CustomEvent> customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList());
List<CustomEvent> customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomEvent.class::isInstance).map(CustomEvent.class::cast).collect(Collectors.toList());
boolean hasScript = false;
for (CustomScript.CustomEvent customEvent : customEvents) {
for (CustomEvent customEvent : customEvents) {
if (customEvent.eventName().equals(EventType.Scoreboard.name())) {
customEvent.execute(p, valueMap, echoConsumer);
hasScript = true;
@ -288,8 +312,8 @@ public class CustomScriptManager implements Listener {
if (eventType == null) return;
if (!eventType.getEventType().equals(e.getClass())) return;
List<CustomScript.CustomEvent> customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList());
for (CustomScript.CustomEvent customEvent : customEvents) {
List<CustomEvent> customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomEvent.class::isInstance).map(CustomEvent.class::cast).collect(Collectors.toList());
for (CustomEvent customEvent : customEvents) {
if (customEvent.eventName().equals(eventType.name())) {
Map<String, Value> variables = eventType.getEventValues().apply(e);
if (variables == null) {
@ -308,85 +332,11 @@ public class CustomScriptManager implements Listener {
}
public void callHotkey(int modifiers, char pressedHotkey, Player p, boolean pressed) {
List<CustomScript.Hotkey> hotkeys = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.Hotkey.class::isInstance).map(CustomScript.Hotkey.class::cast).collect(Collectors.toList());
for (CustomScript.Hotkey hotkey : hotkeys) {
List<Hotkey> hotkeys = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(Hotkey.class::isInstance).map(Hotkey.class::cast).collect(Collectors.toList());
for (Hotkey hotkey : hotkeys) {
if (Hotkeys.isSame(modifiers, pressedHotkey, hotkey.hotkey())) {
hotkey.execute(p, pressed);
}
}
}
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
if (e.getMessage().startsWith("/script:")) {
e.setMessage("/" + e.getMessage().substring(8));
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 (CustomScript.CustomCommand customCommand : customCommands) {
if (customCommand.execute(command, e)) {
return;
}
}
}
/*
{
final Class<?> packetPlayOutCommands = Reflection.getClass("{nms}.PacketPlayOutCommands");
final Reflection.FieldAccessor<RootCommandNode> playerInfoData = Reflection.getField(packetPlayOutCommands, RootCommandNode.class, 0);
ProtocolAPI.setOutgoingHandler(packetPlayOutCommands, (player, packet) -> {
PacketPlayOutCommands commands = (PacketPlayOutCommands) packet;
RootCommandNode rootCommandNode = playerInfoData.get(commands);
if (!playerMap.containsKey(player)) {
load(player);
}
// rootCommandNode.addChild(new LiteralCommandNode<Player>("Hello", null, t -> true, null, null, false));
List<CustomScript.CustomCommand> customCommands;
synchronized (playerMap) {
customCommands = playerMap.getOrDefault(player, new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList());
}
for (CustomScript.CustomCommand customCommand : customCommands) {
System.out.println("ADDING: " + customCommand);
String[] args = customCommand.command();
List<CommandNode<?>> commandNodes = new ArrayList<>();
for (int i = args.length - 1; i >= 0; i--) {
if (i == 0) {
CommandNode current = new LiteralCommandNode<Player>(args[i].substring(1), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false);
commandNodes.forEach(current::addChild);
rootCommandNode.addChild(current);
} else {
String arg = args[i];
if (arg.startsWith("<") && arg.endsWith(">")) {
CommandNode current = new ArgumentCommandNode<Player, String>(arg.substring(1, arg.length() - 1), StringArgumentType.word(), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false, (commandContext, suggestionsBuilder) -> suggestionsBuilder.buildFuture());
commandNodes.forEach(current::addChild);
commandNodes.clear();
commandNodes.add(current);
} else if (arg.startsWith("(<") && arg.endsWith(">)")) {
// CommandNode current = new ArgumentCommandNode<Player, String>(arg, StringArgumentType.word(), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false, (commandContext, suggestionsBuilder) -> suggestionsBuilder.buildFuture());
// commandNodes.forEach(current::addChild);
// commandNodes.add(current);
CommandNode current = new LiteralCommandNode<Player>(arg, null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false);
commandNodes.forEach(current::addChild);
commandNodes.add(current);
} else if (arg.startsWith("(") && arg.endsWith(")")) {
CommandNode current = new LiteralCommandNode<Player>(arg.substring(1, arg.length() - 1), null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false);
commandNodes.forEach(current::addChild);
commandNodes.add(current);
} else {
CommandNode current = new LiteralCommandNode<Player>(arg, null, currentPlayer -> currentPlayer.getUniqueId().equals(player.getUniqueId()), null, null, false);
commandNodes.forEach(current::addChild);
commandNodes.clear();
commandNodes.add(current);
}
}
}
}
return packet;
});
}
*/
}

Datei anzeigen

@ -20,6 +20,7 @@
package de.steamwar.bausystem.features.script;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.custom.event.EventType;
import de.steamwar.bausystem.features.script.expression.Expression;
import de.steamwar.linkage.Linked;
import lombok.SneakyThrows;

Datei anzeigen

@ -0,0 +1,29 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom;
import de.steamwar.inventory.SWItem;
import org.bukkit.entity.Player;
import yapion.hierarchy.types.YAPIONMap;
public interface MenuScript {
void save(YAPIONMap yapionMap);
SWItem toItem(Player p);
}

Datei anzeigen

@ -0,0 +1,23 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom;
public interface Script {
}

Datei anzeigen

@ -0,0 +1,45 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.command;
import de.steamwar.bausystem.features.script.CustomScriptManager;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
@Linked
public class CommandListener implements Listener {
@LinkedInstance
public CustomScriptManager manager;
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
if (e.getMessage().startsWith("/script:")) {
e.setMessage("/" + e.getMessage().substring(8));
return;
}
manager.callCommand(e.getPlayer(), e, e.getMessage());
}
}

Datei anzeigen

@ -0,0 +1,87 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.command;
import de.steamwar.bausystem.features.script.custom.Script;
import de.steamwar.bausystem.features.script.variables.Value;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import java.util.HashMap;
import java.util.Map;
public interface CustomCommand extends Script {
String[] command();
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);
}

Datei anzeigen

@ -0,0 +1,51 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.command;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.variables.Value;
import lombok.RequiredArgsConstructor;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.inventory.meta.BookMeta;
import java.util.Map;
@RequiredArgsConstructor
public
class InventoryCommand implements CustomCommand {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String[] command() {
return 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, null);
return true;
}
}

Datei anzeigen

@ -0,0 +1,99 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.command;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.custom.MenuScript;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.inventory.SWItem;
import lombok.RequiredArgsConstructor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.inventory.meta.BookMeta;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
public
class MenuCommand implements CustomCommand, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String[] command() {
return 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, null);
return true;
}
public boolean equals(CustomScript.MenuCommand menuCommand) {
if (menuCommand.args.length != args.length) {
return false;
}
if (!args[0].equals(menuCommand.args[0])) {
return false;
}
for (int i = 0; i < args.length; i++) {
if (i == 0) continue;
String s1 = args[i];
String s2 = menuCommand.args[i];
if (!s1.equals(s2)) {
return false;
}
if (!(s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">"))) {
return false;
}
if (!(s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)"))) {
return false;
}
}
return true;
}
@Override
public void save(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_COMMAND_ITEM_NAME", p, String.join(" ", args)));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}

Datei anzeigen

@ -0,0 +1,34 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.event;
import de.steamwar.bausystem.features.script.custom.Script;
import de.steamwar.bausystem.features.script.variables.Value;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.function.Consumer;
public interface CustomEvent extends Script {
String eventName();
boolean execute(Player p, Map<String, Value> variables, Consumer<String> echoConsumer);
}

Datei anzeigen

@ -17,9 +17,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script;
package de.steamwar.bausystem.features.script.custom.event;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.CustomScriptManager;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
@ -37,7 +38,6 @@ import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.*;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.HashMap;
import java.util.HashSet;
@ -45,14 +45,10 @@ import java.util.Map;
import java.util.Set;
@Linked
public class ScriptEventListener implements Listener, PluginMessageListener {
{
Bukkit.getServer().getMessenger().registerIncomingPluginChannel(BauSystem.getInstance(), "sw:hotkeys", this);
}
public class EventListener implements Listener {
@LinkedInstance
public CustomScriptManager customScriptManager;
public CustomScriptManager manager;
private static final Map<Player, Long> LAST_FS = new HashMap<>();
@ -64,18 +60,18 @@ public class ScriptEventListener implements Listener, PluginMessageListener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
customScriptManager.callEvent(EventType.SelfJoin, event.getPlayer(), event);
manager.callEvent(EventType.SelfJoin, event.getPlayer(), event);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
customScriptManager.callEvent(EventType.SelfLeave, event.getPlayer(), event);
manager.callEvent(EventType.SelfLeave, event.getPlayer(), event);
}
@EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
if (LAST_FS.containsKey(event.getPlayer())) {
customScriptManager.callEvent(EventType.FF, event.getPlayer(), event);
manager.callEvent(EventType.FF, event.getPlayer(), event);
} else {
LAST_FS.put(event.getPlayer(), System.currentTimeMillis());
}
@ -83,12 +79,12 @@ public class ScriptEventListener implements Listener, PluginMessageListener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
customScriptManager.callEvent(EventType.PlaceBlock, event.getPlayer(), event);
manager.callEvent(EventType.PlaceBlock, event.getPlayer(), event);
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
customScriptManager.callEvent(EventType.BreakBlock, event.getPlayer(), event);
manager.callEvent(EventType.BreakBlock, event.getPlayer(), event);
}
private Set<Player> ignore = new HashSet<>();
@ -99,10 +95,10 @@ public class ScriptEventListener implements Listener, PluginMessageListener {
return;
}
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
customScriptManager.callEvent(EventType.RightClick, event.getPlayer(), event);
manager.callEvent(EventType.RightClick, event.getPlayer(), event);
}
if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
customScriptManager.callEvent(EventType.LeftClick, event.getPlayer(), event);
manager.callEvent(EventType.LeftClick, event.getPlayer(), event);
}
}
@ -115,7 +111,7 @@ public class ScriptEventListener implements Listener, PluginMessageListener {
for (Player player : Bukkit.getOnlinePlayers()) {
if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) {
customScriptManager.callEvent(EventType.TNTSpawn, player, event);
manager.callEvent(EventType.TNTSpawn, player, event);
}
}
}
@ -129,7 +125,7 @@ public class ScriptEventListener implements Listener, PluginMessageListener {
for (Player player : Bukkit.getOnlinePlayers()) {
if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) {
customScriptManager.callEvent(EventType.TNTExplode, player, event);
manager.callEvent(EventType.TNTExplode, player, event);
}
}
}
@ -137,31 +133,13 @@ public class ScriptEventListener implements Listener, PluginMessageListener {
@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event) {
ignore.add(event.getPlayer());
customScriptManager.callEvent(EventType.DropItem, event.getPlayer(), event);
manager.callEvent(EventType.DropItem, event.getPlayer(), event);
}
@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
for (Player player : Bukkit.getOnlinePlayers()) {
customScriptManager.callEvent(EventType.EntityDeath, player, event);
}
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("sw:hotkeys")) return;
if (message.length < 5) return;
int action = message[4] & 0xFF;
if (action == 2) return;
int key = (message[0] & 0xFF) << 24 | (message[1] & 0xFF) << 16 | (message[2] & 0xFF) << 8 | (message[3] & 0xFF);
if (!(key >= 'A' && key <= 'Z' || key >= '0' && key <= '9')) return;
if (message.length >= 9) {
int mods = (message[5] & 0xFF) << 24 | (message[6] & 0xFF) << 16 | (message[7] & 0xFF) << 8 | (message[8] & 0xFF);
// player.sendMessage("Hotkey: " + (char) key + " " + action + " " + Long.toBinaryString(mods));
customScriptManager.callHotkey(mods, ((char) key), player, action == 1);
} else {
// player.sendMessage("Hotkey: " + (char) key + " " + action);
customScriptManager.callHotkey(0, ((char) key), player, action == 1);
manager.callEvent(EventType.EntityDeath, player, event);
}
}
}

Datei anzeigen

@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script;
package de.steamwar.bausystem.features.script.custom.event;
import de.steamwar.bausystem.features.script.variables.Value;
import lombok.Getter;

Datei anzeigen

@ -0,0 +1,47 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.event;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.variables.Value;
import lombok.RequiredArgsConstructor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.BookMeta;
import java.util.Map;
import java.util.function.Consumer;
@RequiredArgsConstructor
public
class InventoryEvent implements CustomEvent {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String eventName() {
return args[0];
}
@Override
public boolean execute(Player p, Map<String, Value> variables, Consumer<String> echoConsumer) {
new ScriptExecutor(bookMeta, p, variables, echoConsumer);
return true;
}
}

Datei anzeigen

@ -0,0 +1,76 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.event;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.custom.MenuScript;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.inventory.SWItem;
import lombok.RequiredArgsConstructor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.BookMeta;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@RequiredArgsConstructor
public
class MenuEvent implements CustomEvent, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String eventName() {
return args[0];
}
@Override
public boolean execute(Player p, Map<String, Value> variables, Consumer<String> echoConsumer) {
new ScriptExecutor(pages, p, variables, echoConsumer);
return false;
}
@Override
public void save(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
StringBuilder st = new StringBuilder();
for (int i = 1; i < args.length; i++) {
if (i != 1) st.append(" ");
st.append(args[i]);
}
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_EVENT_ITEM_NAME", p, args[0], st.toString()));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}

Datei anzeigen

@ -0,0 +1,29 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.hotkey;
import de.steamwar.bausystem.features.script.custom.Script;
import org.bukkit.entity.Player;
public interface Hotkey extends Script {
String hotkey();
boolean execute(Player p, boolean pressed);
}

Datei anzeigen

@ -0,0 +1,57 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.hotkey;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.CustomScriptManager;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
@Linked
public class HotkeyListener implements PluginMessageListener {
{
Bukkit.getServer().getMessenger().registerIncomingPluginChannel(BauSystem.getInstance(), "sw:hotkeys", this);
}
@LinkedInstance
public CustomScriptManager manager;
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("sw:hotkeys")) return;
if (message.length < 5) return;
int action = message[4] & 0xFF;
if (action == 2) return;
int key = (message[0] & 0xFF) << 24 | (message[1] & 0xFF) << 16 | (message[2] & 0xFF) << 8 | (message[3] & 0xFF);
if (!(key >= 'A' && key <= 'Z' || key >= '0' && key <= '9')) return;
if (message.length >= 9) {
int mods = (message[5] & 0xFF) << 24 | (message[6] & 0xFF) << 16 | (message[7] & 0xFF) << 8 | (message[8] & 0xFF);
// player.sendMessage("Hotkey: " + (char) key + " " + action + " " + Long.toBinaryString(mods));
manager.callHotkey(mods, ((char) key), player, action == 1);
} else {
// player.sendMessage("Hotkey: " + (char) key + " " + action);
manager.callHotkey(0, ((char) key), player, action == 1);
}
}
}

Datei anzeigen

@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script;
package de.steamwar.bausystem.features.script.custom.hotkey;
import java.util.HashSet;
import java.util.Set;

Datei anzeigen

@ -0,0 +1,51 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.hotkey;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.variables.Value;
import lombok.RequiredArgsConstructor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.BookMeta;
import java.util.HashMap;
import java.util.Map;
@RequiredArgsConstructor
public
class InventoryHotkey implements Hotkey {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String hotkey() {
return args[0];
}
@Override
public boolean execute(Player p, boolean pressed) {
Map<String, Value> variables = new HashMap<>();
variables.put("pressed", new Value.BooleanValue(pressed));
variables.put("released", new Value.BooleanValue(!pressed));
new ScriptExecutor(bookMeta, p, variables, null);
return false;
}
}

Datei anzeigen

@ -0,0 +1,80 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.script.custom.hotkey;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.ScriptExecutor;
import de.steamwar.bausystem.features.script.custom.MenuScript;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.inventory.SWItem;
import lombok.RequiredArgsConstructor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.BookMeta;
import yapion.hierarchy.types.YAPIONArray;
import yapion.hierarchy.types.YAPIONMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
public
class MenuHotkey implements Hotkey, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String hotkey() {
return args[0];
}
@Override
public boolean execute(Player p, boolean pressed) {
Map<String, Value> variables = new HashMap<>();
variables.put("pressed", new Value.BooleanValue(pressed));
variables.put("released", new Value.BooleanValue(!pressed));
new ScriptExecutor(pages, p, variables, null);
return false;
}
@Override
public void save(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
StringBuilder st = new StringBuilder();
for (int i = 1; i < args.length; i++) {
if (i != 1) st.append(" ");
st.append(args[i]);
}
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_HOTKEY_ITEM_NAME", p, args[0], st.toString()));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}