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

View File

@ -112,6 +112,11 @@ public class WorldEdit {
*/ */
public static final Logger logger = Logger.getLogger("Minecraft.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. * Holds WorldEdit's version.
*/ */
@ -132,6 +137,11 @@ public class WorldEdit {
*/ */
private final CommandsManager<LocalPlayer> commands; 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 * Stores a list of WorldEdit sessions, keyed by players' names. Sessions
* persist only for the user's session. On disconnect, the session will be * persist only for the user's session. On disconnect, the session will be
@ -155,6 +165,7 @@ public class WorldEdit {
* @param config * @param config
*/ */
public WorldEdit(ServerInterface server, final LocalConfiguration config) { public WorldEdit(ServerInterface server, final LocalConfiguration config) {
instance = this;
this.server = server; this.server = server;
this.config = config; this.config = config;
@ -253,6 +264,15 @@ private void reg(Class<?> clazz) {
server.onCommandRegistration(commands.registerAndReturn(clazz), commands); 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 * Gets the LocalSession for a player name if it exists
* *
@ -1529,6 +1549,28 @@ public ServerInterface getServer() {
return server; 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. * Get the version.
* *

View File

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

View File

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

View File

@ -46,7 +46,7 @@ public boolean actPrimary(ServerInterface server, LocalConfiguration config,
BlockBag bag = session.getBlockBag(player); BlockBag bag = session.getBlockBag(player);
LocalWorld world = clicked.getWorld(); LocalWorld world = clicked.getWorld();
EditSession editSession = new EditSession(world, -1, bag); EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1, bag);
try { try {
editSession.setBlock(clicked, targetBlock); editSession.setBlock(clicked, targetBlock);
@ -66,7 +66,8 @@ public boolean actSecondary(ServerInterface server,
LocalSession session, WorldVector clicked) { LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld(); 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()); BlockType type = BlockType.fromID(targetBlock.getType());
if (type != null) { if (type != null) {

View File

@ -37,7 +37,8 @@ public boolean actPrimary(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, WorldVector clicked) { LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld(); 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()); BlockType type = BlockType.fromID(block.getType());
player.print("\u00A79@" + clicked + ": " + "\u00A7e" player.print("\u00A79@" + clicked + ": " + "\u00A7e"