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

170 lines
5.5 KiB
Java
Raw Normal View History

2011-10-09 21:57:43 +02:00
package com.massivecraft.factions.cmd;
2014-04-04 20:55:21 +02:00
import com.massivecraft.factions.*;
import com.massivecraft.factions.event.LandUnclaimEvent;
import com.massivecraft.factions.integration.Econ;
2011-10-09 18:35:39 +02:00
import com.massivecraft.factions.struct.Permission;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.struct.Role;
2016-05-30 02:20:50 +02:00
import com.massivecraft.factions.util.SpiralTask;
import com.massivecraft.factions.zcore.util.TL;
2014-04-04 20:55:21 +02:00
import org.bukkit.Bukkit;
public class CmdUnclaim extends FCommand {
2014-04-04 20:55:21 +02:00
public CmdUnclaim() {
2014-07-01 22:10:18 +02:00
this.aliases.add("unclaim");
this.aliases.add("declaim");
2014-04-04 20:55:21 +02:00
2016-05-30 02:20:50 +02:00
this.optionalArgs.put("radius", "1");
this.optionalArgs.put("faction", "your");
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
this.permission = Permission.UNCLAIM.node;
this.disableOnLock = true;
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
senderMustBePlayer = true;
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
2014-04-04 20:55:21 +02:00
}
@Override
public void perform() {
2016-05-30 02:20:50 +02:00
// Read and validate input
int radius = this.argAsInt(0, 1); // Default to 1
final Faction forFaction = this.argAsFaction(1, myFaction); // Default to own
2014-04-04 20:55:21 +02:00
2016-05-30 02:20:50 +02:00
if (radius < 1) {
msg(TL.COMMAND_CLAIM_INVALIDRADIUS);
return;
}
if (radius < 2) {
// single chunk
2016-05-30 19:00:53 +02:00
unClaim(new FLocation(me));
2016-05-30 02:20:50 +02:00
} else {
// radius claim
if (!Permission.CLAIM_RADIUS.has(sender, false)) {
msg(TL.COMMAND_CLAIM_DENIED);
return;
}
new SpiralTask(new FLocation(me), radius) {
private final int limit = Conf.radiusClaimFailureLimit - 1;
2018-07-12 18:11:07 +02:00
private int failCount = 0;
2016-05-30 02:20:50 +02:00
@Override
public boolean work() {
boolean success = unClaim(this.currentFLocation());
if (success) {
failCount = 0;
} else if (failCount++ >= limit) {
this.stop();
return false;
}
return true;
}
};
}
}
private boolean unClaim(FLocation target) {
Faction targetFaction = Board.getInstance().getFactionAt(target);
if (targetFaction.isSafeZone()) {
2014-04-04 20:55:21 +02:00
if (Permission.MANAGE_SAFE_ZONE.has(sender)) {
2016-05-30 02:20:50 +02:00
Board.getInstance().removeAt(target);
msg(TL.COMMAND_UNCLAIM_SAFEZONE_SUCCESS);
2014-04-04 20:55:21 +02:00
2014-07-01 21:49:42 +02:00
if (Conf.logLandUnclaims) {
2016-05-30 02:20:50 +02:00
P.p.log(TL.COMMAND_UNCLAIM_LOG.format(fme.getName(), target.getCoordString(), targetFaction.getTag()));
2014-07-01 21:49:42 +02:00
}
2016-05-30 02:20:50 +02:00
return true;
2014-07-01 21:52:40 +02:00
} else {
msg(TL.COMMAND_UNCLAIM_SAFEZONE_NOPERM);
2016-05-30 02:20:50 +02:00
return false;
2014-07-01 22:10:18 +02:00
}
2016-05-30 02:20:50 +02:00
} else if (targetFaction.isWarZone()) {
2014-04-04 20:55:21 +02:00
if (Permission.MANAGE_WAR_ZONE.has(sender)) {
2016-05-30 02:20:50 +02:00
Board.getInstance().removeAt(target);
msg(TL.COMMAND_UNCLAIM_WARZONE_SUCCESS);
2014-04-04 20:55:21 +02:00
2014-07-01 21:49:42 +02:00
if (Conf.logLandUnclaims) {
2016-05-30 02:20:50 +02:00
P.p.log(TL.COMMAND_UNCLAIM_LOG.format(fme.getName(), target.getCoordString(), targetFaction.getTag()));
2014-07-01 21:49:42 +02:00
}
2016-05-30 02:20:50 +02:00
return true;
2014-07-01 21:52:40 +02:00
} else {
msg(TL.COMMAND_UNCLAIM_WARZONE_NOPERM);
2016-05-30 02:20:50 +02:00
return false;
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
}
if (fme.isAdminBypassing()) {
LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(target, targetFaction, fme);
Bukkit.getServer().getPluginManager().callEvent(unclaimEvent);
if (unclaimEvent.isCancelled()) {
return false;
}
2016-05-30 02:20:50 +02:00
Board.getInstance().removeAt(target);
2014-04-04 20:55:21 +02:00
2016-05-30 02:20:50 +02:00
targetFaction.msg(TL.COMMAND_UNCLAIM_UNCLAIMED, fme.describeTo(targetFaction, true));
msg(TL.COMMAND_UNCLAIM_UNCLAIMS);
2014-04-04 20:55:21 +02:00
2014-07-01 21:49:42 +02:00
if (Conf.logLandUnclaims) {
2016-05-30 02:20:50 +02:00
P.p.log(TL.COMMAND_UNCLAIM_LOG.format(fme.getName(), target.getCoordString(), targetFaction.getTag()));
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
2016-05-30 02:20:50 +02:00
return true;
2014-04-04 20:55:21 +02:00
}
if (!assertHasFaction()) {
2016-05-30 02:20:50 +02:00
return false;
2014-04-04 20:55:21 +02:00
}
if (!assertMinRole(Role.MODERATOR)) {
2016-05-30 02:20:50 +02:00
return false;
2014-04-04 20:55:21 +02:00
}
2016-05-30 02:20:50 +02:00
if (myFaction != targetFaction) {
msg(TL.COMMAND_UNCLAIM_WRONGFACTION);
2016-05-30 02:20:50 +02:00
return false;
2014-04-04 20:55:21 +02:00
}
2016-05-30 02:20:50 +02:00
LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(target, targetFaction, fme);
2014-07-01 22:10:18 +02:00
Bukkit.getServer().getPluginManager().callEvent(unclaimEvent);
if (unclaimEvent.isCancelled()) {
2016-05-30 02:20:50 +02:00
return false;
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
if (Econ.shouldBeUsed()) {
double refund = Econ.calculateClaimRefund(myFaction.getLandRounded());
if (Conf.bankEnabled && Conf.bankFactionPaysLandCosts) {
if (!Econ.modifyMoney(myFaction, refund, TL.COMMAND_UNCLAIM_TOUNCLAIM.toString(), TL.COMMAND_UNCLAIM_FORUNCLAIM.toString())) {
2016-05-30 02:20:50 +02:00
return false;
2014-07-01 21:49:42 +02:00
}
2014-07-01 21:52:40 +02:00
} else {
if (!Econ.modifyMoney(fme, refund, TL.COMMAND_UNCLAIM_TOUNCLAIM.toString(), TL.COMMAND_UNCLAIM_FORUNCLAIM.toString())) {
2016-05-30 02:20:50 +02:00
return false;
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
}
}
2016-05-30 02:20:50 +02:00
Board.getInstance().removeAt(target);
myFaction.msg(TL.COMMAND_UNCLAIM_FACTIONUNCLAIMED, fme.describeTo(myFaction, true));
2014-04-04 20:55:21 +02:00
2014-07-01 21:49:42 +02:00
if (Conf.logLandUnclaims) {
2016-05-30 02:20:50 +02:00
P.p.log(TL.COMMAND_UNCLAIM_LOG.format(fme.getName(), target.getCoordString(), targetFaction.getTag()));
2014-07-01 21:49:42 +02:00
}
2016-05-30 02:20:50 +02:00
return true;
2014-04-04 20:55:21 +02:00
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_UNCLAIM_DESCRIPTION;
}
}