Claim command refactoring in progress.

This commit is contained in:
Olof Larsson 2011-10-22 16:00:24 +02:00
parent 5bf38ab0aa
commit 45d6a6628a
7 changed files with 86 additions and 107 deletions

View File

@ -1,6 +1,8 @@
package com.massivecraft.factions; package com.massivecraft.factions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; 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 String error = null;
// return value is false on failure, true on success FLocation flocation = new FLocation(location);
Faction myFaction = getFaction(); Faction myFaction = getFaction();
Location loc = this.getPlayer().getLocation(); Faction currentFaction = Board.getFactionAt(flocation);
FLocation flocation = new FLocation(loc); int ownedLand = forFaction.getLandRounded();
Faction otherFaction = Board.getFactionAt(flocation);
if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(loc)) if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(location))
{ {
// Checks for WorldGuard regions in the chunk attempting to be claimed // Checks for WorldGuard regions in the chunk attempting to be claimed
msg("<b>This land is protected"); error = P.p.txt.parse("<b>This land is protected");
return false;
} }
else if (Conf.worldsNoClaiming.contains(flocation.getWorldName()))
if (myFaction == otherFaction)
{ {
if (notifyFailure) error = P.p.txt.parse("<b>Sorry, this world has land claiming disabled.");
msg("<i>You already own this land.");
return false;
} }
else if (this.isAdminBypassing())
if (this.getRole().value < Role.MODERATOR.value && ! this.isAdminBypassing())
{ {
msg("<i>You must be "+Role.MODERATOR+" to claim land."); return true;
return false;
} }
else if (myFaction != forFaction)
if (myFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers && ! this.isAdminBypassing())
{ {
msg("<b>Your faction must have at least <h>%s<b> members to claim land.", Conf.claimsRequireMinFactionMembers); error = P.p.txt.parse("<b>You can't claim land for <h>%s<b>.", forFaction.describeTo(this));
return false;
} }
else if (forFaction == currentFaction)
if (Conf.worldsNoClaiming.contains(flocation.getWorldName()))
{ {
msg("<b>Sorry, this world has land claiming disabled."); error = P.p.txt.parse("%s<i> already own this land.", forFaction.describeTo(this, true));
return false;
} }
else if (this.getRole().value < Role.MODERATOR.value)
if (otherFaction.isSafeZone())
{ {
if (notifyFailure) error = P.p.txt.parse("<b>You must be <h>%s<b> to claim land.", Role.MODERATOR.toString());
msg("<b>You can not claim a Safe Zone.");
return false;
} }
else if (otherFaction.isWarZone()) else if (forFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers)
{ {
if (notifyFailure) error = P.p.txt.parse("Factions must have at least <h>%s<b> members to claim land.", Conf.claimsRequireMinFactionMembers);
msg("<b>You can not claim a War Zone.");
return false;
} }
else if (currentFaction.isSafeZone())
int ownedLand = myFaction.getLandRounded();
if (ownedLand >= myFaction.getPowerRounded())
{ {
msg("<b>You can't claim more land! You need more power!"); error = P.p.txt.parse("<b>You can not claim a Safe Zone.");
return false;
} }
else if (currentFaction.isWarZone())
if (otherFaction.getRelationTo(this) == Relation.ALLY)
{ {
if (notifyFailure) error = P.p.txt.parse("<b>You can not claim a War Zone.");
msg("<b>You can't claim the land of your allies.");
return false;
} }
else if (ownedLand >= forFaction.getPowerRounded())
if {
error = P.p.txt.parse("<b>You can't claim more land! You need more power!");
}
else if (currentFaction.getRelationTo(forFaction) == Relation.ALLY)
{
error = P.p.txt.parse("<b>You can't claim the land of your allies.");
}
else if
( (
Conf.claimsMustBeConnected Conf.claimsMustBeConnected
&& ! this.isAdminBypassing() && ! this.isAdminBypassing()
&& myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0 && myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0
&& !Board.isConnectedLocation(flocation, myFaction) && !Board.isConnectedLocation(flocation, myFaction)
&& (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !otherFaction.isNormal()) && (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal())
) )
{ {
if (Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction) if (Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction)
msg("<b>You can only claim additional land which is connected to your first claim or controlled by another faction!"); error = P.p.txt.parse("<b>You can only claim additional land which is connected to your first claim or controlled by another faction!");
else else
msg("<b>You can only claim additional land which is connected to your first claim!"); error = P.p.txt.parse("<b>You can only claim additional land which is connected to your first claim!");
return false;
} }
else if (currentFaction.isNormal())
if (otherFaction.isNormal())
{ {
if (myFaction.isPeaceful()) if (myFaction.isPeaceful())
{ {
msg("%s<i> owns this land. Your faction is peaceful, so you cannot claim land from other factions.", otherFaction.getTag(this)); error = P.p.txt.parse("%s<i> owns this land. Your faction is peaceful, so you cannot claim land from other factions.", currentFaction.getTag(this));
return false;
} }
else if (currentFaction.isPeaceful())
if (otherFaction.isPeaceful())
{ {
msg("%s<i> owns this land, and is a peaceful faction. You cannot claim land from them.", otherFaction.getTag(this)); error = P.p.txt.parse("%s<i> owns this land, and is a peaceful faction. You cannot claim land from them.", currentFaction.getTag(this));
return false;
} }
else if ( ! currentFaction.hasLandInflation())
if ( ! otherFaction.hasLandInflation())
{ {
// TODO more messages WARN current faction most importantly // TODO more messages WARN current faction most importantly
msg("%s<i> owns this land and is strong enough to keep it.", otherFaction.getTag(this)); error = P.p.txt.parse("%s<i> owns this land and is strong enough to keep it.", currentFaction.getTag(this));
return false;
} }
else if ( ! Board.isBorderLocation(flocation))
if ( ! Board.isBorderLocation(flocation))
{ {
msg("<b>You must start claiming land at the border of the territory."); error = P.p.txt.parse("<b>You must start claiming land at the border of the territory.");
return false;
} }
} }
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 economy is enabled and they're not on the bypass list, make 'em pay
if (Econ.shouldBeUsed() && ! this.isAdminBypassing()) if (Econ.shouldBeUsed() && ! this.isAdminBypassing())
{ {
double cost = Econ.calculateClaimCost(ownedLand, otherFaction.isNormal()); double cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal());
//String costString = Econ.moneyString(cost); //String costString = Econ.moneyString(cost);
if(Conf.bankFactionPaysLandCosts && this.hasFaction()) if(Conf.bankFactionPaysLandCosts && this.hasFaction())
{ {
Faction faction = this.getFaction(); Faction faction = this.getFaction();
if ( ! Econ.modifyMoney(faction, -cost, "to claim this land", "for claiming this land")) return false; if ( ! Econ.modifyMoney(faction, -cost, "to claim this land", "for claiming this land")) return false;
/*
if( ! faction.removeMoney(cost))
{
msg("<b>It costs <h>%s<b> to claim this land, which your faction can't currently afford.", costString);
return false;
}
else
{
// TODO: Only I can see this right?
msg("%s<i> has paid <h>%s<i> to claim some land.", faction.getTag(this), costString);
}*/
} }
else else
{ {
if ( ! Econ.modifyMoney(this, -cost, "to claim this land", "for claiming this land")) return false; if ( ! Econ.modifyMoney(this, -cost, "to claim this land", "for claiming this land")) return false;
/*if ( ! Econ.deductMoney(this.getId(), cost))
{
msg("<b>Claiming this land will cost <h>%s<b>, which you can't currently afford.", costString);
return false;
}
sendMessage("You have paid "+costString+" to claim this land.");*/
} }
} }
// announce success // announce success
if (otherFaction.isNormal()) Set<FPlayer> informTheseFPlayers = new HashSet<FPlayer>();
informTheseFPlayers.add(this);
informTheseFPlayers.addAll(forFaction.getFPlayersWhereOnline(true));
for (FPlayer fp : informTheseFPlayers)
{ {
// ASDF claimed some of your land 450 blocks NNW of you. fp.msg("<h>%s<i> claimed land for <h>%s<i> from <h>%s<i>.", this.describeTo(fp, true), forFaction.describeTo(fp), currentFaction.describeTo(fp));
// ASDf claimed some land from FACTION NAME
otherFaction.msg("%s<i> stole some of your land :O", this.describeTo(otherFaction, true));
myFaction.msg("%s<i> claimed some land from %s.", this.describeTo(myFaction, true), otherFaction.describeTo(myFaction));
}
else
{
myFaction.msg("%s<i> claimed some new land :D", this.describeTo(myFaction));
} }
Board.setFactionAt(myFaction, flocation); Board.setFactionAt(forFaction, flocation);
return true; return true;
} }

