diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java index 5602f90d..e8c48493 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java @@ -19,38 +19,16 @@ package de.steamwar.bausystem.features.gui; -import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; - -import java.util.HashSet; -import java.util.Set; @Linked(LinkageType.LISTENER) public class BauGuiListener implements Listener { - private static final Set LAST_FS = new HashSet<>(); - - public BauGuiListener() { - Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20); - } - - @EventHandler - public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { - if (LAST_FS.contains(event.getPlayer())) { - BauGUI.openBauGui(event.getPlayer()); - } else { - LAST_FS.add(event.getPlayer()); - } - } - @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (event.getItem() == null) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java deleted file mode 100644 index 4945a789..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java +++ /dev/null @@ -1,342 +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 . - */ - -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; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.BookMeta; -import yapion.hierarchy.output.LengthOutput; -import yapion.hierarchy.output.StringOutput; -import yapion.hierarchy.types.YAPIONArray; -import yapion.hierarchy.types.YAPIONMap; -import yapion.hierarchy.types.YAPIONObject; -import yapion.hierarchy.types.YAPIONValue; -import yapion.parser.YAPIONParser; - -import java.util.*; -import java.util.stream.Collectors; - -@Linked(LinkageType.LISTENER) -public class CustomCommandListener implements Listener { - - private interface CustomCommand { - default Map check(String[] args, String[] command) { - if (args.length < command.length) { - return null; - } - - if (!args[0].equals(command[0])) { - return null; - } - - Map arguments = new HashMap<>(); - if (!check(arguments, args, command, 0, 0)) { - return null; - } - return arguments; - } - - default boolean check(Map 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 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 pages; - private final String[] args; - - public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { - Map 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> playerMap = new HashMap<>(); - - private void updateInventory(Player p) { - playerMap.computeIfPresent(p, (player, customCommands) -> { - customCommands.removeIf(InventoryCommand.class::isInstance); - return customCommands; - }); - for (ItemStack item : p.getInventory().getContents()) { - if (item == null || isNoBook(item) || item.getItemMeta() == null) { - continue; - } - - BookMeta bookMeta = ((BookMeta) item.getItemMeta()); - if (bookMeta.getPageCount() == 0) { - continue; - } - if (bookMeta.getPage(1).isEmpty()) { - continue; - } - String s = bookMeta.getPage(1).split("\n")[0]; - if (!s.startsWith("#!CMD /")) { - continue; - } - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryCommand(bookMeta, s.substring(6).split(" "))); - } - } - - @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - Player p = event.getPlayer(); - updateInventory(p); - - String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-commands"); - YAPIONObject yapionObject; - if (s == null) { - yapionObject = new YAPIONObject(); - } else { - yapionObject = YAPIONParser.parse(s); - } - - yapionObject.getYAPIONMapOrSetDefault("", new YAPIONMap()).forEach((key, value) -> { - String[] command = ((YAPIONValue) key).get().split(" "); - List 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)); - }); - } - - @EventHandler - public void onPlayerQuit(PlayerQuitEvent e) { - save(e.getPlayer()); - playerMap.remove(e.getPlayer()); - } - - private YAPIONObject output(Player p) { - if (!playerMap.containsKey(p)) return new YAPIONObject(); - YAPIONObject yapionObject = new YAPIONObject(); - YAPIONMap yapionMap = new YAPIONMap(); - yapionObject.add("", yapionMap); - playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { - menuCommand.toYAPION(yapionMap); - }); - return yapionObject; - } - - private boolean save(Player p) { - if (!playerMap.containsKey(p)) { - UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-commands"); - return true; - } - YAPIONObject yapionObject = output(p); - if (yapionObject.toYAPION(new LengthOutput()).getLength() > 64 * 1024) { - return false; - } - UserConfig.updatePlayerConfig(p.getUniqueId(), "bausystem-commands", yapionObject.toYAPION(new StringOutput()).getResult()); - return true; - } - - @EventHandler - public void onInventoryClose(InventoryCloseEvent e) { - if (e.getPlayer() instanceof Player) { - updateInventory((Player) e.getPlayer()); - } - } - - @EventHandler - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { - if (e.getMessage().startsWith("/script:")) { - e.setMessage("/" + e.getMessage().substring(8)); - return; - } - - List inventoryCommands = playerMap.get(e.getPlayer()); - if (inventoryCommands == null) { - return; - } - - String[] command = e.getMessage().split(" "); - for (CustomCommand customCommand : inventoryCommands) { - if (customCommand.execute(command, e)) { - return; - } - } - } - - private boolean isNoBook(ItemStack item) { - return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15)); - } - - public void openCommandsMenu(Player p) { - List> 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); - - menuCommands.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, command, Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"), false, clickType -> { - }), menuCommand)); - }); - int length = (int) output(p).toYAPION(new LengthOutput()).getLength(); - StringBuilder menuName = new StringBuilder(); - menuName.append("§eScript Commands "); - double percentage = ((int) ((length / 655336.0) * 1000)) / 10.0; - if (percentage > 99) { - menuName.append("§c"); - } else if (percentage >= 75) { - menuName.append("§6"); - } else { - menuName.append("§a"); - } - menuName.append(percentage).append("§7%"); - SWListInv 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()); - p.closeInventory(); - save(p); - }); - menuCommandSWListInv.setItem(49, new SWItem(Material.HOPPER, "§eHinzufügen", Arrays.asList("§7Klicke mit einem Buch zum hinzufügen"), false, clickType -> { - ItemStack item = p.getItemOnCursor(); - if (item.getType().isAir()) { - return; - } - if (isNoBook(item) || item.getItemMeta() == null) { - return; - } - - BookMeta bookMeta = ((BookMeta) item.getItemMeta()); - if (bookMeta.getPageCount() == 0) { - return; - } - if (bookMeta.getPage(1).isEmpty()) { - 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 (Arrays.equals(((MenuCommand) customCommand).args, menuCommand.args)) { - p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); - return; - } - } - 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 CMD-Buch Limit erreicht"); - return; - } - p.setItemOnCursor(null); - openCommandsMenu(p); - })); - menuCommandSWListInv.open(); - } -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java new file mode 100644 index 00000000..deb73c3e --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -0,0 +1,240 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script; + +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; + +@UtilityClass +public class CustomScript { + + public interface Script { + } + + public interface MenuScript { + void toYAPION(YAPIONMap yapionMap); + SWItem toItem(); + } + + public interface CustomEvent extends Script { + String eventName(); + boolean execute(Event e, Player p, Map variables); + } + + @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(Event e, Player p, Map variables) { + new ScriptExecutor(bookMeta, p, variables); + return true; + } + } + + @RequiredArgsConstructor + public static class MenuEvent implements CustomEvent, MenuScript { + public final List pages; + public final String[] args; + + @Override + public String eventName() { + return args[0]; + } + + @Override + public boolean execute(Event e, Player p, Map variables) { + new ScriptExecutor(pages, p, variables); + 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() { + StringBuilder st = new StringBuilder(); + for (int i = 0; i < args.length; i++) { + if (i != 0) st.append(" "); + if (i == 0) st.append("§e"); + st.append(args[i]); + if (i == 0) st.append("§8 -§7"); + } + + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: " + st); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem; + } + } + + public interface CustomCommand extends Script { + default Map check(String[] args, String[] command) { + if (args.length < command.length) { + return null; + } + + if (!args[0].equals(command[0])) { + return null; + } + + Map arguments = new HashMap<>(); + if (!check(arguments, args, command, 0, 0)) { + return null; + } + return arguments; + } + + default boolean check(Map 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; + + public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { + Map arguments = check(args, command); + if (arguments == null) { + return false; + } + + e.setCancelled(true); + new ScriptExecutor(bookMeta, e.getPlayer(), arguments); + return true; + } + } + + @RequiredArgsConstructor + public static class MenuCommand implements CustomCommand, MenuScript { + public final List pages; + public final String[] args; + + public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { + Map arguments = check(args, command); + if (arguments == null) { + return false; + } + + e.setCancelled(true); + new ScriptExecutor(pages, e.getPlayer(), arguments); + return true; + } + + public boolean equals(MenuCommand menuCommand) { + if (menuCommand.args.length != args.length) { + 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 true; + } + if (s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">")) { + return true; + } + if (s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)")) { + return true; + } + } + 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() { + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Command§8: §e" + String.join(" ", args)); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem; + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java new file mode 100644 index 00000000..c76bc6f9 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -0,0 +1,385 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script; + +import de.steamwar.bausystem.BauSystem; +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.AllArgsConstructor; +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.*; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import yapion.hierarchy.output.LengthOutput; +import yapion.hierarchy.output.StringOutput; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONMap; +import yapion.hierarchy.types.YAPIONObject; +import yapion.hierarchy.types.YAPIONValue; +import yapion.parser.YAPIONParser; + +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Linked(LinkageType.LISTENER) +public class CustomScriptListener implements Listener { + + private Map> playerMap = new HashMap<>(); + + private void updateInventory(Player p) { + playerMap.computeIfPresent(p, (player, customCommands) -> { + customCommands.removeIf(script -> !(script instanceof CustomScript.MenuScript)); + return customCommands; + }); + for (ItemStack item : p.getInventory().getContents()) { + if (item == null || isNoBook(item) || item.getItemMeta() == null) { + continue; + } + + BookMeta bookMeta = ((BookMeta) item.getItemMeta()); + if (bookMeta.getPageCount() == 0) { + continue; + } + if (bookMeta.getPage(1).isEmpty()) { + continue; + } + 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(" "))); + } else if (s.startsWith("#!EVENT ")) { + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryEvent(bookMeta, s.substring(8).split(" "))); + } + } + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) { + Player p = e.getPlayer(); + updateInventory(p); + + String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-scripts"); + if (s == null) { + s = UserConfig.getConfig(p.getUniqueId(), "bausystem-commands"); + UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-commands"); + } + YAPIONObject yapionObject; + if (s == null) { + yapionObject = new YAPIONObject(); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui")); + } else { + yapionObject = YAPIONParser.parse(s); + if (yapionObject.containsKey("")) { + yapionObject.add("commands", yapionObject.getMap("")); + yapionObject.remove(""); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui")); + } + } + + yapionObject.getYAPIONMapOrSetDefault("commands", new YAPIONMap()).forEach((key, value) -> { + String[] command = ((YAPIONValue) key).get().split(" "); + List 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)); + }); + + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> { + String[] event = ((YAPIONValue) key).get().split(" "); + List 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)); + }); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + save(e.getPlayer()); + playerMap.remove(e.getPlayer()); + } + + private YAPIONObject output(Player p) { + if (!playerMap.containsKey(p)) return new YAPIONObject(); + YAPIONObject yapionObject = new YAPIONObject(); + 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(eventsMap); + }); + return yapionObject; + } + + private boolean save(Player p) { + if (!playerMap.containsKey(p)) { + UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-scripts"); + return true; + } + YAPIONObject yapionObject = output(p); + if (yapionObject.toYAPION(new LengthOutput()).getLength() > 64 * 1024) { + return false; + } + UserConfig.updatePlayerConfig(p.getUniqueId(), "bausystem-scripts", yapionObject.toYAPION(new StringOutput()).getResult()); + return true; + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent e) { + if (e.getPlayer() instanceof Player) { + updateInventory((Player) e.getPlayer()); + } + } + + private boolean isNoBook(ItemStack item) { + return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15)); + } + + public void openCommandsMenu(Player p) { + List> 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(); + ItemStack itemStack = swItem.getItemStack(); + if (menuItem instanceof CustomScript.MenuEvent) { + itemStack.setType(Material.REPEATING_COMMAND_BLOCK); + } else { + itemStack.setType(Material.COMMAND_BLOCK); + } + swItem.setItemStack(itemStack); + swItem.setLore(Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren")); + + menuCommands.add(new SWListInv.SWListEntry<>(swItem, menuItem)); + }); + + int length = (int) output(p).toYAPION(new LengthOutput()).getLength(); + StringBuilder menuName = new StringBuilder(); + menuName.append("§eScript Commands "); + double percentage = ((int) ((length / 655336.0) * 1000)) / 10.0; + if (percentage > 99) { + menuName.append("§c"); + } else if (percentage >= 75) { + menuName.append("§6"); + } else { + menuName.append("§a"); + } + menuName.append(percentage).append("§7%"); + + SWListInv 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().getItemStack()); + p.closeInventory(); + save(p); + }); + menuCommandSWListInv.setItem(49, new SWItem(Material.HOPPER, "§eHinzufügen", Arrays.asList("§7Klicke mit einem Buch zum hinzufügen"), false, clickType -> { + ItemStack item = p.getItemOnCursor(); + if (item.getType().isAir()) { + return; + } + if (isNoBook(item) || item.getItemMeta() == null) { + return; + } + + BookMeta bookMeta = ((BookMeta) item.getItemMeta()); + if (bookMeta.getPageCount() == 0) { + return; + } + if (bookMeta.getPage(1).isEmpty()) { + return; + } + 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)) { + continue; + } + if (((CustomScript.MenuCommand) script).equals(menuCommand)) { + p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); + return; + } + } + scriptBookLimit(p, menuCommand); + } else if (s.startsWith("#!EVENT ")) { + CustomScript.MenuEvent menuEvent = new CustomScript.MenuEvent(bookMeta.getPages(), s.substring(8).split(" ")); + try { + EventType.valueOf(menuEvent.eventName()); + } catch (Exception e) { + p.sendMessage("§cEvent '" + menuEvent.eventName() + "' ist nicht definierbar"); + return; + } + scriptBookLimit(p, menuEvent); + } + })); + menuCommandSWListInv.open(); + } + + private void scriptBookLimit(Player p, CustomScript.Script menuScript) { + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuScript); + if (!save(p)) { + playerMap.get(p).removeIf(script -> script == menuScript); + p.closeInventory(); + SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); + save(p); + p.sendMessage("§cScript-Buch Limit erreicht"); + return; + } + p.setItemOnCursor(null); + openCommandsMenu(p); + } + + @Getter + public enum EventType { + FF(PlayerSwapHandItemsEvent.class, event -> null), + PlaceBlock(BlockPlaceEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockX", new Value.LongValue(event.getBlockPlaced().getX())); + valueMap.put("blockY", new Value.LongValue(event.getBlockPlaced().getY())); + valueMap.put("blockZ", new Value.LongValue(event.getBlockPlaced().getZ())); + valueMap.put("blockType", new Value.StringValue(event.getBlockPlaced().getType().name())); + return valueMap; + }), + BreakBlock(BlockBreakEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockX", new Value.LongValue(event.getBlock().getX())); + valueMap.put("blockY", new Value.LongValue(event.getBlock().getY())); + valueMap.put("blockZ", new Value.LongValue(event.getBlock().getZ())); + valueMap.put("blockType", new Value.StringValue(event.getBlock().getType().name())); + return valueMap; + }), + RightClick(PlayerInteractEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand())); + return valueMap; + }), + LeftClick(PlayerInteractEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand())); + return valueMap; + }); + + private Class eventType; + private Function> eventValues; + + EventType(Class eventType, Function> eventValues) { + this.eventType = eventType; + this.eventValues = event -> eventValues.apply((T) event); + } + } + + private void callEvent(EventType eventType, Player p, Event e) { + if (!eventType.getEventType().equals(e.getClass())) { + return; + } + + List 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) { + if (customEvent.eventName().equals(eventType.name())) { + Map variables = eventType.getEventValues().apply(e); + if (variables == null) { + variables = new HashMap<>(); + } + if (e instanceof Cancellable) { + variables.put("cancel", new Value.BooleanValue(false)); + } + customEvent.execute(e, p, variables); + if (variables.containsKey("cancel")) { + Value value = variables.get("cancel"); + if (value.asBoolean()) { + ((Cancellable) e).setCancelled(true); + } + } + } + } + } + + // EventListener for Commands as well as Events + + // Event Command + @EventHandler + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { + if (e.getMessage().startsWith("/script:")) { + e.setMessage("/" + e.getMessage().substring(8)); + return; + } + + List 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; + } + } + } + + // Event FF + private static final Set LAST_FS = new HashSet<>(); + + { + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20); + } + + @EventHandler + public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { + if (LAST_FS.contains(event.getPlayer())) { + callEvent(EventType.FF, event.getPlayer(), event); + } else { + LAST_FS.add(event.getPlayer()); + } + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent event) { + callEvent(EventType.PlaceBlock, event.getPlayer(), event); + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + callEvent(EventType.BreakBlock, event.getPlayer(), event); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { + callEvent(EventType.RightClick, event.getPlayer(), event); + } + if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { + callEvent(EventType.LeftClick, event.getPlayer(), event); + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index f2828e72..554f5437 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -24,7 +24,7 @@ public class ScriptCommand extends SWCommand { } @LinkedInstance - private CustomCommandListener customCommandListener = null; + private CustomScriptListener customScriptListener = null; private static List> swItems = new ArrayList<>(); @@ -41,9 +41,9 @@ public class ScriptCommand extends SWCommand { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } - 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 -> { + 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. Jedes Event kann durch", "§7'var cancel true' gecancelt werden.", "§7Hinter dem Event Namen stehen die Variablen,", "§7welche im Script durch das Event nutztbar sind.", "§7Nutzbare Events sind:", "§eFF", "§ePlaceBlock §8-§7 blockX, blockY, blockZ, blockType", "§eBreakBlock §8-§7 blockX, blockY, blockZ, blockType", "§eRightClick §8-§7 blockInHand", "§eLeftClick §8-§7 blockInHand"), 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)); @@ -80,7 +80,7 @@ public class ScriptCommand extends SWCommand { }); swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand)); }); - for (int i = 0; i < 9 + 4; i++) { + for (int i = 0; i < 9 + 3; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } @@ -104,17 +104,25 @@ public class ScriptCommand extends SWCommand { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.OBSIDIAN, "§7Constant §eprotect", Arrays.asList("§etrue§7 wenn Protect angeschaltet ist."), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers."), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers."), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers."), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.NAME_TAG, "§7Constant §ename", Arrays.asList("§eDisplay§7 Name des Spielers."), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> { }), null)); - for (int i = 0; i < 6 + 2 * 9; i++) { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.DIAMOND_BOOTS, "§7Constant §esprinting", Arrays.asList("§etrue§7 wenn der Spieler gerade rennt."), false, clickType -> { + }), null)); + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§7Constant §eslot", Arrays.asList("§e0-8§7 für den ausgewählten slot.", "§eÜberschreibbar"), false, clickType -> { + }), null)); + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRASS_BLOCK, "§7Constant §eslotmaterial", Arrays.asList("§eMaterial§7 des Items im Slot"), false, clickType -> { + }), null)); + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BLOCK, "§7Constant §eoffhandmaterial", Arrays.asList("§eMaterial§7 des Items in oder Off Hand"), false, clickType -> { + }), null)); + for (int i = 0; i < 2 + 2 * 9; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } @@ -134,6 +142,6 @@ public class ScriptCommand extends SWCommand { @Register({"menu"}) public void menuGUICommand(Player p) { - customCommandListener.openCommandsMenu(p); + customScriptListener.openCommandsMenu(p); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java index 46529054..d2da1bb6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -42,6 +42,10 @@ public final class ScriptExecutor { this(bookMeta, player, new HashMap<>()); } + public ScriptExecutor(List pages, Player player) { + this(pages, player, new HashMap<>()); + } + public ScriptExecutor(BookMeta bookMeta, Player player, Map localVariables) { this.player = player; globalVariables = ScriptListener.getGlobalContext(player); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java index 47b4e2d3..4bb50b4e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java @@ -44,11 +44,11 @@ public class Add implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java index 17e26444..ff4499f7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java @@ -44,11 +44,11 @@ public class Div implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java index 8844a282..a1238ef6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java @@ -44,11 +44,11 @@ public class Mul implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java index 036de18b..e7ff65b2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java @@ -44,11 +44,11 @@ public class Sub implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java index e7526748..e19f2c59 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java @@ -44,11 +44,11 @@ public class And implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.BooleanValue.class) { + if (!(v1 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } - if (v2.getClass() != Value.BooleanValue.class) { + if (!(v2 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java index a02aaf71..6fe644a7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java @@ -45,7 +45,7 @@ public class Equal implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value result; - if (v1.getClass() != v2.getClass()) { + if (!v1.getClass().isInstance(v2.getClass()) && !v2.getClass().isInstance(v1.getClass())) { result = new Value.BooleanValue(false); } else if (v1.asString().equals(v2.asString())) { result = new Value.BooleanValue(true); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java index 196b3ad4..8539de02 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java @@ -44,11 +44,11 @@ public class Greater implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java index 32cfd20d..a9e086d4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java @@ -44,11 +44,11 @@ public class Less implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java index 72c9a59f..9be6f034 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java @@ -39,7 +39,7 @@ public class Not implements SpecialCommand { } Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.BooleanValue.class) { + if (!(v1 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können genichtet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java index ce67eede..7d6c9bae 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java @@ -44,11 +44,11 @@ public class Or implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.BooleanValue.class) { + if (!(v1 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } - if (v2.getClass() != Value.BooleanValue.class) { + if (!(v2 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java index e3629d0e..0036af54 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java @@ -70,15 +70,15 @@ public class Insert implements SpecialCommand { Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v3.getClass() != Value.LongValue.class) { + if (!(v3 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java index 749c1511..77d463eb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java @@ -64,7 +64,7 @@ public class Length implements SpecialCommand { String resultName = command[1]; Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java index f6bad4d7..ff692ee0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java @@ -65,11 +65,11 @@ public class Remove implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java index 3aacb7de..0e8eebd1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java @@ -70,15 +70,15 @@ public class Replace implements SpecialCommand { Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v3.getClass() != Value.StringValue.class) { + if (!(v3 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java index edba61f8..88623656 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java @@ -65,11 +65,11 @@ public class Substring implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java new file mode 100644 index 00000000..79915ddc --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java @@ -0,0 +1,73 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script.command.variable; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.SpecialCommand; +import de.steamwar.bausystem.features.script.variables.Constants; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import org.bukkit.Material; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Const implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "§econst §8<§7Variable§8> §8[§7Value§8(§7s§8)§8]", + "", + "Schreibt in eine Konstante einen Wert rein, welcher eine Zahl sein kann, ein Boolscher Wert oder ein Text." + }; + } + + @Override + public Material material() { + return Material.STRUCTURE_BLOCK; + } + + @Override + public String command() { + return "const"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlt und sollte eine Variable sein"); + return true; + } + if (command.length <= 2) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein"); + return true; + } + String varName = command[1]; + StringBuilder varValue = new StringBuilder(); + for (int i = 2; i < command.length; i++) { + if (varValue.length() != 0) { + varValue.append(" "); + } + varValue.append(command[i]); + } + Constants.getConstant(varName, scriptExecutor.getPlayer()).fromValue(scriptExecutor.parse(varValue.toString())); + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java index 9c983fd6..97821477 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java @@ -3,7 +3,6 @@ package de.steamwar.bausystem.features.script.command.variable; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.ScriptExecutor; import de.steamwar.bausystem.features.script.SpecialCommand; -import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import org.bukkit.Material; @@ -48,17 +47,7 @@ public class Global implements SpecialCommand { } varValue.append(command[i]); } - try { - long value = Long.parseLong(varValue.toString()); - scriptExecutor.getLocalVariables().putValue(varName, new Value.LongValue(value)); - } catch (NumberFormatException e) { - String s = varValue.toString(); - if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) { - scriptExecutor.getGlobalVariables().putValue(varName, new Value.BooleanValue(s.equalsIgnoreCase("true"))); - } else { - scriptExecutor.getGlobalVariables().putValue(varName, new Value.StringValue(s)); - } - } + scriptExecutor.getGlobalVariables().putValue(varName, scriptExecutor.parse(varValue.toString())); return true; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java index e8dc2cb5..82033387 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java @@ -3,6 +3,7 @@ package de.steamwar.bausystem.features.script.command.variable; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.ScriptExecutor; import de.steamwar.bausystem.features.script.SpecialCommand; +import de.steamwar.bausystem.features.script.variables.Constants; import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java index df1a8720..eeb53f07 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java @@ -8,54 +8,212 @@ import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode; import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode; import de.steamwar.bausystem.region.flags.flagvalues.TNTMode; import lombok.experimental.UtilityClass; +import org.bukkit.Location; import org.bukkit.entity.Player; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; @UtilityClass public class Constants { private final Map> CONSTANTS = new HashMap<>(); + private static class ConstantLongValue extends Value.LongValue { + + private Supplier longSupplier; + private Consumer longConsumer = ignored -> {}; + + public ConstantLongValue(Supplier longSupplier) { + super(longSupplier.get()); + this.longSupplier = longSupplier; + } + + public ConstantLongValue(Supplier longSupplier, Consumer longConsumer) { + super(longSupplier.get()); + this.longSupplier = longSupplier; + this.longConsumer = longConsumer; + } + + @Override + public long asLong() { + value = longSupplier.get(); + return super.asLong(); + } + + @Override + public boolean asBoolean() { + value = longSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = longSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + longConsumer.accept(this.value); + } + } + + private static class ConstantBooleanValue extends Value.BooleanValue { + + private Supplier booleanSupplier; + private Consumer booleanConsumer = ignored -> {}; + + public ConstantBooleanValue(Supplier booleanSupplier) { + super(booleanSupplier.get()); + this.booleanSupplier = booleanSupplier; + } + + public ConstantBooleanValue(Supplier booleanSupplier, Consumer booleanConsumer) { + super(booleanSupplier.get()); + this.booleanSupplier = booleanSupplier; + this.booleanConsumer = booleanConsumer; + } + + @Override + public long asLong() { + value = booleanSupplier.get(); + return super.asLong(); + } + + @Override + public boolean asBoolean() { + value = booleanSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = booleanSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + booleanConsumer.accept(this.value); + } + } + + private static class ConstantStringValue extends Value.StringValue { + + private Supplier stringSupplier; + private Consumer stringConsumer = ignored -> {}; + + public ConstantStringValue(Supplier stringSupplier) { + super(stringSupplier.get()); + this.stringSupplier = stringSupplier; + } + + public ConstantStringValue(Supplier stringSupplier, Consumer stringConsumer) { + super(stringSupplier.get()); + this.stringSupplier = stringSupplier; + this.stringConsumer = stringConsumer; + } + + @Override + public long asLong() { + value = stringSupplier.get(); + return super.asLong(); + } + + @Override + public boolean asBoolean() { + value = stringSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = stringSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + stringConsumer.accept(this.value); + } + } + static { CONSTANTS.put("trace", player -> { - return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isTracing()); + return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isTracing); }); CONSTANTS.put("autotrace", player -> { - return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isAutoTrace()); + return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isAutoTrace); }); CONSTANTS.put("tnt", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY); }); CONSTANTS.put("tnt-onlytb", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB); }); CONSTANTS.put("freeze", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE); }); CONSTANTS.put("fire", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW); }); CONSTANTS.put("protect", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE); }); CONSTANTS.put("x", player -> { - return new Value.LongValue(player.getLocation().getBlockX()); + return new ConstantLongValue(() -> (long) player.getLocation().getBlockX(), aLong -> { + Location location = player.getLocation(); + location.setX((double) aLong); + player.teleport(location); + }); }); CONSTANTS.put("y", player -> { - return new Value.LongValue(player.getLocation().getBlockY()); + return new ConstantLongValue(() -> (long) player.getLocation().getBlockY(), aLong -> { + Location location = player.getLocation(); + location.setY((double) aLong); + player.teleport(location); + }); }); CONSTANTS.put("z", player -> { - return new Value.LongValue(player.getLocation().getBlockZ()); + return new ConstantLongValue(() -> (long) player.getLocation().getBlockZ(), aLong -> { + Location location = player.getLocation(); + location.setZ((double) aLong); + player.teleport(location); + }); }); CONSTANTS.put("name", player -> { - return new Value.StringValue(player.getDisplayName()); + return new ConstantStringValue(player::getDisplayName); }); CONSTANTS.put("sneaking", player -> { - return new Value.BooleanValue(player.isSneaking()); + return new ConstantBooleanValue(player::isSneaking); + }); + CONSTANTS.put("sprinting", player -> { + return new ConstantBooleanValue(player::isSprinting); + }); + CONSTANTS.put("slot", player -> { + return new ConstantLongValue(() -> (long) player.getInventory().getHeldItemSlot(), slot -> { + if (slot > 8) { + slot = 8L; + } + if (slot < 0) { + slot = 0L; + } + player.getInventory().setHeldItemSlot((int) (long) slot); + }); + }); + CONSTANTS.put("slotmaterial", player -> { + return new ConstantStringValue(() -> player.getInventory().getItemInMainHand().getType().name()); + }); + CONSTANTS.put("offhandmaterial", player -> { + return new ConstantStringValue(() -> player.getInventory().getItemInOffHand().getType().name()); }); } @@ -70,5 +228,4 @@ public class Constants { public Value getConstant(String variableName, Player player) { return CONSTANTS.get(variableName).apply(player); } - } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java index a5bb62f8..1ad1e22b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java @@ -9,7 +9,11 @@ public class Context { private Map variables = new HashMap<>(); public void putValue(String variableName, Value value) { - variables.put(variableName, value); + if (variables.containsKey(variableName)) { + variables.get(variableName).fromValue(value); + } else { + variables.put(variableName, value); + } } public void removeValue(String variableName) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java index 98c1cb46..b70c776e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java @@ -8,15 +8,13 @@ public interface Value { boolean asBoolean(); String asString(); - void fromLong(long value); - void fromBoolean(boolean value); - void fromString(String value); + void fromValue(Value value); @AllArgsConstructor @ToString class LongValue implements Value { - private long value; + protected long value; @Override public long asLong() { @@ -34,22 +32,8 @@ public interface Value { } @Override - public void fromLong(long value) { - this.value = value; - } - - @Override - public void fromBoolean(boolean value) { - this.value = value ? 1 : 0; - } - - @Override - public void fromString(String value) { - try { - this.value = Long.parseLong(value); - } catch (NumberFormatException e) { - this.value = 0; - } + public void fromValue(Value value) { + this.value = value.asLong(); } } @@ -57,7 +41,7 @@ public interface Value { @ToString class BooleanValue implements Value { - private boolean value; + protected boolean value; @Override public long asLong() { @@ -75,18 +59,8 @@ public interface Value { } @Override - public void fromLong(long value) { - this.value = value != 0; - } - - @Override - public void fromBoolean(boolean value) { - this.value = value; - } - - @Override - public void fromString(String value) { - this.value = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); + public void fromValue(Value value) { + this.value = value.asBoolean(); } } @@ -94,7 +68,7 @@ public interface Value { @ToString class StringValue implements Value { - private String value; + protected String value; @Override public long asLong() { @@ -116,18 +90,8 @@ public interface Value { } @Override - public void fromLong(long value) { - this.value = value + ""; - } - - @Override - public void fromBoolean(boolean value) { - this.value = value + ""; - } - - @Override - public void fromString(String value) { - this.value = value; + public void fromValue(Value value) { + this.value = value.asString(); } }