Implement EditSessionFactory

This commit is contained in:
Ammar Askar 2012-10-18 15:34:09 +05:00
parent ae75061492
commit 04c00034cf
7 changed files with 85 additions and 13 deletions

View File

@ -0,0 +1,28 @@
package com.sk89q.worldedit;
import com.sk89q.worldedit.bags.BlockBag;
public class EditSessionFactory {
/**
* Construct an edit session with a maximum number of blocks.
*
* @param world
* @param maxBlocks
*/
public EditSession getEditSession(LocalWorld world, int maxBlocks) {
return new EditSession(world, maxBlocks);
}
/**
* Construct an edit session with a maximum number of blocks and a block bag.
*
* @param world
* @param maxBlocks
* @param blockBag
*/
public EditSession getEditSession(LocalWorld world, int maxBlocks, BlockBag blockBag) {
return new EditSession(world, maxBlocks, blockBag);
}
}

View File

@ -142,8 +142,8 @@ public class LocalSession {
--historyPointer;
if (historyPointer >= 0) {
EditSession editSession = history.get(historyPointer);
EditSession newEditSession =
new EditSession(editSession.getWorld(), -1, newBlockBag);
EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag);
newEditSession.enableQueue();
newEditSession.setFastMode(fastMode);
editSession.undo(newEditSession);
@ -163,8 +163,8 @@ public class LocalSession {
public EditSession redo(BlockBag newBlockBag) {
if (historyPointer < history.size()) {
EditSession editSession = history.get(historyPointer);
EditSession newEditSession =
new EditSession(editSession.getWorld(), -1, newBlockBag);
EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag);
newEditSession.enableQueue();
newEditSession.setFastMode(fastMode);
editSession.redo(newEditSession);
@ -698,8 +698,8 @@ public class LocalSession {
BlockBag blockBag = getBlockBag(player);
// Create an edit session
EditSession editSession =
new EditSession(player.isPlayer() ? player.getWorld() : null,
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(player.isPlayer() ? player.getWorld() : null,
getBlockChangeLimit(), blockBag);
editSession.setFastMode(fastMode);
if (mask != null) {

View File

@ -112,6 +112,11 @@ public class WorldEdit {
*/
public static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Holds the current instance of this class, for static access
*/
private static WorldEdit instance;
/**
* Holds WorldEdit's version.
*/
@ -132,6 +137,11 @@ public class WorldEdit {
*/
private final CommandsManager<LocalPlayer> commands;
/**
* Holds the factory responsible for the creation of edit sessions
*/
private EditSessionFactory editSessionFactory = new EditSessionFactory();
/**
* Stores a list of WorldEdit sessions, keyed by players' names. Sessions
* persist only for the user's session. On disconnect, the session will be
@ -155,6 +165,7 @@ public class WorldEdit {
* @param config
*/
public WorldEdit(ServerInterface server, final LocalConfiguration config) {
instance = this;
this.server = server;
this.config = config;
@ -253,6 +264,15 @@ public class WorldEdit {
server.onCommandRegistration(commands.registerAndReturn(clazz), commands);
}
/**
* Gets the current instance of this class
*
* @return
*/
public static WorldEdit getInstance() {
return instance;
}
/**
* Gets the LocalSession for a player name if it exists
*
@ -1529,6 +1549,28 @@ public class WorldEdit {
return server;
}
/**
* Get the edit session factory
*
* @return
*/
public EditSessionFactory getEditSessionFactory() {
return this.editSessionFactory;
}
/**
* Set the edit session factory
*
* @param factory
*/
public void setEditSessionFactory(EditSessionFactory factory) {
if (factory == null) {
throw new IllegalArgumentException("New EditSessionFactory may not be null");
}
logger.info("Accepted EditSessionFactory of type " + factory.getClass().getName() + " from " + factory.getClass().getPackage().getName());
this.editSessionFactory = factory;
}
/**
* Get the version.
*

View File

@ -220,8 +220,8 @@ public class WorldEditPlugin extends JavaPlugin {
LocalSession session = controller.getSession(wePlayer);
BlockBag blockBag = session.getBlockBag(wePlayer);
EditSession editSession =
new EditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag);
EditSession editSession = controller.getEditSessionFactory()
.getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag);
editSession.enableQueue();
return editSession;

View File

@ -60,8 +60,8 @@ public class CraftScriptContext extends CraftScriptEnvironment {
* @return
*/
public EditSession remember() {
EditSession editSession =
new EditSession(player.getWorld(),
EditSession editSession = controller.getEditSessionFactory()
.getEditSession(player.getWorld(),
session.getBlockChangeLimit(), session.getBlockBag(player));
editSession.enableQueue();
editSessions.add(editSession);

View File

@ -46,7 +46,7 @@ public class BlockReplacer implements DoubleActionBlockTool {
BlockBag bag = session.getBlockBag(player);
LocalWorld world = clicked.getWorld();
EditSession editSession = new EditSession(world, -1, bag);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1, bag);
try {
editSession.setBlock(clicked, targetBlock);
@ -66,7 +66,8 @@ public class BlockReplacer implements DoubleActionBlockTool {
LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
targetBlock = (new EditSession(world, -1)).getBlock(clicked);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
targetBlock = (editSession).getBlock(clicked);
BlockType type = BlockType.fromID(targetBlock.getType());
if (type != null) {

View File

@ -37,7 +37,8 @@ public class QueryTool implements BlockTool {
LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
BaseBlock block = (new EditSession(world, 0)).rawGetBlock(clicked);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, 0);
BaseBlock block = (editSession).rawGetBlock(clicked);
BlockType type = BlockType.fromID(block.getType());
player.print("\u00A79@" + clicked + ": " + "\u00A7e"