From ecc207b561fe5bcc5d8299a15eb4d3a7830d8c91 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 28 Apr 2021 08:35:10 +0200 Subject: [PATCH] Add Var --- .../features/script/command/variable/Var.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) 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 3335d47f..750f583d 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 @@ -1,4 +1,55 @@ package de.steamwar.bausystem.features.script.command.variable; -public class Var { +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; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Var implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "§evar §8<§7Name§8> §8[§7Value§8(§7s§8)§8]", + "", + "Schreibt in eine Variable einen Wert rein, welcher eine Zahl sein kann, ein Boolscher Wert oder ein Text." + }; + } + + @Override + public String command() { + return "var"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + return true; + } + if (command.length <= 2) { + 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]); + } + 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.getLocalVariables().putValue(varName, new Value.BooleanValue(s.equalsIgnoreCase("true"))); + } else { + scriptExecutor.getLocalVariables().putValue(varName, new Value.StringValue(s)); + } + } + return true; + } }