Cleaned up some code, changed WorldEdit to be world-aware (finally).

This commit is contained in:
sk89q
2011-01-01 10:33:18 -08:00
parent 6d172891e2
commit ac4e6e8ddf
12 changed files with 783 additions and 442 deletions

View File

@ -0,0 +1,101 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
* Extension of Vector that supports being compared as ints (for accuracy).
*
* @author sk89q
*/
public class BlockWorldVector extends WorldVector {
/**
* Construct the Vector object.
*
* @param pt
*/
public BlockWorldVector(WorldVector pt) {
super(pt.getWorld(), pt);
}
/**
* Construct the Vector object.
*
* @param pt
*/
public BlockWorldVector(LocalWorld world, Vector pt) {
super(world, pt);
}
/**
* Construct the Vector object.
*
* @param pt
*/
public BlockWorldVector(LocalWorld world, int x, int y, int z) {
super(world, x, y, z);
}
/**
* Construct the Vector object.
*
* @param pt
*/
public BlockWorldVector(LocalWorld world, float x, float y, float z) {
super(world, x, y, z);
}
/**
* Construct the Vector object.
*
* @param pt
*/
public BlockWorldVector(LocalWorld world, double x, double y, double z) {
super(world, x, y, z);
}
/**
* Checks if another object is equivalent.
*
* @param obj
* @return whether the other object is equivalent
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof WorldVector)) {
return false;
}
WorldVector other = (WorldVector)obj;
return (int)other.x == (int)this.x && (int)other.y == (int)this.y
&& (int)other.z == (int)this.z;
}
/**
* Gets the hash code.
*
* @return hash code
*/
@Override
public int hashCode() {
return (Integer.valueOf((int)x).hashCode() >> 13) ^
(Integer.valueOf((int)y).hashCode() >> 7) ^
Integer.valueOf((int)z).hashCode();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
* Represents a world.
*
* @author sk89q
*/
public abstract class LocalWorld {
/**
* Compare if the other world is equal.
*
* @param other
* @return
*/
public abstract boolean equals(Object other);
/**
* Hash code.
*
* @return
*/
public abstract int hashCode();
}

View File

@ -15,113 +15,92 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit;
import com.sk89q.worldedit.blocks.BaseItemStack;
/**
*
*
* @author sk89q
*/
public abstract class ServerInterface {
/**
* Instance.
*/
private static ServerInterface instance;
/**
* Get the current instance.
*
* @return
*/
public static ServerInterface getInstance() {
return instance;
}
/**
* Set up an instance.
* @param instance
*/
public static void setup(ServerInterface instance) {
ServerInterface.instance = instance;
}
/**
* Set block type.
*
*
* @param pt
* @param type
* @return
*/
public abstract boolean setBlockType(Vector pt, int type);
public abstract boolean setBlockType(LocalWorld world, Vector pt, int type);
/**
* Get block type.
*
*
* @param pt
* @return
*/
public abstract int getBlockType(Vector pt);
public abstract int getBlockType(LocalWorld world, Vector pt);
/**
* Set block data.
*
*
* @param pt
* @param data
* @return
*/
public abstract void setBlockData(Vector pt, int data);
public abstract void setBlockData(LocalWorld world, Vector pt, int data);
/**
* Get block data.
*
*
* @param pt
* @return
*/
public abstract int getBlockData(Vector pt);
public abstract int getBlockData(LocalWorld world, Vector pt);
/**
* Set sign text.
*
*
* @param pt
* @param text
*/
public abstract void setSignText(Vector pt, String[] text);
public abstract void setSignText(LocalWorld world, Vector pt, String[] text);
/**
* Get sign text.
*
*
* @param pt
* @return
*/
public abstract String[] getSignText(Vector pt);
public abstract String[] getSignText(LocalWorld world, Vector pt);
/**
* Gets the contents of chests. Will return null if the chest does not
* really exist or it is the second block for a double chest.
*
*
* @param pt
* @return
*/
public abstract BaseItemStack[] getChestContents(Vector pt);
public abstract BaseItemStack[] getChestContents(LocalWorld world, Vector pt);
/**
* Sets a chest slot.
*
*
* @param pt
* @param contents
* @return
*/
public abstract boolean setChestContents(Vector pt, BaseItemStack[] contents);
public abstract boolean setChestContents(LocalWorld world, Vector pt,
BaseItemStack[] contents);
/**
* Clear a chest's contents.
*
* @param pt
*/
public abstract boolean clearChest(Vector pt);
public abstract boolean clearChest(LocalWorld world, Vector pt);
/**
* Checks if a mob type is valid.
@ -133,19 +112,20 @@ public abstract class ServerInterface {
/**
* Set mob spawner mob type.
*
*
* @param pt
* @param mobType
*/
public abstract void setMobSpawnerType(Vector pt, String mobType);
public abstract void setMobSpawnerType(LocalWorld world, Vector pt,
String mobType);
/**
* Get mob spawner mob type. May return an empty string.
*
*
* @param pt
* @param mobType
*/
public abstract String getMobSpawnerType(Vector pt);
public abstract String getMobSpawnerType(LocalWorld world, Vector pt);
/**
* Generate a tree at a location.
@ -153,45 +133,48 @@ public abstract class ServerInterface {
* @param pt
* @return
*/
public abstract boolean generateTree(EditSession editSession, Vector pt);
public abstract boolean generateTree(EditSession editSession,
LocalWorld world, Vector pt);
/**
* Drop an item.
*
*
* @param pt
* @param type
* @param count
* @param times
*/
public abstract void dropItem(Vector pt, int type, int count, int times);
public abstract void dropItem(LocalWorld world, Vector pt, int type,
int count, int times);
/**
* Drop an item.
*
*
* @param pt
* @param type
* @param count
* @param times
*/
public abstract void dropItem(Vector pt, int type, int count);
public abstract void dropItem(LocalWorld world, Vector pt, int type,
int count);
/**
* Drop an item.
*
*
* @param pt
* @param type
* @param count
* @param times
*/
public abstract void dropItem(Vector pt, int type);
public abstract void dropItem(LocalWorld world, Vector pt, int type);
/**
* Simulate a block being mined.
*
* @param pt
*/
public abstract void simulateBlockMine(Vector pt);
public abstract void simulateBlockMine(LocalWorld world, Vector pt);
/**
* Resolves an item name to its ID.
*
@ -207,5 +190,5 @@ public abstract class ServerInterface {
* @param radius
* @return
*/
public abstract int killMobs(Vector origin, int radius);
public abstract int killMobs(LocalWorld world, Vector origin, int radius);
}

View File

@ -21,7 +21,7 @@ package com.sk89q.worldedit;
/**
*
* @author Albert
* @author sk89q
*/
public class Vector {
protected final double x, y, z;

View File

@ -95,10 +95,12 @@ public class WorldEditController {
public boolean useInventoryOverride = false;
/**
* Construct an instance of the plugin.
* Construct an instance of the plugin
*
* @param server
*/
public WorldEditController() {
server = ServerInterface.getInstance();
public WorldEditController(ServerInterface server) {
this.server = server;
// Note: Commands should only have the phrase 'air' at the end
// for now (see SMWorldEditListener.canUseCommand)
@ -442,9 +444,9 @@ public class WorldEditController {
// Jump to the block in sight
} else if (split[0].equalsIgnoreCase("/jumpto")) {
checkArgs(split, 0, 0, split[0]);
Vector pos = player.getSolidBlockTrace(300);
WorldVector pos = player.getSolidBlockTrace(300);
if (pos != null) {
player.findFreePosition(pos);
player.findFreePosition(pos.getWorld(), pos);
player.print("Poof!");
} else {
player.printError("No block in sight!");
@ -1323,7 +1325,7 @@ public class WorldEditController {
Math.max(1, Integer.parseInt(split[1])) : -1;
Vector origin = session.getPlacementPosition(player);
int killed = server.killMobs(origin, radius);
int killed = server.killMobs(player.getWorld(), origin, radius);
player.print("Killed " + killed + " mobs.");
return true;
@ -1732,10 +1734,10 @@ public class WorldEditController {
} else if (player.isHoldingPickAxe()
&& session.getTool() == WorldEditSession.Tool.TREE) {
EditSession editSession =
new EditSession(session.getBlockChangeLimit());
new EditSession(server, player.getWorld(), session.getBlockChangeLimit());
try {
if (!server.generateTree(editSession, clicked)) {
if (!server.generateTree(editSession, player.getWorld(), clicked)) {
player.printError("Notch won't let you put a tree there.");
}
} finally {
@ -1745,7 +1747,7 @@ public class WorldEditController {
return true;
} else if (player.isHoldingPickAxe()
&& session.getTool() == WorldEditSession.Tool.INFO) {
BaseBlock block = (new EditSession(0)).rawGetBlock(clicked);
BaseBlock block = (new EditSession(server, player.getWorld(), 0)).rawGetBlock(clicked);
player.print("\u00A79@" + clicked + ": " + "\u00A7e"
+ "Type: " + block.getID() + "\u00A77" + " ("
@ -1805,20 +1807,22 @@ public class WorldEditController {
} else if (player.isHoldingPickAxe()) {
if (session.hasSuperPickAxe()) {
boolean canBedrock = canUseCommand(player, "/worldeditbedrock");
LocalWorld world = player.getWorld();
// Single block super pickaxe
if (session.getSuperPickaxeMode() ==
WorldEditSession.SuperPickaxeMode.SINGLE) {
if (server.getBlockType(clicked) == 7 && !canBedrock) {
if (server.getBlockType(world, clicked) == 7 && !canBedrock) {
return true;
} else if (server.getBlockType(clicked) == 46) {
} else if (server.getBlockType(world, clicked) == 46) {
return false;
}
if (superPickaxeDrop) {
server.simulateBlockMine(clicked);
server.simulateBlockMine(world, clicked);
} else {
server.setBlockType(clicked, 0);
server.setBlockType(world, clicked, 0);
}
// Area super pickaxe
@ -1828,7 +1832,7 @@ public class WorldEditController {
int oy = clicked.getBlockY();
int oz = clicked.getBlockZ();
int size = session.getSuperPickaxeRange();
int initialType = server.getBlockType(clicked);
int initialType = server.getBlockType(world, clicked);
if (initialType == 7 && !canBedrock) {
return true;
@ -1838,11 +1842,11 @@ public class WorldEditController {
for (int y = oy - size; y <= oy + size; y++) {
for (int z = oz - size; z <= oz + size; z++) {
Vector pos = new Vector(x, y, z);
if (server.getBlockType(pos) == initialType) {
if (server.getBlockType(world, pos) == initialType) {
if (superPickaxeManyDrop) {
server.simulateBlockMine(pos);
server.simulateBlockMine(world, pos);
} else {
server.setBlockType(pos, 0);
server.setBlockType(world, pos, 0);
}
}
}
@ -1855,13 +1859,13 @@ public class WorldEditController {
} else if (session.getSuperPickaxeMode() ==
WorldEditSession.SuperPickaxeMode.SAME_TYPE_RECURSIVE) {
int size = session.getSuperPickaxeRange();
int initialType = server.getBlockType(clicked);
int initialType = server.getBlockType(world, clicked);
if (initialType == 7 && !canBedrock) {
return true;
}
recursiveSuperPickaxe(clicked.toBlockVector(), clicked, size,
recursiveSuperPickaxe(world, clicked.toBlockVector(), clicked, size,
initialType, new HashSet<BlockVector>());
return true;
@ -1881,7 +1885,7 @@ public class WorldEditController {
* @param canBedrock
* @return
*/
private void recursiveSuperPickaxe(BlockVector pos, Vector origin,
private void recursiveSuperPickaxe(LocalWorld world, BlockVector pos, Vector origin,
int size, int initialType, Set<BlockVector> visited) {
if (origin.distance(pos) > size || visited.contains(pos)) {
return;
@ -1889,27 +1893,27 @@ public class WorldEditController {
visited.add(pos);
if (server.getBlockType(pos) == initialType) {
if (server.getBlockType(world, pos) == initialType) {
if (superPickaxeManyDrop) {
server.simulateBlockMine(pos);
server.simulateBlockMine(world, pos);
} else {
server.setBlockType(pos, 0);
server.setBlockType(world, pos, 0);
}
} else {
return;
}
recursiveSuperPickaxe(pos.add(1, 0, 0).toBlockVector(), origin, size,
recursiveSuperPickaxe(world, pos.add(1, 0, 0).toBlockVector(), origin, size,
initialType, visited);
recursiveSuperPickaxe(pos.add(-1, 0, 0).toBlockVector(), origin, size,
recursiveSuperPickaxe(world, pos.add(-1, 0, 0).toBlockVector(), origin, size,
initialType, visited);
recursiveSuperPickaxe(pos.add(0, 0, 1).toBlockVector(), origin, size,
recursiveSuperPickaxe(world, pos.add(0, 0, 1).toBlockVector(), origin, size,
initialType, visited);
recursiveSuperPickaxe(pos.add(0, 0, -1).toBlockVector(), origin, size,
recursiveSuperPickaxe(world, pos.add(0, 0, -1).toBlockVector(), origin, size,
initialType, visited);
recursiveSuperPickaxe(pos.add(0, 1, 0).toBlockVector(), origin, size,
recursiveSuperPickaxe(world, pos.add(0, 1, 0).toBlockVector(), origin, size,
initialType, visited);
recursiveSuperPickaxe(pos.add(0, -1, 0).toBlockVector(), origin, size,
recursiveSuperPickaxe(world, pos.add(0, -1, 0).toBlockVector(), origin, size,
initialType, visited);
}
@ -1943,7 +1947,8 @@ public class WorldEditController {
BlockBag blockBag = session.getBlockBag(player);
EditSession editSession =
new EditSession(session.getBlockChangeLimit(), blockBag);
new EditSession(server, player.getWorld(),
session.getBlockChangeLimit(), blockBag);
editSession.enableQueue();
long start = System.currentTimeMillis();

View File

@ -48,9 +48,11 @@ public abstract class WorldEditPlayer {
/**
* Construct the object.
*
* @param server
*/
protected WorldEditPlayer() {
server = ServerInterface.getInstance();
protected WorldEditPlayer(ServerInterface server) {
this.server = server;
}
/**
@ -72,7 +74,7 @@ public abstract class WorldEditPlayer {
*
* @param searchPos search position
*/
public void findFreePosition(Vector searchPos) {
public void findFreePosition(LocalWorld world, Vector searchPos) {
int x = searchPos.getBlockX();
int y = Math.max(0, searchPos.getBlockY());
int origY = y;
@ -81,7 +83,8 @@ public abstract class WorldEditPlayer {
byte free = 0;
while (y <= 129) {
if (BlockType.canPassThrough(server.getBlockType(new Vector(x, y, z)))) {
if (BlockType.canPassThrough(server.getBlockType(world,
new Vector(x, y, z)))) {
free++;
} else {
free = 0;
@ -106,7 +109,7 @@ public abstract class WorldEditPlayer {
* that free position.
*/
public void findFreePosition() {
findFreePosition(getBlockIn());
findFreePosition(getPosition().getWorld(), getBlockIn());
}
/**
@ -119,12 +122,13 @@ public abstract class WorldEditPlayer {
int x = pos.getBlockX();
int y = Math.max(0, pos.getBlockY());
int z = pos.getBlockZ();
LocalWorld world = getPosition().getWorld();
byte free = 0;
byte spots = 0;
while (y <= 129) {
if (BlockType.canPassThrough(server.getBlockType(new Vector(x, y, z)))) {
if (BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
free++;
} else {
free = 0;
@ -133,7 +137,7 @@ public abstract class WorldEditPlayer {
if (free == 2) {
spots++;
if (spots == 2) {
int type = server.getBlockType(new Vector(x, y - 2, z));
int type = server.getBlockType(world, new Vector(x, y - 2, z));
// Don't get put in lava!
if (type == 10 || type == 11) {
@ -161,11 +165,12 @@ public abstract class WorldEditPlayer {
int x = pos.getBlockX();
int y = Math.max(0, pos.getBlockY() - 1);
int z = pos.getBlockZ();
LocalWorld world = getPosition().getWorld();
byte free = 0;
while (y >= 1) {
if (BlockType.canPassThrough(server.getBlockType(new Vector(x, y, z)))) {
if (BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
free++;
} else {
free = 0;
@ -176,7 +181,7 @@ public abstract class WorldEditPlayer {
// lightly and also check to see if there's something to
// stand upon
while (y >= 0) {
int type = server.getBlockType(new Vector(x, y, z));
int type = server.getBlockType(world, new Vector(x, y, z));
// Don't want to end up in lava
if (type != 0 && type != 10 && type != 11) {
@ -209,17 +214,18 @@ public abstract class WorldEditPlayer {
int initialY = Math.max(0, pos.getBlockY());
int y = Math.max(0, pos.getBlockY() + 2);
int z = pos.getBlockZ();
LocalWorld world = getPosition().getWorld();
// No free space above
if (server.getBlockType(new Vector(x, y, z)) != 0) {
if (server.getBlockType(world, new Vector(x, y, z)) != 0) {
return false;
}
while (y <= 127) {
// Found a ceiling!
if (!BlockType.canPassThrough(server.getBlockType(new Vector(x, y, z)))) {
if (!BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
int platformY = Math.max(initialY, y - 3 - clearance);
server.setBlockType(new Vector(x, platformY, z),
server.setBlockType(world, new Vector(x, platformY, z),
BlockType.GLASS.getID());
setPosition(new Vector(x + 0.5, platformY + 1, z + 0.5));
return true;
@ -244,14 +250,15 @@ public abstract class WorldEditPlayer {
int y = Math.max(0, pos.getBlockY() + 1);
int z = pos.getBlockZ();
int maxY = Math.min(128, initialY + distance);
LocalWorld world = getPosition().getWorld();
while (y <= 129) {
if (!BlockType.canPassThrough(server.getBlockType(new Vector(x, y, z)))) {
if (!BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
break; // Hit something
} else if (y > maxY + 1) {
break;
} else if (y == maxY + 1) {
server.setBlockType(new Vector(x, y - 2, z),
server.setBlockType(world, new Vector(x, y - 2, z),
BlockType.GLASS.getID());
setPosition(new Vector(x + 0.5, y - 1, z + 0.5));
return true;
@ -268,8 +275,8 @@ public abstract class WorldEditPlayer {
*
* @return point
*/
public Vector getBlockIn() {
return getPosition().toBlockVector();
public WorldVector getBlockIn() {
return getPosition();
}
/**
@ -277,8 +284,9 @@ public abstract class WorldEditPlayer {
*
* @return point
*/
public Vector getBlockOn() {
return getPosition().subtract(0, 1, 0).toBlockVector();
public WorldVector getBlockOn() {
WorldVector pos = getPosition();
return new WorldVector(pos.getWorld(), pos.subtract(0, 1, 0));
}
/**
@ -287,7 +295,7 @@ public abstract class WorldEditPlayer {
* @param range
* @return point
*/
public abstract Vector getBlockTrace(int range);
public abstract WorldVector getBlockTrace(int range);
/**
* Get the point of the block being looked at. May return null.
@ -295,7 +303,7 @@ public abstract class WorldEditPlayer {
* @param range
* @return point
*/
public abstract Vector getSolidBlockTrace(int range);
public abstract WorldVector getSolidBlockTrace(int range);
/**
* Get the player's cardinal direction (N, W, NW, etc.). May return null.
@ -360,7 +368,14 @@ public abstract class WorldEditPlayer {
*
* @return point
*/
public abstract Vector getPosition();
public abstract WorldVector getPosition();
/**
* Get the player's world.
*
* @return point
*/
public abstract LocalWorld getWorld();
/**
* Get the player's view pitch.

View File

@ -0,0 +1,104 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
* A vector with a world component.
*
* @author sk89q
*/
public class WorldVector extends Vector {
/**
* Represents the world.
*/
private LocalWorld world;
/**
* Construct the Vector object.
*
* @param x
* @param y
* @param z
*/
public WorldVector(LocalWorld world, double x, double y, double z) {
super(x, y, z);
this.world = world;
}
/**
* Construct the Vector object.
*
* @param x
* @param y
* @param z
*/
public WorldVector(LocalWorld world, int x, int y, int z) {
super(x, y, z);
this.world = world;
}
/**
* Construct the Vector object.
*
* @param x
* @param y
* @param z
*/
public WorldVector(LocalWorld world, float x, float y, float z) {
super(x, y, z);
this.world = world;
}
/**
* Construct the Vector object.
*
* @param pt
*/
public WorldVector(LocalWorld world, Vector pt) {
super(pt);
this.world = world;
}
/**
* Construct the Vector object.
*/
public WorldVector(LocalWorld world) {
super();
this.world = world;
}
/**
* Get the world.
*
* @return
*/
public LocalWorld getWorld() {
return world;
}
/**
* Gets a BlockVector version.
*
* @return BlockWorldVector
*/
public BlockWorldVector toWorldBlockVector() {
return new BlockWorldVector(this);
}
}