From abaeaa99acc5c579d023351924a8334e0798eab7 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 18 Feb 2011 21:12:15 -0800 Subject: [PATCH] Added some API methods. --- .../sk89q/worldedit/WorldEditOperation.java | 30 +++++ .../worldedit/bukkit/WorldEditPlugin.java | 105 ++++++++++++++++-- 2 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 src/com/sk89q/worldedit/WorldEditOperation.java diff --git a/src/com/sk89q/worldedit/WorldEditOperation.java b/src/com/sk89q/worldedit/WorldEditOperation.java new file mode 100644 index 000000000..1b7805685 --- /dev/null +++ b/src/com/sk89q/worldedit/WorldEditOperation.java @@ -0,0 +1,30 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 sk89q + * + * 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 . +*/ + +package com.sk89q.worldedit; + +/** + * Represents a WorldEdit operation. + * + * @author sk89q + */ +public abstract class WorldEditOperation { + public abstract void run(LocalSession session, + LocalPlayer player, EditSession editSession) throws Throwable; +} diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index b92edb636..68f13d35a 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -36,6 +36,8 @@ import org.bukkit.plugin.java.JavaPlugin; import com.sk89q.bukkit.migration.PermissionsResolverManager; import com.sk89q.bukkit.migration.PermissionsResolverServerListener; import com.sk89q.worldedit.*; +import com.sk89q.worldedit.bags.BlockBag; +import com.sk89q.worldedit.regions.Region; /** * Plugin for Bukkit. @@ -45,9 +47,9 @@ import com.sk89q.worldedit.*; public class WorldEditPlugin extends JavaPlugin { private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit"); - public final ServerInterface server; - public final WorldEdit controller; - public final WorldEditAPI api; + private final ServerInterface server; + private final WorldEdit controller; + private final WorldEditAPI api; private final LocalConfiguration config; private final PermissionsResolverManager perms; @@ -141,7 +143,7 @@ public class WorldEditPlugin extends JavaPlugin { } } - public void loadConfiguration() { + void loadConfiguration() { getConfiguration().load(); config.load(); perms.load(); @@ -178,6 +180,97 @@ public class WorldEditPlugin extends JavaPlugin { return true; } + /** + * Get a reference to the WorldEdit object. + * + * @return + */ + public WorldEdit getWorldEdit() { + return controller; + } + + /** + * Gets the session for the player. + * + * @param player + * @return + */ + public LocalSession getSession(Player player) { + return controller.getSession(wrapPlayer(player)); + } + + /** + * Gets the region selection for the player. + * + * @param player + * @return + * @throws IncompleteRegionException + */ + public Region getPlayerSelection(Player player) + throws IncompleteRegionException { + return controller.getSession(wrapPlayer(player)).getRegion(); + } + + /** + * Gets the session for the player. + * + * @param player + * @return + */ + public EditSession createEditSession(Player player) { + LocalPlayer wePlayer = wrapPlayer(player); + LocalSession session = controller.getSession(wePlayer); + BlockBag blockBag = session.getBlockBag(wePlayer); + + EditSession editSession = + new EditSession(wePlayer.getWorld(), + session.getBlockChangeLimit(), blockBag); + editSession.enableQueue(); + + return editSession; + } + + /** + * Remember an edit session. + * + * @param player + * @param editSession + */ + public void remember(Player player, EditSession editSession) { + LocalPlayer wePlayer = wrapPlayer(player); + LocalSession session = controller.getSession(wePlayer); + + session.remember(editSession); + editSession.flushQueue(); + + controller.flushBlockBag(wePlayer, editSession); + } + + /** + * Wrap an operation into an EditSession. + * + * @param player + * @param op + * @throws Throwable + */ + public void perform(Player player, WorldEditOperation op) + throws Throwable { + LocalPlayer wePlayer = wrapPlayer(player); + LocalSession session = controller.getSession(wePlayer); + + EditSession editSession = createEditSession(player); + try { + op.run(session, wePlayer, editSession); + } finally { + remember(player, editSession); + } + } + + @Deprecated + public WorldEditAPI getAPI() { + return api; + } + String[] getGroups(Player player) { return perms.getGroups(player.getName()); } @@ -193,8 +286,4 @@ public class WorldEditPlugin extends JavaPlugin { BukkitPlayer wrapPlayer(Player player) { return new BukkitPlayer(this, this.server, player); } - - public WorldEditAPI getAPI() { - return api; - } }