Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
37f3726ccc
Commit
403ca8dae7
@ -36,7 +36,9 @@ import org.bukkit.Material;
|
|||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -66,14 +68,15 @@ public class ColorReplaceCommand extends SWCommand {
|
|||||||
|
|
||||||
World world = player.getWorld();
|
World world = player.getWorld();
|
||||||
|
|
||||||
Set<String> toReplace = types.stream().map(s -> "minecraft:" + from.name().toLowerCase() + "_" + s.toLowerCase()).collect(Collectors.toSet());
|
Map<String, String> replacements = new HashMap<>();
|
||||||
String fromString = "minecraft:" + from.name().toLowerCase() + "_";
|
for (String type : types) {
|
||||||
String toString = "minecraft:" + to.name().toLowerCase() + "_";
|
replacements.put("minecraft:" + from.name().toLowerCase() + "_" + type.toLowerCase(), "minecraft:" + to.name().toLowerCase() + "_" + type.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
Region region = WorldEditUtils.getRegion(player);
|
Region region = WorldEditUtils.getRegion(player);
|
||||||
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(BukkitAdapter.adapt(world), -1, BukkitAdapter.adapt(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);
|
int affected = editSession.replaceBlocks(region, specialReplace, specialReplace);
|
||||||
editSession.flushSession();
|
editSession.flushSession();
|
||||||
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
|
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
|
||||||
|
@ -29,24 +29,23 @@ import com.sk89q.worldedit.world.block.BaseBlock;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class SpecialReplace implements Mask, Pattern {
|
public class SpecialReplace implements Mask, Pattern {
|
||||||
|
|
||||||
private EditSession editSession;
|
private EditSession editSession;
|
||||||
private Set<String> toReplace;
|
private Map<String, String> replacements;
|
||||||
private Function<String, String> replacer;
|
|
||||||
|
|
||||||
public SpecialReplace(EditSession editSession, Set<String> toReplace, Function<String, String> replacer) {
|
public SpecialReplace(EditSession editSession, Map<String, String> replacements) {
|
||||||
this.editSession = editSession;
|
this.editSession = editSession;
|
||||||
this.toReplace = toReplace;
|
this.replacements = replacements;
|
||||||
this.replacer = replacer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(BlockVector3 blockVector3) {
|
public boolean test(BlockVector3 blockVector3) {
|
||||||
return toReplace.contains(editSession.getFullBlock(blockVector3).getBlockType().getId());
|
return replacements.containsKey(editSession.getFullBlock(blockVector3).getBlockType().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -57,8 +56,15 @@ public class SpecialReplace implements Mask, Pattern {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseBlock applyBlock(BlockVector3 position) {
|
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 {
|
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) {
|
} catch (IllegalArgumentException e) {
|
||||||
return editSession.getFullBlock(position);
|
return editSession.getFullBlock(position);
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,9 @@ import org.bukkit.Material;
|
|||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -73,6 +75,17 @@ public class TypeReplaceCommand extends SWCommand {
|
|||||||
private Set<String> types = new HashSet<>();
|
private Set<String> types = new HashSet<>();
|
||||||
{
|
{
|
||||||
types.add("");
|
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("STAIRS");
|
||||||
types.add("SLAB");
|
types.add("SLAB");
|
||||||
types.add("FENCE");
|
types.add("FENCE");
|
||||||
@ -90,14 +103,19 @@ public class TypeReplaceCommand extends SWCommand {
|
|||||||
|
|
||||||
World world = player.getWorld();
|
World world = player.getWorld();
|
||||||
|
|
||||||
Set<String> toReplace = types.stream().map(s -> "minecraft:" + from.name().toLowerCase() + "_" + s.toLowerCase()).collect(Collectors.toSet());
|
Map<String, String> replacements = new HashMap<>();
|
||||||
String fromString = "minecraft:" + from.name().toLowerCase() + "_";
|
for (String type : types) {
|
||||||
String toString = "minecraft:" + to.name().toLowerCase() + "_";
|
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);
|
Region region = WorldEditUtils.getRegion(player);
|
||||||
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(BukkitAdapter.adapt(world), -1, BukkitAdapter.adapt(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);
|
int affected = editSession.replaceBlocks(region, specialReplace, specialReplace);
|
||||||
editSession.flushSession();
|
editSession.flushSession();
|
||||||
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
|
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren