Added BukkitCommandSender and WorldEditPlugin.wrapCommandSender.

This commit is contained in:
TomyLobo 2011-12-15 12:02:00 +01:00
parent 6d4b4718db
commit 3bec3c169c
4 changed files with 136 additions and 0 deletions

View File

@ -657,4 +657,8 @@ public void checkPermission(String permission) throws WorldEditPermissionExcepti
throw new WorldEditPermissionException();
}
}
public boolean isPlayer() {
return true;
}
}

View File

@ -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.");
}
}

View File

@ -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();
}
}

View File

@ -345,6 +345,14 @@ public BukkitPlayer wrapPlayer(Player player) {
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.
*