View File

@ -68,7 +68,7 @@ public class Faction extends Entity implements EconomyParticipator
{ {
return getTag(); return getTag();
} }
return this.getTag(otherFaction.getColorTo(this).toString()); return this.getTag(this.getColorTo(otherFaction).toString());
} }
public String getTag(FPlayer otherFplayer) { public String getTag(FPlayer otherFplayer) {
if (otherFplayer == null) if (otherFplayer == null)

View File

@ -53,7 +53,7 @@ public class CmdAutoClaim extends FCommand
} }
msg("<i>Auto-claiming of land enabled."); msg("<i>Auto-claiming of land enabled.");
fme.attemptClaim(false); fme.attemptClaim(myFaction, me.getLocation(), false);
} }
} }

View File

@ -1,5 +1,6 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
public class CmdClaim extends FCommand public class CmdClaim extends FCommand
@ -11,7 +12,7 @@ public class CmdClaim extends FCommand
this.aliases.add("claim"); this.aliases.add("claim");
//this.requiredArgs.add(""); //this.requiredArgs.add("");
//this.optionalArgs.put("", ""); this.optionalArgs.put("faction", "your");
this.permission = Permission.CLAIM.node; this.permission = Permission.CLAIM.node;
this.disableOnLock = true; this.disableOnLock = true;
@ -27,7 +28,8 @@ public class CmdClaim extends FCommand
@Override @Override
public void perform() public void perform()
{ {
fme.attemptClaim(true); Faction forFaction = this.argAsFaction(0, myFaction);
fme.attemptClaim(forFaction, me.getLocation(), true);
} }
} }

View File

@ -279,7 +279,7 @@ public class FactionsPlayerListener extends PlayerListener
me.setIsAutoClaimEnabled(false); me.setIsAutoClaimEnabled(false);
} }
else else
me.attemptClaim(false); me.attemptClaim(myFaction, player.getLocation(), false);
} }
else if (me.isAutoSafeClaimEnabled()) else if (me.isAutoSafeClaimEnabled())
{ {

View File

@ -29,7 +29,7 @@ public class RelationUtil
} }
else else
{ {
ret = "the faction " + thatFaction.getTag(); ret = thatFaction.getTag();
} }
} }
else if (that instanceof FPlayer) else if (that instanceof FPlayer)

View File

@ -287,7 +287,7 @@ public class TextUtil
public static int commonStartLength(String a, String b) 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; int i;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {