From 403ca8dae706c3c09e53f15fbf3c9909fcae76c9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 10 Sep 2022 15:15:38 +0200 Subject: [PATCH] Update stuff Signed-off-by: yoyosource --- .../worldedit/ColorReplaceCommand.java | 11 +++++--- .../features/worldedit/SpecialReplace.java | 20 +++++++++----- .../worldedit/TypeReplaceCommand.java | 26 ++++++++++++++++--- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/ColorReplaceCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/ColorReplaceCommand.java index 69234184..734b8b7d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/ColorReplaceCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/ColorReplaceCommand.java @@ -36,7 +36,9 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.entity.Player; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -66,14 +68,15 @@ public class ColorReplaceCommand extends SWCommand { World world = player.getWorld(); - Set 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() + "_"; + Map replacements = new HashMap<>(); + for (String type : types) { + replacements.put("minecraft:" + from.name().toLowerCase() + "_" + type.toLowerCase(), "minecraft:" + to.name().toLowerCase() + "_" + type.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)); + SpecialReplace specialReplace = new SpecialReplace(editSession, replacements); int affected = editSession.replaceBlocks(region, specialReplace, specialReplace); editSession.flushSession(); BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)})); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/SpecialReplace.java b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/SpecialReplace.java index 59c20b86..a7205aef 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/SpecialReplace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/SpecialReplace.java @@ -29,24 +29,23 @@ import com.sk89q.worldedit.world.block.BaseBlock; import org.bukkit.Bukkit; import javax.annotation.Nullable; +import java.util.Map; import java.util.Set; import java.util.function.Function; public class SpecialReplace implements Mask, Pattern { private EditSession editSession; - private Set toReplace; - private Function replacer; + private Map replacements; - public SpecialReplace(EditSession editSession, Set toReplace, Function replacer) { + public SpecialReplace(EditSession editSession, Map replacements) { this.editSession = editSession; - this.toReplace = toReplace; - this.replacer = replacer; + this.replacements = replacements; } @Override public boolean test(BlockVector3 blockVector3) { - return toReplace.contains(editSession.getFullBlock(blockVector3).getBlockType().getId()); + return replacements.containsKey(editSession.getFullBlock(blockVector3).getBlockType().getId()); } @Nullable @@ -57,8 +56,15 @@ public class SpecialReplace implements Mask, Pattern { @Override public BaseBlock applyBlock(BlockVector3 position) { + String s = BukkitAdapter.adapt(editSession.getFullBlock(position)).getAsString(); + String rest = ""; + if (s.contains("[")) { + int i = s.indexOf("["); + rest = s.substring(i); + s = s.substring(0, i); + } try { - return BukkitAdapter.adapt(Bukkit.createBlockData(replacer.apply(BukkitAdapter.adapt(editSession.getFullBlock(position)).getAsString()))).toBaseBlock(); + return BukkitAdapter.adapt(Bukkit.createBlockData(replacements.getOrDefault(s, s) + rest)).toBaseBlock(); } catch (IllegalArgumentException e) { return editSession.getFullBlock(position); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/TypeReplaceCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/TypeReplaceCommand.java index ab82aa4c..15fba6be 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/TypeReplaceCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/worldedit/TypeReplaceCommand.java @@ -36,7 +36,9 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.entity.Player; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -73,6 +75,17 @@ public class TypeReplaceCommand extends SWCommand { private Set types = new HashSet<>(); { types.add(""); + types.add("WOOD"); + types.add("PLANKS"); + types.add("BUTTON"); + types.add("DOOR"); + types.add("LOG"); + types.add("LEAVED"); + types.add("SIGN"); + types.add("WALL_SIGN"); + types.add("PRESSURE_PLATE"); + types.add("SAPLING"); + types.add("TRAPDOOR"); types.add("STAIRS"); types.add("SLAB"); types.add("FENCE"); @@ -90,14 +103,19 @@ public class TypeReplaceCommand extends SWCommand { World world = player.getWorld(); - Set 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() + "_"; + Map replacements = new HashMap<>(); + for (String type : types) { + if (type.isEmpty()) { + replacements.put("minecraft:" + from.name().toLowerCase(), "minecraft:" + to.name().toLowerCase()); + } else { + replacements.put("minecraft:" + from.name().toLowerCase() + "_" + type.toLowerCase(), "minecraft:" + to.name().toLowerCase() + "_" + type.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)); + SpecialReplace specialReplace = new SpecialReplace(editSession, replacements); int affected = editSession.replaceBlocks(region, specialReplace, specialReplace); editSession.flushSession(); BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));