From 45d6a6628a74755d969dde3a509a55e55bc0b95e Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 22 Oct 2011 16:00:24 +0200 Subject: [PATCH] Claim command refactoring in progress. --- src/com/massivecraft/factions/FPlayer.java | 177 ++++++++---------- src/com/massivecraft/factions/Faction.java | 2 +- .../factions/cmd/CmdAutoClaim.java | 2 +- .../massivecraft/factions/cmd/CmdClaim.java | 6 +- .../listeners/FactionsPlayerListener.java | 2 +- .../factions/util/RelationUtil.java | 2 +- .../factions/zcore/util/TextUtil.java | 2 +- 7 files changed, 86 insertions(+), 107 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 373f39a3..7f659d88 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -1,6 +1,8 @@ package com.massivecraft.factions; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -624,167 +626,142 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator } } - public boolean attemptClaim(boolean notifyFailure) + public boolean canClaimForFactionAtLocation(Faction forFaction, Location location, boolean notifyFailure) { - // notifyFailure is false if called by auto-claim; no need to notify on every failure for it - // return value is false on failure, true on success - + String error = null; + FLocation flocation = new FLocation(location); Faction myFaction = getFaction(); - Location loc = this.getPlayer().getLocation(); - FLocation flocation = new FLocation(loc); - Faction otherFaction = Board.getFactionAt(flocation); - - if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(loc)) + Faction currentFaction = Board.getFactionAt(flocation); + int ownedLand = forFaction.getLandRounded(); + + if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(location)) { // Checks for WorldGuard regions in the chunk attempting to be claimed - msg("This land is protected"); - return false; + error = P.p.txt.parse("This land is protected"); } - - if (myFaction == otherFaction) + else if (Conf.worldsNoClaiming.contains(flocation.getWorldName())) { - if (notifyFailure) - msg("You already own this land."); - return false; + error = P.p.txt.parse("Sorry, this world has land claiming disabled."); } - - if (this.getRole().value < Role.MODERATOR.value && ! this.isAdminBypassing()) + else if (this.isAdminBypassing()) { - msg("You must be "+Role.MODERATOR+" to claim land."); - return false; + return true; } - - if (myFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers && ! this.isAdminBypassing()) + else if (myFaction != forFaction) { - msg("Your faction must have at least %s members to claim land.", Conf.claimsRequireMinFactionMembers); - return false; + error = P.p.txt.parse("You can't claim land for %s.", forFaction.describeTo(this)); } - - if (Conf.worldsNoClaiming.contains(flocation.getWorldName())) + else if (forFaction == currentFaction) { - msg("Sorry, this world has land claiming disabled."); - return false; + error = P.p.txt.parse("%s already own this land.", forFaction.describeTo(this, true)); } - - if (otherFaction.isSafeZone()) + else if (this.getRole().value < Role.MODERATOR.value) { - if (notifyFailure) - msg("You can not claim a Safe Zone."); - return false; + error = P.p.txt.parse("You must be %s to claim land.", Role.MODERATOR.toString()); } - else if (otherFaction.isWarZone()) + else if (forFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers) { - if (notifyFailure) - msg("You can not claim a War Zone."); - return false; + error = P.p.txt.parse("Factions must have at least %s members to claim land.", Conf.claimsRequireMinFactionMembers); } - - int ownedLand = myFaction.getLandRounded(); - if (ownedLand >= myFaction.getPowerRounded()) + else if (currentFaction.isSafeZone()) { - msg("You can't claim more land! You need more power!"); - return false; + error = P.p.txt.parse("You can not claim a Safe Zone."); } - - if (otherFaction.getRelationTo(this) == Relation.ALLY) + else if (currentFaction.isWarZone()) { - if (notifyFailure) - msg("You can't claim the land of your allies."); - return false; + error = P.p.txt.parse("You can not claim a War Zone."); } - - if + else if (ownedLand >= forFaction.getPowerRounded()) + { + error = P.p.txt.parse("You can't claim more land! You need more power!"); + } + else if (currentFaction.getRelationTo(forFaction) == Relation.ALLY) + { + error = P.p.txt.parse("You can't claim the land of your allies."); + } + else if ( Conf.claimsMustBeConnected && ! this.isAdminBypassing() && myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0 && !Board.isConnectedLocation(flocation, myFaction) - && (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !otherFaction.isNormal()) + && (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal()) ) { if (Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction) - msg("You can only claim additional land which is connected to your first claim or controlled by another faction!"); + error = P.p.txt.parse("You can only claim additional land which is connected to your first claim or controlled by another faction!"); else - msg("You can only claim additional land which is connected to your first claim!"); - return false; + error = P.p.txt.parse("You can only claim additional land which is connected to your first claim!"); } - - if (otherFaction.isNormal()) + else if (currentFaction.isNormal()) { if (myFaction.isPeaceful()) { - msg("%s owns this land. Your faction is peaceful, so you cannot claim land from other factions.", otherFaction.getTag(this)); - return false; + error = P.p.txt.parse("%s owns this land. Your faction is peaceful, so you cannot claim land from other factions.", currentFaction.getTag(this)); } - - if (otherFaction.isPeaceful()) + else if (currentFaction.isPeaceful()) { - msg("%s owns this land, and is a peaceful faction. You cannot claim land from them.", otherFaction.getTag(this)); - return false; + error = P.p.txt.parse("%s owns this land, and is a peaceful faction. You cannot claim land from them.", currentFaction.getTag(this)); } - - if ( ! otherFaction.hasLandInflation()) + else if ( ! currentFaction.hasLandInflation()) { // TODO more messages WARN current faction most importantly - msg("%s owns this land and is strong enough to keep it.", otherFaction.getTag(this)); - return false; + error = P.p.txt.parse("%s owns this land and is strong enough to keep it.", currentFaction.getTag(this)); } - - if ( ! Board.isBorderLocation(flocation)) + else if ( ! Board.isBorderLocation(flocation)) { - msg("You must start claiming land at the border of the territory."); - return false; + error = P.p.txt.parse("You must start claiming land at the border of the territory."); } } - + + if (notifyFailure && error != null) + { + msg(error); + } + return error == null; + } + + public boolean attemptClaim(Faction forFaction, Location location, boolean notifyFailure) + { + // notifyFailure is false if called by auto-claim; no need to notify on every failure for it + // return value is false on failure, true on success + + FLocation flocation = new FLocation(location); + Faction currentFaction = Board.getFactionAt(flocation); + + int ownedLand = forFaction.getLandRounded(); + + if ( ! this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) return false; + // if economy is enabled and they're not on the bypass list, make 'em pay if (Econ.shouldBeUsed() && ! this.isAdminBypassing()) { - double cost = Econ.calculateClaimCost(ownedLand, otherFaction.isNormal()); + double cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal()); //String costString = Econ.moneyString(cost); if(Conf.bankFactionPaysLandCosts && this.hasFaction()) { Faction faction = this.getFaction(); if ( ! Econ.modifyMoney(faction, -cost, "to claim this land", "for claiming this land")) return false; - /* - if( ! faction.removeMoney(cost)) - { - msg("It costs %s to claim this land, which your faction can't currently afford.", costString); - return false; - } - else - { - // TODO: Only I can see this right? - msg("%s has paid %s to claim some land.", faction.getTag(this), costString); - }*/ + } else { if ( ! Econ.modifyMoney(this, -cost, "to claim this land", "for claiming this land")) return false; - /*if ( ! Econ.deductMoney(this.getId(), cost)) - { - msg("Claiming this land will cost %s, which you can't currently afford.", costString); - return false; - } - sendMessage("You have paid "+costString+" to claim this land.");*/ + } } - + // announce success - if (otherFaction.isNormal()) + Set informTheseFPlayers = new HashSet(); + informTheseFPlayers.add(this); + informTheseFPlayers.addAll(forFaction.getFPlayersWhereOnline(true)); + for (FPlayer fp : informTheseFPlayers) { - // ASDF claimed some of your land 450 blocks NNW of you. - // ASDf claimed some land from FACTION NAME - otherFaction.msg("%s stole some of your land :O", this.describeTo(otherFaction, true)); - myFaction.msg("%s claimed some land from %s.", this.describeTo(myFaction, true), otherFaction.describeTo(myFaction)); + fp.msg("%s claimed land for %s from %s.", this.describeTo(fp, true), forFaction.describeTo(fp), currentFaction.describeTo(fp)); } - else - { - myFaction.msg("%s claimed some new land :D", this.describeTo(myFaction)); - } - - Board.setFactionAt(myFaction, flocation); + + Board.setFactionAt(forFaction, flocation); return true; } diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index ab1835be..8666cd95 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -68,7 +68,7 @@ public class Faction extends Entity implements EconomyParticipator { return getTag(); } - return this.getTag(otherFaction.getColorTo(this).toString()); + return this.getTag(this.getColorTo(otherFaction).toString()); } public String getTag(FPlayer otherFplayer) { if (otherFplayer == null) diff --git a/src/com/massivecraft/factions/cmd/CmdAutoClaim.java b/src/com/massivecraft/factions/cmd/CmdAutoClaim.java index ca687029..98951597 100644 --- a/src/com/massivecraft/factions/cmd/CmdAutoClaim.java +++ b/src/com/massivecraft/factions/cmd/CmdAutoClaim.java @@ -53,7 +53,7 @@ public class CmdAutoClaim extends FCommand } msg("Auto-claiming of land enabled."); - fme.attemptClaim(false); + fme.attemptClaim(myFaction, me.getLocation(), false); } } diff --git a/src/com/massivecraft/factions/cmd/CmdClaim.java b/src/com/massivecraft/factions/cmd/CmdClaim.java index aa6605ca..f5c6b0d2 100644 --- a/src/com/massivecraft/factions/cmd/CmdClaim.java +++ b/src/com/massivecraft/factions/cmd/CmdClaim.java @@ -1,5 +1,6 @@ package com.massivecraft.factions.cmd; +import com.massivecraft.factions.Faction; import com.massivecraft.factions.struct.Permission; public class CmdClaim extends FCommand @@ -11,7 +12,7 @@ public class CmdClaim extends FCommand this.aliases.add("claim"); //this.requiredArgs.add(""); - //this.optionalArgs.put("", ""); + this.optionalArgs.put("faction", "your"); this.permission = Permission.CLAIM.node; this.disableOnLock = true; @@ -27,7 +28,8 @@ public class CmdClaim extends FCommand @Override public void perform() { - fme.attemptClaim(true); + Faction forFaction = this.argAsFaction(0, myFaction); + fme.attemptClaim(forFaction, me.getLocation(), true); } } diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index a21c4f74..902802ef 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -279,7 +279,7 @@ public class FactionsPlayerListener extends PlayerListener me.setIsAutoClaimEnabled(false); } else - me.attemptClaim(false); + me.attemptClaim(myFaction, player.getLocation(), false); } else if (me.isAutoSafeClaimEnabled()) { diff --git a/src/com/massivecraft/factions/util/RelationUtil.java b/src/com/massivecraft/factions/util/RelationUtil.java index 0762033a..12104ca7 100644 --- a/src/com/massivecraft/factions/util/RelationUtil.java +++ b/src/com/massivecraft/factions/util/RelationUtil.java @@ -29,7 +29,7 @@ public class RelationUtil } else { - ret = "the faction " + thatFaction.getTag(); + ret = thatFaction.getTag(); } } else if (that instanceof FPlayer) diff --git a/src/com/massivecraft/factions/zcore/util/TextUtil.java b/src/com/massivecraft/factions/zcore/util/TextUtil.java index c3f1019f..8db25be6 100644 --- a/src/com/massivecraft/factions/zcore/util/TextUtil.java +++ b/src/com/massivecraft/factions/zcore/util/TextUtil.java @@ -287,7 +287,7 @@ public class TextUtil public static int commonStartLength(String a, String b) { - int len = a.length() > b.length() ? a.length() : b.length(); + int len = a.length() < b.length() ? a.length() : b.length(); int i; for (i = 0; i < len; i++) {