Changed Location to use Extents rather than worlds and overhauled the new Entity code a bit.

This commit is contained in:
sk89q
2014-06-29 15:36:41 -07:00
parent c9612c05a7
commit eee2c5d9f4
24 changed files with 312 additions and 158 deletions

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit;
import com.sk89q.worldedit.internal.LocalWorldAdapter;
import com.sk89q.worldedit.world.World;
/**
* @deprecated Use {@link com.sk89q.worldedit.util.Location} wherever possible
@ -97,7 +98,7 @@ public class WorldVector extends Vector {
* @param location the location
*/
public WorldVector(com.sk89q.worldedit.util.Location location) {
this(LocalWorldAdapter.adapt(location.getWorld()), location.getX(), location.getY(), location.getZ());
this(LocalWorldAdapter.adapt((World) location.getExtent()), location.getX(), location.getY(), location.getZ());
}
/**

View File

@ -25,6 +25,7 @@ import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.world.World;
/**
* A super pickaxe mode that will remove blocks in an area.
@ -48,7 +49,7 @@ public class AreaPickaxe implements BlockTool {
int ox = clicked.getBlockX();
int oy = clicked.getBlockY();
int oz = clicked.getBlockZ();
int initialType = clicked.getWorld().getBlockType(clicked.toVector());
int initialType = ((World) clicked.getExtent()).getBlockType(clicked.toVector());
if (initialType == 0) {
return true;
@ -70,7 +71,7 @@ public class AreaPickaxe implements BlockTool {
continue;
}
clicked.getWorld().queueBlockBreakEffect(server, pos, initialType, clicked.toVector().distanceSq(pos));
((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType, clicked.toVector().distanceSq(pos));
editSession.setBlock(pos, air);
}

View File

@ -41,7 +41,7 @@ public class BlockDataCyler implements DoubleActionBlockTool {
private boolean handleCycle(Platform server, LocalConfiguration config,
Player player, LocalSession session, Location clicked, boolean forward) {
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
int type = world.getBlockType(clicked.toVector());
int data = world.getBlockData(clicked.toVector());

View File

@ -48,7 +48,7 @@ public class BlockReplacer implements DoubleActionBlockTool {
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
BlockBag bag = session.getBlockBag(player);
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1, bag, player);
try {
@ -67,7 +67,7 @@ BlockBag bag = session.getBlockBag(player);
@Override
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1, player);
targetBlock = (editSession).getBlock(clicked.toVector());
BlockType type = BlockType.fromID(targetBlock.getType());

View File

@ -53,7 +53,7 @@ public class FloatingTreeRemover implements BlockTool {
public boolean actPrimary(Platform server, LocalConfiguration config,
Player player, LocalSession session, Location clicked) {
final World world = clicked.getWorld();
final World world = (World) clicked.getExtent();
switch (world.getBlockType(clicked.toVector())) {
case BlockID.LOG:

View File

@ -51,7 +51,7 @@ public class FloodFillTool implements BlockTool {
@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, Location clicked) {
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
int initialType = world.getBlockType(clicked.toVector());

View File

@ -42,7 +42,7 @@ public class QueryTool implements BlockTool {
@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, 0, player);
BaseBlock block = (editSession).rawGetBlock(clicked.toVector());
BlockType type = BlockType.fromID(block.getType());

View File

@ -50,7 +50,7 @@ public class RecursivePickaxe implements BlockTool {
@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
int initialType = world.getBlockType(clicked.toVector());

View File

@ -42,7 +42,7 @@ public class SinglePickaxe implements BlockTool {
@Override
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
World world = clicked.getWorld();
World world = (World) clicked.getExtent();
final int blockType = world.getBlockType(clicked.toVector());
if (blockType == BlockID.BEDROCK
&& !player.canDestroyBedrock()) {

View File

@ -1,51 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.entity;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.world.DataException;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An abstract implementation of {@link BaseEntity} that implementations can
* subclass to simplify implementation.
*/
public abstract class AbstractBaseEntity implements BaseEntity {
private CompoundTag nbtData;
@Override
public boolean hasNbtData() {
return getNbtData() != null;
}
@Override
public CompoundTag getNbtData() {
return nbtData;
}
@Override
public void setNbtData(CompoundTag nbtData) throws DataException {
checkNotNull(nbtData);
this.nbtData = nbtData;
}
}

View File

