From 3ef7812aa77dfcbde36fc767975c19b31be3977c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 10 Sep 2022 15:22:49 +0200 Subject: [PATCH] Add MultiReplaceCommand Signed-off-by: yoyosource --- .../worldedit/MultiReplaceCommand.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/MultiReplaceCommand.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/MultiReplaceCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/MultiReplaceCommand.java new file mode 100644 index 00000000..3462cbab --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/MultiReplaceCommand.java @@ -0,0 +1,107 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.worldedit; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.utils.WorldEditUtils; +import de.steamwar.command.SWCommand; +import de.steamwar.command.TypeMapper; +import lombok.AllArgsConstructor; +import lombok.SneakyThrows; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.*; +import java.util.stream.Collectors; + +@Linked(LinkageType.COMMAND) +public class MultiReplaceCommand extends SWCommand { + + public MultiReplaceCommand() { + super("/multireplace", "/multirep", "/mrep", "/multir", "/mr"); + } + + @AllArgsConstructor + private static class Replacement { + private Material from; + private Material to; + } + + @Register + @SneakyThrows + public void genericCommand(Player player, Replacement... replacements) { + World world = player.getWorld(); + + Map stringReplacements = new HashMap<>(); + for (Replacement replacement : replacements) { + stringReplacements.put(replacement.from.name(), replacement.to.name()); + } + + Region region = WorldEditUtils.getRegion(player); + EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(BukkitAdapter.adapt(world), -1, BukkitAdapter.adapt(player)); + + SpecialReplace specialReplace = new SpecialReplace(editSession, stringReplacements); + int affected = editSession.replaceBlocks(region, specialReplace, specialReplace); + editSession.flushSession(); + BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)})); + } + + @ClassMapper(value = Replacement.class, local = true) + public TypeMapper replacementTypeMapper() { + Set materials = Arrays.stream(Material.values()).filter(Material::isBlock).collect(Collectors.toSet()); + + return new TypeMapper() { + @Override + public Replacement map(CommandSender commandSender, String[] previousArguments, String s) { + String[] split = s.split(":"); + if(split.length != 2) { + return null; + } + Material from = Material.matchMaterial(split[0]); + Material to = Material.matchMaterial(split[1]); + if(from == null || to == null) { + return null; + } + return new Replacement(from, to); + } + + @Override + public Collection tabCompletes(CommandSender commandSender, String[] strings, String s) { + int index = s.indexOf(":"); + if (index != -1 && index == s.lastIndexOf(":")) { + return materials.stream().map(Material::name).map(t -> s + t).collect(Collectors.toList()); + } else if (index == -1) { + return materials.stream().map(Material::name).collect(Collectors.toList()); + } + return Collections.emptyList(); + } + }; + } +}