From 92dc88562c67850c03c866d2b89825c6a7552099 Mon Sep 17 00:00:00 2001 From: sk89q Date: Thu, 14 Oct 2010 11:59:45 -0700 Subject: [PATCH] Renamed hey0's plugin to SMWorldEdit. Made WorldEdit somewhat a singleton to hold the ServerInterface instance. --- src/EditSession.java | 5 ++- src/{WorldEditSM.java => SMWorldEdit.java} | 22 ++++++---- ...Listener.java => SMWorldEditListener.java} | 21 +++------- src/WorldEdit.java | 42 ++++++++++++++++++- src/WorldEditPlayer.java | 9 +++- 5 files changed, 73 insertions(+), 26 deletions(-) rename src/{WorldEditSM.java => SMWorldEdit.java} (88%) rename src/{WorldEditSMListener.java => SMWorldEditListener.java} (93%) diff --git a/src/EditSession.java b/src/EditSession.java index 8355d1f18..a02faedcb 100644 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -41,7 +41,7 @@ public class EditSession { /** * Server interface. */ - public static ServerInterface server; + protected ServerInterface server; /** * Stores the original blocks before modification. @@ -88,6 +88,7 @@ public class EditSession { * Default constructor. There is no maximum blocks limit. */ public EditSession() { + server = WorldEdit.getServer(); } /** @@ -98,6 +99,8 @@ public class EditSession { throw new IllegalArgumentException("Max blocks must be >= -1"); } this.maxBlocks = maxBlocks; + + server = WorldEdit.getServer(); } /** diff --git a/src/WorldEditSM.java b/src/SMWorldEdit.java similarity index 88% rename from src/WorldEditSM.java rename to src/SMWorldEdit.java index 56eb8fca8..7f70adcf4 100644 --- a/src/WorldEditSM.java +++ b/src/SMWorldEdit.java @@ -26,11 +26,21 @@ import com.sk89q.worldedit.ServerInterface; * * @author sk89q */ -public class WorldEditSM extends Plugin { +public class SMWorldEdit extends Plugin { + /** + * WorldEdit's properties file. + */ private PropertiesFile properties; - private static final WorldEdit worldEdit = new WorldEdit(); - private static final WorldEditSMListener listener = - new WorldEditSMListener(worldEdit); + /** + * WorldEdit instance. + */ + private static final WorldEdit worldEdit = + WorldEdit.setup(new SMServerInterface()); + /** + * Listener for the plugin system. + */ + private static final SMWorldEditListener listener = + new SMWorldEditListener(); /** * Initializes the plugin. @@ -49,10 +59,6 @@ public class WorldEditSM extends Plugin { PluginListener.Priority.MEDIUM); loader.addListener(PluginLoader.Hook.LOGIN, listener, this, PluginListener.Priority.MEDIUM); - - ServerInterface server = new SMServerInterface(); - WorldEditPlayer.server = server; - EditSession.server = server; } /** diff --git a/src/WorldEditSMListener.java b/src/SMWorldEditListener.java similarity index 93% rename from src/WorldEditSMListener.java rename to src/SMWorldEditListener.java index 1e7958154..a642e7a0e 100644 --- a/src/WorldEditSMListener.java +++ b/src/SMWorldEditListener.java @@ -24,27 +24,14 @@ import com.sk89q.worldedit.*; * * @author sk89q */ -public class WorldEditSMListener extends PluginListener { - /** - * Stores a reference to the WorldEdit object. - */ - private WorldEdit worldEdit; - - /** - * Construct the listener with a reference to the WorldEdit object. - * - * @param worldEdit - */ - public WorldEditSMListener(WorldEdit worldEdit) { - this.worldEdit = worldEdit; - } - +public class SMWorldEditListener extends PluginListener { /** * * @param player */ @Override public void onDisconnect(Player player) { + WorldEdit worldEdit = WorldEdit.getInstance(); worldEdit.removeSession(new SMWorldEditPlayer(player)); } @@ -60,6 +47,7 @@ public class WorldEditSMListener extends PluginListener { @Override public boolean onBlockCreate(Player modPlayer, Block blockPlaced, Block blockClicked, int itemInHand) { + WorldEdit worldEdit = WorldEdit.getInstance(); WorldEditPlayer player = new SMWorldEditPlayer(modPlayer); if (itemInHand != 271) { return false; } @@ -94,6 +82,7 @@ public class WorldEditSMListener extends PluginListener { if (!modPlayer.canUseCommand("/editpos1") && !modPlayer.canUseCommand("/.")) { return false; } + WorldEdit worldEdit = WorldEdit.getInstance(); WorldEditPlayer player = new SMWorldEditPlayer(modPlayer); WorldEditSession session = worldEdit.getSession(player); @@ -133,6 +122,8 @@ public class WorldEditSMListener extends PluginListener { */ @Override public boolean onCommand(Player modPlayer, String[] split) { + WorldEdit worldEdit = WorldEdit.getInstance(); + try { if (worldEdit.getCommands().containsKey(split[0])) { if (modPlayer.canUseCommand(split[0])) { diff --git a/src/WorldEdit.java b/src/WorldEdit.java index 3e940dee0..858a33fc0 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -33,6 +33,15 @@ import com.sk89q.worldedit.*; * @author sk89q */ public class WorldEdit { + /** + * WorldEdit instance. + */ + private static WorldEdit instance; + /** + * Server interface. + */ + private ServerInterface server; + /** * List of default allowed blocks. */ @@ -65,10 +74,41 @@ public class WorldEdit { */ private int defaultChangeLimit = -1; + /** + * Set up an instance. + * + * @param server + * @return + */ + public static WorldEdit setup(ServerInterface server) { + WorldEdit worldEdit = new WorldEdit(); + worldEdit.server = server; + instance = worldEdit; + return worldEdit; + } + + /** + * Get WorldEdit instance. + * + * @return + */ + public static WorldEdit getInstance() { + return instance; + } + + /** + * Get server interface. + * + * @return + */ + public static ServerInterface getServer() { + return instance.server; + } + /** * Construct an instance of the plugin. */ - public WorldEdit() { + private WorldEdit() { commands.put("/editpos1", "Set editing position #1"); commands.put("/editpos2", "Set editing position #2"); commands.put("/toggleplace", "Toggle placing at pos #1"); diff --git a/src/WorldEditPlayer.java b/src/WorldEditPlayer.java index d0d9c8f25..87a508b7e 100644 --- a/src/WorldEditPlayer.java +++ b/src/WorldEditPlayer.java @@ -28,7 +28,14 @@ public abstract class WorldEditPlayer { /** * Server interface. */ - public static ServerInterface server; + protected ServerInterface server; + + /** + * Construct the player. + */ + public WorldEditPlayer() { + server = WorldEdit.getServer(); + } /** * Get the name of the player.