diff --git a/plugin.yml b/plugin.yml index b29a27dd..25051e14 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: mcore1 version: 1 -main: com.massivecraft.core1.MCore +main: com.massivecraft.mcore1.MCore authors: [Olof Larsson] softdepend: [Vault, Spout] commands: diff --git a/src/com/massivecraft/mcore1/cmd/MCommand.java b/src/com/massivecraft/mcore1/cmd/MCommand.java index 9c99a294..f56e7783 100644 --- a/src/com/massivecraft/mcore1/cmd/MCommand.java +++ b/src/com/massivecraft/mcore1/cmd/MCommand.java @@ -9,6 +9,7 @@ import org.bukkit.entity.Player; import com.massivecraft.mcore1.Lang; import com.massivecraft.mcore1.MCore; import com.massivecraft.mcore1.cmd.arg.IArgHandler; +import com.massivecraft.mcore1.cmd.req.IReq; import com.massivecraft.mcore1.persist.IClassManager; import com.massivecraft.mcore1.persist.Persist; import com.massivecraft.mcore1.plugin.MPlugin; @@ -55,10 +56,17 @@ public abstract class MCommand // FIELD: errorOnToManyArgs // Should an error be thrown if "to many" args are sent. - protected boolean errorOnToManyArgs = true; + protected boolean errorOnToManyArgs; public boolean getErrorOnToManyArgs() { return this.errorOnToManyArgs; } public void setErrorOnToManyArgs(boolean val) { this.errorOnToManyArgs = val; } + // FIELD: requirements + // All these requirements must be met for the command to be executable; + protected List requirements; + public List getRequirements() { return this.requirements; } + public void setRequirements(List val) { this.requirements = val; } + public void addRequirements(IReq... requirements) { this.requirements.addAll(Arrays.asList(requirements)); } + // FIELD: desc // This field may be left blank and will in such case be loaded from the permissions node instead. // Thus make sure the permissions node description is an action description like "eat hamburgers" or "do admin stuff". @@ -68,7 +76,7 @@ public abstract class MCommand { if (this.desc == null) { - String pdesc = getPlugin().perm.getPermissionDescription(this.permission); + String pdesc = getPlugin().perm.getPermissionDescription(this.descPermission); if (pdesc != null) { return pdesc; @@ -78,6 +86,17 @@ public abstract class MCommand return this.desc; } + // FIELD: descPermission + // This permission node IS NOT TESTED AT ALL. It is rather used in the method above. + protected String descPermission; + public String getDescPermission() { return this.descPermission; } + public void setDescPermission(String val) { this.descPermission = val; } + + // FIELD: visibilityMode + protected VisibilityMode visibilityMode; + public VisibilityMode getVisibilityMode() { return this.visibilityMode; } + public void setVisibilityMode(VisibilityMode val) { this.visibilityMode = val; } + // -------------------------------------------- // // EXECUTION INFO // -------------------------------------------- // @@ -123,18 +142,9 @@ public abstract class MCommand return null; } - // -------------------------------------------- // - // TODO: PURE DERP - // -------------------------------------------- // - - // Some information on permissions - // this is part of validating if the sender is ok... - public boolean senderMustBePlayer; - public String permission; - public MCommand() { - this.permission = null; + this.descPermission = null; this.subCommands = new ArrayList(); this.aliases = new ArrayList(); @@ -142,7 +152,13 @@ public abstract class MCommand this.requiredArgs = new ArrayList(); this.optionalArgs = new LinkedHashMap(); + this.requirements = new ArrayList(); + + this.errorOnToManyArgs = true; + this.desc = null; + + this.visibilityMode = VisibilityMode.VISIBLE; } // The commandChain is a list of the parent command chain used to get to this command. @@ -193,17 +209,12 @@ public abstract class MCommand */ public boolean validCall(CommandSender sender, List args) { - if ( ! validSenderType(sender, true)) + if ( ! this.requirementsAreMet(sender, true)) { return false; } - if ( ! validSenderPermissions(sender, true)) - { - return false; - } - - if ( ! validArgs(args, sender)) + if ( ! this.validArgs(args, sender)) { return false; } @@ -216,25 +227,22 @@ public abstract class MCommand return true; } - public boolean validSenderType(CommandSender sender, boolean informSenderIfNot) + public boolean requirementsAreMet(CommandSender sender, boolean informSenderIfNot) { - if (this.senderMustBePlayer && ! (sender instanceof Player)) + for (IReq req : this.getRequirements()) { - if (informSenderIfNot) + if ( ! req.test(sender, this)) { - msg(Lang.commandSenderMustBePlayer); + if (informSenderIfNot) + { + this.msg(req.createErrorMessage(sender, this)); + } + return false; } - return false; } return true; } - public boolean validSenderPermissions(CommandSender sender, boolean informSenderIfNot) - { - if (this.permission == null) return true; - return getPlugin().perm.has(sender, this.permission, informSenderIfNot); - } - public boolean validArgs(List args, CommandSender sender) { if (args.size() < this.requiredArgs.size()) diff --git a/src/com/massivecraft/mcore1/cmd/VisibilityMode.java b/src/com/massivecraft/mcore1/cmd/VisibilityMode.java new file mode 100644 index 00000000..1cb0065d --- /dev/null +++ b/src/com/massivecraft/mcore1/cmd/VisibilityMode.java @@ -0,0 +1,9 @@ +package com.massivecraft.mcore1.cmd; + +public enum VisibilityMode +{ + VISIBLE, // Visible commands are visible to anyone. Even those who don't have permission to use it or is of invalid sender type. + SECRET, // Secret commands are visible only to those who can use the command. These commands are usually some kind of admin commands. + INVISIBLE, // Invisible commands are invisible to everyone, even those who can use the command. + ; +} diff --git a/src/com/massivecraft/mcore1/cmd/req/IReq.java b/src/com/massivecraft/mcore1/cmd/req/IReq.java new file mode 100644 index 00000000..54edd137 --- /dev/null +++ b/src/com/massivecraft/mcore1/cmd/req/IReq.java @@ -0,0 +1,14 @@ +package com.massivecraft.mcore1.cmd.req; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.mcore1.cmd.MCommand; + +public interface IReq +{ + // This just tests wether the requirement is met or not. + public boolean test(CommandSender sender, MCommand command); + + // This just composes the error message and does NOT test the requirement at all. + public String createErrorMessage(CommandSender sender, MCommand command); +} diff --git a/src/com/massivecraft/mcore1/cmd/req/ReqHasPerm.java b/src/com/massivecraft/mcore1/cmd/req/ReqHasPerm.java new file mode 100644 index 00000000..ceb24133 --- /dev/null +++ b/src/com/massivecraft/mcore1/cmd/req/ReqHasPerm.java @@ -0,0 +1,30 @@ +package com.massivecraft.mcore1.cmd.req; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.mcore1.cmd.MCommand; + +public class ReqHasPerm implements IReq +{ + private String perm; + public String getPerm() { return this.perm; } + public void setPerm(String val) { this.perm = val; } + + public ReqHasPerm(String perm) + { + this.perm = perm; + } + + @Override + public boolean test(CommandSender sender, MCommand command) + { + return sender.hasPermission(this.perm); + } + + @Override + public String createErrorMessage(CommandSender sender, MCommand command) + { + return command.getPlugin().perm.getForbiddenMessage(this.perm); + } + +} diff --git a/src/com/massivecraft/mcore1/cmd/req/ReqIsPlayer.java b/src/com/massivecraft/mcore1/cmd/req/ReqIsPlayer.java new file mode 100644 index 00000000..7f84f578 --- /dev/null +++ b/src/com/massivecraft/mcore1/cmd/req/ReqIsPlayer.java @@ -0,0 +1,22 @@ +package com.massivecraft.mcore1.cmd.req; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.massivecraft.mcore1.Lang; +import com.massivecraft.mcore1.cmd.MCommand; + +public class ReqIsPlayer implements IReq +{ + @Override + public boolean test(CommandSender sender, MCommand command) + { + return sender instanceof Player; + } + + @Override + public String createErrorMessage(CommandSender sender, MCommand command) + { + return Lang.commandSenderMustBePlayer; + } +}