Added untested support for Vault permission handling.

This commit is contained in:
Tobias Ottenweller 2013-02-10 01:05:12 +01:00
parent cab78c34cc
commit 58695cadd3
15 changed files with 190 additions and 63 deletions

View File

@ -10,12 +10,15 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import de.craftinc.gates.commands.*;
@ -30,9 +33,11 @@ public class Plugin extends JavaPlugin
public static final String permissionInfo = "craftincgates.info";
public static final String permissionManage = "craftincgates.manage";
public static final String permissionAll = "craftincgates.*";
// public static final String permissionAll = "craftincgates.*";
public static final String permissionUse = "craftincgates.use";
public static Permission permission = null;
public PluginPlayerListener playerListener = new PluginPlayerListener();
public PluginBlockListener blockListener = new PluginBlockListener();
public PluginPortalListener portalListener = new PluginPortalListener();
@ -55,10 +60,26 @@ public class Plugin extends JavaPlugin
@Override
public void onLoad()
{
setupPermissions();
ConfigurationSerialization.registerClass(Gate.class);
}
private void setupPermissions()
{
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return;
}
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
if (rsp != null)
{
permission = rsp.getProvider();
}
}
@Override
public void onDisable()
{

View File

@ -12,24 +12,26 @@ import de.craftinc.gates.util.TextUtil;
public abstract class BaseCommand
{
public List<String> aliases;
public List<String> requiredParameters;
public List<String> optionalParameters;
protected List<String> aliases;
protected List<String> requiredParameters;
protected List<String> optionalParameters;
public String helpDescription;
protected String helpDescription;
public CommandSender sender;
public boolean senderMustBePlayer;
public boolean hasGateParam;
public Player player;
public Gate gate;
protected List<String> parameters;
protected CommandSender sender;
protected Player player;
protected Gate gate;
public List<String> parameters;
protected boolean senderMustBePlayer;
protected boolean hasGateParam;
public String requiredPermission;
protected String requiredPermission;
protected boolean needsPermissionAtCurrentLocation;
public BaseCommand() {
public BaseCommand()
{
aliases = new ArrayList<String>();
requiredParameters = new ArrayList<String>();
optionalParameters = new ArrayList<String>();
@ -44,11 +46,12 @@ public abstract class BaseCommand
return aliases;
}
public void execute(CommandSender sender, List<String> parameters) {
this.sender = sender;
this.parameters = parameters;
if ( ! validateCall()) {
if (!this.validateCall()) {
return;
}
@ -56,79 +59,146 @@ public abstract class BaseCommand
this.player = (Player)sender;
}
perform();
this.perform();
}
public void perform() {
}
abstract protected void perform();
public void sendMessage(String message) {
protected void sendMessage(String message) {
sender.sendMessage(message);
}
public void sendMessage(List<String> messages) {
protected void sendMessage(List<String> messages) {
for(String message : messages) {
this.sendMessage(message);
}
}
public boolean validateCall()
protected boolean validateCall()
{
// validate player
if ( this.senderMustBePlayer && ! (sender instanceof Player))
{
sendMessage("This command can only be used by ingame players.");
return false;
boolean allParamtertersThere = parameters.size() < requiredParameters.size();
boolean senderIsPlayer = this.sender instanceof Player;
boolean parameterIsGate = this.getGateForParamater(this.parameters.get(0));
boolean senderHasPermission;
try {
senderHasPermission = this.hasPermission();
}
catch (Exception e) { // the gate paramter is missing or incorrect!
senderHasPermission = parameterIsGate ? false : true; // only display the lack of permission message if there is a gate
// this should prevent giving permission to the user if there is
// a bug inside the permission validation code.
}
// validate permission
if( !hasPermission(sender))
if(!senderHasPermission)
{
sendMessage("You lack the permissions to " + this.helpDescription.toLowerCase() + ".");
return false;
}
// valide parameter count
if (parameters.size() < requiredParameters.size())
if (this.senderMustBePlayer && !senderIsPlayer)
{
sendMessage("This command can only be used by ingame players.");
return false;
}
if (this.hasGateParam && !parameterIsGate)
{
sendMessage("There exists no gate with id " + this.parameters.get(0));
return false;
}
if (allParamtertersThere)
{
sendMessage("Usage: " + this.getUseageTemplate(true));
return false;
}
// validate gate parameter
if (this.hasGateParam)
{
String id = parameters.get(0);
return true;
}
if ( ! Gate.exists(id))
protected boolean getGateForParamater(String param)
{
if (!Gate.exists(param))
{
sendMessage("There exists no gate with id "+id);
return false;
}
gate = Gate.get(id);
}
return true;
}
public boolean hasPermission(CommandSender sender)
else
{
if (sender.hasPermission(Plugin.permissionAll)) {
gate = Gate.get(param);
return true;
}
}
if (sender.hasPermission(requiredPermission)) {
return true;
protected boolean hasPermission() throws Exception
{
if (Plugin.permission == null) // fallback Ð use the standard bukkit permission system
{
return this.sender.hasPermission(this.requiredPermission);
}
if (this.requiredPermission.equals(Plugin.permissionInfo))
{
return Plugin.permission.has(this.player.getWorld(), this.player.getName(), this.requiredPermission);
}
if (this.requiredPermission.equals(Plugin.permissionUse) )
{
return this.hasPermissionAtGateLocationAndExit();
}
if (this.requiredPermission.equals(Plugin.permissionManage))
{
if (this.needsPermissionAtCurrentLocation && this.hasGateParam)
{
boolean hasPersmissionAtCurrentLocation = Plugin.permission.has(this.player.getWorld(), this.player.getName(), this.requiredPermission);
return hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit();
}
else if (this.needsPermissionAtCurrentLocation)
{
return Plugin.permission.has(this.player.getWorld(), this.player.getName(), this.requiredPermission);
}
else
{
return this.hasPermissionAtGateLocationAndExit();
}
}
return false;
}
protected boolean hasPermissionAtGateLocationAndExit() throws Exception
{
if (this.gate == null) // make sure we don't run into a nullpointer exception
{
throw new Exception("Cannot check permissons with no gate provided!");
}
boolean permAtLocation = Plugin.permission.has(this.gate.getLocation().getWorld(), player.getName(), this.requiredPermission);
boolean permAtExit = Plugin.permission.has(this.gate.getExit().getWorld(), player.getName(), this.requiredPermission);
return permAtLocation && permAtExit;
}
// -------------------------------------------- //
// Help and usage description
// -------------------------------------------- //
public String getUsageTemplate(boolean withColor, boolean withDescription) {
protected String getUsageTemplate(boolean withColor, boolean withDescription) {
String ret = "";
// if (withColor) {
@ -159,11 +229,11 @@ public abstract class BaseCommand
return ret;
}
public String getUseageTemplate(boolean withColor) {
protected String getUseageTemplate(boolean withColor) {
return getUsageTemplate(withColor, false);
}
public String getUseageTemplate() {
protected String getUseageTemplate() {
return getUseageTemplate(true);
}
}

View File

@ -14,6 +14,8 @@ public class CommandClose extends BaseCommand
helpDescription = "Closes a gate to prevent players from using it.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
}

View File

@ -21,6 +21,8 @@ public class CommandCreate extends BaseLocationCommand
helpDescription = "Create a gate at your current location.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
}

View File

@ -19,6 +19,8 @@ public class CommandDelete extends BaseCommand
helpDescription = "Removes the gate from the game.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import org.bukkit.command.CommandSender;
import de.craftinc.gates.Gate;
import de.craftinc.gates.util.TextUtil;
public class CommandHelp extends BaseCommand
@ -19,10 +20,12 @@ public class CommandHelp extends BaseCommand
hasGateParam = false;
helpDescription = "Prints a list of all availible commands.";
needsPermissionAtCurrentLocation = false;
}
@Override
public boolean hasPermission(CommandSender sender)
public boolean hasPermission(CommandSender sender, Gate gate)
{
return true;
}

View File

@ -17,6 +17,8 @@ public class CommandInfo extends BaseCommand
helpDescription = "Prints detailed informations about a certain gate.";
requiredPermission = Plugin.permissionInfo;
needsPermissionAtCurrentLocation = false;
}

View File

@ -25,6 +25,8 @@ public class CommandList extends BaseCommand
helpDescription = "Prints a list of all availible gates.";
requiredPermission = Plugin.permissionInfo;
needsPermissionAtCurrentLocation = false;
}

View File

@ -15,6 +15,8 @@ public class CommandOpen extends BaseCommand
helpDescription = "Open a gate so players can use it.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
}

View File

@ -21,6 +21,8 @@ public class CommandRename extends BaseCommand
helpDescription = "Changes the id of a gate.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
}

View File

@ -16,6 +16,8 @@ public class CommandSetExit extends BaseCommand
helpDescription = "Changes the location where the gate will teleport players to your current location.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
}

View File

@ -15,6 +15,8 @@ public class CommandSetHidden extends BaseCommand
helpDescription = "Makes a gate NOT consist of gate blocks while open.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
}

