diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 97818046..deb73c3e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -24,8 +24,9 @@ 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.event.player.PlayerEvent; import org.bukkit.inventory.meta.BookMeta; import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONMap; @@ -47,7 +48,7 @@ public class CustomScript { public interface CustomEvent extends Script { String eventName(); - boolean execute(PlayerEvent e, Map variables); + boolean execute(Event e, Player p, Map variables); } @RequiredArgsConstructor @@ -61,8 +62,8 @@ public class CustomScript { } @Override - public boolean execute(PlayerEvent e, Map variables) { - new ScriptExecutor(bookMeta, e.getPlayer(), variables); + public boolean execute(Event e, Player p, Map variables) { + new ScriptExecutor(bookMeta, p, variables); return true; } } @@ -78,8 +79,8 @@ public class CustomScript { } @Override - public boolean execute(PlayerEvent e, Map variables) { - new ScriptExecutor(pages, e.getPlayer(), variables); + public boolean execute(Event e, Player p, Map variables) { + new ScriptExecutor(pages, p, variables); return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 30030db3..03f56d65 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -33,8 +33,13 @@ 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; @@ -95,13 +100,13 @@ public class CustomScriptListener implements Listener { YAPIONObject yapionObject; if (s == null) { yapionObject = new YAPIONObject(); - yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); + 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\ngui")); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui")); } } @@ -251,28 +256,66 @@ public class CustomScriptListener implements Listener { openCommandsMenu(p); } - @AllArgsConstructor @Getter public enum EventType { - FF(PlayerSwapHandItemsEvent.class, event -> null); + 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; + private Class eventType; + private Function> eventValues; + + EventType(Class eventType, Function> eventValues) { + this.eventType = eventType; + this.eventValues = event -> { + return eventValues.apply((T) event); + }; + } } - private void callEvent(EventType eventType, PlayerEvent e) { + private void callEvent(EventType eventType, Player p, Event e) { if (!eventType.getEventType().equals(e.getClass())) { return; } - List customEvents = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); + 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<>(); } - customEvent.execute(e, variables); + customEvent.execute(e, p, variables); + if (e instanceof Cancellable && variables.containsKey("cancel")) { + Value value = variables.get("cancel"); + if (value.asBoolean()) { + ((Cancellable) e).setCancelled(true); + } + } return; } } @@ -307,9 +350,29 @@ public class CustomScriptListener implements Listener { @EventHandler public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { if (LAST_FS.contains(event.getPlayer())) { - callEvent(EventType.FF, event); + 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.RightClick, 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 46474e93..fbf8053b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§eFF"), 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,21 @@ 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.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)); + for (int i = 0; i < 4 + 2 * 9; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } 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..8a78d10e 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,206 @@ 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("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()); }); } @@ -70,5 +222,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(); } }