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;
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);
Faction currentFaction = Board.getFactionAt(flocation);
int ownedLand = forFaction.getLandRounded();
if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(loc))
if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(location))
{
// Checks for WorldGuard regions in the chunk attempting to be claimed
msg("<b>This land is protected");
return false;
error = P.p.txt.parse("<b>This land is protected");
}
if (myFaction == otherFaction)
else if (Conf.worldsNoClaiming.contains(flocation.getWorldName()))
{
if (notifyFailure)
msg("<i>You already own this land.");
return false;
error = P.p.txt.parse("<b>Sorry, this world has land claiming disabled.");
}
if (this.getRole().value < Role.MODERATOR.value && ! this.isAdminBypassing())
else if (this.isAdminBypassing())
{
msg("<i>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("<b>Your faction must have at least <h>%s<b> members to claim land.", Conf.claimsRequireMinFactionMembers);
return false;
error = P.p.txt.parse("<b>You can't claim land for <h>%s<b>.", forFaction.describeTo(this));
}
if (Conf.worldsNoClaiming.contains(flocation.getWorldName()))
else if (forFaction == currentFaction)
{
msg("<b>Sorry, this world has land claiming disabled.");
return false;
error = P.p.txt.parse("%s<i> already own this land.", forFaction.describeTo(this, true));
}
if (otherFaction.isSafeZone())
else if (this.getRole().value < Role.MODERATOR.value)
{
if (notifyFailure)
msg("<b>You can not claim a Safe Zone.");
return false;
error = P.p.txt.parse("<b>You must be <h>%s<b> to claim land.", Role.MODERATOR.toString());
}
else if (otherFaction.isWarZone())
else if (forFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers)
{
if (notifyFailure)
msg("<b>You can not claim a War Zone.");
return false;
error = P.p.txt.parse("Factions must have at least <h>%s<b> members to claim land.", Conf.claimsRequireMinFactionMembers);
}
int ownedLand = myFaction.getLandRounded();
if (ownedLand >= myFaction.getPowerRounded())
else if (currentFaction.isSafeZone())
{
msg("<b>You can't claim more land! You need more power!");
return false;
error = P.p.txt.parse("<b>You can not claim a Safe Zone.");
}
if (otherFaction.getRelationTo(this) == Relation.ALLY)
else if (currentFaction.isWarZone())
{
if (notifyFailure)
msg("<b>You can't claim the land of your allies.");
return false;
error = P.p.txt.parse("<b>You can not claim a War Zone.");
}
if
else if (ownedLand >= forFaction.getPowerRounded())
{
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
&& ! this.isAdminBypassing()
&& myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0
&& !Board.isConnectedLocation(flocation, myFaction)
&& (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !otherFaction.isNormal())
&& (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal())
)
{
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
msg("<b>You can only claim additional land which is connected to your first claim!");
return false;
error = P.p.txt.parse("<b>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<i> 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<i> 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<i> 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<i> 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<i> owns this land and is strong enough to keep it.", otherFaction.getTag(this));
return false;
error = P.p.txt.parse("%s<i> owns this land and is strong enough to keep it.", currentFaction.getTag(this));
}
if ( ! Board.isBorderLocation(flocation))
else if ( ! Board.isBorderLocation(flocation))
{
msg("<b>You must start claiming land at the border of the territory.");
return false;
error = P.p.txt.parse("<b>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("<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
{
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
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.
// 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));
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));
}
Board.setFactionAt(myFaction, flocation);
Board.setFactionAt(forFaction, flocation);
return true;
}

View File

@ -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)

View File

@ -53,7 +53,7 @@ public class CmdAutoClaim extends FCommand
}
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;
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);
}
}

View File

@ -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())
{

View File

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

View File

@ -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++)
{