From 93ba4efa1f95da2ea1ca868efe59e1ef9d1128e9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 3 May 2021 18:20:46 +0200 Subject: [PATCH] Add Replace --- .../script/command/string/Replace.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java 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 new file mode 100644 index 00000000..08c9390e --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java @@ -0,0 +1,80 @@ +/* + * 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 Replace implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "§ereplace §8<§7Variable§8> §8<§7Von Variable§8> §8<§7Zu Variable§8>", + "§ereplace §8<§7Variable§8> §8<§7Variable§8> §8<§7Von Variable§8> §8<§7Zu Variable§8>", + "", + "Ersetzte eine Zeichenkette aus dem Ursprung mit einer neuen Zeichenkette. Dies ersetzt nicht nur die erste Stelle sondern alle.", + }; + } + + @Override + public String command() { + return "replace"; + } + + @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; + } + if (command.length <= 3) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas dritte Argument fehlt und sollte eine Variable sein"); + return true; + } + + String resultName = command[1]; + Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 3]); + Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); + Value v3 = 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.StringValue.class) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); + return true; + } + if (v3.getClass() != Value.StringValue.class) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); + return true; + } + + scriptExecutor.getLocalVariables().putValue(resultName, new Value.StringValue(v1.asString().replace(v2.asString(), v3.asString()))); + return true; + } +}