// $Id$ /* * WorldEdit * Copyright (C) 2010 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.commands; import java.util.Set; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.Logging; import static com.sk89q.minecraft.util.commands.Logging.LogMode.*; import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.expression.ExpressionException; import com.sk89q.worldedit.filtering.GaussianKernel; import com.sk89q.worldedit.filtering.HeightMapFilter; import com.sk89q.worldedit.masks.Mask; import com.sk89q.worldedit.patterns.*; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.RegionOperationException; /** * Region related commands. * * @author sk89q */ public class RegionCommands { @Command( aliases = { "/set" }, usage = "", desc = "Set all the blocks inside the selection to a block", min = 1, max = 1 ) @CommandPermissions("worldedit.region.set") @Logging(REGION) public static void set(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Pattern pattern = we.getBlockPattern(player, args.getString(0)); int affected; if (pattern instanceof SingleBlockPattern) { affected = editSession.setBlocks(session.getSelection(player.getWorld()), ((SingleBlockPattern) pattern).getBlock()); } else { affected = editSession.setBlocks(session.getSelection(player.getWorld()), pattern); } player.print(affected + " block(s) have been changed."); } @Command( aliases = { "/replace" }, usage = "[from-block] ", desc = "Replace all blocks in the selection with another", flags = "f", min = 1, max = 2 ) @CommandPermissions("worldedit.region.replace") @Logging(REGION) public static void replace(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Set from; Pattern to; if (args.argsLength() == 1) { from = null; to = we.getBlockPattern(player, args.getString(0)); } else { from = we.getBlocks(player, args.getString(0), true, !args.hasFlag('f')); to = we.getBlockPattern(player, args.getString(1)); } int affected = 0; if (to instanceof SingleBlockPattern) { affected = editSession.replaceBlocks(session.getSelection(player.getWorld()), from, ((SingleBlockPattern) to).getBlock()); } else { affected = editSession.replaceBlocks(session.getSelection(player.getWorld()), from, to); } player.print(affected + " block(s) have been replaced."); } @Command( aliases = { "/overlay" }, usage = "", desc = "Set a block on top of blocks in the region", min = 1, max = 1 ) @CommandPermissions("worldedit.region.overlay") @Logging(REGION) public static void overlay(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Pattern pat = we.getBlockPattern(player, args.getString(0)); Region region = session.getSelection(player.getWorld()); int affected = 0; if (pat instanceof SingleBlockPattern) { affected = editSession.overlayCuboidBlocks(region, ((SingleBlockPattern) pat).getBlock()); } else { affected = editSession.overlayCuboidBlocks(region, pat); } player.print(affected + " block(s) have been overlayed."); } @Command( aliases = { "/naturalize" }, usage = "", desc = "3 layers of dirt on top then rock below", min = 0, max = 0 ) @CommandPermissions("worldedit.region.naturalize") @Logging(REGION) public static void naturalize(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Region region = session.getSelection(player.getWorld()); int affected = editSession.naturalizeCuboidBlocks(region); player.print(affected + " block(s) have been naturalized."); } @Command( aliases = { "/walls" }, usage = "", desc = "Build the four sides of the selection", min = 1, max = 1 ) @CommandPermissions("worldedit.region.walls") @Logging(REGION) public static void walls(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Pattern pattern = we.getBlockPattern(player, args.getString(0)); int affected; if (pattern instanceof SingleBlockPattern) { affected = editSession.makeCuboidWalls(session.getSelection(player.getWorld()), ((SingleBlockPattern) pattern).getBlock()); } else { affected = editSession.makeCuboidWalls(session.getSelection(player.getWorld()), pattern); } player.print(affected + " block(s) have been changed."); } @Command( aliases = { "/faces", "/outline" }, usage = "", desc = "Build the walls, ceiling, and floor of a selection", min = 1, max = 1 ) @CommandPermissions("worldedit.region.faces") @Logging(REGION) public static void faces(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Pattern pattern = we.getBlockPattern(player, args.getString(0)); int affected; if (pattern instanceof SingleBlockPattern) { affected = editSession.makeCuboidFaces(session.getSelection(player.getWorld()), ((SingleBlockPattern) pattern).getBlock()); } else { affected = editSession.makeCuboidFaces(session.getSelection(player.getWorld()), pattern); } player.print(affected + " block(s) have been changed."); } @Command( aliases = { "/smooth" }, usage = "[iterations]", flags = "n", desc = "Smooth the elevation in the selection", help = "Smooths the elevation in the selection.\n" + "The -n flag makes it only consider naturally occuring blocks.", min = 0, max = 1 ) @CommandPermissions("worldedit.region.smooth") @Logging(REGION) public static void smooth(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { int iterations = 1; if (args.argsLength() > 0) { iterations = args.getInteger(0); } HeightMap heightMap = new HeightMap(editSession, session.getSelection(player.getWorld()), args.hasFlag('n')); HeightMapFilter filter = new HeightMapFilter(new GaussianKernel(5, 1.0)); int affected = heightMap.applyFilter(filter, iterations); player.print("Terrain's height map smoothed. " + affected + " block(s) changed."); } @Command( aliases = { "/move" }, usage = "[count] [direction] [leave-id]", flags = "s", desc = "Move the contents of the selection", help = "Moves the contents of the selection.\n" + "The -s flag shifts the selection to the target location.\n" + "Optionally fills the old location with .", min = 0, max = 3 ) @CommandPermissions("worldedit.region.move") @Logging(ORIENTATION_REGION) public static void move(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { int count = args.argsLength() > 0 ? Math.max(1, args.getInteger(0)) : 1; Vector dir = we.getDirection(player, args.argsLength() > 1 ? args.getString(1).toLowerCase() : "me"); BaseBlock replace; // Replacement block argument if (args.argsLength() > 2) { replace = we.getBlock(player, args.getString(2)); } else { replace = new BaseBlock(BlockID.AIR); } int affected = editSession.moveCuboidRegion(session.getSelection(player.getWorld()), dir, count, true, replace); if (args.hasFlag('s')) { try { Region region = session.getSelection(player.getWorld()); region.expand(dir.multiply(count)); region.contract(dir.multiply(count)); session.getRegionSelector().learnChanges(); session.getRegionSelector().explainRegionAdjust(player, session); } catch (RegionOperationException e) { player.printError(e.getMessage()); } } player.print(affected + " blocks moved."); } @Command( aliases = { "/stack" }, usage = "[count] [direction]", flags = "sa", desc = "Repeat the contents of the selection", help = "Repeats the contents of the selection.\n" + "Flags:\n" + " -s shifts the selection to the last stacked copy\n" + " -a skips air blocks", min = 0, max = 2 ) @CommandPermissions("worldedit.region.stack") @Logging(ORIENTATION_REGION) public static void stack(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { int count = args.argsLength() > 0 ? Math.max(1, args.getInteger(0)) : 1; Vector dir = we.getDiagonalDirection(player, args.argsLength() > 1 ? args.getString(1).toLowerCase() : "me"); int affected = editSession.stackCuboidRegion(session.getSelection(player.getWorld()), dir, count, !args.hasFlag('a')); if (args.hasFlag('s')) { try { final Region region = session.getSelection(player.getWorld()); final Vector size = region.getMaximumPoint().subtract(region.getMinimumPoint()); final Vector shiftVector = dir.multiply(count * Math.abs(size.dot(size))); region.expand(shiftVector); region.contract(shiftVector); session.getRegionSelector().learnChanges(); session.getRegionSelector().explainRegionAdjust(player, session); } catch (RegionOperationException e) { player.printError(e.getMessage()); } } player.print(affected + " blocks changed. Undo with //undo"); } @Command( aliases = { "/regen" }, usage = "", desc = "Regenerates the contents of the selection", help = "Regenerates the contents of the current selection.\n" + "This command might affect things outside the selection,\n" + "if they are within the same chunk.", min = 0, max = 0 ) @CommandPermissions("worldedit.regen") @Logging(REGION) public static void regenerateChunk(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { Region region = session.getSelection(player.getWorld()); Mask mask = session.getMask(); session.setMask(null); player.getWorld().regenerate(region, editSession); session.setMask(mask); player.print("Region regenerated."); } @Command( aliases = { "/deform" }, usage = "", desc = "Deforms a selected region with an expression", help = "Deforms a selected region with an expression\n" + "The expression is executed for each block and is expected\n" + "to modify the variables x, y and z to point to a new block\n" + "to fetch. See also tinyurl.com/wesyntax.", flags = "ro", min = 1, max = -1 ) @CommandPermissions("worldedit.region.deform") @Logging(ALL) public static void deform(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { final Region region = session.getSelection(player.getWorld()); final String expression = args.getJoinedStrings(0); final Vector zero; Vector unit; if (args.hasFlag('r')) { zero = new Vector(0, 0, 0); unit = new Vector(1, 1, 1); } else if (args.hasFlag('o')) { zero = session.getPlacementPosition(player); unit = new Vector(1, 1, 1); } else { final Vector min = region.getMinimumPoint(); final Vector max = region.getMaximumPoint(); zero = max.add(min).multiply(0.5); unit = max.subtract(zero); if (unit.getX() == 0) unit = unit.setX(1.0); if (unit.getY() == 0) unit = unit.setY(1.0); if (unit.getZ() == 0) unit = unit.setZ(1.0); } try { final int affected = editSession.deformRegion(region, zero, unit, expression); player.findFreePosition(); player.print(affected + " block(s) have been deformed."); } catch (ExpressionException e) { player.printError(e.getMessage()); } } }