Skip to content

Instantly share code, notes, and snippets.

@riking
Last active December 14, 2015 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riking/5061940 to your computer and use it in GitHub Desktop.
Save riking/5061940 to your computer and use it in GitHub Desktop.
Extensible version of Factions' Player.canClaimForFactionAtLocation(Faction forFaction, Location location, boolean notifyFailure). More work will need to be done to make this usable, like sending the message from the ClaimResult, keeping a modifiable global instance of a IFClaimChecker. Even per-player claim checkers would be possible.
package com.aethercraft.factions;
import org.bukkit.Location;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.claims.DefaultClaimChecker;
public class AetherCraftClaimChecker extends DefaultClaimChecker
{
public static int[][] orthro = new int[2][4]
{
/* */ { 0, 1}, /* */
{-1, 0}, /* */ { 1, 0},
/* */ { 0,-1} /* */
};
public static int[][] adjacent = new int[2][4]
{
{-1, 1}, { 0, 1}, { 1, 1},
{-1, 0}, /* */ { 1, 0},
{-1,-1}, { 0,-1}, { 1,-1}
};
private static boolean useOrthro;
private static int minSafeCount;
private static ClaimResult result;
public static final ClaimResult strictResult = ClaimResult.create(false, "<b>You must claim from the corners of the enemy faction's territory <i>(straight surround count < <h>3<i>)")
public static final ClaimResult strictSuperResult = ClaimResult.create(false, "<b>You must claim from obtuse angles of the enemy faction territory <i>(surround count < <h>5<i>)")
public static final ClaimResult easyResult = ClaimResult.create(false, "<b>You must claim from the sides of the enemy faction <i>(surround count < <h>6<i>)")
public void useStrictMode()
{
useOrthro = true;
minSafeCount = 3;
result = strictResult;
}
public void useSuperStrictMode()
{
useOrthro = false;
minSafeCount = 5;
result = superStrictResult;
}
public void useEasyMode()
{
useOrthro = false;
minSafeCount = 6;
result = easyResult;
}
private int countNeighbors(Faction defender, FLocation floc)
{
FLocation[] surrounds;
if (useOrthro)
{
surrounds = new FLocation[4] {
floc.getRelative(orthro[0][0], orthro[0][1]);
floc.getRelative(orthro[1][0], orthro[1][1]);
floc.getRelative(orthro[2][0], orthro[2][1]);
floc.getRelative(orthro[3][0], orthro[3][1]);
};
}
else
{
surrounds = new FLocation[8] {
floc.getRelative(orthro[0][0], orthro[0][1]);
floc.getRelative(orthro[1][0], orthro[1][1]);
floc.getRelative(orthro[2][0], orthro[2][1]);
floc.getRelative(orthro[3][0], orthro[3][1]);
floc.getRelative(orthro[4][0], orthro[4][1]);
floc.getRelative(orthro[5][0], orthro[5][1]);
floc.getRelative(orthro[6][0], orthro[6][1]);
floc.getRelative(orthro[7][0], orthro[7][1]);
};
}
int surroundCount = 0;
for (FLocation surrFLoc : surrounds) {
surroundCount += (faction == Board.getFactionAt(surrFLoc)) ? 1 : 0;
}
return surroundCount;
}
@Override
public ClaimResult checkClaim(FPlayer player, Faction forFaction, Faction fromFaction, Location location)
{
FLocation flocation = new FLocation(location);
ClaimResult baseResult = super.checkClaim(player, forFaction, fromFaction, location);
if (baseResult == ClaimResult.success_default)
{
int neighbors = countNeighbors(fromFaction, flocation);
if (neighbors < minSafeCount)
{
return result;
}
}
return baseResult;
}
}
package com.massivecraft.factions.claims;
import org.bukkit.Location;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Rel;
import static FClaimChecker.ClaimResult;
public abstract class BaseClaimChecker implements IFClaimChecker
{
/**
* Extending classes - do the following:
* ClaimResult baseResult = super.checkClaim(player, forFaction, fromFaction, location);
* Then, do your additional processing.
* <p />
* Note that <b>if the claim would succeed by default, null is returned.</b>
*
* @return ClaimResult - null if no problems, failing ClaimResult if there are problems with the claim
*/
@Override
public ClaimResult checkClaim(FPlayer player, Faction forFaction, Faction fromFaction, Location location)
{
if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(location))
{
return ClaimResult.landProtect;
}
FLocation flocation = new FLocation(location);
if (Conf.worldsNoClaiming.contains(flocation.getWorldName()))
{
return ClaimResult.fail_worldProtect;
}
if (forFaction == fromFaction)
{
return ClaimResult.fail_sameFaction(forFaction.describeTo(player, true));
}
if (forFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers)
{
return ClaimResult.fail_notEnoughMembers(Conf.claimsRequireMinFactionMembers);
}
if (forFaction.getLandRounded() >= forFaction.getPowerRounded())
{
return ClaimResult.fail_morePowerNeeded;
}
if (Conf.claimedLandsMax != 0
&& forFaction.getLandRounded() >= Conf.claimedLandsMax
&& !forFaction.getFlag(FFlag.INFPOWER))
{
return ClaimResult.fail_morePowerNeeded;
}
if (!Conf.claimingFromOthersAllowed && fromFaction.isNormal())
{
return ClaimResult.fail_noEnemyClaim;
}
if (!fromFaction.isNone())
{
Rel relation = fromFaction.getRelationTo(forFaction)
if (relation.isAtLeast(Rel.TRUCE))
{
return ClaimResult.fail_relation(relation);
}
}
if (Conf.claimsMustBeConnected
&& myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0
&& !Board.isConnectedLocation(flocation, myFaction))
&& !(Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction && fromFaction.isNormal())
{
if (Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction)
{
return ClaimResult.fail_mustConnect;
}
else
{
return ClaimResult.fail_mustConnectWilderness;
}
}
if (fromFaction.isNormal())
{
if (!fromFaction.hasLandInflation())
{
return ClaimResult.fail_powerfulEnemy(fromFaction.getTag(player));
}
if (!Board.isBorderLocation(flocation))
{
return ClaimResult.fail_insideEnemyTerr;
}
}
return null;
}
package com.massivecraft.factions.claims;
public class DefaultClaimChecker extends BaseClaimChecker
{
@Override
public ClaimResult checkClaim(FPlayer player, Faction forFaction, Faction fromFaction, Location location)
{
ClaimResult baseResult = super.checkClaim(player, forFaction, fromFaction, location);
if (baseResult == null)
{
return ClaimResult.success_default;
}
// Admin overrides all except world protect
if (player.hasAdminMode() && (baseResult != ClaimResult.fail_worldProtect))
{
return ClaimResult.success_admin;
}
return baseResult;
}
}
package com.massivecraft.factions.claims;
import org.bukkit.Location;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.struct.Rel;
/**
* This file was written by Kane York.
* This code is released to the public domain as much as possible through the CC0 license.
*/
public interface IFClaimChecker
{
/**
* Can this player claim land for this faction at this location?
* @param player - player attempting the claim
* @param forFaction - faction player is claiming for
* @param fromFaction - faction player is claiming from (often Wilderness)
* @param location - bukkit location the claim was done at
* @return ClaimResult describing the result of the claim attempt
*/
public ClaimResult checkClaim(FPlayer player, Faction forFaction, Faction fromFaction, Location location);
/**
* Class to describe the result of a claim attempt
*/
public static class ClaimResult
{
/**
* Something about this location is protected.
*/
public static final ClaimResult fail_landProtect = new ClaimResult(false, "<b>This land is protected");
/**
* This world is protected.
*/
public static final ClaimResult fail_worldProtect = new ClaimResult(false, "<b>Cannot claim land in this world.");
/**
* This faction's power <= land
*/
public static final ClaimResult fail_morePowerNeeded = new ClaimResult(false, "<b>You can't claim more land! You need more power!");
/**
* This faction has reached the land cap
*/
public static final ClaimResult fail_maxLandReached = new ClaimResult(false, "<b>Limit reached. You can't claim more land!");
/**
* Normal factions unable to claim from others
*/
public static final ClaimResult fail_noEnemyClaim = new ClaimResult(false, "<b>You may not claim land from others.");
/**
* Can't claim from inside enemy territory
*/
public static final ClaimResult fail_insideEnemyTerr = new ClaimResult(false, "<b>You can't claim land from inside of enemy territory - start on the outside.");
/**
* All land has to be connected to other lands
*/
public static final ClaimResult fail_mustConnect = new ClaimResult(false, "<b>You can only claim additional land which is connected to your first claim!");
/**
* All land has to connect, except for enemy land
*/
public static final ClaimResult fail_mustConnectWilderness = new ClaimResult(false, "<b>You can only claim additional land which is connected to your first claim, or controlled by another faction!");
/**
* Admins can do anything
*/
public static final ClaimResult success_admin = new ClaimResult(true, "<b>Claim succeeded due to admin");
/**
* No failure conditions met
*/
public static final ClaimResult success_default = new ClaimResult(true, "");
/**
* This land already claimed for your faction.
* @param ourFaction - colored name of land owner via Faction.describeTo()
*/
public static ClaimResult fail_sameFaction(String ourFaction)
{
ClaimResult cr = new ClaimResult(false);
cr.message = P.p.txt.parse("%s<i> already own this land.", ourfaction);
return cr;
}
/**
* Enemy faction can keep this land
* @param otherFaction - colored name of land owner via Faction.describeTo()
*/
public static ClaimResult fail_powerfulEnemy(String otherFaction)
{
return createPreFormatted(false, P.p.txt.parse("%s<i> owns this land and is strong enough to keep it.", otherFaction));
return cr;
}
/**
* Your faction needs more people to meet the minimum member requirement
* @param minMembers - minimum members to claim land via Conf.claimsRequireMinFactionMembers
*/
public static final ClaimResult fail_notEnoughMembers(int minMembers)
{
return createPreFormatted(false, P.p.txt.parse("Factions must have at least <h>%s<b> members to claim land.", Integer.toString(minMembers)));
}
/**
* Failure due to relationship being too high
* @param relation - relation between the factions
*/
public static final ClaimResult fail_relation(Rel relation)
{
return createPreFormatted(false, P.p.txt.parse("<b>You may not claim land from <i>%s<b>.", relation.getDescFactionMany()));
}
/**
* Create a custom ClaimResult with the specified result and message (run through the parser).
* @param success - the value of getResult()
* @param message - string to run through TextUtils.parse()
*
* @return ClaimResult with specified values
*/
public static ClaimResult create(boolean success, String message)
{
return new ClaimResult(success, message);
}
public static ClaimResult createPreFormatted(boolean success, String formattedMessage)
{
if (formattedMessage == null) throw new IllegalArgumentException("Message cannot be null");
ClaimResult cr = new ClaimResult(success);
cr.message = formattedMessage;
return cr;
}
private boolean result;
private String message;
private ClaimResult(boolean success)
{
this.result = success;
}
private ClaimResult(boolean success, String unformatted)
{
this.result = success;
this.message = P.p.txt.parse(unformatted);
}
public boolean getResult()
{
return this.result;
}
public String getMessage()
{
return this.message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment