SteamWar/BauSystem2.0
Archiviert
12
0

Add percentages to //cr and //tr
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-09-13 16:50:19 +02:00
Ursprung 718247a3d4
Commit 71e51654ce
3 geänderte Dateien mit 98 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -30,17 +30,18 @@ import de.steamwar.bausystem.features.world.WorldEditListener;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import de.steamwar.bausystem.region.Color;
import de.steamwar.bausystem.shared.Pair;
import de.steamwar.bausystem.utils.WorldEditUtils;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import lombok.SneakyThrows;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
@Linked(LinkageType.COMMAND)
public class ColorReplaceCommand extends SWCommand {
@ -62,8 +63,8 @@ public class ColorReplaceCommand extends SWCommand {
@Register(description = "COLORREPLACE_HELP")
@SneakyThrows
public void genericCommand(Player player, Color from, Color to) {
if (from == to) {
public void genericCommand(Player player, Pair<Integer, Color> from, Color to) {
if (from.getValue() == to) {
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(0)}));
return;
}
@ -72,15 +73,51 @@ public class ColorReplaceCommand extends SWCommand {
Map<String, String> replacements = new HashMap<>();
for (String type : types) {
replacements.put("minecraft:" + from.name().toLowerCase() + "_" + type.toLowerCase(), "minecraft:" + to.name().toLowerCase() + "_" + type.toLowerCase());
replacements.put("minecraft:" + from.getValue().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, replacements);
SpecialReplace specialReplace = new SpecialReplace(editSession, replacements, from.getKey());
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 = Pair.class, local = true)
public TypeMapper<Pair<Integer, Color>> typeMapper() {
return new TypeMapper<Pair<Integer, Color>>() {
@Override
public Pair<Integer, Color> map(CommandSender commandSender, String[] previousArguments, String s) {
if (s.contains("%")) {
String[] split = s.split("%");
if (split.length != 2) {
return null;
}
Color color = Color.valueOf(split[1].toUpperCase());
try {
return new Pair<>(Integer.parseInt(split[0]), color);
} catch (NumberFormatException e) {
return null;
}
}
return new Pair<>(100, Color.valueOf(s.toUpperCase()));
}
@Override
public Collection<String> tabCompletes(CommandSender commandSender, String[] strings, String s) {
if (s.matches("[0-9].*")) {
if (s.contains("%")) {
String sub = s.substring(0, s.indexOf("%") + 1);
return Arrays.stream(Color.values()).map(Color::name).map(t -> sub + t.toLowerCase()).collect(Collectors.toSet());
} else {
return Arrays.asList(s);
}
} else {
return Arrays.stream(Color.values()).map(Color::name).map(String::toLowerCase).collect(Collectors.toSet());
}
}
};
}
}

Datei anzeigen

@ -33,27 +33,32 @@ import org.bukkit.block.data.BlockData;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.Random;
import java.util.function.BiConsumer;
public class SpecialReplace implements Mask, Pattern {
private EditSession editSession;
private Map<String, String> replacements;
private Random random = new Random();
private int percentage;
private BiConsumer<BlockData, BlockData> blockDataMerger;
public SpecialReplace(EditSession editSession, Map<String, String> replacements) {
this(editSession, replacements, (_1, _2) -> {
public SpecialReplace(EditSession editSession, Map<String, String> replacements, int percentage) {
this(editSession, replacements, percentage, (_1, _2) -> {
});
}
public SpecialReplace(EditSession editSession, Map<String, String> replacements, BiConsumer<BlockData, BlockData> blockDataMerger) {
public SpecialReplace(EditSession editSession, Map<String, String> replacements, int percentage, BiConsumer<BlockData, BlockData> blockDataMerger) {
this.editSession = editSession;
this.replacements = replacements;
this.percentage = percentage;
this.blockDataMerger = blockDataMerger;
}
@Override
public boolean test(BlockVector3 blockVector3) {
if (random.nextInt(100) > percentage) return false;
return replacements.containsKey(editSession.getFullBlock(blockVector3).getBlockType().getId());
}

Datei anzeigen

@ -30,8 +30,10 @@ import de.steamwar.bausystem.features.world.WorldEditListener;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import de.steamwar.bausystem.linkage.MinVersion;
import de.steamwar.bausystem.shared.Pair;
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;
@ -39,10 +41,14 @@ import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.Fence;
import org.bukkit.block.data.type.Wall;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import static org.bukkit.Material.*;
@ -61,8 +67,8 @@ public class TypeReplaceCommand extends SWCommand {
@Register(description = "TYPEREPLACE_HELP")
@SneakyThrows
public void genericCommand(Player player, Type from, Type to) {
if (from == to) {
public void genericCommand(Player player, Pair<Integer, Type> from, Type to) {
if (from.getValue() == to) {
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(0)}));
return;
}
@ -72,7 +78,7 @@ public class TypeReplaceCommand extends SWCommand {
Region region = WorldEditUtils.getRegion(player);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(BukkitAdapter.adapt(world), -1, BukkitAdapter.adapt(player));
SpecialReplace specialReplace = new SpecialReplace(editSession, to.replacements(from), (oldBlockData, newBlockData) -> {
SpecialReplace specialReplace = new SpecialReplace(editSession, to.replacements(from.getValue()), from.getKey(), (oldBlockData, newBlockData) -> {
if (newBlockData instanceof Fence && oldBlockData instanceof Wall) {
Wall wall = (Wall) oldBlockData;
Fence fence = (Fence) newBlockData;
@ -97,6 +103,42 @@ public class TypeReplaceCommand extends SWCommand {
BukkitAdapter.adapt(player).printInfo(TranslatableComponent.of("worldedit.replace.replaced", new Component[]{TextComponent.of(affected)}));
}
@ClassMapper(value = Pair.class, local = true)
public TypeMapper<Pair<Integer, Type>> typeMapper() {
return new TypeMapper<Pair<Integer, Type>>() {
@Override
public Pair<Integer, Type> map(CommandSender commandSender, String[] previousArguments, String s) {
if (s.contains("%")) {
String[] split = s.split("%");
if (split.length != 2) {
return null;
}
Type type = Type.valueOf(split[1].toUpperCase());
try {
return new Pair<>(Integer.parseInt(split[0]), type);
} catch (NumberFormatException e) {
return null;
}
}
return new Pair<>(100, Type.valueOf(s.toUpperCase()));
}
@Override
public Collection<String> tabCompletes(CommandSender commandSender, String[] strings, String s) {
if (s.matches("[0-9].*")) {
if (s.contains("%")) {
String sub = s.substring(0, s.indexOf("%") + 1);
return Arrays.stream(Type.values()).map(Type::name).map(t -> sub + t.toLowerCase()).collect(Collectors.toSet());
} else {
return Arrays.asList(s);
}
} else {
return Arrays.stream(Type.values()).map(Type::name).map(String::toLowerCase).collect(Collectors.toSet());
}
}
};
}
@AllArgsConstructor
private enum Type {
IRON(IRON_BLOCK, null, null, null, null, null, null, IRON_BARS, IRON_DOOR, IRON_TRAPDOOR, null, HEAVY_WEIGHTED_PRESSURE_PLATE, null, null, null, null),