Say hi to the requirements.
This commit is contained in:
parent
d823098f44
commit
9c2838efc5
@ -1,6 +1,6 @@
|
|||||||
name: mcore1
|
name: mcore1
|
||||||
version: 1
|
version: 1
|
||||||
main: com.massivecraft.core1.MCore
|
main: com.massivecraft.mcore1.MCore
|
||||||
authors: [Olof Larsson]
|
authors: [Olof Larsson]
|
||||||
softdepend: [Vault, Spout]
|
softdepend: [Vault, Spout]
|
||||||
commands:
|
commands:
|
||||||
|
@ -9,6 +9,7 @@ import org.bukkit.entity.Player;
|
|||||||
import com.massivecraft.mcore1.Lang;
|
import com.massivecraft.mcore1.Lang;
|
||||||
import com.massivecraft.mcore1.MCore;
|
import com.massivecraft.mcore1.MCore;
|
||||||
import com.massivecraft.mcore1.cmd.arg.IArgHandler;
|
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.IClassManager;
|
||||||
import com.massivecraft.mcore1.persist.Persist;
|
import com.massivecraft.mcore1.persist.Persist;
|
||||||
import com.massivecraft.mcore1.plugin.MPlugin;
|
import com.massivecraft.mcore1.plugin.MPlugin;
|
||||||
@ -55,10 +56,17 @@ public abstract class MCommand
|
|||||||
|
|
||||||
// FIELD: errorOnToManyArgs
|
// FIELD: errorOnToManyArgs
|
||||||
// Should an error be thrown if "to many" args are sent.
|
// 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 boolean getErrorOnToManyArgs() { return this.errorOnToManyArgs; }
|
||||||
public void setErrorOnToManyArgs(boolean val) { this.errorOnToManyArgs = val; }
|
public void setErrorOnToManyArgs(boolean val) { this.errorOnToManyArgs = val; }
|
||||||
|
|
||||||
|
// FIELD: requirements
|
||||||
|
// All these requirements must be met for the command to be executable;
|
||||||
|
protected List<IReq> requirements;
|
||||||
|
public List<IReq> getRequirements() { return this.requirements; }
|
||||||
|
public void setRequirements(List<IReq> val) { this.requirements = val; }
|
||||||
|
public void addRequirements(IReq... requirements) { this.requirements.addAll(Arrays.asList(requirements)); }
|
||||||
|
|
||||||
// FIELD: desc
|
// FIELD: desc
|
||||||
// This field may be left blank and will in such case be loaded from the permissions node instead.
|
// 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".
|
// 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)
|
if (this.desc == null)
|
||||||
{
|
{
|
||||||
String pdesc = getPlugin().perm.getPermissionDescription(this.permission);
|
String pdesc = getPlugin().perm.getPermissionDescription(this.descPermission);
|
||||||
if (pdesc != null)
|
if (pdesc != null)
|
||||||
{
|
{
|
||||||
return pdesc;
|
return pdesc;
|
||||||
@ -78,6 +86,17 @@ public abstract class MCommand
|
|||||||
return this.desc;
|
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
|
// EXECUTION INFO
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
@ -123,18 +142,9 @@ public abstract class MCommand
|
|||||||
return null;
|
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()
|
public MCommand()
|
||||||
{
|
{
|
||||||
this.permission = null;
|
this.descPermission = null;
|
||||||
|
|
||||||
this.subCommands = new ArrayList<MCommand>();
|
this.subCommands = new ArrayList<MCommand>();
|
||||||
this.aliases = new ArrayList<String>();
|
this.aliases = new ArrayList<String>();
|
||||||
@ -142,7 +152,13 @@ public abstract class MCommand
|
|||||||
this.requiredArgs = new ArrayList<String>();
|
this.requiredArgs = new ArrayList<String>();
|
||||||
this.optionalArgs = new LinkedHashMap<String, String>();
|
this.optionalArgs = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
|
this.requirements = new ArrayList<IReq>();
|
||||||
|
|
||||||
|
this.errorOnToManyArgs = true;
|
||||||
|
|
||||||
this.desc = null;
|
this.desc = null;
|
||||||
|
|
||||||
|
this.visibilityMode = VisibilityMode.VISIBLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The commandChain is a list of the parent command chain used to get to this command.
|
// 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<String> args)
|
public boolean validCall(CommandSender sender, List<String> args)
|
||||||
{
|
{
|
||||||
if ( ! validSenderType(sender, true))
|
if ( ! this.requirementsAreMet(sender, true))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! validSenderPermissions(sender, true))
|
if ( ! this.validArgs(args, sender))
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! validArgs(args, sender))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -216,23 +227,20 @@ public abstract class MCommand
|
|||||||
return true;
|
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 ( ! req.test(sender, this))
|
||||||
{
|
{
|
||||||
if (informSenderIfNot)
|
if (informSenderIfNot)
|
||||||
{
|
{
|
||||||
msg(Lang.commandSenderMustBePlayer);
|
this.msg(req.createErrorMessage(sender, this));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
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<String> args, CommandSender sender)
|
public boolean validArgs(List<String> args, CommandSender sender)
|
||||||
|
9
src/com/massivecraft/mcore1/cmd/VisibilityMode.java
Normal file
9
src/com/massivecraft/mcore1/cmd/VisibilityMode.java
Normal file
@ -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.
|
||||||
|
;
|
||||||
|
}
|
14
src/com/massivecraft/mcore1/cmd/req/IReq.java
Normal file
14
src/com/massivecraft/mcore1/cmd/req/IReq.java
Normal file
@ -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);
|
||||||
|
}
|
30
src/com/massivecraft/mcore1/cmd/req/ReqHasPerm.java
Normal file
30
src/com/massivecraft/mcore1/cmd/req/ReqHasPerm.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
22
src/com/massivecraft/mcore1/cmd/req/ReqIsPlayer.java
Normal file
22
src/com/massivecraft/mcore1/cmd/req/ReqIsPlayer.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user