Plex-FAWE/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java

431 lines
14 KiB
Java
Raw Normal View History

2011-01-01 02:36:25 +00:00
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
2011-01-01 02:36:25 +00:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.bukkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Handler;
import java.util.zip.ZipEntry;
import org.bukkit.Bukkit;
import org.bukkit.World;
2011-01-30 04:38:41 +00:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
2011-01-01 02:36:25 +00:00
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.wepif.PermissionsResolverManager;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.ServerInterface;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditOperation;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection;
import com.sk89q.worldedit.bukkit.selections.Selection;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
2011-01-01 02:36:25 +00:00
2011-01-08 21:03:18 +00:00
/**
* Plugin for Bukkit.
2012-03-02 05:32:33 +00:00
*
2011-02-22 06:28:23 +00:00
* @author sk89q
2011-01-08 21:03:18 +00:00
*/
2011-01-01 02:36:25 +00:00
public class WorldEditPlugin extends JavaPlugin {
/**
* The name of the CUI's plugin channel registration
*/
public static final String CUI_PLUGIN_CHANNEL = "WECUI";
2011-02-22 06:28:23 +00:00
/**
* The server interface that all server-related API goes through.
*/
private BukkitServerInterface server;
2011-02-22 06:28:23 +00:00
/**
* Main WorldEdit instance.
*/
2011-04-01 23:18:40 +00:00
private WorldEdit controller;
2011-02-22 06:28:23 +00:00
/**
* Deprecated API.
*/
2011-04-01 23:18:40 +00:00
private WorldEditAPI api;
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
/**
* Holds the configuration for WorldEdit.
*/
private BukkitConfiguration config;
2011-01-01 02:36:25 +00:00
2011-02-22 06:28:23 +00:00
/**
* Called on plugin enable.
*/
@Override
public void onEnable() {
final String pluginYmlVersion = getDescription().getVersion();
final String manifestVersion = WorldEdit.getVersion();
if (!manifestVersion.equalsIgnoreCase(pluginYmlVersion)) {
WorldEdit.setVersion(manifestVersion + " (" + pluginYmlVersion + ")");
}
2011-02-22 06:28:23 +00:00
// Make the data folders that WorldEdit uses
getDataFolder().mkdirs();
File targetDir = new File(getDataFolder() + File.separator + "nmsblocks");
targetDir.mkdir();
copyNmsBlockClasses(targetDir);
2011-02-22 06:28:23 +00:00
// Create the default configuration file
createDefaultConfiguration("config.yml");
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
// Set up configuration and such, including the permissions
// resolver
config = new BukkitConfiguration(new YAMLProcessor(new File(getDataFolder(), "config.yml"), true), this);
PermissionsResolverManager.initialize(this);
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
// Load the configuration
2012-03-02 05:32:33 +00:00
config.load();
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
// Setup interfaces
2011-02-19 09:22:28 +00:00
server = new BukkitServerInterface(this, getServer());
controller = new WorldEdit(server, config);
2013-06-22 03:54:28 +00:00
WorldEdit.logger.setParent(Bukkit.getLogger());
api = new WorldEditAPI(this);
getServer().getMessenger().registerIncomingPluginChannel(this, CUI_PLUGIN_CHANNEL, new CUIChannelListener(this));
getServer().getMessenger().registerOutgoingPluginChannel(this, CUI_PLUGIN_CHANNEL);
2011-02-22 06:28:23 +00:00
// Now we can register events!
2012-01-18 04:35:34 +00:00
getServer().getPluginManager().registerEvents(new WorldEditListener(this), this);
2011-11-23 01:29:48 +00:00
2013-06-22 03:54:28 +00:00
getServer().getScheduler().runTaskTimerAsynchronously(this,
new SessionTimer(controller, getServer()), 120, 120);
}
private void copyNmsBlockClasses(File target) {
try {
JarFile jar = new JarFile(getFile());
2013-06-22 03:54:28 +00:00
@SuppressWarnings("rawtypes")
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) entries.nextElement();
if (!jarEntry.getName().startsWith("nmsblocks") || jarEntry.isDirectory()) continue;
File file = new File(target + File.separator + jarEntry.getName().replace("nmsblocks", ""));
if (file.exists()) continue;
InputStream is = jar.getInputStream(jarEntry);
FileOutputStream fos = new FileOutputStream(file);
fos = new FileOutputStream(file);
byte[] buf = new byte[8192];
int length = 0;
while ((length = is.read(buf)) > 0) {
fos.write(buf, 0, length);
}
fos.close();
is.close();
}
} catch (Throwable e) {}
2011-01-01 03:28:28 +00:00
}
2011-02-22 06:28:23 +00:00
/**
* Called on plugin disable.
*/
@Override
2011-01-01 02:36:25 +00:00
public void onDisable() {
controller.clearSessions();
for (Handler h : controller.commandLogger.getHandlers()) {
h.close();
}
config.unload();
server.unregisterCommands();
this.getServer().getScheduler().cancelTasks(this);
2011-01-01 02:36:25 +00:00
}
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
/**
* Loads and reloads all configuration.
*/
protected void loadConfiguration() {
config.unload();
2011-02-22 06:28:23 +00:00
config.load();
getPermissionsResolver().load();
2011-02-22 06:28:23 +00:00
}
2011-01-01 02:36:25 +00:00
2011-02-22 06:28:23 +00:00
/**
* Create a default configuration file from the .jar.
2012-03-02 05:32:33 +00:00
*
2011-02-22 06:28:23 +00:00
* @param name
*/
protected void createDefaultConfiguration(String name) {
File actual = new File(getDataFolder(), name);
if (!actual.exists()) {
InputStream input =
null;
try {
JarFile file = new JarFile(getFile());
ZipEntry copy = file.getEntry("defaults/" + name);
if (copy == null) throw new FileNotFoundException();
input = file.getInputStream(copy);
} catch (IOException e) {
getLogger().severe("Unable to read default configuration: " + name);
}
if (input != null) {
FileOutputStream output = null;
try {
output = new FileOutputStream(actual);
byte[] buf = new byte[8192];
int length = 0;
while ((length = input.read(buf)) > 0) {
output.write(buf, 0, length);
}
2011-11-23 01:29:48 +00:00
getLogger().info("Default configuration file written: " + name);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
2011-11-23 01:29:48 +00:00
if (input != null) {
input.close();
2011-11-23 01:29:48 +00:00
}
} catch (IOException e) {}
try {
2011-11-23 01:29:48 +00:00
if (output != null) {
output.close();
2011-11-23 01:29:48 +00:00
}
} catch (IOException e) {}
}
}
}
}
2011-01-30 04:38:41 +00:00
2011-02-22 06:28:23 +00:00
/**
* Called on WorldEdit command.
*/
@Override
public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd,
2011-01-30 04:38:41 +00:00
String commandLabel, String[] args) {
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
// Add the command to the array because the underlying command handling
// code of WorldEdit expects it
String[] split = new String[args.length + 1];
System.arraycopy(args, 0, split, 1, args.length);
split[0] = "/" + cmd.getName();
2011-11-23 01:29:48 +00:00
controller.handleCommand(wrapCommandSender(sender), split);
2011-11-23 01:29:48 +00:00
return true;
}
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
/**
* Gets the session for the player.
2012-03-02 05:32:33 +00:00
*
2011-02-19 05:12:15 +00:00
* @param player
* @return
*/
public LocalSession getSession(Player player) {
return controller.getSession(wrapPlayer(player));
}
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
/**
* Gets the session for the player.
2012-03-02 05:32:33 +00:00
*
2011-02-19 05:12:15 +00:00
* @param player
* @return
*/
public EditSession createEditSession(Player player) {
LocalPlayer wePlayer = wrapPlayer(player);
LocalSession session = controller.getSession(wePlayer);
BlockBag blockBag = session.getBlockBag(wePlayer);
2011-11-23 01:29:48 +00:00
2012-10-18 10:34:09 +00:00
EditSession editSession = controller.getEditSessionFactory()
.getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag, wePlayer);
2011-02-19 05:12:15 +00:00
editSession.enableQueue();
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
return editSession;
}
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
/**
* Remember an edit session.
2012-03-02 05:32:33 +00:00
*
2011-02-19 05:12:15 +00:00
* @param player
* @param editSession
*/
public void remember(Player player, EditSession editSession) {
LocalPlayer wePlayer = wrapPlayer(player);
LocalSession session = controller.getSession(wePlayer);
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
session.remember(editSession);
editSession.flushQueue();
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
controller.flushBlockBag(wePlayer, editSession);
}
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
/**
* Wrap an operation into an EditSession.
2012-03-02 05:32:33 +00:00
*
2011-02-19 05:12:15 +00:00
* @param player
* @param op
* @throws Throwable
*/
public void perform(Player player, WorldEditOperation op)
throws Throwable {
LocalPlayer wePlayer = wrapPlayer(player);
LocalSession session = controller.getSession(wePlayer);
2011-11-23 01:29:48 +00:00
2011-02-19 05:12:15 +00:00
EditSession editSession = createEditSession(player);
try {
op.run(session, wePlayer, editSession);
} finally {
remember(player, editSession);
}
}
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
/**
* Get the API.
2012-03-02 05:32:33 +00:00
*
2011-02-22 06:28:23 +00:00
* @return
*/
2011-02-19 05:12:15 +00:00
@Deprecated
public WorldEditAPI getAPI() {
return api;
}
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
/**
* Returns the configuration used by WorldEdit.
2012-03-02 05:32:33 +00:00
*
2011-02-22 06:28:23 +00:00
* @return
*/
public BukkitConfiguration getLocalConfiguration() {
return config;
}
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
/**
* Get the permissions resolver in use.
2012-03-02 05:32:33 +00:00
*
2011-02-22 06:28:23 +00:00
* @return
*/
public PermissionsResolverManager getPermissionsResolver() {
return PermissionsResolverManager.getInstance();
}
2011-11-23 01:29:48 +00:00
2011-02-22 06:28:23 +00:00
/**
* Used to wrap a Bukkit Player as a LocalPlayer.
2012-03-02 05:32:33 +00:00
*
2011-02-22 06:28:23 +00:00
* @param player
* @return
*/
public BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(this, this.server, player);
}
2011-11-23 01:29:48 +00:00
public LocalPlayer wrapCommandSender(CommandSender sender) {
if (sender instanceof Player) {
return wrapPlayer((Player) sender);
}
return new BukkitCommandSender(this, this.server, sender);
}
2012-03-02 05:32:33 +00:00
2011-04-01 23:18:40 +00:00
/**
* Get the server interface.
2012-03-02 05:32:33 +00:00
*
2011-04-01 23:18:40 +00:00
* @return
*/
public ServerInterface getServerInterface() {
return server;
}
2011-11-23 01:29:48 +00:00
2011-04-01 23:18:40 +00:00
/**
* Get WorldEdit.
2012-03-02 05:32:33 +00:00
*
2011-04-01 23:18:40 +00:00
* @return
*/
public WorldEdit getWorldEdit() {
return controller;
}
2011-11-23 01:29:48 +00:00
/**
* Gets the region selection for the player.
2012-03-02 05:32:33 +00:00
*
* @param player
* @return the selection or null if there was none
*/
public Selection getSelection(Player player) {
2011-04-03 18:03:57 +00:00
if (player == null) {
throw new IllegalArgumentException("Null player not allowed");
}
if (!player.isOnline()) {
throw new IllegalArgumentException("Offline player not allowed");
}
2011-11-23 01:29:48 +00:00
LocalSession session = controller.getSession(wrapPlayer(player));
2011-12-13 03:20:31 +00:00
RegionSelector selector = session.getRegionSelector(BukkitUtil.getLocalWorld(player.getWorld()));
2011-11-23 01:29:48 +00:00
try {
Region region = selector.getRegion();
World world = ((BukkitWorld) session.getSelectionWorld()).getWorld();
2011-11-23 01:29:48 +00:00
if (region instanceof CuboidRegion) {
2011-11-23 01:29:48 +00:00
return new CuboidSelection(world, selector, (CuboidRegion) region);
} else if (region instanceof Polygonal2DRegion) {
2011-11-23 01:29:48 +00:00
return new Polygonal2DSelection(world, selector, (Polygonal2DRegion) region);
} else {
return null;
}
} catch (IncompleteRegionException e) {
return null;
}
}
2011-11-23 01:29:48 +00:00
2011-04-03 18:03:57 +00:00
/**
* Sets the region selection for a player.
2012-03-02 05:32:33 +00:00
*
2011-04-03 18:03:57 +00:00
* @param player
* @param selection
*/
public void setSelection(Player player, Selection selection) {
if (player == null) {
throw new IllegalArgumentException("Null player not allowed");
}
if (!player.isOnline()) {
throw new IllegalArgumentException("Offline player not allowed");
}
if (selection == null) {
throw new IllegalArgumentException("Null selection not allowed");
}
2011-11-23 01:29:48 +00:00
LocalSession session = controller.getSession(wrapPlayer(player));
2011-04-03 18:03:57 +00:00
RegionSelector sel = selection.getRegionSelector();
session.setRegionSelector(BukkitUtil.getLocalWorld(player.getWorld()), sel);
2011-04-03 18:03:57 +00:00
session.dispatchCUISelection(wrapPlayer(player));
}
2011-01-01 02:36:25 +00:00
}