Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clearblocks.js CraftScript #1370

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions contrib/craftscripts/clearblocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Marks all blocks that have at least 3 exposed sides (adjacent to an air block)
// and then deletes those blocks.
// Based on jsa's MCEdit Filter clearBlocks.py

importClass(Packages.com.sk89q.worldedit.world.block.BlockTypes)

context.checkArgs(0, -1, "")

var blocks = context.remember()
JsKingBoo marked this conversation as resolved.
Show resolved Hide resolved
var region = context.getSession().getRegionSelector(player.getWorld()).getRegion()

var min = region.getMinimumPoint()
var xi = region.getWidth()
var yi = region.getHeight()
var zi = region.getLength()
JsKingBoo marked this conversation as resolved.
Show resolved Hide resolved

function sidesExposed(block) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire function can be replaced by the #surface mask, don't recall the exact implementing class but it should be sufficient. I would rather not add more clunky code for us to maintain in the craftscripts dir.

if (blocks.getBlock(block).getBlockType().equals(BlockTypes.AIR)) {
return 0
}
var sides = 0
if (blocks.getBlock(block.add(0, 0, 1)).getBlockType().equals(BlockTypes.AIR)) {
sides += 1
}
if (blocks.getBlock(block.add(0, 1, 0)).getBlockType().equals(BlockTypes.AIR)) {
sides += 1
}
if (blocks.getBlock(block.add(1, 0, 0)).getBlockType().equals(BlockTypes.AIR)) {
sides += 1
}
if (blocks.getBlock(block.add(0, 0, -1)).getBlockType().equals(BlockTypes.AIR)) {
sides += 1
}
if (blocks.getBlock(block.add(0, -1, 0)).getBlockType().equals(BlockTypes.AIR)) {
sides += 1
}
if (blocks.getBlock(block.add(-1, 0, 0)).getBlockType().equals(BlockTypes.AIR)) {
sides += 1
}
return sides
}

var destroyThese = []

for (var i = 0; i < xi; i += 1) {
for (var j = 0; j < zi; j += 1) {
for (var k = 0; k < yi; k += 1) {
var block = min.add(i, k, j)
if (sidesExposed(block) >= 3) {
destroyThese.push(block)
}
}
}
}

for (var i = 0; i < destroyThese.length; i += 1) {
blocks.setBlock(destroyThese[i], BlockTypes.AIR.getDefaultState())
}

context.print("[Clear Blocks] " + destroyThese.length + " blocks cleared.")