From 638a12819539561fbc78a277796be262082ab115 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 3 May 2021 18:37:17 +0200 Subject: [PATCH] Add Substring --- .../script/command/string/Substring.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java 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 new file mode 100644 index 00000000..38db6c66 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java @@ -0,0 +1,77 @@ +/* + * 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.string; + +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; + +public class Substring implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "§esubstring §8<§7Variable§8> §8<§7Zahl§8>", + "§esubstring §8<§7Variable§8> §8<§7Variable§8> §8<§7Zahl§8>", + "", + "Kürze einen String entweder vorne oder hinter, je nachdem ob die Zahl positiv (vorne) oder negativ (hinten) ist." + }; + } + + @Override + public String command() { + return "substring"; + } + + @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 eine Variable sein"); + return true; + } + + String resultName = command[1]; + Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); + Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); + + if (v1.getClass() != Value.StringValue.class) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); + return true; + } + if (v2.getClass() != Value.LongValue.class) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); + return true; + } + + Value result; + if (v2.asLong() < 0) { + result = new Value.StringValue(v1.asString().substring(v1.asString().length() - 1 - (int) v2.asLong())); + } else { + result = new Value.StringValue(v1.asString().substring((int) v2.asLong())); + } + scriptExecutor.getLocalVariables().putValue(resultName, result); + return true; + } +}