diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java new file mode 100644 index 00000000..358ba86e --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java @@ -0,0 +1,83 @@ +/* + * 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; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Insert implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "§einsert §8<§7Variable§8> §8<§7Variable§8> §8<§7Zahl§8>", + "§einsert §8<§7Variable§8> §8<§7Variable§8> §8<§7Variable§8> §8<§7Zahl§8>", + "", + "Füge etwas in einen String an einer Stelle ein. Die Zahl ist für die Position und die Variable davor für was. Schreibe optional dies in eine neue oder andere Variable." + }; + } + + @Override + public String command() { + return "insert"; + } + + @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/Zahl 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.LongValue.class) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); + return true; + } + + scriptExecutor.getLocalVariables().putValue(resultName, new Value.StringValue(new StringBuilder(v1.asString()).insert((int) v3.asLong(), v2.asString()).toString())); + return true; + } +}