Abstracted permissions. Cleaned up DynmapPlugin.

This commit is contained in:
FrozenCow 2011-03-11 16:05:54 +01:00
parent 47e1f20a15
commit 10e03c4219
4 changed files with 131 additions and 83 deletions

View File

@ -7,9 +7,11 @@ import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Timer; import java.util.Timer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -30,14 +32,15 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.WorldEvent; import org.bukkit.event.world.WorldEvent;
import org.bukkit.event.world.WorldListener; import org.bukkit.event.world.WorldListener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
import org.dynmap.Event.Listener; import org.dynmap.Event.Listener;
import org.dynmap.debug.Debug; import org.dynmap.debug.Debug;
import org.dynmap.debug.Debugger; import org.dynmap.debug.Debugger;
import org.dynmap.permissions.NijikokunPermissions;
import org.dynmap.permissions.OpPermissions;
import org.dynmap.permissions.PermissionProvider;
import org.dynmap.web.HttpServer; import org.dynmap.web.HttpServer;
import org.dynmap.web.Json; import org.dynmap.web.Json;
import org.dynmap.web.handlers.ClientConfigurationHandler; import org.dynmap.web.handlers.ClientConfigurationHandler;
@ -46,28 +49,7 @@ import org.dynmap.web.handlers.FilesystemHandler;
import org.dynmap.web.handlers.SendMessageHandler; import org.dynmap.web.handlers.SendMessageHandler;
import org.dynmap.web.handlers.SendMessageHandler.Message; import org.dynmap.web.handlers.SendMessageHandler.Message;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
public class DynmapPlugin extends JavaPlugin { public class DynmapPlugin extends JavaPlugin {
public static PermissionHandler Permissions = null;
public void setupPermissions() {
Plugin scrap = this.getServer().getPluginManager().getPlugin("Permissions");
PluginDescriptionFile pdfFile = this.getDescription();
if (this.Permissions == null) {
if (scrap!= null) {
this.getServer().getPluginManager().enablePlugin(scrap);
this.Permissions = ((Permissions) scrap).getHandler();
}
else {
System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " not enabled. Permissions not detected");
this.getServer().getPluginManager().disablePlugin(this);
}
}
}
protected static final Logger log = Logger.getLogger("Minecraft"); protected static final Logger log = Logger.getLogger("Minecraft");
public HttpServer webServer = null; public HttpServer webServer = null;
@ -76,6 +58,8 @@ public class DynmapPlugin extends JavaPlugin {
public Configuration configuration; public Configuration configuration;
public HashSet<String> enabledTriggers = new HashSet<String>(); public HashSet<String> enabledTriggers = new HashSet<String>();
public PermissionProvider permissions;
public Timer timer; public Timer timer;
public static File dataDirectory; public static File dataDirectory;
@ -94,8 +78,9 @@ public class DynmapPlugin extends JavaPlugin {
} }
public void onEnable() { public void onEnable() {
permissions = NijikokunPermissions.create(getServer(), "dynmap");
setupPermissions(); if (permissions == null)
permissions = new OpPermissions(new String[] { "fullrender" });
dataDirectory = this.getDataFolder(); dataDirectory = this.getDataFolder();
@ -307,6 +292,12 @@ public class DynmapPlugin extends JavaPlugin {
} }
} }
private static final Set<String> commands = new HashSet<String>(Arrays.asList(new String[] {
"render",
"hide",
"show",
"fullrender" }));
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (!cmd.getName().equalsIgnoreCase("dynmap")) if (!cmd.getName().equalsIgnoreCase("dynmap"))
@ -315,19 +306,24 @@ public class DynmapPlugin extends JavaPlugin {
if (sender instanceof Player) if (sender instanceof Player)
player = (Player) sender; player = (Player) sender;
if (args.length > 0) { if (args.length > 0) {
if (args[0].equals("render")) { String c = args[0];
if (player == null || DynmapPlugin.Permissions.has((Player) sender, "dynmap.render") || DynmapPlugin.Permissions.has((Player) sender, "dynmap.*")) { if (!commands.contains(c)) {
if (sender instanceof Player) { return false;
int invalidates = mapManager.touch(((Player) sender).getLocation()); }
sender.sendMessage("Queued " + invalidates + " tiles" + (invalidates == 0 ? " (world is not loaded?)" : "...")); if (!permissions.has(sender, c.toLowerCase())) {
sender.sendMessage("You don't have permission to use this command!");
return true; return true;
} }
} else if (player != null) {
player.sendMessage("You don't have permission to use this command!"); if (c.equals("render")) {
if (player != null) {
int invalidates = mapManager.touch(player.getLocation());
sender.sendMessage("Queued " + invalidates + " tiles" + (invalidates == 0
? " (world is not loaded?)"
: "..."));
return true; return true;
} }
} else if (args[0].equals("hide")) { } else if (c.equals("hide")) {
if (player == null || DynmapPlugin.Permissions.has((Player) sender, "dynmap.showhide") || DynmapPlugin.Permissions.has((Player) sender, "dynmap.*")) {
if (args.length == 1 && player != null) { if (args.length == 1 && player != null) {
playerList.hide(player.getName()); playerList.hide(player.getName());
sender.sendMessage("You are now hidden on Dynmap."); sender.sendMessage("You are now hidden on Dynmap.");
@ -339,12 +335,7 @@ public class DynmapPlugin extends JavaPlugin {
} }
return true; return true;
} }
} else if (player != null) { } else if (c.equals("show")) {
player.sendMessage("You don't have permission to use this command!");
return true;
}
} else if (args[0].equals("show")) {
if (player == null || DynmapPlugin.Permissions.has((Player) sender, "dynmap.showhide") || DynmapPlugin.Permissions.has((Player) sender, "dynmap.*")) {
if (args.length == 1 && player != null) { if (args.length == 1 && player != null) {
playerList.show(player.getName()); playerList.show(player.getName());
sender.sendMessage("You are now visible on Dynmap."); sender.sendMessage("You are now visible on Dynmap.");
@ -356,12 +347,7 @@ public class DynmapPlugin extends JavaPlugin {
} }
return true; return true;
} }
} else if (player != null) { } else if (c.equals("fullrender")) {
player.sendMessage("You don't have permission to use this command!");
return true;
}
} else if (args[0].equals("fullrender")) {
if (player == null || DynmapPlugin.Permissions.has((Player) sender, "dynmap.fullrender") || DynmapPlugin.Permissions.has((Player) sender, "dynmap.*")) {
if (args.length > 1) { if (args.length > 1) {
for (int i = 1; i < args.length; i++) { for (int i = 1; i < args.length; i++) {
World w = getServer().getWorld(args[i]); World w = getServer().getWorld(args[i]);
@ -372,10 +358,6 @@ public class DynmapPlugin extends JavaPlugin {
mapManager.renderFullWorld(player.getLocation()); mapManager.renderFullWorld(player.getLocation());
return true; return true;
} }
} else if (player != null) {
player.sendMessage("You don't have permission to use this command!");
return true;
}
} }
} }
return false; return false;
@ -401,8 +383,7 @@ public class DynmapPlugin extends JavaPlugin {
} }
} }
public void webChat(String name, String message) public void webChat(String name, String message) {
{
mapManager.pushUpdate(new Client.WebChatMessage(name, message)); mapManager.pushUpdate(new Client.WebChatMessage(name, message));
log.info("[WEB]" + name + ": " + message); log.info("[WEB]" + name + ": " + message);
getServer().broadcastMessage("[WEB]" + name + ": " + message); getServer().broadcastMessage("[WEB]" + name + ": " + message);

View File

@ -0,0 +1,35 @@
package org.dynmap.permissions;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
public class NijikokunPermissions implements PermissionProvider {
String name;
PermissionHandler permissions;
public static NijikokunPermissions create(Server server, String name) {
Plugin permissionsPlugin = server.getPluginManager().getPlugin("Permissions");
if (permissionsPlugin == null)
return null;
server.getPluginManager().enablePlugin(permissionsPlugin);
return new NijikokunPermissions(permissionsPlugin, name);
}
public NijikokunPermissions(Plugin permissionsPlugin, String name) {
this.name = name;
permissions = ((Permissions) permissionsPlugin).getHandler();
}
@Override
public boolean has(CommandSender sender, String permission) {
Player player = sender instanceof Player ? (Player) sender : null;
return player != null
? permissions.has(player, name + "." + permission) || permissions.has(player, name + ".*")
: true;
}
}

View File

@ -0,0 +1,25 @@
package org.dynmap.permissions;
import java.util.HashSet;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class OpPermissions implements PermissionProvider {
public HashSet<String> opCommands = new HashSet<String>();
public OpPermissions(String[] opCommands) {
for (String opCommand : opCommands) {
this.opCommands.add(opCommand);
}
}
@Override
public boolean has(CommandSender sender, String permission) {
return (sender instanceof Player)
? opCommands.contains(permission)
? ((Player) sender).isOp()
: true
: true;
}
}

View File

@ -0,0 +1,7 @@
package org.dynmap.permissions;
import org.bukkit.command.CommandSender;
public interface PermissionProvider {
boolean has(CommandSender sender, String permission);
}