Improved the eraser debug tool (#4427)
Added shift clicking to delete the selected block and common blocks like dirt, stone, logs, leaves, water, lava, etc. Now limited to a +- 48 block cube around the origin.
This commit is contained in:
@@ -18,13 +18,17 @@
|
||||
|
||||
package appeng.debug;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.HashSet;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.ItemUseContext;
|
||||
import net.minecraft.tags.BlockTags;
|
||||
import net.minecraft.util.ActionResultType;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
@@ -34,7 +38,9 @@ import appeng.items.AEBaseItem;
|
||||
|
||||
public class ToolEraser extends AEBaseItem {
|
||||
|
||||
private static final int BLOCK_ERASE_LIMIT = 90000;
|
||||
private static final int BOX_SIZE = 48;
|
||||
private static final int BLOCK_ERASE_LIMIT = BOX_SIZE * BOX_SIZE * BOX_SIZE;
|
||||
final static Set<Block> COMMON_BLOCKS = new HashSet<>();
|
||||
|
||||
public ToolEraser(Properties properties) {
|
||||
super(properties);
|
||||
@@ -46,37 +52,49 @@ public class ToolEraser extends AEBaseItem {
|
||||
return ActionResultType.PASS;
|
||||
}
|
||||
|
||||
PlayerEntity player = context.getPlayer();
|
||||
World world = context.getWorld();
|
||||
BlockPos pos = context.getPos();
|
||||
final PlayerEntity player = context.getPlayer();
|
||||
final World world = context.getWorld();
|
||||
final BlockPos pos = context.getPos();
|
||||
|
||||
if (player == null) {
|
||||
return ActionResultType.PASS;
|
||||
}
|
||||
|
||||
final BlockState state = world.getBlockState(pos);
|
||||
final Block state = world.getBlockState(pos).getBlock();
|
||||
final boolean bulk = player.isSneaking();
|
||||
final Queue<BlockPos> next = new ArrayDeque<>();
|
||||
final Set<BlockPos> closed = new HashSet<>();
|
||||
final Set<Block> commonBlocks = this.getCommonBlocks();
|
||||
|
||||
List<BlockPos> next = new ArrayList<>();
|
||||
next.add(pos);
|
||||
|
||||
int blocks = 0;
|
||||
while (blocks < BLOCK_ERASE_LIMIT && !next.isEmpty()) {
|
||||
final List<BlockPos> c = next;
|
||||
next = new ArrayList<>();
|
||||
|
||||
for (final BlockPos wc : c) {
|
||||
final BlockState c_state = world.getBlockState(wc);
|
||||
while (blocks < BLOCK_ERASE_LIMIT && next.peek() != null) {
|
||||
final BlockPos wc = next.poll();
|
||||
final Block c_state = world.getBlockState(wc).getBlock();
|
||||
final boolean contains = state == c_state || (bulk && (commonBlocks.contains(c_state)));
|
||||
|
||||
if (state == c_state) {
|
||||
blocks++;
|
||||
world.removeBlock(wc, false);
|
||||
closed.add(wc);
|
||||
|
||||
next.add(wc.add(1, 0, 0));
|
||||
next.add(wc.add(-1, 0, 0));
|
||||
next.add(wc.add(0, 1, 0));
|
||||
next.add(wc.add(0, -1, 0));
|
||||
next.add(wc.add(0, 0, 1));
|
||||
next.add(wc.add(0, 0, -1));
|
||||
if (contains) {
|
||||
blocks++;
|
||||
world.setBlockState(wc, Blocks.AIR.getDefaultState(), 2);
|
||||
world.destroyBlock(wc, false);
|
||||
|
||||
if (isInsideBox(wc, pos)) {
|
||||
for (int x = -1; x <= 1; x++) {
|
||||
for (int y = -1; y <= 1; y++) {
|
||||
for (int z = -1; z <= 1; z++) {
|
||||
if (0 == x && 0 == y && 0 == z) {
|
||||
continue;
|
||||
}
|
||||
final BlockPos nextPos = wc.add(x, y, z);
|
||||
if (!closed.contains(nextPos)) {
|
||||
next.add(nextPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,4 +103,49 @@ public class ToolEraser extends AEBaseItem {
|
||||
|
||||
return ActionResultType.SUCCESS;
|
||||
}
|
||||
|
||||
private boolean isInsideBox(BlockPos pos, BlockPos origin) {
|
||||
boolean ret = true;
|
||||
|
||||
if (pos.getX() > origin.getX() + BOX_SIZE || pos.getX() < origin.getX() - BOX_SIZE) {
|
||||
ret = false;
|
||||
}
|
||||
if (pos.getY() > origin.getY() + BOX_SIZE || pos.getY() < origin.getY() - BOX_SIZE) {
|
||||
ret = false;
|
||||
}
|
||||
if (pos.getZ() > origin.getZ() + BOX_SIZE || pos.getZ() < origin.getZ() - BOX_SIZE) {
|
||||
ret = false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filling needs to be deferred as the tags might not be populated at
|
||||
* construction time.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Set<Block> getCommonBlocks() {
|
||||
if (COMMON_BLOCKS.isEmpty()) {
|
||||
COMMON_BLOCKS.add(Blocks.STONE);
|
||||
COMMON_BLOCKS.add(Blocks.DIRT);
|
||||
COMMON_BLOCKS.add(Blocks.GRASS_BLOCK);
|
||||
COMMON_BLOCKS.add(Blocks.COBBLESTONE);
|
||||
COMMON_BLOCKS.add(Blocks.ANDESITE);
|
||||
COMMON_BLOCKS.add(Blocks.GRANITE);
|
||||
COMMON_BLOCKS.add(Blocks.DIORITE);
|
||||
COMMON_BLOCKS.add(Blocks.GRAVEL);
|
||||
COMMON_BLOCKS.add(Blocks.SANDSTONE);
|
||||
COMMON_BLOCKS.add(Blocks.NETHERRACK);
|
||||
COMMON_BLOCKS.add(Blocks.WATER);
|
||||
COMMON_BLOCKS.add(Blocks.LAVA);
|
||||
|
||||
COMMON_BLOCKS.addAll(BlockTags.LEAVES.getAllElements());
|
||||
COMMON_BLOCKS.addAll(BlockTags.SAND.getAllElements());
|
||||
COMMON_BLOCKS.addAll(BlockTags.LOGS.getAllElements());
|
||||
}
|
||||
|
||||
return COMMON_BLOCKS;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user