2019-07-02 03:26:13 +02:00
|
|
|
package com.massivecraft.factions.cmd.claim;
|
2011-03-22 15:45:41 +01:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.massivecraft.factions.*;
|
2019-09-14 21:13:01 +02:00
|
|
|
import com.massivecraft.factions.cmd.CommandContext;
|
|
|
|
import com.massivecraft.factions.cmd.CommandRequirements;
|
2019-07-02 03:26:13 +02:00
|
|
|
import com.massivecraft.factions.cmd.FCommand;
|
2012-03-13 13:58:51 +01:00
|
|
|
import com.massivecraft.factions.event.LandUnclaimEvent;
|
2011-10-05 12:13:54 +02:00
|
|
|
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;
|
2018-07-17 16:03:01 +02:00
|
|
|
import com.massivecraft.factions.zcore.fperms.Access;
|
|
|
|
import com.massivecraft.factions.zcore.fperms.PermissableAction;
|
2014-12-08 00:12:52 +01:00
|
|
|
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-08-05 17:17:27 +02:00
|
|
|
|
2019-08-24 19:18:50 +02:00
|
|
|
public CmdUnclaim() {
|
|
|
|
this.aliases.add("unclaim");
|
|
|
|
this.aliases.add("declaim");
|
|
|
|
|
|
|
|
this.optionalArgs.put("radius", "1");
|
2019-09-14 21:13:01 +02:00
|
|
|
this.optionalArgs.put("faction", "yours");
|
2019-08-24 19:18:50 +02:00
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
this.requirements = new CommandRequirements.Builder(Permission.UNCLAIM)
|
|
|
|
.playerOnly()
|
|
|
|
.memberOnly()
|
|
|
|
.withAction(PermissableAction.TERRITORY)
|
|
|
|
.build();
|
2019-08-24 19:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-14 21:13:01 +02:00
|
|
|
public void perform(CommandContext context) {
|
|
|
|
|
|
|
|
if (context.args.size() == 2) {
|
|
|
|
Faction target = context.argAsFaction(1);
|
|
|
|
// Dont have to say anything since the argsAsFaction method will tell the player for me.
|
|
|
|
if (target == null) return;
|
|
|
|
context.faction = target;
|
|
|
|
if (context.faction != context.fPlayer.getFaction() && !context.fPlayer.isAdminBypassing()) {
|
|
|
|
context.msg(TL.COMMAND_UNCLAIM_WRONGFACTION);
|
2019-08-24 19:18:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
// Read and validate input
|
|
|
|
int radius = context.argAsInt(0, 1); // Default to 1
|
|
|
|
|
2019-08-24 19:18:50 +02:00
|
|
|
if (radius < 1) {
|
2019-09-14 21:13:01 +02:00
|
|
|
context.msg(TL.COMMAND_CLAIM_INVALIDRADIUS);
|
2019-08-24 19:18:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
if (radius == 1) {
|
2019-08-24 19:18:50 +02:00
|
|
|
// single chunk
|
2019-09-14 21:13:01 +02:00
|
|
|
unClaim(new FLocation(context.player), context);
|
2019-08-24 19:18:50 +02:00
|
|
|
} else {
|
|
|
|
// radius claim
|
2019-09-14 21:13:01 +02:00
|
|
|
if (!Permission.CLAIM_RADIUS.has(context.sender, false)) {
|
|
|
|
context.msg(TL.COMMAND_CLAIM_DENIED);
|
2019-08-24 19:18:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
new SpiralTask(new FLocation(context.player), radius) {
|
2019-08-24 19:18:50 +02:00
|
|
|
private final int limit = Conf.radiusClaimFailureLimit - 1;
|
|
|
|
private int failCount = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean work() {
|
2019-09-14 21:13:01 +02:00
|
|
|
boolean success = unClaim(currentFLocation(), context);
|
2019-08-24 19:18:50 +02:00
|
|
|
if (success) {
|
|
|
|
failCount = 0;
|
|
|
|
} else if (failCount++ >= limit) {
|
|
|
|
this.stop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
private boolean unClaim(FLocation target, CommandContext context) {
|
2019-08-24 19:18:50 +02:00
|
|
|
Faction targetFaction = Board.getInstance().getFactionAt(target);
|
|
|
|
if (targetFaction.isSafeZone()) {
|
2019-09-14 21:13:01 +02:00
|
|
|
if (Permission.MANAGE_SAFE_ZONE.has(context.sender)) {
|
2019-08-24 19:18:50 +02:00
|
|
|
Board.getInstance().removeAt(target);
|
2019-09-14 21:13:01 +02:00
|
|
|
context.msg(TL.COMMAND_UNCLAIM_SAFEZONE_SUCCESS);
|
2019-08-24 19:18:50 +02:00
|
|
|
|
|
|
|
if (Conf.logLandUnclaims) {
|
2019-09-14 21:13:01 +02:00
|
|
|
FactionsPlugin.getInstance().log(TL.COMMAND_UNCLAIM_LOG.format(context.fPlayer.getName(), target.getCoordString(), targetFaction.getTag()));
|
2019-08-24 19:18:50 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
2019-09-14 21:13:01 +02:00
|
|
|
context.msg(TL.COMMAND_UNCLAIM_SAFEZONE_NOPERM);
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (targetFaction.isWarZone()) {
|
2019-09-14 21:13:01 +02:00
|
|
|
if (Permission.MANAGE_WAR_ZONE.has(context.sender)) {
|
2019-08-24 19:18:50 +02:00
|
|
|
Board.getInstance().removeAt(target);
|
2019-09-14 21:13:01 +02:00
|
|
|
context.msg(TL.COMMAND_UNCLAIM_WARZONE_SUCCESS);
|
2019-08-24 19:18:50 +02:00
|
|
|
|
|
|
|
if (Conf.logLandUnclaims) {
|
2019-09-14 21:13:01 +02:00
|
|
|
FactionsPlugin.getInstance().log(TL.COMMAND_UNCLAIM_LOG.format(context.fPlayer.getName(), target.getCoordString(), targetFaction.getTag()));
|
2019-08-24 19:18:50 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
2019-09-14 21:13:01 +02:00
|
|
|
context.msg(TL.COMMAND_UNCLAIM_WARZONE_NOPERM);
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
if (context.fPlayer.isAdminBypassing()) {
|
|
|
|
LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(target, targetFaction, context.fPlayer);
|
|
|
|
Bukkit.getServer().getPluginManager().callEvent(unclaimEvent);
|
2019-08-24 19:18:50 +02:00
|
|
|
if (unclaimEvent.isCancelled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Board.getInstance().removeAt(target);
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
targetFaction.msg(TL.COMMAND_UNCLAIM_UNCLAIMED, context.fPlayer.describeTo(targetFaction, true));
|
|
|
|
context.msg(TL.COMMAND_UNCLAIM_UNCLAIMS);
|
2019-08-24 19:18:50 +02:00
|
|
|
|
|
|
|
if (Conf.logLandUnclaims) {
|
2019-09-14 21:13:01 +02:00
|
|
|
FactionsPlugin.getInstance().log(TL.COMMAND_UNCLAIM_LOG.format(context.fPlayer.getName(), target.getCoordString(), targetFaction.getTag()));
|
2019-08-24 19:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
if (targetFaction.getClaimOwnership().containsKey(target) && !targetFaction.isPlayerInOwnerList(context.fPlayer, target)) {
|
|
|
|
context.msg(TL.GENERIC_FPERM_OWNER_NOPERMISSION, "unclaim");
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
if (targetFaction.getAccess(context.fPlayer, PermissableAction.TERRITORY) == Access.DENY && context.fPlayer.getRole() != Role.LEADER) {
|
|
|
|
context.msg(TL.GENERIC_FPERM_NOPERMISSION, "unclaim");
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
if (!context.assertHasFaction()) {
|
|
|
|
context.msg(TL.ACTIONS_NOFACTION);
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
if (context.faction != targetFaction) {
|
|
|
|
context.msg(TL.COMMAND_UNCLAIM_WRONGFACTION);
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-14 21:13:01 +02:00
|
|
|
LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(target, targetFaction, context.fPlayer);
|
|
|
|
Bukkit.getScheduler().runTask(FactionsPlugin.getInstance(), () -> Bukkit.getServer().getPluginManager().callEvent(unclaimEvent));
|
2019-08-24 19:18:50 +02:00
|
|
|
if (unclaimEvent.isCancelled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Econ.shouldBeUsed()) {
|
2019-09-14 21:13:01 +02:00
|
|
|
double refund = Econ.calculateClaimRefund(context.faction.getLandRounded());
|
2019-08-24 19:18:50 +02:00
|
|
|
|
|
|
|
if (Conf.bankEnabled && Conf.bankFactionPaysLandCosts) {
|
2019-09-14 21:13:01 +02:00
|
|
|
if (!Econ.modifyMoney(context.faction, refund, TL.COMMAND_UNCLAIM_TOUNCLAIM.toString(), TL.COMMAND_UNCLAIM_FORUNCLAIM.toString())) {
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-14 21:13:01 +02:00
|
|
|
if (!Econ.modifyMoney(context.fPlayer, refund, TL.COMMAND_UNCLAIM_TOUNCLAIM.toString(), TL.COMMAND_UNCLAIM_FORUNCLAIM.toString())) {
|
2019-08-24 19:18:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Board.getInstance().removeAt(target);
|
2019-09-14 21:13:01 +02:00
|
|
|
context.faction.msg(TL.COMMAND_UNCLAIM_FACTIONUNCLAIMED, context.fPlayer.describeTo(context.faction, true));
|
2019-08-24 19:18:50 +02:00
|
|
|
|
|
|
|
if (Conf.logLandUnclaims) {
|
2019-09-14 21:13:01 +02:00
|
|
|
FactionsPlugin.getInstance().log(TL.COMMAND_UNCLAIM_LOG.format(context.fPlayer.getName(), target.getCoordString(), targetFaction.getTag()));
|
2019-08-24 19:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TL getUsageTranslation() {
|
|
|
|
return TL.COMMAND_UNCLAIM_DESCRIPTION;
|
|
|
|
}
|
2015-01-22 00:58:33 +01:00
|
|
|
|
2011-03-22 15:45:41 +01:00
|
|
|
}
|