275 lines
6.4 KiB
Java
Raw Normal View History

2011-03-18 17:33:23 +01:00
package com.bukkit.mcteam.factions.commands;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.bukkit.mcteam.factions.Conf;
import com.bukkit.mcteam.factions.FPlayer;
2011-03-19 13:00:03 +01:00
import com.bukkit.mcteam.factions.Faction;
2011-03-18 17:33:23 +01:00
import com.bukkit.mcteam.factions.Factions;
2011-03-19 13:00:03 +01:00
import com.bukkit.mcteam.factions.struct.Role;
import com.bukkit.mcteam.factions.util.TextUtil;
2011-03-18 17:33:23 +01:00
public class FBaseCommand {
public List<String> aliases;
2011-03-18 17:33:23 +01:00
public List<String> requiredParameters;
public List<String> optionalParameters;
public String permissions;
public String helpNameAndParams;
public String helpDescription;
public CommandSender sender;
public boolean senderMustBePlayer;
public Player player;
2011-03-19 13:00:03 +01:00
public FPlayer me;
2011-03-18 17:33:23 +01:00
public List<String> parameters;
public FBaseCommand() {
2011-03-18 17:33:23 +01:00
requiredParameters = new ArrayList<String>();
optionalParameters = new ArrayList<String>();
permissions = "";
senderMustBePlayer = false;
helpNameAndParams = "fail!";
helpDescription = "no description";
}
public List<String> getAliases() {
2011-03-18 17:33:23 +01:00
return aliases;
}
public String getBaseName() {
// TODO fetch from the plugin.yaml or something...
return "f";
}
public void execute(CommandSender sender, List<String> parameters) {
this.sender = sender;
this.parameters = parameters;
if ( ! validateCall()) {
return;
}
if (this.senderMustBePlayer) {
this.player = (Player)sender;
2011-03-19 13:00:03 +01:00
this.me = FPlayer.get(this.player);
2011-03-18 17:33:23 +01:00
}
perform();
}
public void perform() {
}
public void sendMessage(String message) {
sender.sendMessage(Conf.colorSystem+message);
}
public void sendMessage(List<String> messages) {
for(String message : messages) {
this.sendMessage(message);
}
}
// Test if the number of params is correct.
// TODO print usage
2011-03-18 17:33:23 +01:00
public boolean validateCall() {
if( ! testPermission(sender)) {
sendMessage("You do not have sufficient permissions to use this command.");
return false;
}
if ( this.senderMustBePlayer && ! (sender instanceof Player)) {
sendMessage("This command can only be used by ingame players.");
return false;
}
if (parameters.size() < requiredParameters.size()) {
sendMessage("Usage: "+this.getUseageTemplate(true));
2011-03-18 17:33:23 +01:00
return false;
}
return true;
}
public boolean testPermission(CommandSender sender) {
if (sender.isOp()) {
return true;
}
if (this.permissions.length() == 0) {
return true;
}
if ( ! (sender instanceof Player)) {
return false;
}
if (Factions.Permissions == null) {
return false;
}
Player player = (Player)sender;
return Factions.Permissions.has(player, this.permissions);
}
2011-03-19 13:00:03 +01:00
// -------------------------------------------- //
// Help and usage description
// -------------------------------------------- //
public String getUseageTemplate(boolean withColor, boolean withDescription) {
String ret = "";
if (withColor) {
ret += Conf.colorCommand;
}
ret += this.getBaseName()+ " " +TextUtil.implode(this.getAliases(), ",")+" ";
List<String> parts = new ArrayList<String>();
for (String requiredParameter : this.requiredParameters) {
parts.add("["+requiredParameter+"]");
}
for (String optionalParameter : this.optionalParameters) {
parts.add("*["+optionalParameter+"]");
}
if (withColor) {
ret += Conf.colorParameter;
}
ret += TextUtil.implode(parts, " ");
if (withDescription) {
ret += " "+Conf.colorSystem + this.helpDescription;
}
return ret;
}
public String getUseageTemplate(boolean withColor) {
return getUseageTemplate(withColor, false);
}
public String getUseageTemplate() {
return getUseageTemplate(true);
}
public void helpRegister() {
Factions.helpPlugin.registerCommand(this.getUseageTemplate(false), this.helpDescription, Factions.instance, false, permissions);
}
// -------------------------------------------- //
// Assertions
// -------------------------------------------- //
public boolean assertHasFaction() {
if ( ! me.hasFaction()) {
sendMessage("You are not member of any faction.");
return false;
}
return true;
}
public boolean assertMinRole(Role role) {
2011-03-22 17:20:21 +01:00
if (me.getRole().value < role.value) {
sendMessage("You must be "+role+" to "+this.helpDescription+".");
return false;
}
return true;
}
2011-03-19 13:00:03 +01:00
// -------------------------------------------- //
// Commonly used logic
// -------------------------------------------- //
public FPlayer findFPlayer(String playerName, boolean defaultToMe) {
FPlayer fp = FPlayer.find(playerName);
if (fp == null) {
if (defaultToMe) {
return me;
}
sendMessage("The player \""+playerName+"\" could not be found");
}
return fp;
}
public FPlayer findFPlayer(String playerName) {
return findFPlayer(playerName, false);
}
public Faction findFaction(String factionName, boolean defaultToMine) {
// First we search player names
FPlayer fp = FPlayer.find(factionName);
if (fp != null) {
return fp.getFaction();
}
// Secondly we search faction names
Faction faction = Faction.findByTag(factionName);
if (faction != null) {
return faction;
}
if (defaultToMine) {
return me.getFaction();
}
me.sendMessage(Conf.colorSystem+"No faction or player \""+factionName+"\" was found");
return null;
}
public Faction findFaction(String factionName) {
return findFaction(factionName, false);
}
public boolean canIAdministerYou(FPlayer i, FPlayer you) {
if ( ! i.getFaction().equals(you.getFaction())) {
i.sendMessage(you.getNameAndRelevant(i)+Conf.colorSystem+" is not in the same faction as you.");
return false;
}
2011-03-22 17:20:21 +01:00
if (i.getRole().value > you.getRole().value || i.getRole().equals(Role.ADMIN) ) {
2011-03-19 13:00:03 +01:00
return true;
}
2011-03-22 17:20:21 +01:00
if (you.getRole().equals(Role.ADMIN)) {
2011-03-19 13:00:03 +01:00
i.sendMessage(Conf.colorSystem+"Only the faction admin can do that.");
2011-03-22 17:20:21 +01:00
} else if (i.getRole().equals(Role.MODERATOR)) {
2011-03-19 13:00:03 +01:00
i.sendMessage(Conf.colorSystem+"Moderators can't control each other...");
} else {
i.sendMessage(Conf.colorSystem+"You must be a faction moderator to do that.");
}
return false;
}
public boolean parseBool(String str) {
List<String> aliasTrue = new ArrayList<String>();
aliasTrue.add("true");
aliasTrue.add("yes");
aliasTrue.add("y");
aliasTrue.add("ok");
aliasTrue.add("on");
aliasTrue.add("+");
return aliasTrue.contains(str.toLowerCase());
}
2011-03-18 17:33:23 +01:00
}