Saber-Factions/src/main/java/com/massivecraft/factions/cmd/FCommand.java

294 lines
9.1 KiB
Java
Raw Normal View History

2011-10-09 21:57:43 +02:00
package com.massivecraft.factions.cmd;
2011-10-08 23:22:02 +02:00
2014-04-04 20:55:21 +02:00
import com.massivecraft.factions.*;
2011-10-08 23:22:02 +02:00
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.zcore.MCommand;
import com.massivecraft.factions.zcore.util.TL;
2014-04-04 20:55:21 +02:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
public abstract class FCommand extends MCommand<P> {
2014-04-04 20:55:21 +02:00
public boolean disableOnLock;
public FPlayer fme;
public Faction myFaction;
public boolean senderMustBeMember;
public boolean senderMustBeModerator;
public boolean senderMustBeAdmin;
public boolean isMoneyCommand;
public FCommand() {
super(P.p);
// Due to safety reasons it defaults to disable on lock.
disableOnLock = true;
// The money commands must be disabled if money should not be used.
isMoneyCommand = false;
2014-07-01 22:10:18 +02:00
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
2014-04-04 20:55:21 +02:00
}
@Override
public void execute(CommandSender sender, List<String> args, List<MCommand<?>> commandChain) {
if (sender instanceof Player) {
this.fme = FPlayers.getInstance().getByPlayer((Player) sender);
2014-07-01 22:10:18 +02:00
this.myFaction = this.fme.getFaction();
2014-07-01 21:52:40 +02:00
} else {
2014-07-01 22:10:18 +02:00
this.fme = null;
this.myFaction = null;
}
super.execute(sender, args, commandChain);
2014-04-04 20:55:21 +02:00
}
@Override
public boolean isEnabled() {
if (p.getLocked() && this.disableOnLock) {
2014-07-01 22:10:18 +02:00
msg("<b>Factions was locked by an admin. Please try again later.");
return false;
2014-04-04 20:55:21 +02:00
}
if (this.isMoneyCommand && !Conf.econEnabled) {
2014-07-01 22:10:18 +02:00
msg("<b>Faction economy features are disabled on this server.");
return false;
2014-04-04 20:55:21 +02:00
}
if (this.isMoneyCommand && !Conf.bankEnabled) {
2014-07-01 22:10:18 +02:00
msg("<b>The faction bank system is disabled on this server.");
return false;
2014-04-04 20:55:21 +02:00
}
return true;
}
@Override
public boolean validSenderType(CommandSender sender, boolean informSenderIfNot) {
2014-07-01 22:10:18 +02:00
boolean superValid = super.validSenderType(sender, informSenderIfNot);
if (!superValid) {
return false;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if (!(this.senderMustBeMember || this.senderMustBeModerator || this.senderMustBeAdmin)) {
return true;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if (!(sender instanceof Player)) {
return false;
}
2014-04-04 20:55:21 +02:00
if (!fme.hasFaction()) {
2014-07-01 22:10:18 +02:00
sender.sendMessage(p.txt.parse("<b>You are not member of any faction."));
return false;
2014-04-04 20:55:21 +02:00
}
if (this.senderMustBeModerator && !fme.getRole().isAtLeast(Role.MODERATOR)) {
2014-07-01 22:10:18 +02:00
sender.sendMessage(p.txt.parse("<b>Only faction moderators can %s.", this.getHelpShort()));
return false;
2014-04-04 20:55:21 +02:00
}
if (this.senderMustBeAdmin && !fme.getRole().isAtLeast(Role.ADMIN)) {
2014-07-01 22:10:18 +02:00
sender.sendMessage(p.txt.parse("<b>Only faction admins can %s.", this.getHelpShort()));
return false;
2014-04-04 20:55:21 +02:00
}
return true;
}
// -------------------------------------------- //
// Assertions
// -------------------------------------------- //
public boolean assertHasFaction() {
2014-07-01 22:10:18 +02:00
if (me == null) {
return true;
}
2014-04-04 20:55:21 +02:00
if (!fme.hasFaction()) {
2014-07-01 22:10:18 +02:00
sendMessage("You are not member of any faction.");
return false;
}
return true;
2014-04-04 20:55:21 +02:00
}
public boolean assertMinRole(Role role) {
2014-07-01 22:10:18 +02:00
if (me == null) {
return true;
}
2014-04-04 20:55:21 +02:00
if (fme.getRole().value < role.value) {
2014-07-01 22:10:18 +02:00
msg("<b>You <h>must be " + role + "<b> to " + this.getHelpShort() + ".");
return false;
}
return true;
2014-04-04 20:55:21 +02:00
}
// -------------------------------------------- //
// Argument Readers
// -------------------------------------------- //
// FPLAYER ======================
public FPlayer strAsFPlayer(String name, FPlayer def, boolean msg) {
FPlayer ret = def;
if (name != null) {
for (FPlayer fplayer : FPlayers.getInstance().getAllFPlayers()) {
if (fplayer.getName().equalsIgnoreCase(name)) {
ret = fplayer;
break;
}
}
2014-04-04 20:55:21 +02:00
}
if (msg && ret == null) {
this.msg("<b>No player \"<p>%s<b>\" could be found.", name);
}
return ret;
}
public FPlayer argAsFPlayer(int idx, FPlayer def, boolean msg) {
return this.strAsFPlayer(this.argAsString(idx), def, msg);
}
public FPlayer argAsFPlayer(int idx, FPlayer def) {
return this.argAsFPlayer(idx, def, true);
}
public FPlayer argAsFPlayer(int idx) {
return this.argAsFPlayer(idx, null);
}
// BEST FPLAYER MATCH ======================
public FPlayer strAsBestFPlayerMatch(String name, FPlayer def, boolean msg) {
2014-04-17 05:15:14 +02:00
return strAsFPlayer(name, def, msg);
2014-04-04 20:55:21 +02:00
}
public FPlayer argAsBestFPlayerMatch(int idx, FPlayer def, boolean msg) {
return this.strAsBestFPlayerMatch(this.argAsString(idx), def, msg);
}
public FPlayer argAsBestFPlayerMatch(int idx, FPlayer def) {
return this.argAsBestFPlayerMatch(idx, def, true);
}
public FPlayer argAsBestFPlayerMatch(int idx) {
return this.argAsBestFPlayerMatch(idx, null);
}
// FACTION ======================
public Faction strAsFaction(String name, Faction def, boolean msg) {
Faction ret = def;
if (name != null) {
Faction faction = null;
// First we try an exact match
if (faction == null) {
faction = Factions.getInstance().getByTag(name); // Checks for faction name match.
2014-04-04 20:55:21 +02:00
}
// Next we match faction tags
if (faction == null) {
faction = Factions.getInstance().getBestTagMatch(name);
2014-04-04 20:55:21 +02:00
}
// Next we match player names
if (faction == null) {
FPlayer fplayer = strAsFPlayer(name, null, false);
if (fplayer != null) {
faction = fplayer.getFaction();
2014-04-04 20:55:21 +02:00
}
}
if (faction != null) {
ret = faction;
}
}
if (msg && ret == null) {
this.msg("<b>The faction or player \"<p>%s<b>\" could not be found.", name);
}
return ret;
}
public Faction argAsFaction(int idx, Faction def, boolean msg) {
return this.strAsFaction(this.argAsString(idx), def, msg);
}
public Faction argAsFaction(int idx, Faction def) {
return this.argAsFaction(idx, def, true);
}
public Faction argAsFaction(int idx) {
return this.argAsFaction(idx, null);
}
// -------------------------------------------- //
// Commonly used logic
// -------------------------------------------- //
public boolean canIAdministerYou(FPlayer i, FPlayer you) {
if (!i.getFaction().equals(you.getFaction())) {
i.sendMessage(p.txt.parse("%s <b>is not in the same faction as you.", you.describeTo(i, true)));
return false;
}
if (i.getRole().value > you.getRole().value || i.getRole().equals(Role.ADMIN)) {
return true;
}
if (you.getRole().equals(Role.ADMIN)) {
i.sendMessage(p.txt.parse("<b>Only the faction admin can do that."));
2014-07-01 21:52:40 +02:00
} else if (i.getRole().equals(Role.MODERATOR)) {
2014-04-04 20:55:21 +02:00
if (i == you) {
return true; //Moderators can control themselves
2014-07-01 21:52:40 +02:00
} else {
2014-04-04 20:55:21 +02:00
i.sendMessage(p.txt.parse("<b>Moderators can't control each other..."));
}
2014-07-01 21:52:40 +02:00
} else {
2014-04-04 20:55:21 +02:00
i.sendMessage(p.txt.parse("<b>You must be a faction moderator to do that."));
}
return false;
}
// if economy is enabled and they're not on the bypass list, make 'em pay; returns true unless person can't afford the cost
public boolean payForCommand(double cost, String toDoThis, String forDoingThis) {
2014-07-01 22:10:18 +02:00
if (!Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing()) {
return true;
}
2014-04-04 20:55:21 +02:00
2014-07-01 21:49:42 +02:00
if (Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction()) {
2014-04-04 20:55:21 +02:00
return Econ.modifyMoney(myFaction, -cost, toDoThis, forDoingThis);
2014-07-01 22:10:18 +02:00
} else {
return Econ.modifyMoney(fme, -cost, toDoThis, forDoingThis);
}
2014-04-04 20:55:21 +02:00
}
2014-12-11 17:05:04 +01:00
public boolean payForCommand(double cost, TL toDoThis, TL forDoingThis) {
return payForCommand(cost, toDoThis.toString(), forDoingThis.toString());
}
2011-10-08 23:22:02 +02:00
2014-04-04 20:55:21 +02:00
// like above, but just make sure they can pay; returns true unless person can't afford the cost
public boolean canAffordCommand(double cost, String toDoThis) {
2014-07-01 22:10:18 +02:00
if (!Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing()) {
return true;
}
2011-10-08 23:22:02 +02:00
2014-07-01 21:49:42 +02:00
if (Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction()) {
2014-04-04 20:55:21 +02:00
return Econ.hasAtLeast(myFaction, cost, toDoThis);
2014-07-01 22:10:18 +02:00
} else {
return Econ.hasAtLeast(fme, cost, toDoThis);
}
2014-04-04 20:55:21 +02:00
}
2011-10-08 23:22:02 +02:00
}