@ -19,11 +19,74 @@
package com.sk89q.worldedit.entity;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.world.NbtValued;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A snapshot of an entity that can be reused and passed around.
*/
public interface BaseEntity extends NbtValued {
public class BaseEntity implements NbtValued {
private String id;
private CompoundTag nbtData;
/**
* Create a new base entity.
*
* @param id the entity type ID
* @param nbtData NBT data
*/
public BaseEntity(String id, CompoundTag nbtData) {
setTypeId(id);
setNbtData(nbtData);
}
/**
* Make a clone of a {@link BaseEntity}.
*
* @param other the object to clone
*/
public BaseEntity(BaseEntity other) {
checkNotNull(other);
setTypeId(other.getTypeId());
setNbtData(other.getNbtData());
}
@Override
public boolean hasNbtData() {
return true;
}
@Override
public CompoundTag getNbtData() {
return nbtData;
}
@Override
public void setNbtData(CompoundTag nbtData) {
checkNotNull(nbtData);
this.nbtData = nbtData;
}
/**
* Get the entity that determines the type of entity.
*
* @return the entity ID
*/
public String getTypeId() {
return id;
}
/**
* Set the entity ID that determines the type of entity.
*
* @param id the id
*/
public void setTypeId(String id) {
checkNotNull(id);
this.id = id;
}
}

View File

@ -21,7 +21,6 @@ package com.sk89q.worldedit.entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
/**
* A reference to an instance of an entity that exists in an {@link Extent}
@ -50,10 +49,17 @@ public interface Entity {
Location getLocation();
/**
* Get the world that this entity is on.
* Get the extent that this entity is on.
*
* @return the world
* @return the extent
*/
World getWorld();
Extent getExtent();
/**
* Remove this entity from it container.
*
* @return true if removal was successful
*/
boolean remove();
}

View File

