From 09c0ff5e8286f83b5f64b32dc60bddab17fd0028 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 2 Jan 2022 14:03:06 +0100 Subject: [PATCH] Update script system Add Value.DoubleValue Add Ceil, Floor and Round Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 32 ++++++- .../features/region/TNTListener.java | 17 ++++ .../features/script/CustomScriptListener.java | 62 ++++++++++-- .../features/script/ScriptCommand.java | 4 +- .../features/script/ScriptExecutor.java | 14 ++- .../features/script/ScriptVarsCommand.java | 7 ++ .../script/command/arithmetic/Add.java | 10 +- .../script/command/arithmetic/Div.java | 10 +- .../script/command/arithmetic/Mul.java | 10 +- .../script/command/arithmetic/Sub.java | 10 +- .../script/command/arithmetic/other/Ceil.java | 94 +++++++++++++++++++ .../command/arithmetic/other/Floor.java | 94 +++++++++++++++++++ .../command/arithmetic/other/Round.java | 94 +++++++++++++++++++ .../features/script/variables/Constants.java | 72 +++++++++++++- .../features/script/variables/Value.java | 57 +++++++++++ 15 files changed, 552 insertions(+), 35 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Ceil.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Floor.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Round.java diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 81d8555f..4ccf69d7 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -262,9 +262,27 @@ SCRIPT_COMMAND_ARITHMETIC_SUB_HELP_2 = §esub §8<§7Variable§8> §8<§7Variabl SCRIPT_COMMAND_ARITHMETIC_SUB_HELP_3 = Subtraktion zwischen den letzten beiden Variablen und schreibt es in die erste. SCRIPT_COMMAND_ARITHMETIC_SUB_ERROR = §cNur Zahlen können subtrahiert werden +SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_HELP_1 = §eceil §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> +SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_HELP_2 = §eceil §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> §8<§7Variable§8|§7Wert§8> +SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_HELP_3 = Aufrunden der zweiten Zahl und schreibt es in die erste oder aufrunden der zweiten Zahl auf die Nachkommastellenanzahl aus der dritten Zahl und schreibt es in die erste. +SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_ERROR_1 = §cNur Fließ-Komma-Zahlen können aufgerundet werden +SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_ERROR_2 = §cEs kann nur auf ganze Nachkommastellen gerundet werden + +SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_HELP_1 = §efloor §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> +SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_HELP_2 = §efloor §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> §8<§7Variable§8|§7Wert§8> +SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_HELP_3 = Abrunden der zweiten Zahl und schreibt es in die erste oder abrunden der zweiten Zahl auf die Nachkommastellenanzahl aus der dritten Zahl und schreibt es in die erste. +SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_ERROR_1 = §cNur Fließ-Komma-Zahlen können abgerundet werden +SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_ERROR_2 = §cEs kann nur auf ganze Nachkommastellen gerundet werden + +SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_HELP_1 = §eround §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> +SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_HELP_2 = §eround §8<§7Variable§8> §8<§7Variable§8|§7Wert§8> §8<§7Variable§8|§7Wert§8> +SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_HELP_3 = Runden der zweiten Zahl und schreibt es in die erste oder runden der zweiten Zahl auf die Nachkommastellenanzahl aus der dritten Zahl und schreibt es in die erste. +SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_ERROR_1 = §cNur Fließ-Komma-Zahlen können gerundet werden +SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_ERROR_2 = §cEs kann nur auf ganze Nachkommastellen gerundet werden + SCRIPT_COMMAND_IO_ECHO_HELP_1 = §eecho §8<§7Wert§8> SCRIPT_COMMAND_IO_ECHO_HELP_2 = §7Schreibe etwas dem Spieler. Wenn kein Wert angegeben wurde ist die Nachricht leer. -SCRIPT_COMMAND_IO_ECHO_MESSAGE = {0} +SCRIPT_COMMAND_IO_ECHO_MESSAGE = §f{0} SCRIPT_COMMAND_IO_ECHOACTIONBAR_HELP_1 = §eechoactionbar §8<§7Wert§8> SCRIPT_COMMAND_IO_ECHOACTIONBAR_HELP_2 = §7Schreibe etwas dem Spieler in der ActionBar. Wenn kein Wert angegeben wurde ist die Nachricht leer. @@ -413,9 +431,15 @@ SCRIPT_GUI_CUSTOM_EVENTS_LORE_7 = §7Nutzbare Events sind: SCRIPT_GUI_CUSTOM_EVENTS_LORE_8 = §eFF SCRIPT_GUI_CUSTOM_EVENTS_LORE_9 = §ePlaceBlock §8-§7 blockX, blockY, blockZ, blockType SCRIPT_GUI_CUSTOM_EVENTS_LORE_10 = §eBreakBlock §8-§7 blockX, blockY, blockZ, blockType -SCRIPT_GUI_CUSTOM_EVENTS_LORE_11 = §eRightClick §8-§7 blockInHand -SCRIPT_GUI_CUSTOM_EVENTS_LORE_12 = §eLeftClick §8-§7 blockInHand -SCRIPT_GUI_CUSTOM_EVENTS_LORE_13 = §eTNTSpawn +SCRIPT_GUI_CUSTOM_EVENTS_LORE_11 = §eRightClick §8-§7 blockInHand, action, handType, +SCRIPT_GUI_CUSTOM_EVENTS_LORE_12 = §7 hasBlock §8[§7blockX, blockY, blockZ, blockFace§8]§e* +SCRIPT_GUI_CUSTOM_EVENTS_LORE_13 = §eLeftClick §8-§7 blockInHand, action, handType, +SCRIPT_GUI_CUSTOM_EVENTS_LORE_14 = §7 hasBlock §8[§7blockX, blockY, blockZ, blockFace§8]§e* +SCRIPT_GUI_CUSTOM_EVENTS_LORE_15 = §eTNTSpawn +SCRIPT_GUI_CUSTOM_EVENTS_LORE_16 = §eTNTExplode §8-§7 x, y, z +SCRIPT_GUI_CUSTOM_EVENTS_LORE_17 = §eTNTExplodeInBuild §8-§7 x, y, z +SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_1 = §e* §8-§7 Alles in den Klammern ist nur gesetzt, +SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_2 = §7wenn die Variable dafür gesetzt ist. SCRIPT_GUI_OTHER = §eOther SCRIPT_GUI_OTHER_LORE_1 = §7Kommentare fangen mit §e#§7 an. diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java index 53c5bc28..f661ee54 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java @@ -19,14 +19,18 @@ package de.steamwar.bausystem.features.region; +import de.steamwar.bausystem.features.script.CustomScriptListener; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.linkage.LinkedInstance; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.flagvalues.TNTMode; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityExplodeEvent; @@ -34,6 +38,9 @@ import org.bukkit.event.entity.EntityExplodeEvent; @Linked(LinkageType.LISTENER) public class TNTListener implements Listener { + @LinkedInstance + private CustomScriptListener customScriptListener; + @EventHandler public void onExplode(EntityExplodeEvent event) { event.blockList().removeIf(block -> { @@ -44,10 +51,20 @@ public class TNTListener implements Listener { } if (region.hasType(RegionType.BUILD) && region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.NORMAL)) { RegionUtils.actionBar(region, "REGION_TNT_BUILD"); + for (Player player : Bukkit.getOnlinePlayers()) { + if (region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { + customScriptListener.callEvent(CustomScriptListener.EventType.TNTExplodeInBuild, player, event); + } + } return true; } if (region.hasType(RegionType.BUILD) && region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION)) { RegionUtils.actionBar(region, "REGION_TNT_BUILD"); + for (Player player : Bukkit.getOnlinePlayers()) { + if (region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { + customScriptListener.callEvent(CustomScriptListener.EventType.TNTExplodeInBuild, player, event); + } + } return true; } return value == TNTMode.DENY; 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 eb164bcf..173b50f2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -25,6 +25,8 @@ import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.utils.RegionExtensionType; +import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.core.VersionedCallable; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; @@ -41,6 +43,7 @@ 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.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.*; @@ -281,14 +284,46 @@ public class CustomScriptListener implements Listener { RightClick(PlayerInteractEvent.class, event -> { Map valueMap = new HashMap<>(); valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand())); + valueMap.put("action", new Value.StringValue(event.getAction().name())); + valueMap.put("handType", new Value.StringValue(event.getMaterial().name())); + valueMap.put("hasBlock", new Value.BooleanValue(event.hasBlock())); + if (event.hasBlock()) { + valueMap.put("blockX", new Value.LongValue(event.getClickedBlock().getX())); + valueMap.put("blockY", new Value.LongValue(event.getClickedBlock().getY())); + valueMap.put("blockZ", new Value.LongValue(event.getClickedBlock().getZ())); + valueMap.put("blockFace", new Value.StringValue(event.getBlockFace().name())); + } return valueMap; }), LeftClick(PlayerInteractEvent.class, event -> { Map valueMap = new HashMap<>(); valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand())); + valueMap.put("action", new Value.StringValue(event.getAction().name())); + valueMap.put("handType", new Value.StringValue(event.getMaterial().name())); + valueMap.put("hasBlock", new Value.BooleanValue(event.hasBlock())); + if (event.hasBlock()) { + valueMap.put("blockX", new Value.LongValue(event.getClickedBlock().getX())); + valueMap.put("blockY", new Value.LongValue(event.getClickedBlock().getY())); + valueMap.put("blockZ", new Value.LongValue(event.getClickedBlock().getZ())); + valueMap.put("blockFace", new Value.StringValue(event.getBlockFace().name())); + } return valueMap; }), - TNTSpawn(EntitySpawnEvent.class, event -> null); + TNTSpawn(EntitySpawnEvent.class, event -> null), + TNTExplode(EntityExplodeEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("x", new Value.DoubleValue(event.getLocation().getX())); + valueMap.put("y", new Value.DoubleValue(event.getLocation().getY())); + valueMap.put("z", new Value.DoubleValue(event.getLocation().getZ())); + return valueMap; + }), + TNTExplodeInBuild(EntityExplodeEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("x", new Value.DoubleValue(event.getLocation().getX())); + valueMap.put("y", new Value.DoubleValue(event.getLocation().getY())); + valueMap.put("z", new Value.DoubleValue(event.getLocation().getZ())); + return valueMap; + }); private Class eventType; private Function> eventValues; @@ -299,7 +334,7 @@ public class CustomScriptListener implements Listener { } } - private void callEvent(EventType eventType, Player p, Event e) { + public void callEvent(EventType eventType, Player p, Event e) { if (!eventType.getEventType().equals(e.getClass())) { return; } @@ -312,14 +347,12 @@ public class CustomScriptListener implements Listener { variables = new HashMap<>(); } if (e instanceof Cancellable) { - variables.put("cancel", new Value.BooleanValue(false)); + variables.put("cancel", new Value.BooleanValue(((Cancellable) e).isCancelled())); } customEvent.execute(e, p, variables); if (variables.containsKey("cancel")) { Value value = variables.get("cancel"); - if (value.asBoolean()) { - ((Cancellable) e).setCancelled(true); - } + ((Cancellable) e).setCancelled(value.asBoolean()); } } } @@ -446,10 +479,23 @@ public class CustomScriptListener implements Listener { Region tntRegion = Region.getRegion(event.getLocation()); for (Player player : Bukkit.getOnlinePlayers()) { - Region region = Region.getRegion(player.getLocation()); - if (region == tntRegion) { + if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { callEvent(EventType.TNTSpawn, player, event); } } } + + @EventHandler + public void onEntityExplode(EntityExplodeEvent event) { + if (event.getEntityType() != EntityType.PRIMED_TNT) { + return; + } + Region tntRegion = Region.getRegion(event.getLocation()); + + for (Player player : Bukkit.getOnlinePlayers()) { + if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { + callEvent(EventType.TNTExplode, player, 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 1d8eeb48..474aad72 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -38,7 +38,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS", p), Arrays.asList(BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_1", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_2", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_3", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_4", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_5", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_6", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_7", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_8", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_9", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_10", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_11", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_12", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS_LORE_13", p)), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS", p), Arrays.asList(BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_1", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_2", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_3", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_4", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_5", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_6", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_7", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_8", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_9", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_10", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_11", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_12", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_13", p)), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS", p), Arrays.asList(BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_1", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_2", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_3", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_4", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_5", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_6", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_7", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_8", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_9", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_10", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_11", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_12", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_13", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_14", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_15", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_16", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_17", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_1", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_2", p)), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER", p), Arrays.asList(BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_1", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_2", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_3", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_4", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_5", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_6", p), BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER_LORE_7", p)), false, clickType -> { }), null)); @@ -78,7 +78,7 @@ public class ScriptCommand extends SWCommand { }); swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand)); }); - for (int i = 0; i < 8; i++) { + for (int i = 0; i < 5; 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/ScriptExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java index fbeb0e73..12775076 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -172,11 +172,15 @@ public final class ScriptExecutor { public Value parse(String varValue) { try { return new Value.LongValue(Long.parseLong(varValue)); - } catch (NumberFormatException e) { - if (varValue.equalsIgnoreCase("true") || varValue.equalsIgnoreCase("false")) { - return new Value.BooleanValue(varValue.equalsIgnoreCase("true")); - } else { - return new Value.StringValue(varValue); + } catch (NumberFormatException ne) { + try { + return new Value.DoubleValue(Double.parseDouble(varValue)); + } catch (NumberFormatException e) { + if (varValue.equalsIgnoreCase("true") || varValue.equalsIgnoreCase("false")) { + return new Value.BooleanValue(varValue.equalsIgnoreCase("true")); + } else { + return new Value.StringValue(varValue); + } } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java index 391930b1..9ce3403e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptVarsCommand.java @@ -81,6 +81,13 @@ public class ScriptVarsCommand extends SWCommand { BauSystem.MESSAGE.send("SCRIPT_COMMAND_VARS_SET_VALUE", p, varName, value); } + @Register(description = "SCRIPT_COMMAND_VARS_HELP_SET") + public void setValueCommand(Player p, String varName, double value) { + Context context = ScriptListener.getGlobalContext(p); + context.putValue(varName, new Value.DoubleValue(value)); + BauSystem.MESSAGE.send("SCRIPT_COMMAND_VARS_SET_VALUE", p, varName, value); + } + @Register public void setValueCommand(Player p, String varName, boolean value) { Context context = ScriptListener.getGlobalContext(p); 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 0462f422..1fec025b 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,16 +44,20 @@ public class Add implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (!(v1 instanceof Value.LongValue)) { + if (!(v1 instanceof Value.LongValue) && !(v1 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_ADD_ERROR", scriptExecutor.getPlayer()); return true; } - if (!(v2 instanceof Value.LongValue)) { + if (!(v2 instanceof Value.LongValue) && !(v2 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_ADD_ERROR", scriptExecutor.getPlayer()); return true; } - scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() + v2.asLong())); + if (v1 instanceof Value.DoubleValue || v2 instanceof Value.DoubleValue) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(v1.asDouble() + v2.asDouble())); + } else { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() + v2.asLong())); + } 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 0b6f7f60..f4f4cbc8 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,16 +44,20 @@ public class Div implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (!(v1 instanceof Value.LongValue)) { + if (!(v1 instanceof Value.LongValue) && !(v1 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_DIV_ERROR", scriptExecutor.getPlayer()); return true; } - if (!(v2 instanceof Value.LongValue)) { + if (!(v2 instanceof Value.LongValue) && !(v2 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_DIV_ERROR", scriptExecutor.getPlayer()); return true; } - scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() / v2.asLong())); + if (v1 instanceof Value.DoubleValue || v2 instanceof Value.DoubleValue) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(v1.asDouble() / v2.asDouble())); + } else { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() / v2.asLong())); + } 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 a79fe103..bdb382fd 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,16 +44,20 @@ public class Mul implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (!(v1 instanceof Value.LongValue)) { + if (!(v1 instanceof Value.LongValue) && !(v1 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_MUL_ERROR", scriptExecutor.getPlayer()); return true; } - if (!(v2 instanceof Value.LongValue)) { + if (!(v2 instanceof Value.LongValue) && !(v2 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_MUL_ERROR", scriptExecutor.getPlayer()); return true; } - scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() * v2.asLong())); + if (v1 instanceof Value.DoubleValue || v2 instanceof Value.DoubleValue) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(v1.asDouble() * v2.asDouble())); + } else { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() * v2.asLong())); + } 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 25aa029c..c327931b 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,16 +44,20 @@ public class Sub implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (!(v1 instanceof Value.LongValue)) { + if (!(v1 instanceof Value.LongValue) && !(v1 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_SUB_ERROR", scriptExecutor.getPlayer()); return true; } - if (!(v2 instanceof Value.LongValue)) { + if (!(v2 instanceof Value.LongValue) && !(v2 instanceof Value.DoubleValue)) { BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_SUB_ERROR", scriptExecutor.getPlayer()); return true; } - scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() - v2.asLong())); + if (v1 instanceof Value.DoubleValue || v2 instanceof Value.DoubleValue) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(v1.asDouble() - v2.asDouble())); + } else { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(v1.asLong() - v2.asLong())); + } return true; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Ceil.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Ceil.java new file mode 100644 index 00000000..abe38f4b --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Ceil.java @@ -0,0 +1,94 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script.command.arithmetic.other; + +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; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Ceil implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_HELP_1", + "SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_HELP_2", + "", + "SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_HELP_3" + }; + } + + @Override + public Material material() { + return Material.REDSTONE_TORCH; + } + + @Override + public String command() { + return "ceil"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ERROR_FIRST_ARG_NOVAR", scriptExecutor.getPlayer()); + return true; + } + if (command.length <= 2) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ERROR_SECOND_ARG_NOVAR", scriptExecutor.getPlayer()); + return true; + } + + Value v1; + Value v2; + if (command.length == 3) { + v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + v2 = new Value.LongValue(0); + } else { + v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); + v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + } + if (!(v1 instanceof Value.DoubleValue)) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_ERROR_1", scriptExecutor.getPlayer()); + return true; + } + if (!(v2 instanceof Value.LongValue)) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_ERROR_2", scriptExecutor.getPlayer()); + return true; + } + if (v2.asLong() < 0) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_CEIL_ERROR_2", scriptExecutor.getPlayer()); + return true; + } + + if (v2.asLong() == 0) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue((long) Math.ceil(v1.asDouble()))); + } else { + double pow = Math.pow(10, v2.asLong()); + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(Math.ceil(v1.asDouble() * pow) / pow)); + } + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Floor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Floor.java new file mode 100644 index 00000000..86ee993a --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Floor.java @@ -0,0 +1,94 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script.command.arithmetic.other; + +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; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Floor implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_HELP_1", + "SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_HELP_2", + "", + "SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_HELP_3" + }; + } + + @Override + public Material material() { + return Material.REDSTONE_TORCH; + } + + @Override + public String command() { + return "floor"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ERROR_FIRST_ARG_NOVAR", scriptExecutor.getPlayer()); + return true; + } + if (command.length <= 2) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ERROR_SECOND_ARG_NOVAR", scriptExecutor.getPlayer()); + return true; + } + + Value v1; + Value v2; + if (command.length == 3) { + v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + v2 = new Value.LongValue(0); + } else { + v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); + v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + } + if (!(v1 instanceof Value.DoubleValue)) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_ERROR_1", scriptExecutor.getPlayer()); + return true; + } + if (!(v2 instanceof Value.LongValue)) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_ERROR_2", scriptExecutor.getPlayer()); + return true; + } + if (v2.asLong() < 0) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_FLOOR_ERROR_2", scriptExecutor.getPlayer()); + return true; + } + + if (v2.asLong() == 0) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue((long) Math.floor(v1.asDouble()))); + } else { + double pow = Math.pow(10, v2.asLong()); + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(Math.floor(v1.asDouble() * pow) / pow)); + } + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Round.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Round.java new file mode 100644 index 00000000..ed037b8c --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/other/Round.java @@ -0,0 +1,94 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script.command.arithmetic.other; + +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; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Round implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_HELP_1", + "SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_HELP_2", + "", + "SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_HELP_3" + }; + } + + @Override + public Material material() { + return Material.REDSTONE_TORCH; + } + + @Override + public String command() { + return "round"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ERROR_FIRST_ARG_NOVAR", scriptExecutor.getPlayer()); + return true; + } + if (command.length <= 2) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ERROR_SECOND_ARG_NOVAR", scriptExecutor.getPlayer()); + return true; + } + + Value v1; + Value v2; + if (command.length == 3) { + v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + v2 = new Value.LongValue(0); + } else { + v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); + v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + } + if (!(v1 instanceof Value.DoubleValue)) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_ERROR_1", scriptExecutor.getPlayer()); + return true; + } + if (!(v2 instanceof Value.LongValue)) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_ERROR_2", scriptExecutor.getPlayer()); + return true; + } + if (v2.asLong() < 0) { + BauSystem.MESSAGE.send("SCRIPT_COMMAND_ARITHMETIC_OTHER_ROUND_ERROR_2", scriptExecutor.getPlayer()); + return true; + } + + if (v2.asLong() == 0) { + scriptExecutor.getLocalVariables().putValue(command[1], new Value.LongValue(Math.round(v1.asDouble()))); + } else { + double pow = Math.pow(10, v2.asLong()); + scriptExecutor.getLocalVariables().putValue(command[1], new Value.DoubleValue(Math.round(v1.asDouble() * pow) / pow)); + } + return true; + } +} 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 2add5240..1bced425 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 @@ -1,7 +1,6 @@ package de.steamwar.bausystem.features.script.variables; import de.steamwar.bausystem.features.tracer.record.RecordStateMachine; -import de.steamwar.bausystem.region.GlobalRegion; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.flagvalues.FireMode; @@ -24,6 +23,53 @@ public class Constants { private final Map> CONSTANTS = new HashMap<>(); + private static class ConstantDoubleValue extends Value.DoubleValue { + + private Supplier doubleSupplier; + private Consumer doubleConsumer = ignored -> {}; + + public ConstantDoubleValue(Supplier doubleSupplier) { + super(doubleSupplier.get()); + this.doubleSupplier = doubleSupplier; + } + + public ConstantDoubleValue(Supplier doubleSupplier, Consumer doubleConsumer) { + super(doubleSupplier.get()); + this.doubleSupplier = doubleSupplier; + this.doubleConsumer = doubleConsumer; + } + + @Override + public long asLong() { + value = doubleSupplier.get(); + return super.asLong(); + } + + @Override + public double asDouble() { + value = doubleSupplier.get(); + return super.asDouble(); + } + + @Override + public boolean asBoolean() { + value = doubleSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = doubleSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + doubleConsumer.accept(this.value); + } + } + private static class ConstantLongValue extends Value.LongValue { private Supplier longSupplier; @@ -46,6 +92,12 @@ public class Constants { return super.asLong(); } + @Override + public double asDouble() { + value = longSupplier.get(); + return super.asDouble(); + } + @Override public boolean asBoolean() { value = longSupplier.get(); @@ -87,6 +139,12 @@ public class Constants { return super.asLong(); } + @Override + public double asDouble() { + value = booleanSupplier.get(); + return super.asDouble(); + } + @Override public boolean asBoolean() { value = booleanSupplier.get(); @@ -128,6 +186,12 @@ public class Constants { return super.asLong(); } + @Override + public double asDouble() { + value = stringSupplier.get(); + return super.asDouble(); + } + @Override public boolean asBoolean() { value = stringSupplier.get(); @@ -170,21 +234,21 @@ public class Constants { return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE); }); CONSTANTS.put("x", player -> { - return new ConstantLongValue(() -> (long) player.getLocation().getBlockX(), aLong -> { + return new ConstantDoubleValue(() -> player.getLocation().getX(), aLong -> { Location location = player.getLocation(); location.setX((double) aLong); player.teleport(location); }); }); CONSTANTS.put("y", player -> { - return new ConstantLongValue(() -> (long) player.getLocation().getBlockY(), aLong -> { + return new ConstantDoubleValue(() -> player.getLocation().getY(), aLong -> { Location location = player.getLocation(); location.setY((double) aLong); player.teleport(location); }); }); CONSTANTS.put("z", player -> { - return new ConstantLongValue(() -> (long) player.getLocation().getBlockZ(), aLong -> { + return new ConstantDoubleValue(() -> player.getLocation().getZ(), aLong -> { Location location = player.getLocation(); location.setZ((double) aLong); player.teleport(location); 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 68fd935a..87f0879e 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 @@ -6,11 +6,49 @@ import lombok.ToString; public interface Value { String type(); long asLong(); + double asDouble(); boolean asBoolean(); String asString(); void fromValue(Value value); + @AllArgsConstructor + @ToString + class DoubleValue implements Value { + + protected double value; + + @Override + public String type() { + return "floating_number"; + } + + @Override + public long asLong() { + return Math.round(value); + } + + @Override + public double asDouble() { + return value; + } + + @Override + public boolean asBoolean() { + return value != 0; + } + + @Override + public String asString() { + return value + ""; + } + + @Override + public void fromValue(Value value) { + this.value = value.asDouble(); + } + } + @AllArgsConstructor @ToString class LongValue implements Value { @@ -27,6 +65,11 @@ public interface Value { return value; } + @Override + public double asDouble() { + return value; + } + @Override public boolean asBoolean() { return value != 0; @@ -59,6 +102,11 @@ public interface Value { return value ? 1 : 0; } + @Override + public double asDouble() { + return value ? 1 : 0; + } + @Override public boolean asBoolean() { return value; @@ -95,6 +143,15 @@ public interface Value { } } + @Override + public double asDouble() { + try { + return Double.parseDouble(value); + } catch (NumberFormatException e) { + return 0; + } + } + @Override public boolean asBoolean() { return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");