Skip to content

Commit

Permalink
Added selection setting API.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk89q committed Apr 3, 2011
1 parent fd2de1d commit fc3531c
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 14 deletions.
34 changes: 32 additions & 2 deletions src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java
Expand Up @@ -353,6 +353,13 @@ public WorldEdit getWorldEdit() {
* @return the selection or null if there was none
*/
public Selection getSelection(Player player) {
if (player == null) {
throw new IllegalArgumentException("Null player not allowed");
}
if (!player.isOnline()) {
throw new IllegalArgumentException("Offline player not allowed");
}

LocalSession session = controller.getSession(wrapPlayer(player));
RegionSelector selector = session.getRegionSelector();

Expand All @@ -361,14 +368,37 @@ public Selection getSelection(Player player) {
World world = ((BukkitWorld) session.getSelectionWorld()).getWorld();

if (region instanceof CuboidRegion) {
return new CuboidSelection(world, (CuboidRegion)region);
return new CuboidSelection(world, selector, (CuboidRegion)region);
} else if (region instanceof Polygonal2DRegion) {
return new Polygonal2DSelection(world, (Polygonal2DRegion)region);
return new Polygonal2DSelection(world, selector, (Polygonal2DRegion)region);
} else {
return null;
}
} catch (IncompleteRegionException e) {
return null;
}
}

/**
* Sets the region selection for a player.
*
* @param player
* @param selection
*/
public void setSelection(Player player, Selection selection) {
if (player == null) {
throw new IllegalArgumentException("Null player not allowed");
}
if (!player.isOnline()) {
throw new IllegalArgumentException("Offline player not allowed");
}
if (selection == null) {
throw new IllegalArgumentException("Null selection not allowed");
}

LocalSession session = controller.getSession(wrapPlayer(player));
RegionSelector sel = selection.getRegionSelector();
session.setRegionSelector(new BukkitWorld(player.getWorld()), sel);
session.dispatchCUISelection(wrapPlayer(player));
}
}
38 changes: 35 additions & 3 deletions src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java
Expand Up @@ -19,16 +19,48 @@

package com.sk89q.worldedit.bukkit.selections;

import org.bukkit.Location;
import org.bukkit.World;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bukkit.BukkitUtil;
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;
public CuboidSelection(World world, Location pt1, Location pt2) {
this(world, BukkitUtil.toVector(pt1), BukkitUtil.toVector(pt2));
}

public CuboidSelection(World world, Vector pt1, Vector pt2) {
super(world);

if (pt1 == null) {
throw new IllegalArgumentException("Null point 1 not permitted");
}

if (pt2 == null) {
throw new IllegalArgumentException("Null point 2 not permitted");
}

CuboidRegionSelector sel = new CuboidRegionSelector();
sel.selectPrimary(pt1);
sel.selectSecondary(pt2);

try {
cuboid = sel.getRegion();
} catch (IncompleteRegionException e) {
throw new RuntimeException("IncompleteRegionException unexpectedly thrown");
}

setRegionSelector(sel);
setRegion(cuboid);
}

public CuboidSelection(World world, RegionSelector sel, CuboidRegion region) {
super(world, sel, region);
this.cuboid = region;
}
}
Expand Up @@ -19,6 +19,7 @@

package com.sk89q.worldedit.bukkit.selections;

