From e9d072b06b93622cf43a3c74f8f1803d7faf3c20 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 27 Apr 2021 20:12:51 +0200 Subject: [PATCH] Add If Add Jump --- .../features/script/ScriptExecutor.java | 26 +++++++++ .../features/script/SpecialCommand.java | 7 +++ .../features/script/command/Exit.java | 2 +- .../bausystem/features/script/command/If.java | 57 ++++++++++++++++++- .../features/script/command/Jump.java | 34 ++++++++++- .../features/script/command/Sleep.java | 6 +- 6 files changed, 126 insertions(+), 6 deletions(-) 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 9a13e08d..4b4a862a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -3,6 +3,7 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.variables.Constants; import de.steamwar.bausystem.features.script.variables.Context; +import de.steamwar.bausystem.features.script.variables.Value; import lombok.Getter; import lombok.Setter; import org.bukkit.Bukkit; @@ -120,6 +121,31 @@ public final class ScriptExecutor { return s.split(" "); } + public Value getOrItselfValue(String variable) { + if (!isVariable(variable)) { + return new Value.StringValue(variable); + } + + if (Constants.isConstant(variable)) { + return Constants.getConstant(variable, player); + } + if (globalVariables.hasValue(variable)) { + return globalVariables.getValue(variable); + } + return localVariables.getValue(variable); + } + + public String getOrItself(String variable) { + if (isVariable(variable)) { + return getValue(variable); + } + return variable; + } + + public boolean isVariable(String variable) { + return Constants.isConstant(variable) || globalVariables.hasValue(variable) || localVariables.hasValue(variable); + } + public String getValue(String variable) { if (Constants.isConstant(variable)) { return Constants.getConstant(variable, player).asString(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/SpecialCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/SpecialCommand.java index 55aa90ed..d001be9e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/SpecialCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/SpecialCommand.java @@ -2,6 +2,13 @@ package de.steamwar.bausystem.features.script; public interface SpecialCommand { + default String[] descriptions() { + if (description().equals("")) { + return new String[0]; + } + return new String[]{description()}; + } + default String description() { return ""; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Exit.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Exit.java index 37dd8257..4c4e6e91 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Exit.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Exit.java @@ -10,7 +10,7 @@ public class Exit implements SpecialCommand { @Override public String description() { - return "§eexit §8-§7 Beendet das ausführen des Scripts"; + return "§eexit §8-§7 Beendet das ausführen des Scripts."; } @Override diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/If.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/If.java index d755c15b..ca803ea0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/If.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/If.java @@ -1,4 +1,59 @@ package de.steamwar.bausystem.features.script.command; -public class If { +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; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class If implements SpecialCommand { + + @Override + public String[] descriptions() { + return new String[]{ + "§eif §8<§7Variable§8/§7Wert§8> §8<§7Variable§8/§7Wert§8> §8<§7Jump-Point§8>", + "§eif §8<§7Variable§8/§7Wert§8> §8<§7Variable§8/§7Wert§8> §8<§7Jump-Point§8> §8<§7Jump-Point§8>", + "", + "§7Springe zu einer Stelle, wenn zwei Werte gleich sind. Oder zu einer anderen, wenn dies nicht der fall ist." + }; + } + + @Override + public String command() { + return "if"; + } + + @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 oder ein Wert sein"); + return true; + } + if (command.length <= 2) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte eine Variable oder ein Wert sein"); + return true; + } + if (command.length <= 3) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas dritte Argument fehlt und sollte ein Jump-Point sein"); + return true; + } + + Value v1 = scriptExecutor.getOrItselfValue(command[1]); + Value v2 = scriptExecutor.getOrItselfValue(command[2]); + if (v1.getClass() != v2.getClass()) { + // This is intended + if (command.length > 4) { + scriptExecutor.setIndex((int) asLong(command[4])); + } + return true; + } + if (v1.asString().equals(v2.asString())) { + scriptExecutor.setIndex((int) asLong(command[3])); + } else if (command.length > 4) { + scriptExecutor.setIndex((int) asLong(command[4])); + } + return true; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Jump.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Jump.java index ef4cf61f..7ed5fd64 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Jump.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Jump.java @@ -1,4 +1,36 @@ package de.steamwar.bausystem.features.script.command; -public class Jump { +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.SpecialCommand; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Jump implements SpecialCommand { + + @Override + public String description() { + return "§ejump §8<§7Jump-Point§8> §8- §7Springe zu einer anderen Zeile. Hierbei ist ein Jump-Point eine Zeile mit §8'§7.§8'§7 vor."; + } + + @Override + public String command() { + return "jump"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlt"); + return true; + } + Integer jumpPoint = scriptExecutor.jumpPoints.getOrDefault(command[1], null); + if (jumpPoint == null) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDer Jump-Point (" + command[1] + ") ist nicht definiert."); + return true; + } + scriptExecutor.setIndex(jumpPoint); + return true; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Sleep.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Sleep.java index 55f9e363..f3f3e19e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Sleep.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/Sleep.java @@ -12,7 +12,7 @@ public class Sleep implements SpecialCommand { @Override public String description() { - return "§esleep §8-§7 Pausiert das Ausführen des Scripts. Das erste Argument ist eine Zahl und gibt die GameTicks an, die pausiert werden sollen."; + return "§esleep §8<§8Time§8> §8-§7 Pausiert das Ausführen des Scripts. Das erste Argument ist in GameTicks"; } @Override @@ -22,8 +22,8 @@ public class Sleep implements SpecialCommand { @Override public boolean execute(String[] command, ScriptExecutor scriptExecutor) { - if (command.length == 0) { - scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlte"); + if (command.length <= 1) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlt"); return true; } long sleepTime = asLong(command[1]);