View File

@ -19,6 +19,8 @@ public class CommandSetLocation extends BaseLocationCommand
helpDescription = "Set the entrance of the gate to your current location.";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
}

View File

@ -16,6 +16,8 @@ public class CommandSetVisible extends BaseCommand
helpDescription = "Make that gate visible";
requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
}

View File

@ -25,10 +25,6 @@ public class PluginPlayerListener extends BaseLocationListener implements Listen
return;
}
// Check for permission
if (!hasPermission(event.getPlayer())) {
return;
}
// Find the gate at the current location.
Gate gateAtLocation = getValidGateAtPlayerLocation(event);
@ -38,6 +34,11 @@ public class PluginPlayerListener extends BaseLocationListener implements Listen
return;
}
// Check for permission
if (!hasPermission(event.getPlayer(), gateAtLocation)) {
return;
}
// Teleport the player
checkChunkLoad(gateAtLocation.getLocation().getBlock());
@ -72,7 +73,17 @@ public class PluginPlayerListener extends BaseLocationListener implements Listen
}
protected boolean hasPermission(Player player) {
return player.hasPermission(Plugin.permissionUse) || player.hasPermission(Plugin.permissionAll);
protected boolean hasPermission(Player player, Gate gate)
{
if (Plugin.permission == null) // fallback Ð use the standard bukkit permission system
{
return player.hasPermission(Plugin.permissionUse);
}
else {
boolean permAtLocation = Plugin.permission.has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse);
boolean permAtExit = Plugin.permission.has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse);
return permAtLocation && permAtExit;
}
}
}