SteamWar/BauSystem2.0
Archiviert
12
0

Add MultiReplaceCommand
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-09-10 15:22:49 +02:00
Ursprung 403ca8dae7
Commit 3ef7812aa7

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, String> 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<Replacement> replacementTypeMapper() {
Set<Material> materials = Arrays.stream(Material.values()).filter(Material::isBlock).collect(Collectors.toSet());
return new TypeMapper<Replacement>() {
@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<String> 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();
}
};
}
}