import java.util.Collections;
import java.util.List;
import org.bukkit.World;
import com.sk89q.worldedit.BlockVector2D;
Expand All @@ -28,13 +29,38 @@ public class Polygonal2DSelection extends RegionSelection {

protected Polygonal2DRegion poly2d;

public Polygonal2DSelection(World world, Polygonal2DRegion region) {
super(world, region);
this.world = world;
public Polygonal2DSelection(World world, RegionSelector sel, Polygonal2DRegion region) {
super(world, sel, region);
this.poly2d = region;
}

public Polygonal2DSelection(World world, List<BlockVector2D> points, int minY, int maxY) {
super(world);

minY = Math.min(Math.max(0, minY), 127);
maxY = Math.min(Math.max(0, maxY), 127);

Polygonal2DRegionSelector sel = new Polygonal2DRegionSelector();
poly2d = sel.getIncompleteRegion();

for (BlockVector2D pt : points) {
if (pt == null) {
throw new IllegalArgumentException("Null point not permitted");
}

poly2d.addPoint(pt);
}

poly2d.setMinimumY(minY);
poly2d.setMaximumY(maxY);

sel.learnChanges();

setRegionSelector(sel);
setRegion(poly2d);
}

public List<BlockVector2D> getNativePoints() {
return poly2d.getPoints();
return Collections.unmodifiableList(poly2d.getPoints());
}
}
29 changes: 26 additions & 3 deletions src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java
Expand Up @@ -25,16 +25,39 @@
import org.bukkit.World;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;

public abstract class RegionSelection implements Selection {

protected World world;
protected Region region;
private World world;
private RegionSelector selector;
private Region region;

public RegionSelection(World world, Region region) {
public RegionSelection(World world) {
this.world = world;
}

public RegionSelection(World world, RegionSelector selector, Region region) {
this.world = world;
this.region = region;
this.selector = selector;
}

protected Region getRegion() {
return region;
}

protected void setRegion(Region region) {
this.region = region;
}

public RegionSelector getRegionSelector() {
return selector;
}

protected void setRegionSelector(RegionSelector selector) {
this.selector = selector;
}

@Override
public Location getMinimumPoint() {
Expand Down
8 changes: 8 additions & 0 deletions src/com/sk89q/worldedit/bukkit/selections/Selection.java
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.Location;
import org.bukkit.World;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.RegionSelector;

public interface Selection {
/**
Expand Down Expand Up @@ -52,6 +53,13 @@ public interface Selection {
*/
public Vector getNativeMaximumPoint();

/**
* Get the region selector. This is for internal use.
*
* @return
*/
public RegionSelector getRegionSelector();

/**
* Get the world.
*
Expand Down
6 changes: 5 additions & 1 deletion src/com/sk89q/worldedit/regions/CuboidRegionSelector.java
Expand Up @@ -106,14 +106,18 @@ public boolean isDefined() {
return pos1 != null && pos2 != null;
}

public Region getRegion() throws IncompleteRegionException {
public CuboidRegion getRegion() throws IncompleteRegionException {
if (pos1 == null || pos2 == null) {
throw new IncompleteRegionException();
}

return region;
}

public CuboidRegion getIncompleteRegion() {
return region;
}

public void learnChanges() {
pos1 = region.getPos1().toBlockVector();
pos2 = region.getPos2().toBlockVector();
Expand Down
22 changes: 22 additions & 0 deletions src/com/sk89q/worldedit/regions/Polygonal2DRegion.java
Expand Up @@ -142,6 +142,28 @@ public void addPoint(Vector pt) {
points.add(new BlockVector2D(pt.getBlockX(), pt.getBlockZ()));
recalculate();
}

/**
* Set the minimum Y.
*
* @param y
*/
public void setMinimumY(int y) {
hasY = true;
minY = y;
recalculate();
}

/**
* Se the maximum Y.
*
* @param y
*/
public void setMaximumY(int y) {
hasY = true;
maxY = y;
recalculate();
}

/**
* Get the lower point of a region.
Expand Down
13 changes: 12 additions & 1 deletion src/com/sk89q/worldedit/regions/Polygonal2DRegionSelector.java
Expand Up @@ -87,19 +87,26 @@ public BlockVector getPrimaryPosition() throws IncompleteRegionException {
return pos1;
}

public Region getRegion() throws IncompleteRegionException {
public Polygonal2DRegion getRegion() throws IncompleteRegionException {
if (!isDefined()) {
throw new IncompleteRegionException();
}

return region;
}

public Polygonal2DRegion getIncompleteRegion() {
return region;
}

public boolean isDefined() {
return region.size() > 2;
}

public void learnChanges() {
BlockVector2D pt = region.getPoints().get(0);
pos1 = new BlockVector(pt.getBlockX(),
region.getMinimumPoint().getBlockY(), pt.getBlockZ());
}

public void clear() {
Expand All @@ -124,5 +131,9 @@ public String getTypeId() {
public int getArea() {
return region.getArea();
}

public int getPointCount() {
return region.getPoints().size();
}

}
7 changes: 7 additions & 0 deletions src/com/sk89q/worldedit/regions/RegionSelector.java
Expand Up @@ -93,6 +93,13 @@ public void explainSecondarySelection(LocalPlayer player,
*/
public Region getRegion() throws IncompleteRegionException;

/**
* Get the region even if it's not fully defined.
*
* @return
*/
public Region getIncompleteRegion();

/**
* Returns whether the region has been fully defined.
*
Expand Down

0 comments on commit fc3531c

Please sign in to comment.