SteamWar/BauSystem2.0
Archiviert
12
0

Add ColorReplaceCommand
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Add TypeReplaceCommand WIP

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-09-10 13:55:56 +02:00
Ursprung 8bdc9c69d8
Commit 37f3726ccc
4 geänderte Dateien mit 255 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,81 @@
/*
* 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.region.Color;
import de.steamwar.bausystem.utils.WorldEditUtils;
import de.steamwar.command.SWCommand;
import lombok.SneakyThrows;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@Linked(LinkageType.COMMAND)
public class ColorReplaceCommand extends SWCommand {
public ColorReplaceCommand() {
super("/colorreplace", "/cr");
}
private Set<String> types = new HashSet<>();
{
for (Material value : Material.values()) {
if (value.name().startsWith("WHITE_")) {
types.add(value.name().substring(6));
}
}
}
@Register
@SneakyThrows
public void genericCommand(Player player, Color from, Color to) {
if (from == to) {
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(0)}));
return;
}
World world = player.getWorld();
Set<String> toReplace = types.stream().map(s -> "minecraft:" + from.name().toLowerCase() + "_" + s.toLowerCase()).collect(Collectors.toSet());
String fromString = "minecraft:" + from.name().toLowerCase() + "_";
String toString = "minecraft:" + to.name().toLowerCase() + "_";
Region region = WorldEditUtils.getRegion(player);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(BukkitAdapter.adapt(world), -1, BukkitAdapter.adapt(player));
SpecialReplace specialReplace = new SpecialReplace(editSession, toReplace, s -> s.replace(fromString, toString));
int affected = editSession.replaceBlocks(region, specialReplace, specialReplace);
editSession.flushSession();
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
}
}

Datei anzeigen

@ -0,0 +1,66 @@
/*
* 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.bukkit.BukkitAdapter;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Mask2D;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import org.bukkit.Bukkit;
import javax.annotation.Nullable;
import java.util.Set;
import java.util.function.Function;
public class SpecialReplace implements Mask, Pattern {
private EditSession editSession;
private Set<String> toReplace;
private Function<String, String> replacer;
public SpecialReplace(EditSession editSession, Set<String> toReplace, Function<String, String> replacer) {
this.editSession = editSession;
this.toReplace = toReplace;
this.replacer = replacer;
}
@Override
public boolean test(BlockVector3 blockVector3) {
return toReplace.contains(editSession.getFullBlock(blockVector3).getBlockType().getId());
}
@Nullable
@Override
public Mask2D toMask2D() {
return null;
}
@Override
public BaseBlock applyBlock(BlockVector3 position) {
try {
return BukkitAdapter.adapt(Bukkit.createBlockData(replacer.apply(BukkitAdapter.adapt(editSession.getFullBlock(position)).getAsString()))).toBaseBlock();
} catch (IllegalArgumentException e) {
return editSession.getFullBlock(position);
}
}
}

Datei anzeigen

@ -0,0 +1,105 @@
/*
* 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.region.Color;
import de.steamwar.bausystem.utils.WorldEditUtils;
import de.steamwar.command.SWCommand;
import lombok.SneakyThrows;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@Linked(LinkageType.COMMAND)
public class TypeReplaceCommand extends SWCommand {
private enum Type {
OAK,
SPRUCE,
BIRCH,
JUNGLE,
ACACIA,
DARK_OAK,
CRIMSON,
WARPED,
MANGROVE,
STONE,
COBBLE,
MOSSY_COBBLE,
SANDSTONE,
RED_SANDSTONE,
BRICK,
STONE_BRICK,
NETHER_BRICK,
QUARTZ,
PURPUR,
BLACKSTONE,
}
public TypeReplaceCommand() {
super("/typereplace", "/tr");
}
private Set<String> types = new HashSet<>();
{
types.add("");
types.add("STAIRS");
types.add("SLAB");
types.add("FENCE");
types.add("FENCE_GATE");
types.add("WALL");
}
@Register
@SneakyThrows
public void genericCommand(Player player, Type from, Type to) {
if (from == to) {
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(0)}));
return;
}
World world = player.getWorld();
Set<String> toReplace = types.stream().map(s -> "minecraft:" + from.name().toLowerCase() + "_" + s.toLowerCase()).collect(Collectors.toSet());
String fromString = "minecraft:" + from.name().toLowerCase() + "_";
String toString = "minecraft:" + to.name().toLowerCase() + "_";
Region region = WorldEditUtils.getRegion(player);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(BukkitAdapter.adapt(world), -1, BukkitAdapter.adapt(player));
SpecialReplace specialReplace = new SpecialReplace(editSession, toReplace, s -> s.replace(fromString, toString));
int affected = editSession.replaceBlocks(region, specialReplace, specialReplace);
editSession.flushSession();
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
}
}

Datei anzeigen

@ -24,6 +24,7 @@ import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.regions.RegionSelector;
import de.steamwar.bausystem.shared.Pair; import de.steamwar.bausystem.shared.Pair;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -32,8 +33,6 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Iterator;
@UtilityClass @UtilityClass
public class WorldEditUtils { public class WorldEditUtils {
@ -46,12 +45,12 @@ public class WorldEditUtils {
} }
@SneakyThrows @SneakyThrows
public Iterator<BlockVector3> getSelectionIterator(Player player) { public Region getRegion(Player player) {
RegionSelector regionSelector = WorldEdit.getInstance() RegionSelector regionSelector = WorldEdit.getInstance()
.getSessionManager() .getSessionManager()
.get(BukkitAdapter.adapt(player)) .get(BukkitAdapter.adapt(player))
.getRegionSelector(BukkitAdapter.adapt(player.getWorld())); .getRegionSelector(BukkitAdapter.adapt(player.getWorld()));
return regionSelector.getRegion().iterator(); return regionSelector.getRegion();
} }
public Pair<Location, Location> getSelection(Player player) { public Pair<Location, Location> getSelection(Player player) {