Merge branch 'master' of github.com:craftinc/craftinc-gates

This commit is contained in:
Tobias Ottenweller 2013-02-10 16:17:51 +01:00
commit dae92a0357
20 changed files with 205 additions and 66 deletions

View File

@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="/Users/tobi/Code/craftbukkit-1.4.5-R1.0.jar"/>
<classpathentry kind="lib" path="/Users/tobi/Code/Vault.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,6 +1,7 @@
name: Craft Inc. Gates
version: 2.0.1a
description: A plugin to create gates for fast traveling.
softdepend: [Vault]
author: tomco, s1m0ne
authors: [oloflarsson, locutus, DrAgonmoray, s1m0ne, tomco]
website: http://www.craftinc.de/craftinc-gates/

13
pom.xml
View File

@ -19,6 +19,13 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>1.2.23-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@ -28,7 +35,7 @@
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/resources</directory>
<directory>resources</directory>
</resource>
</resources>
</build>
@ -38,5 +45,9 @@
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
<repository>
<id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything</url>
</repository>
</repositories>
</project>

1
resources/plugin.yml Symbolic link
View File

@ -0,0 +1 @@
../plugin.yml

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,9 +60,25 @@ 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>();
@ -43,12 +45,13 @@ public abstract class BaseCommand
public List<String> getAliases() {
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() {
}
public void sendMessage(String message) {
abstract protected void perform();
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))
boolean allParamtertersThere = parameters.size() < requiredParameters.size();
boolean senderIsPlayer = this.sender instanceof Player;
boolean parameterIsGate = this.parameters.size() > 0 ? this.getGateForParamater(this.parameters.get(0)) : false;
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.
}
if(!senderHasPermission)
{
sendMessage("You lack the permissions to " + this.helpDescription.toLowerCase() + ".");
return false;
}
if (this.senderMustBePlayer && !senderIsPlayer)
{
sendMessage("This command can only be used by ingame players.");
return false;
}
// validate permission
if( !hasPermission(sender))
if (this.hasGateParam && !parameterIsGate)
{
sendMessage("You lack the permissions to "+this.helpDescription.toLowerCase()+".");
sendMessage("There exists no gate with id " + this.parameters.get(0));
return false;
}
// valide parameter count
if (parameters.size() < requiredParameters.size())
if (allParamtertersThere)
{
sendMessage("Usage: "+this.getUseageTemplate(true));
sendMessage("Usage: " + this.getUseageTemplate(true));
return false;
}
// validate gate parameter
if (this.hasGateParam)
{
String id = parameters.get(0);
if ( ! Gate.exists(id))
{
sendMessage("There exists no gate with id "+id);
return false;
}
gate = Gate.get(id);
}
return true;
}
public boolean hasPermission(CommandSender sender)
protected boolean getGateForParamater(String param)
{
if (sender.hasPermission(Plugin.permissionAll)) {
if (!Gate.exists(param))
{
return false;
}
else
{
gate = Gate.get(param);
return true;
}
if (sender.hasPermission(requiredPermission)) {
return true;
}
return false;
}
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

@ -57,8 +57,6 @@ public abstract class BaseLocationListener
Block blockTo = e.getFrom().getBlock();
Block blockToUp = blockTo.getRelative(BlockFace.UP);
System.out.println(blockTo.getLocation().getWorld().getName());
for (Gate g : Gate.getAll()) {
// Check if the location matches

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;
}
}
}