Moved most methods of ServerInterface to LocalWorld.

This commit is contained in:
sk89q
2011-01-08 11:26:27 -08:00
parent da4480ab49
commit b26624bc9b
16 changed files with 666 additions and 625 deletions

View File

@ -50,10 +50,6 @@ public class EditSession {
*/
private static Random prng = new Random();
/**
* Server interface.
*/
private ServerInterface server;
/**
* World.
*/
@ -62,23 +58,28 @@ public class EditSession {
/**
* Stores the original blocks before modification.
*/
private DoubleArrayList<BlockVector, BaseBlock> original = new DoubleArrayList<BlockVector, BaseBlock>(
private DoubleArrayList<BlockVector, BaseBlock> original =
new DoubleArrayList<BlockVector, BaseBlock>(
true);
/**
* Stores the current blocks.
*/
private DoubleArrayList<BlockVector, BaseBlock> current = new DoubleArrayList<BlockVector, BaseBlock>(
private DoubleArrayList<BlockVector, BaseBlock> current =
new DoubleArrayList<BlockVector, BaseBlock>(
false);
/**
* Blocks that should be placed before last.
*/
private DoubleArrayList<BlockVector, BaseBlock> queueAfter = new DoubleArrayList<BlockVector, BaseBlock>(
private DoubleArrayList<BlockVector, BaseBlock> queueAfter =
new DoubleArrayList<BlockVector, BaseBlock>(
false);
/**
* Blocks that should be placed last.
*/
private DoubleArrayList<BlockVector, BaseBlock> queueLast = new DoubleArrayList<BlockVector, BaseBlock>(
private DoubleArrayList<BlockVector, BaseBlock> queueLast =
new DoubleArrayList<BlockVector, BaseBlock>(
false);
/**
* The maximum number of blocks to change at a time. If this number is
* exceeded, a MaxChangedBlocksException exception will be raised. -1
@ -112,7 +113,6 @@ public class EditSession {
}
this.maxBlocks = maxBlocks;
this.server = server;
this.world = world;
}
@ -131,7 +131,6 @@ public class EditSession {
this.maxBlocks = maxBlocks;
this.blockBag = blockBag;
this.server = server;
this.world = world;
}
@ -149,14 +148,14 @@ public class EditSession {
}
// Clear the chest so that it doesn't drop items
if (server.getBlockType(world, pt) == 54 && blockBag == null) {
server.clearChest(world, pt);
if (world.getBlockType(pt) == 54 && blockBag == null) {
world.clearChest(pt);
}
int id = block.getID();
if (blockBag != null) {
int existing = server.getBlockType(world, pt);
int existing = world.getBlockType(pt);
if (id > 0) {
try {
@ -177,25 +176,25 @@ public class EditSession {
}
}
boolean result = server.setBlockType(world, pt, id);
boolean result = world.setBlockType(pt, id);
if (id != 0) {
if (BlockType.usesData(id)) {
server.setBlockData(world, pt, block.getData());
world.setBlockData(pt, block.getData());
}
// Signs
if (block instanceof SignBlock) {
SignBlock signBlock = (SignBlock) block;
String[] text = signBlock.getText();
server.setSignText(world, pt, text);
world.setSignText(pt, text);
// Chests
} else if (block instanceof ChestBlock && blockBag == null) {
ChestBlock chestBlock = (ChestBlock) block;
server.setChestContents(world, pt, chestBlock.getItems());
world.setChestContents(pt, chestBlock.getItems());
// Mob spawners
} else if (block instanceof MobSpawnerBlock) {
MobSpawnerBlock mobSpawnerblock = (MobSpawnerBlock) block;
server.setMobSpawnerType(world, pt, mobSpawnerblock.getMobType());
world.setMobSpawnerType(pt, mobSpawnerblock.getMobType());
}
}
@ -298,21 +297,20 @@ public class EditSession {
* @return BaseBlock
*/
public BaseBlock rawGetBlock(Vector pt) {
int type = server.getBlockType(world, pt);
int data = server.getBlockData(world, pt);
int type = world.getBlockType(pt);
int data = world.getBlockData(pt);
// Sign
if (type == 63 || type == 68) {
String[] text = server.getSignText(world, pt);
String[] text = world.getSignText(pt);
return new SignBlock(type, data, text);
// Chest
} else if (type == 54) {
BaseItemStack[] items = server.getChestContents(world, pt);
BaseItemStack[] items = world.getChestContents(pt);
return new ChestBlock(data, items);
// Mob spawner
} else if (type == 52) {
return new MobSpawnerBlock(data,
server.getMobSpawnerType(world, pt));
return new MobSpawnerBlock(data, world.getMobSpawnerType(pt));
} else {
return new BaseBlock(type, data);
}
@ -1813,8 +1811,7 @@ public class EditSession {
if (pineTree) {
makePineTree(new Vector(x, y + 1, z));
} else {
server.generateTree(this, world,
new Vector(x, y + 1, z));
world.generateTree(this, new Vector(x, y + 1, z));
}
affected++;
break;

View File

@ -83,8 +83,7 @@ public abstract class LocalPlayer {
byte free = 0;
while (y <= 129) {
if (BlockType.canPassThrough(server.getBlockType(world,
new Vector(x, y, z)))) {
if (BlockType.canPassThrough(world.getBlockType(new Vector(x, y, z)))) {
free++;
} else {
free = 0;
@ -128,7 +127,7 @@ public abstract class LocalPlayer {
byte spots = 0;
while (y <= 129) {
if (BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
if (BlockType.canPassThrough(world.getBlockType(new Vector(x, y, z)))) {
free++;
} else {
free = 0;
@ -137,7 +136,7 @@ public abstract class LocalPlayer {
if (free == 2) {
spots++;
if (spots == 2) {
int type = server.getBlockType(world, new Vector(x, y - 2, z));
int type = world.getBlockType(new Vector(x, y - 2, z));
// Don't get put in lava!
if (type == 10 || type == 11) {
@ -170,7 +169,7 @@ public abstract class LocalPlayer {
byte free = 0;
while (y >= 1) {
if (BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
if (BlockType.canPassThrough(world.getBlockType(new Vector(x, y, z)))) {
free++;
} else {
free = 0;
@ -181,7 +180,7 @@ public abstract class LocalPlayer {
// lightly and also check to see if there's something to
// stand upon
while (y >= 0) {
int type = server.getBlockType(world, new Vector(x, y, z));
int type = world.getBlockType(new Vector(x, y, z));
// Don't want to end up in lava
if (type != 0 && type != 10 && type != 11) {
@ -217,15 +216,15 @@ public abstract class LocalPlayer {
LocalWorld world = getPosition().getWorld();
// No free space above
if (server.getBlockType(world, new Vector(x, y, z)) != 0) {
if (world.getBlockType(new Vector(x, y, z)) != 0) {
return false;
}
while (y <= 127) {
// Found a ceiling!
if (!BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
if (!BlockType.canPassThrough(world.getBlockType(new Vector(x, y, z)))) {
int platformY = Math.max(initialY, y - 3 - clearance);
server.setBlockType(world, new Vector(x, platformY, z),
world.setBlockType(new Vector(x, platformY, z),
BlockType.GLASS.getID());
setPosition(new Vector(x + 0.5, platformY + 1, z + 0.5));
return true;
@ -253,12 +252,12 @@ public abstract class LocalPlayer {
LocalWorld world = getPosition().getWorld();
while (y <= 129) {
if (!BlockType.canPassThrough(server.getBlockType(world, new Vector(x, y, z)))) {
if (!BlockType.canPassThrough(world.getBlockType(new Vector(x, y, z)))) {
break; // Hit something
} else if (y > maxY + 1) {
break;
} else if (y == maxY + 1) {
server.setBlockType(world, new Vector(x, y - 2, z),
world.setBlockType(new Vector(x, y - 2, z),
BlockType.GLASS.getID());
setPosition(new Vector(x + 0.5, y - 1, z + 0.5));
return true;

View File

@ -19,12 +19,163 @@
package com.sk89q.worldedit;
import com.sk89q.worldedit.blocks.BaseItemStack;
/**
* Represents a world.
*
* @author sk89q
*/
public abstract class LocalWorld {
/**
* Set block type.
*
* @param pt
* @param type
* @return
*/
public abstract boolean setBlockType(Vector pt, int type);
/**
* Get block type.
*
* @param pt
* @return
*/
public abstract int getBlockType(Vector pt);
/**
* Set block data.
*
* @param pt
* @param data
* @return
*/
public abstract void setBlockData(Vector pt, int data);
/**
* Get block data.
*
* @param pt
* @return
*/
public abstract int getBlockData(Vector pt);
/**
* Set sign text.
*
* @param pt
* @param text
*/
public abstract void setSignText(Vector pt, String[] text);
/**
* Get sign text.
*
* @param pt
* @return
*/
public abstract String[] getSignText(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);
/**
* Sets a chest slot.
*
* @param pt
* @param contents
* @return
*/
public abstract boolean setChestContents(Vector pt,
BaseItemStack[] contents);
/**
* Clear a chest's contents.
*
* @param pt
*/
public abstract boolean clearChest(Vector pt);
/**
* Set mob spawner mob type.
*
* @param pt
* @param mobType
*/
public abstract void setMobSpawnerType(Vector pt,
String mobType);
/**
* Get mob spawner mob type. May return an empty string.
*
* @param pt
* @param mobType
*/
public abstract String getMobSpawnerType(Vector pt);
/**
* Generate a tree at a location.
*
* @param pt
* @return
*/
public abstract boolean generateTree(EditSession editSession, 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);
/**
* Drop an item.
*
* @param pt
* @param type
* @param count
* @param times
*/
public abstract void dropItem(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);
/**
* Simulate a block being mined.
*
* @param pt
*/
public abstract void simulateBlockMine(Vector pt);
/**
* Kill mobs in an area.
*
* @param origin
* @param radius
* @return
*/
public abstract int killMobs(Vector origin, int radius);
/**
* Compare if the other world is equal.
*

View File

@ -19,162 +19,11 @@
package com.sk89q.worldedit;
import com.sk89q.worldedit.blocks.BaseItemStack;
/**
*
* @author sk89q
*/
public abstract class ServerInterface {
/**
* Set block type.
*
* @param pt
* @param type
* @return
*/
public abstract boolean setBlockType(LocalWorld world, Vector pt, int type);
/**
* Get block type.
*
* @param pt
* @return
*/
public abstract int getBlockType(LocalWorld world, Vector pt);
/**
* Set block data.
*
* @param pt
* @param data
* @return
*/
public abstract void setBlockData(LocalWorld world, Vector pt, int data);
/**
* Get block data.
*
* @param pt
* @return
*/
public abstract int getBlockData(LocalWorld world, Vector pt);
/**
* Set sign text.
*
* @param pt
* @param text
*/
public abstract void setSignText(LocalWorld world, Vector pt, String[] text);
/**
* Get sign text.
*
* @param pt
* @return
*/
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(LocalWorld world, Vector pt);
/**
* Sets a chest slot.
*
* @param pt
* @param contents
* @return
*/
public abstract boolean setChestContents(LocalWorld world, Vector pt,
BaseItemStack[] contents);
/**
* Clear a chest's contents.
*
* @param pt
*/
public abstract boolean clearChest(LocalWorld world, Vector pt);
/**
* Checks if a mob type is valid.
*
* @param type
* @return
*/
public abstract boolean isValidMobType(String type);
/**
* Set mob spawner mob type.
*
* @param pt
* @param 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(LocalWorld world, Vector pt);
/**
* Generate a tree at a location.
*
* @param pt
* @return
*/
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(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(LocalWorld world, Vector pt, int type,
int count);
/**
* Drop an item.
*
* @param pt
* @param type
* @param count
* @param times
*/
public abstract void dropItem(LocalWorld world, Vector pt, int type);
/**
* Simulate a block being mined.
*
* @param pt
*/
public abstract void simulateBlockMine(LocalWorld world, Vector pt);
/**
* Resolves an item name to its ID.
*
@ -182,13 +31,12 @@ public abstract class ServerInterface {
* @return
*/
public abstract int resolveItem(String name);
/**
* Kill mobs in an area.
* Checks if a mob type is valid.
*
* @param origin
* @param radius
* @param type
* @return
*/
public abstract int killMobs(LocalWorld world, Vector origin, int radius);
public abstract boolean isValidMobType(String type);
}

View File

@ -436,6 +436,8 @@ public class WorldEditController {
logger.log(Level.INFO, "WorldEdit: " + player.getName() + ": "
+ joinString(split, " "));
}
LocalWorld world = player.getPosition().getWorld();
// Jump to the first free position
if (split[0].equalsIgnoreCase("/unstuck")) {
@ -1359,7 +1361,7 @@ public class WorldEditController {
Math.max(1, Integer.parseInt(split[1])) : -1;
Vector origin = session.getPlacementPosition(player);
int killed = server.killMobs(player.getWorld(), origin, radius);
int killed = world.killMobs(origin, radius);
player.print("Killed " + killed + " mobs.");
return true;
@ -1741,12 +1743,10 @@ public class WorldEditController {
* Called on right click.
*
* @param player
* @param world
* @param clicked
* @return false if you want the action to go through
*/
public boolean handleBlockRightClick(LocalPlayer player, LocalWorld world,
Vector clicked) {
public boolean handleBlockRightClick(LocalPlayer player, WorldVector clicked) {
int itemInHand = player.getItemInHand();
// This prevents needless sessions from being created
@ -1767,8 +1767,7 @@ public class WorldEditController {
return true;
} else if (player.isHoldingPickAxe() && session.getTool() != null) {
return session.getTool().act(server, config, player, session,
world, clicked);
return session.getTool().act(server, config, player, session, clicked);
}
return false;
@ -1778,12 +1777,10 @@ public class WorldEditController {
* Called on left click.
*
* @param player
* @param world
* @param clicked
* @return false if you want the action to go through
*/
public boolean handleBlockLeftClick(LocalPlayer player,
LocalWorld world, Vector clicked) {
public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) {
if (!canUseCommand(player, "/pos1")
&& !canUseCommand(player, "/")) { return false; }
@ -1818,7 +1815,7 @@ public class WorldEditController {
} else if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) {
if (session.getSuperPickaxeMode() != null) {
return session.getSuperPickaxeMode().act(server, config,
player, session, world, clicked);
player, session, clicked);
}
}

View File

@ -38,12 +38,12 @@ public class AreaPickaxe implements SuperPickaxeMode {
@Override
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked) {
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
int ox = clicked.getBlockX();
int oy = clicked.getBlockY();
int oz = clicked.getBlockZ();
int initialType = server.getBlockType(world, clicked);
int initialType = world.getBlockType(clicked);
if (initialType == 0) {
return true;
@ -61,9 +61,9 @@ public class AreaPickaxe implements SuperPickaxeMode {
for (int y = oy - range; y <= oy + range; y++) {
for (int z = oz - range; z <= oz + range; z++) {
Vector pos = new Vector(x, y, z);
if (server.getBlockType(world, pos) == initialType) {
if (world.getBlockType(pos) == initialType) {
if (config.superPickaxeManyDrop) {
server.simulateBlockMine(world, pos);
world.simulateBlockMine(pos);
}
editSession.setBlock(pos, air);

View File

@ -36,9 +36,9 @@ public class BlockReplacer implements SuperPickaxeMode {
@Override
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked) {
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
EditSession editSession = new EditSession(server, world, -1);
try {

View File

@ -31,8 +31,9 @@ public class QueryTool implements SuperPickaxeMode {
@Override
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked) {
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
BaseBlock block = (new EditSession(server, world, 0)).rawGetBlock(clicked);
player.print("\u00A79@" + clicked + ": " + "\u00A7e"

View File

@ -41,9 +41,10 @@ public class RecursivePickaxe implements SuperPickaxeMode {
@Override
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked) {
int initialType = server.getBlockType(world, clicked);
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
int initialType = world.getBlockType(clicked);
if (initialType == 0) {
return true;
@ -95,7 +96,7 @@ public class RecursivePickaxe implements SuperPickaxeMode {
if (editSession.getBlock(pos).getID() == initialType) {
if (drop) {
server.simulateBlockMine(world, pos);
world.simulateBlockMine(pos);
}
editSession.setBlock(pos, air);
} else {

View File

@ -30,21 +30,21 @@ import com.sk89q.worldedit.blocks.BlockID;
public class SinglePickaxe implements SuperPickaxeMode {
@Override
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked) {
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
if (server.getBlockType(world, clicked) == BlockID.BEDROCK
if (world.getBlockType(clicked) == BlockID.BEDROCK
&& !player.canDestroyBedrock()) {
return true;
} else if (server.getBlockType(world, clicked) == BlockID.TNT) {
} else if (world.getBlockType(clicked) == BlockID.TNT) {
return false;
}
if (config.superPickaxeDrop) {
server.simulateBlockMine(world, clicked);
world.simulateBlockMine(clicked);
}
server.setBlockType(world, clicked, 0);
world.setBlockType(clicked, 0);
return true;
}

View File

@ -37,6 +37,5 @@ public interface SuperPickaxeMode {
* @return true to deny
*/
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked);
LocalPlayer player, LocalSession session, WorldVector clicked);
}

View File

@ -30,13 +30,14 @@ public class TreePlanter implements SuperPickaxeMode {
@Override
public boolean act(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, LocalWorld world,
Vector clicked) {
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
EditSession editSession =
new EditSession(server, world, session.getBlockChangeLimit());
try {
if (!server.generateTree(editSession, player.getWorld(), clicked)) {
if (!world.generateTree(editSession, clicked)) {
player.printError("Notch won't let you put a tree there.");
}
} finally {