From 76f1ea9cf219a3d0924ee4567ea509dbb171dead Mon Sep 17 00:00:00 2001 From: zml2008 Date: Wed, 28 Dec 2011 02:03:40 -0800 Subject: [PATCH] Added a gravity brush. --- .../worldedit/commands/BrushCommands.java | 40 +++++++++-- .../worldedit/tools/brushes/GravityBrush.java | 67 +++++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/tools/brushes/GravityBrush.java diff --git a/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java b/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java index 5ac569781..b8d092b7a 100644 --- a/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/BrushCommands.java @@ -36,12 +36,7 @@ import com.sk89q.worldedit.masks.BlockTypeMask; import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.patterns.SingleBlockPattern; import com.sk89q.worldedit.tools.BrushTool; -import com.sk89q.worldedit.tools.brushes.ClipboardBrush; -import com.sk89q.worldedit.tools.brushes.CylinderBrush; -import com.sk89q.worldedit.tools.brushes.HollowCylinderBrush; -import com.sk89q.worldedit.tools.brushes.HollowSphereBrush; -import com.sk89q.worldedit.tools.brushes.SmoothBrush; -import com.sk89q.worldedit.tools.brushes.SphereBrush; +import com.sk89q.worldedit.tools.brushes.*; /** * Brush shape commands. @@ -244,4 +239,37 @@ public class BrushCommands { player.print(String.format("Extinguisher equipped (%.0f).", radius)); } + + @Command( + aliases = { "gravity", "grav" }, + usage = "[radius]", + flags = "h", + desc = "Gravity brush", + help = + "This brush simulates the affect of gravity.\n" + + "The -h flag makes it affect blocks starting at the world's max y, " + + "instead of the clicked block's y + radius.", + min = 0, + max = 1 + ) + @CommandPermissions("worldedit.brush.gravity") + public void gravityBrush(CommandContext args, LocalSession session, + LocalPlayer player, EditSession editSession) throws WorldEditException { + + LocalConfiguration config = we.getConfiguration(); + + double radius = args.argsLength() > 0 ? args.getDouble(0) : 5; + if (radius > config.maxBrushRadius) { + player.printError("Maximum allowed brush radius: " + + config.maxBrushRadius); + return; + } + + BrushTool tool = session.getBrushTool(player.getItemInHand()); + tool.setSize(radius); + tool.setBrush(new GravityBrush(args.hasFlag('h')), "worldedit.brush.gravity"); + + player.print(String.format("Gravity brush equipped (%.0f).", + radius)); + } } diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/GravityBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/GravityBrush.java new file mode 100644 index 000000000..d9a8fd226 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/GravityBrush.java @@ -0,0 +1,67 @@ +/* + * WorldEdit + * Copyright (C) 2011 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.tools.brushes; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.MaxChangedBlocksException; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.blocks.BlockID; +import com.sk89q.worldedit.patterns.Pattern; + +import java.util.*; + +/** + * @author zml2008 + */ +public class GravityBrush implements Brush { + private final boolean fullHeight; + + public GravityBrush(boolean fullHeight) { + this.fullHeight = fullHeight; + } + + @Override + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { + final BaseBlock air = new BaseBlock(BlockID.AIR, 0); + final double startY = fullHeight ? editSession.getWorld().getMaxY() : pos.getBlockY() + size; + for (double x = pos.getBlockX() + size; x > pos.getBlockX() - size; --x) { + for (double z = pos.getBlockZ() + size; z > pos.getBlockZ() - size; --z) { + double y = startY; + final List blockTypes = new ArrayList(); + for (; y > pos.getBlockY() - size; --y) { + final Vector pt = new Vector(x, y, z); + final BaseBlock block = editSession.getBlock(pt); + if (!block.isAir()) { + blockTypes.add(block); + editSession.setBlock(pt, air); + } + } + Vector pt = new Vector(x, y, z); + Collections.reverse(blockTypes); + for (int i = 0; i < blockTypes.size();) { + if (editSession.getBlock(pt).getType() == BlockID.AIR) { + editSession.setBlock(pt, blockTypes.get(i++)); + } + pt = pt.add(0, 1, 0); + } + } + } + } +}