Improve PasteBuilder

This commit is contained in:
2024-09-17 16:51:31 +02:00
parent dbe6d40e36
commit 11a933fede
@@ -24,6 +24,7 @@ import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.pattern.WaterloggedRemover;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import de.steamwar.bausystem.region.Color;
import de.steamwar.bausystem.region.Point;
@@ -34,9 +35,7 @@ import lombok.NonNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
@@ -106,51 +105,28 @@ public class PasteBuilder {
public PasteBuilder color(Color color) {
if (color == Color.PINK) return this;
BaseBlock WOOL = Objects.requireNonNull(BlockTypes.PINK_WOOL).getDefaultState().toBaseBlock();
BaseBlock CLAY = Objects.requireNonNull(BlockTypes.PINK_TERRACOTTA).getDefaultState().toBaseBlock();
BaseBlock GLAZED = Objects.requireNonNull(BlockTypes.PINK_GLAZED_TERRACOTTA).getDefaultState().toBaseBlock();
BaseBlock GLASS = Objects.requireNonNull(BlockTypes.PINK_STAINED_GLASS).getDefaultState().toBaseBlock();
BaseBlock GLASS_PANE = Objects.requireNonNull(BlockTypes.PINK_STAINED_GLASS_PANE).getDefaultState().toBaseBlock();
BaseBlock CONCRETE = Objects.requireNonNull(BlockTypes.PINK_CONCRETE).getDefaultState().toBaseBlock();
BaseBlock CONCRETE_POWDER = Objects.requireNonNull(BlockTypes.PINK_CONCRETE_POWDER).getDefaultState().toBaseBlock();
BaseBlock CARPET = Objects.requireNonNull(BlockTypes.PINK_CARPET).getDefaultState().toBaseBlock();
BaseBlock wool = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_wool")).getDefaultState().toBaseBlock();
BaseBlock clay = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_terracotta")).getDefaultState().toBaseBlock();
BaseBlock glazed = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_glazed_terracotta")).getDefaultState().toBaseBlock();
BaseBlock glass = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_stained_glass")).getDefaultState().toBaseBlock();
BaseBlock glassPane = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_stained_glass_pane")).getDefaultState().toBaseBlock();
BaseBlock carpet = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_carpet")).getDefaultState().toBaseBlock();
BaseBlock concrete = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_concrete")).getDefaultState().toBaseBlock();
BaseBlock concretePowder = Objects.requireNonNull(BlockTypes.get(color.name().toLowerCase() + "_concrete_powder")).getDefaultState().toBaseBlock();
Map<String, BaseBlock> blockCache = new HashMap<>();
return map((clipboard, blockVector3) -> {
BaseBlock block = clipboard.getFullBlock(blockVector3);
if (block.equals(WOOL)) {
clipboard.setBlock(blockVector3, wool);
} else if (block.equals(CLAY)) {
clipboard.setBlock(blockVector3, clay);
} else if (block.equals(GLAZED)) {
clipboard.setBlock(blockVector3, glazed);
} else if (block.equals(GLASS)) {
clipboard.setBlock(blockVector3, glass);
} else if (block.equals(GLASS_PANE)) {
clipboard.setBlock(blockVector3, glassPane);
} else if (block.equals(CARPET)) {
clipboard.setBlock(blockVector3, carpet);
} else if (block.equals(CONCRETE)) {
clipboard.setBlock(blockVector3, concrete);
} else if (block.equals(CONCRETE_POWDER)) {
clipboard.setBlock(blockVector3, concretePowder);
if (block.getBlockType().getId().startsWith("minecraft:pink_")) {
BaseBlock baseBlock = blockCache.computeIfAbsent(block.getBlockType().getId(), s -> {
String replaced = s.replace("minecraft:pink_", "minecraft:" + color.name().toLowerCase() + "_");
BlockType blockType = BlockTypes.get(replaced);
if (blockType == null) return null;
return blockType.getDefaultState().toBaseBlock();
});
if (baseBlock == null) return;
clipboard.setBlock(blockVector3, baseBlock);
}
});
}
/**
* Can only be used before {@link #color(Color)}.
*/
public PasteBuilder onlyColors(boolean onlyColors) {
if (!onlyColors) return this;
return only((baseBlock, s) -> {
return s.endsWith("_wool") || s.endsWith("_terracotta") || s.endsWith("_glazed_terracotta") || s.endsWith("_stained_glass") || s.endsWith("_stained_glass_pane") || s.endsWith("_carpet") || s.endsWith("_concrete") || s.endsWith("_concrete_powder");
});
return only((baseBlock, s) -> s.startsWith("minecraft:pink_"));
}
public PasteBuilder removeTNT(boolean removeTNT) {