From 3bec3c169c3e6a938715b7b86e59cb6a2c672e57 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Thu, 15 Dec 2011 12:02:00 +0100 Subject: [PATCH] Added BukkitCommandSender and WorldEditPlugin.wrapCommandSender. --- .../java/com/sk89q/worldedit/LocalPlayer.java | 4 + .../worldedit/PlayerNeededException.java | 13 ++ .../worldedit/bukkit/BukkitCommandSender.java | 111 ++++++++++++++++++ .../worldedit/bukkit/WorldEditPlugin.java | 8 ++ 4 files changed, 136 insertions(+) create mode 100644 src/main/java/com/sk89q/worldedit/PlayerNeededException.java create mode 100644 src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java diff --git a/src/main/java/com/sk89q/worldedit/LocalPlayer.java b/src/main/java/com/sk89q/worldedit/LocalPlayer.java index 715723e67..aa76e849b 100644 --- a/src/main/java/com/sk89q/worldedit/LocalPlayer.java +++ b/src/main/java/com/sk89q/worldedit/LocalPlayer.java @@ -657,4 +657,8 @@ public abstract class LocalPlayer { throw new WorldEditPermissionException(); } } + + public boolean isPlayer() { + return true; + } } diff --git a/src/main/java/com/sk89q/worldedit/PlayerNeededException.java b/src/main/java/com/sk89q/worldedit/PlayerNeededException.java new file mode 100644 index 000000000..eac92f96c --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/PlayerNeededException.java @@ -0,0 +1,13 @@ +package com.sk89q.worldedit; + +/** + * Thrown when an operation is run that needs an actual player, not a console. + * + */ +public class PlayerNeededException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public PlayerNeededException() { + super("This command cannot be run on the console."); + } +} diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java new file mode 100644 index 000000000..f6a27eaf8 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitCommandSender.java @@ -0,0 +1,111 @@ +package com.sk89q.worldedit.bukkit; + +import java.util.logging.Level; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.sk89q.worldedit.LocalPlayer; +import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.PlayerNeededException; +import com.sk89q.worldedit.ServerInterface; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldVector; +import com.sk89q.worldedit.bags.BlockBag; + +public class BukkitCommandSender extends LocalPlayer { + private CommandSender sender; + private WorldEditPlugin plugin; + + public BukkitCommandSender(WorldEditPlugin plugin, ServerInterface server, CommandSender sender) { + super(server); + this.plugin = plugin; + this.sender = sender; + } + + @Override + public String getName() { + return sender.getName(); + } + + @Override + public void printRaw(String msg) { + System.out.println(msg); + } + + @Override + public void printDebug(String msg) { + Bukkit.getLogger().log(Level.WARNING, msg); + + } + + @Override + public void print(String msg) { + Bukkit.getLogger().info(msg); + } + + @Override + public void printError(String msg) { + Bukkit.getLogger().log(Level.SEVERE, msg); + } + + @Override + public String[] getGroups() { + return new String[0]; + } + + @Override + public boolean hasPermission(String perm) { + if (!plugin.getLocalConfiguration().noOpPermissions && sender.isOp()) { + return true; + } + + return plugin.getPermissionsResolver().hasPermission(null, sender.getName(), perm); + } + + @Override + public boolean isPlayer() { + return sender instanceof Player; + } + + @Override + public int getItemInHand() { + throw new PlayerNeededException(); + } + + @Override + public WorldVector getPosition() { + throw new PlayerNeededException(); + } + + @Override + public LocalWorld getWorld() { + throw new PlayerNeededException(); + } + + @Override + public double getPitch() { + throw new PlayerNeededException(); + } + + @Override + public double getYaw() { + throw new PlayerNeededException(); + } + + @Override + public void giveItem(int type, int amt) { + throw new PlayerNeededException(); + } + + @Override + public void setPosition(Vector pos, float pitch, float yaw) { + throw new PlayerNeededException(); + } + + @Override + public BlockBag getInventoryBlockBag() { + throw new PlayerNeededException(); + } +} diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index b18611113..6f72c3dc8 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -345,6 +345,14 @@ public class WorldEditPlugin extends JavaPlugin { return new BukkitPlayer(this, this.server, player); } + public LocalPlayer wrapCommandSender(CommandSender sender) { + if (sender instanceof Player) { + return wrapPlayer((Player) sender); + } + + return new BukkitCommandSender(this, this.server, sender); + } + /** * Get the server interface. *