Improving command requirement Req
This commit is contained in:
parent
65f84c25fe
commit
5c2752129d
@ -13,7 +13,7 @@ import com.massivecraft.mcore.MCore;
|
|||||||
import com.massivecraft.mcore.MPlugin;
|
import com.massivecraft.mcore.MPlugin;
|
||||||
import com.massivecraft.mcore.cmd.arg.ArgReader;
|
import com.massivecraft.mcore.cmd.arg.ArgReader;
|
||||||
import com.massivecraft.mcore.cmd.arg.ArgResult;
|
import com.massivecraft.mcore.cmd.arg.ArgResult;
|
||||||
import com.massivecraft.mcore.cmd.req.IReq;
|
import com.massivecraft.mcore.cmd.req.Req;
|
||||||
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
|
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
|
||||||
import com.massivecraft.mcore.mixin.Mixin;
|
import com.massivecraft.mcore.mixin.Mixin;
|
||||||
import com.massivecraft.mcore.util.BukkitCommandUtil;
|
import com.massivecraft.mcore.util.BukkitCommandUtil;
|
||||||
@ -72,11 +72,11 @@ public abstract class MCommand
|
|||||||
|
|
||||||
// FIELD: requirements
|
// FIELD: requirements
|
||||||
// All these requirements must be met for the command to be executable;
|
// All these requirements must be met for the command to be executable;
|
||||||
protected List<IReq> requirements;
|
protected List<Req> requirements;
|
||||||
public List<IReq> getRequirements() { return this.requirements; }
|
public List<Req> getRequirements() { return this.requirements; }
|
||||||
public void getRequirements(List<IReq> requirements) { this.requirements = requirements; }
|
public void getRequirements(List<Req> requirements) { this.requirements = requirements; }
|
||||||
|
|
||||||
public void addRequirements(IReq... requirements) { this.requirements.addAll(Arrays.asList(requirements)); }
|
public void addRequirements(Req... 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.
|
||||||
@ -110,7 +110,7 @@ public abstract class MCommand
|
|||||||
{
|
{
|
||||||
if (this.descPermission != null) return this.descPermission;
|
if (this.descPermission != null) return this.descPermission;
|
||||||
// Otherwise we try to find one.
|
// Otherwise we try to find one.
|
||||||
for (IReq req : this.requirements)
|
for (Req req : this.requirements)
|
||||||
{
|
{
|
||||||
if ( ! (req instanceof ReqHasPerm)) continue;
|
if ( ! (req instanceof ReqHasPerm)) continue;
|
||||||
return ((ReqHasPerm)req).getPerm();
|
return ((ReqHasPerm)req).getPerm();
|
||||||
@ -211,7 +211,7 @@ 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.requirements = new ArrayList<Req>();
|
||||||
|
|
||||||
this.errorOnToManyArgs = true;
|
this.errorOnToManyArgs = true;
|
||||||
|
|
||||||
@ -300,9 +300,9 @@ public abstract class MCommand
|
|||||||
|
|
||||||
public boolean requirementsAreMet(CommandSender sender, boolean informSenderIfNot)
|
public boolean requirementsAreMet(CommandSender sender, boolean informSenderIfNot)
|
||||||
{
|
{
|
||||||
for (IReq req : this.getRequirements())
|
for (Req req : this.getRequirements())
|
||||||
{
|
{
|
||||||
if ( ! req.test(sender, this))
|
if ( ! req.apply(sender, this))
|
||||||
{
|
{
|
||||||
if (informSenderIfNot)
|
if (informSenderIfNot)
|
||||||
{
|
{
|
||||||
|
@ -2,13 +2,15 @@ package com.massivecraft.mcore.cmd.req;
|
|||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import com.massivecraft.mcore.Predictate;
|
||||||
import com.massivecraft.mcore.cmd.MCommand;
|
import com.massivecraft.mcore.cmd.MCommand;
|
||||||
|
|
||||||
public interface IReq
|
public interface Req extends Predictate<CommandSender>
|
||||||
{
|
{
|
||||||
// This just tests wether the requirement is met or not.
|
public boolean apply(CommandSender sender, MCommand command);
|
||||||
public boolean test(CommandSender sender, MCommand command);
|
|
||||||
|
|
||||||
// This just composes the error message and does NOT test the requirement at all.
|
// This just composes the error message and does NOT test the requirement at all.
|
||||||
|
|
||||||
|
public String createErrorMessage(CommandSender sender);
|
||||||
public String createErrorMessage(CommandSender sender, MCommand command);
|
public String createErrorMessage(CommandSender sender, MCommand command);
|
||||||
}
|
}
|
22
src/com/massivecraft/mcore/cmd/req/ReqAbstract.java
Normal file
22
src/com/massivecraft/mcore/cmd/req/ReqAbstract.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package com.massivecraft.mcore.cmd.req;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
public abstract class ReqAbstract implements Req, Serializable
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(CommandSender sender)
|
||||||
|
{
|
||||||
|
return this.apply(sender, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String createErrorMessage(CommandSender sender)
|
||||||
|
{
|
||||||
|
return this.createErrorMessage(sender, null);
|
||||||
|
}
|
||||||
|
}
|
@ -5,32 +5,37 @@ import org.bukkit.command.CommandSender;
|
|||||||
import com.massivecraft.mcore.cmd.MCommand;
|
import com.massivecraft.mcore.cmd.MCommand;
|
||||||
import com.massivecraft.mcore.util.PermUtil;
|
import com.massivecraft.mcore.util.PermUtil;
|
||||||
|
|
||||||
public class ReqHasPerm implements IReq
|
public class ReqHasPerm extends ReqAbstract
|
||||||
{
|
{
|
||||||
private String perm;
|
private static final long serialVersionUID = 1L;
|
||||||
public String getPerm() { return this.perm; }
|
|
||||||
public void setPerm(String perm) { this.perm = perm; }
|
|
||||||
|
|
||||||
public ReqHasPerm(String perm)
|
// -------------------------------------------- //
|
||||||
{
|
// INSTANCE & CONSTRUCT
|
||||||
this.perm = perm;
|
// -------------------------------------------- //
|
||||||
}
|
|
||||||
|
public static ReqHasPerm get(String perm) { return new ReqHasPerm(perm); }
|
||||||
|
public ReqHasPerm(String perm) { this.perm = perm; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// FIELDS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private final String perm;
|
||||||
|
public String getPerm() { return this.perm; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// OVERRIDE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(CommandSender sender, MCommand command)
|
public boolean apply(CommandSender sender, MCommand command)
|
||||||
{
|
{
|
||||||
return sender.hasPermission(this.perm);
|
return sender.hasPermission(this.perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createErrorMessage(CommandSender sender, MCommand command)
|
public String createErrorMessage(CommandSender sender, MCommand command)
|
||||||
{
|
{
|
||||||
return PermUtil.getForbiddenMessage(this.perm);
|
return PermUtil.getForbiddenMessage(this.perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReqHasPerm get(String perm)
|
|
||||||
{
|
|
||||||
return new ReqHasPerm(perm);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,36 @@
|
|||||||
package com.massivecraft.mcore.cmd.req;
|
package com.massivecraft.mcore.cmd.req;
|
||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import com.massivecraft.mcore.Lang;
|
import com.massivecraft.mcore.Lang;
|
||||||
import com.massivecraft.mcore.cmd.MCommand;
|
import com.massivecraft.mcore.cmd.MCommand;
|
||||||
|
import com.massivecraft.mcore.util.SenderUtil;
|
||||||
|
|
||||||
public class ReqIsPlayer implements IReq
|
public class ReqIsPlayer extends ReqAbstract
|
||||||
{
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// INSTANCE & CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private static ReqIsPlayer i = new ReqIsPlayer();
|
||||||
|
public static ReqIsPlayer get() { return i; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// OVERRIDE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(CommandSender sender, MCommand command)
|
public boolean apply(CommandSender sender, MCommand command)
|
||||||
{
|
{
|
||||||
return sender instanceof Player;
|
return SenderUtil.isPlayer(sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createErrorMessage(CommandSender sender, MCommand command)
|
public String createErrorMessage(CommandSender sender, MCommand command)
|
||||||
{
|
{
|
||||||
return Lang.commandSenderMustBePlayer;
|
return Lang.commandSenderMustBePlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
|
||||||
// INSTANCE
|
|
||||||
// -------------------------------------------- //
|
|
||||||
|
|
||||||
public static ReqIsPlayer i = new ReqIsPlayer();
|
|
||||||
public static ReqIsPlayer get() { return i; }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user