From 3b29403a9beccde0fea13eff35dcd206975df5f1 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 12 Mar 2011 00:28:19 -0800 Subject: [PATCH] Add a com.sk89q.worldedit.bukkit.WorldEditPlugin.getSelection(Player player) method to return the user's current selection utilizing native Bukkit types. --- src/com/sk89q/worldedit/LocalSession.java | 9 ++ .../worldedit/bukkit/WorldEditPlugin.java | 43 ++++++---- .../bukkit/selections/CuboidSelection.java | 34 ++++++++ .../selections/Polygonal2DSelection.java | 34 ++++++++ .../bukkit/selections/RegionSelection.java | 82 +++++++++++++++++++ .../bukkit/selections/Selection.java | 82 +++++++++++++++++++ 6 files changed, 270 insertions(+), 14 deletions(-) create mode 100644 src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java create mode 100644 src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java create mode 100644 src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java create mode 100644 src/com/sk89q/worldedit/bukkit/selections/Selection.java diff --git a/src/com/sk89q/worldedit/LocalSession.java b/src/com/sk89q/worldedit/LocalSession.java index 8fe4ea059..41a4d503d 100644 --- a/src/com/sk89q/worldedit/LocalSession.java +++ b/src/com/sk89q/worldedit/LocalSession.java @@ -231,6 +231,15 @@ public class LocalSession { } return selector.getRegion(); } + + /** + * Get the selection world. + * + * @return + */ + public LocalWorld getSelectionWorld() { + return selectionWorld; + } /** * Gets the clipboard. diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index a8bcd44cd..b2c0c16ad 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -24,6 +24,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.logging.Logger; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -37,7 +38,8 @@ import com.sk89q.bukkit.migration.PermissionsResolverManager; import com.sk89q.bukkit.migration.PermissionsResolverServerListener; import com.sk89q.worldedit.*; import com.sk89q.worldedit.bags.BlockBag; -import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.bukkit.selections.*; +import com.sk89q.worldedit.regions.*; /** * Plugin for Bukkit. @@ -246,19 +248,6 @@ public class WorldEditPlugin extends JavaPlugin { return controller.getSession(wrapPlayer(player)); } - /** - * Gets the region selection for the player. - * - * @param player - * @return - * @throws IncompleteRegionException - */ - public Region getPlayerSelection(Player player) - throws IncompleteRegionException { - return controller.getSession(wrapPlayer(player)) - .getSelection(new BukkitWorld(player.getWorld())); - } - /** * Gets the session for the player. * @@ -351,4 +340,30 @@ public class WorldEditPlugin extends JavaPlugin { public BukkitPlayer wrapPlayer(Player player) { return new BukkitPlayer(this, this.server, player); } + + /** + * Gets the region selection for the player. + * + * @param player + * @return the selection or null if there was none + */ + public Selection getSelection(Player player) { + LocalSession session = controller.getSession(wrapPlayer(player)); + RegionSelector selector = session.getRegionSelector(); + + try { + Region region = selector.getRegion(); + World world = ((BukkitWorld) session.getSelectionWorld()).getWorld(); + + if (region instanceof CuboidRegion) { + return new CuboidSelection(world, (CuboidRegion)region); + } else if (region instanceof Polygonal2DRegion) { + return new Polygonal2DSelection(world, (Polygonal2DRegion)region); + } else { + return null; + } + } catch (IncompleteRegionException e) { + return null; + } + } } diff --git a/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java b/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java new file mode 100644 index 000000000..fbb81b6f9 --- /dev/null +++ b/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java @@ -0,0 +1,34 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 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.bukkit.selections; + +import org.bukkit.World; +import com.sk89q.worldedit.regions.*; + +public class CuboidSelection extends RegionSelection { + + protected CuboidRegion cuboid; + + public CuboidSelection(World world, CuboidRegion region) { + super(world, region); + this.world = world; + this.cuboid = region; + } +} diff --git a/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java b/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java new file mode 100644 index 000000000..08dc89553 --- /dev/null +++ b/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java @@ -0,0 +1,34 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 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.bukkit.selections; + +import org.bukkit.World; +import com.sk89q.worldedit.regions.*; + +public class Polygonal2DSelection extends RegionSelection { + + protected Polygonal2DRegion poly2d; + + public Polygonal2DSelection(World world, Polygonal2DRegion region) { + super(world, region); + this.world = world; + this.poly2d = region; + } +} diff --git a/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java b/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java new file mode 100644 index 000000000..78c95cbe3 --- /dev/null +++ b/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java @@ -0,0 +1,82 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 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.bukkit.selections; + +import static com.sk89q.worldedit.bukkit.BukkitUtil.toLocation; +import static com.sk89q.worldedit.bukkit.BukkitUtil.toVector; +import org.bukkit.Location; +import org.bukkit.World; +import com.sk89q.worldedit.regions.Region; + +public abstract class RegionSelection implements Selection { + + protected World world; + protected Region region; + + public RegionSelection(World world, Region region) { + this.world = world; + this.region = region; + } + + @Override + public Location getMinimumPoint() { + return toLocation(world, region.getMinimumPoint()); + } + + @Override + public Location getMaximumPoint() { + return toLocation(world, region.getMaximumPoint()); + } + + @Override + public World getWorld() { + return world; + } + + @Override + public int getArea() { + return region.getArea(); + } + + @Override + public int getWidth() { + return region.getWidth(); + } + + @Override + public int getHeight() { + return region.getHeight(); + } + + @Override + public int getLength() { + return region.getLength(); + } + + @Override + public boolean contains(Location pt) { + if (!pt.getWorld().equals(world)) { + return false; + } + + return region.contains(toVector(pt)); + } + +} diff --git a/src/com/sk89q/worldedit/bukkit/selections/Selection.java b/src/com/sk89q/worldedit/bukkit/selections/Selection.java new file mode 100644 index 000000000..47a4264b1 --- /dev/null +++ b/src/com/sk89q/worldedit/bukkit/selections/Selection.java @@ -0,0 +1,82 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 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.bukkit.selections; + +import org.bukkit.Location; +import org.bukkit.World; + +public interface Selection { + /** + * Get the lower point of a region. + * + * @return min. point + */ + public Location getMinimumPoint(); + + /** + * Get the upper point of a region. + * + * @return max. point + */ + public Location getMaximumPoint(); + + /** + * Get the world. + * + * @return + */ + public World getWorld(); + + /** + * Get the number of blocks in the region. + * + * @return number of blocks + */ + public int getArea(); + + /** + * Get X-size. + * + * @return width + */ + public int getWidth(); + + /** + * Get Y-size. + * + * @return height + */ + public int getHeight(); + + /** + * Get Z-size. + * + * @return length + */ + public int getLength(); + + /** + * Returns true based on whether the region contains the point, + * + * @param pt + * @return + */ + public boolean contains(Location pt); +}