@ -19,16 +19,28 @@
package com.sk89q.worldedit.entity;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.PlayerDirection;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.WorldVectorFace;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.world.World;
/**
* A player.
*/
public interface Player extends Entity, Actor {
/**
* Return the world that the player is on.
*
* @return the world
*/
World getWorld();
/**
* Returns true if the entity is holding a pick axe.
*

View File

@ -19,12 +19,21 @@
package com.sk89q.worldedit.extension.platform;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.BlockWorldVector;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.NotABlockException;
import com.sk89q.worldedit.PlayerDirection;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.WorldEditPermissionException;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.WorldVectorFace;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.ItemID;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.util.TargetBlock;
import com.sk89q.worldedit.world.World;
@ -36,7 +45,12 @@ import java.io.File;
* that is intended for implementations of WorldEdit to use to wrap
* players that make use of WorldEdit.
*/
public abstract class AbstractPlayerActor implements Actor, Player {
public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override
public final Extent getExtent() {
return getWorld();
}
/**
* Returns direction according to rotation. May return null.
@ -466,4 +480,15 @@ public abstract class AbstractPlayerActor implements Actor, Player {
return false;
}
@SuppressWarnings("CloneDoesntCallSuperClone")
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Not supported");
}
@Override
public boolean remove() {
return false;
}
}

View File

@ -19,13 +19,21 @@
package com.sk89q.worldedit.internal.command;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.BiomeType;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.UnknownDirectionException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.annotation.Direction;
@ -33,7 +41,12 @@ import com.sk89q.worldedit.internal.annotation.Selection;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.util.command.parametric.*;
import com.sk89q.worldedit.util.command.parametric.ArgumentStack;
import com.sk89q.worldedit.util.command.parametric.BindingBehavior;
import com.sk89q.worldedit.util.command.parametric.BindingHelper;
import com.sk89q.worldedit.util.command.parametric.BindingMatch;
import com.sk89q.worldedit.util.command.parametric.ParameterException;
import com.sk89q.worldedit.world.World;
import java.util.Arrays;
@ -158,7 +171,10 @@ public class WorldEditBinding extends BindingHelper {
ParserContext parserContext = new ParserContext();
parserContext.setActor(context.getContext().getLocals().get(Actor.class));
if (actor instanceof Entity) {
parserContext.setWorld(((Entity) actor).getWorld());
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
}
parserContext.setSession(worldEdit.getSessionManager().get(actor));
try {
@ -184,7 +200,10 @@ public class WorldEditBinding extends BindingHelper {
ParserContext parserContext = new ParserContext();
parserContext.setActor(context.getContext().getLocals().get(Actor.class));
if (actor instanceof Entity) {
parserContext.setWorld(((Entity) actor).getWorld());
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
}
parserContext.setSession(worldEdit.getSessionManager().get(actor));
try {
@ -210,7 +229,10 @@ public class WorldEditBinding extends BindingHelper {
ParserContext parserContext = new ParserContext();
parserContext.setActor(context.getContext().getLocals().get(Actor.class));
if (actor instanceof Entity) {
parserContext.setWorld(((Entity) actor).getWorld());
Extent extent = ((Entity) actor).getExtent();
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
}
parserContext.setSession(worldEdit.getSessionManager().get(actor));
try {

View File

@ -20,7 +20,7 @@
package com.sk89q.worldedit.util;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.extent.Extent;
import static com.google.common.base.Preconditions.checkNotNull;
@ -36,92 +36,92 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class Location {
private final World world;
private final Extent extent;
private final Vector position;
private final Vector direction;
/**
* Create a new instance in the given world at 0, 0, 0 with a
* Create a new instance in the given extent at 0, 0, 0 with a
* direction vector of 0, 0, 0.
*
* @param world the world
* @param extent the extent
*/
public Location(World world) {
this(world, new Vector(), new Vector());
public Location(Extent extent) {
this(extent, new Vector(), new Vector());
}
/**
* Create a new instance in the given world with the given coordinates
* Create a new instance in the given extent with the given coordinates
* with a direction vector of 0, 0, 0.
*
* @param world the world
* @param extent the extent
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
*/
public Location(World world, double x, double y, double z) {
this(world, new Vector(x, y, z), new Vector());
public Location(Extent extent, double x, double y, double z) {
this(extent, new Vector(x, y, z), new Vector());
}
/**
* Create a new instance in the given world with the given position
* Create a new instance in the given extent with the given position
* vector and a direction vector of 0, 0, 0.
*
* @param world the world
* @param extent the extent
* @param position the position vector
*/
public Location(World world, Vector position) {
this(world, position, new Vector());
public Location(Extent extent, Vector position) {
this(extent, position, new Vector());
}
/**
* Create a new instance in the given world with the given coordinates
* Create a new instance in the given extent with the given coordinates
* and the given direction vector.
*
* @param world the world
* @param extent the extent
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @param direction the direction vector
*/
public Location(World world, double x, double y, double z, Vector direction) {
this(world, new Vector(x, y, z), direction);
public Location(Extent extent, double x, double y, double z, Vector direction) {
this(extent, new Vector(x, y, z), direction);
}
/**
* Create a new instance in the given world with the given position vector
* Create a new instance in the given extent with the given position vector
* and the given direction vector.
*
* @param world the world
* @param extent the extent
* @param position the position vector
* @param direction the direction vector
*/
public Location(World world, Vector position, Vector direction) {
checkNotNull(world);
public Location(Extent extent, Vector position, Vector direction) {
checkNotNull(extent);
checkNotNull(position);
checkNotNull(direction);
this.world = world;
this.extent = extent;
this.position = position;
this.direction = direction;
}
/**
* Get the world.
* Get the extent.
*
* @return the world
* @return the extent
*/
public World getWorld() {
return world;
public Extent getExtent() {
return extent;
}
/**
* Create a clone of this object with the given world.
* Create a clone of this object with the given extent.
*
* @param world the new world
* @param extent the new extent
* @return the new instance
*/
public Location setWorld(World world) {
return new Location(world, position, getDirection());
public Location setExtent(Extent extent) {
return new Location(extent, position, getDirection());
}
/**
@ -143,7 +143,7 @@ public class Location {
* @return the new instance
*/
public Location setDirection(Vector direction) {
return new Location(world, position, direction);
return new Location(extent, position, direction);
}
/**
@ -181,7 +181,7 @@ public class Location {
* @return a new immutable instance
*/
public Location setX(double x) {
return new Location(world, position.setX(x), direction);
return new Location(extent, position.setX(x), direction);
}
/**
@ -192,7 +192,7 @@ public class Location {
* @return a new immutable instance
*/
public Location setX(int x) {
return new Location(world, position.setX(x), direction);
return new Location(extent, position.setX(x), direction);
}
/**
@ -221,7 +221,7 @@ public class Location {
* @return a new immutable instance
*/
public Location setY(double y) {
return new Location(world, position.setY(y), direction);
return new Location(extent, position.setY(y), direction);
}
/**
@ -232,7 +232,7 @@ public class Location {
* @return a new immutable instance
*/
public Location setY(int y) {
return new Location(world, position.setY(y), direction);
return new Location(extent, position.setY(y), direction);
}
/**
@ -261,7 +261,7 @@ public class Location {
* @return a new immutable instance
*/
public Location setZ(double z) {
return new Location(world, position.setZ(z), direction);
return new Location(extent, position.setZ(z), direction);
}
/**
@ -272,7 +272,7 @@ public class Location {
* @return a new immutable instance
*/
public Location setZ(int z) {
return new Location(world, position.setZ(z), direction);
return new Location(extent, position.setZ(z), direction);
}
@Override
@ -284,14 +284,14 @@ public class Location {
if (!direction.equals(location.direction)) return false;
if (!position.equals(location.position)) return false;
if (!world.equals(location.world)) return false;
if (!extent.equals(location.extent)) return false;
return true;
}
@Override
public int hashCode() {
int result = world.hashCode();
int result = extent.hashCode();
result = 31 * result + position.hashCode();
result = 31 * result + direction.hashCode();
return result;