Saber-Factions/src/main/java/com/massivecraft/factions/FPlayer.java

784 lines
27 KiB
Java
Raw Normal View History

2011-07-18 22:06:02 +02:00
package com.massivecraft.factions;
2011-02-06 13:36:11 +01:00
import com.massivecraft.factions.event.FPlayerLeaveEvent;
import com.massivecraft.factions.event.LandClaimEvent;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.Worldguard;
import com.massivecraft.factions.scoreboards.sidebar.FInfoSidebar;
import com.massivecraft.factions.scoreboards.FScoreboard;
2011-09-24 12:04:49 +02:00
import com.massivecraft.factions.struct.ChatMode;
import com.massivecraft.factions.struct.Permission;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.factions.zcore.persist.PlayerEntity;
2014-04-04 20:55:21 +02:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
2014-04-04 20:55:21 +02:00
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
2011-02-06 13:36:11 +01:00
/**
2014-04-04 20:55:21 +02:00
* Logged in players always have exactly one FPlayer instance. Logged out players may or may not have an FPlayer
* instance. They will always have one if they are part of a faction. This is because only players with a faction are
* saved to disk (in order to not waste disk space).
* <p/>
2011-03-23 12:00:38 +01:00
* The FPlayer is linked to a minecraft player using the player name.
2014-04-04 20:55:21 +02:00
* <p/>
* The same instance is always returned for the same player. This means you can use the == operator. No .equals method
* necessary.
*/
2014-04-04 20:55:21 +02:00
public class FPlayer extends PlayerEntity implements EconomyParticipator {
//private transient String playerName;
private transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked?
// FIELD: factionId
private String factionId;
public Faction getFaction() {
if (this.factionId == null) {
return null;
2014-07-01 22:10:18 +02:00
}
return Factions.i.get(this.factionId);
2014-04-04 20:55:21 +02:00
}
public String getFactionId() {
return this.factionId;
}
public boolean hasFaction() {
return !factionId.equals("0");
}
public void setFaction(Faction faction) {
2014-07-01 22:10:18 +02:00
Faction oldFaction = this.getFaction();
if (oldFaction != null) {
oldFaction.removeFPlayer(this);
}
faction.addFPlayer(this);
this.factionId = faction.getId();
2014-04-04 20:55:21 +02:00
}
// FIELD: role
private Role role;
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
// FIELD: title
private String title;
// FIELD: power
private double power;
// FIELD: powerBoost
// special increase/decrease to min and max power for this player
private double powerBoost;
public double getPowerBoost() {
return this.powerBoost;
}
public void setPowerBoost(double powerBoost) {
this.powerBoost = powerBoost;
}
// FIELD: lastPowerUpdateTime
private long lastPowerUpdateTime;
// FIELD: lastLoginTime
private long lastLoginTime;
// FIELD: mapAutoUpdating
private transient boolean mapAutoUpdating;
// FIELD: autoClaimEnabled
private transient Faction autoClaimFor;
public Faction getAutoClaimFor() {
return autoClaimFor;
}
public void setAutoClaimFor(Faction faction) {
2014-07-01 22:10:18 +02:00
this.autoClaimFor = faction;
if (this.autoClaimFor != null) {
2014-04-04 20:55:21 +02:00
// TODO: merge these into same autoclaim
2014-07-01 22:10:18 +02:00
this.autoSafeZoneEnabled = false;
this.autoWarZoneEnabled = false;
2014-04-04 20:55:21 +02:00
}
}
// FIELD: autoSafeZoneEnabled
private transient boolean autoSafeZoneEnabled;
public boolean isAutoSafeClaimEnabled() {
return autoSafeZoneEnabled;
}
public void setIsAutoSafeClaimEnabled(boolean enabled) {
2014-07-01 22:10:18 +02:00
this.autoSafeZoneEnabled = enabled;
if (enabled) {
this.autoClaimFor = null;
this.autoWarZoneEnabled = false;
2014-04-04 20:55:21 +02:00
}
}
// FIELD: autoWarZoneEnabled
private transient boolean autoWarZoneEnabled;
public boolean isAutoWarClaimEnabled() {
return autoWarZoneEnabled;
}
public void setIsAutoWarClaimEnabled(boolean enabled) {
2014-07-01 22:10:18 +02:00
this.autoWarZoneEnabled = enabled;
if (enabled) {
this.autoClaimFor = null;
this.autoSafeZoneEnabled = false;
2014-04-04 20:55:21 +02:00
}
}
private transient boolean isAdminBypassing = false;
public boolean isAdminBypassing() {
return this.isAdminBypassing;
}
public void setIsAdminBypassing(boolean val) {
this.isAdminBypassing = val;
}
// FIELD: loginPvpDisabled
private transient boolean loginPvpDisabled;
// FIELD: deleteMe
private transient boolean deleteMe;
// FIELD: chatMode
private ChatMode chatMode;
public void setChatMode(ChatMode chatMode) {
this.chatMode = chatMode;
}
public ChatMode getChatMode() {
if (this.factionId.equals("0") || !Conf.factionOnlyChat) {
this.chatMode = ChatMode.PUBLIC;
2014-07-01 22:10:18 +02:00
}
return chatMode;
2014-04-04 20:55:21 +02:00
}
// FIELD: chatSpy
private transient boolean spyingChat = false;
public void setSpyingChat(boolean chatSpying) {
this.spyingChat = chatSpying;
}
public boolean isSpyingChat() {
return spyingChat;
}
// FIELD: account
public String getAccountId() {
return this.getId();
}
// -------------------------------------------- //
// Construct
// -------------------------------------------- //
// GSON need this noarg constructor.
public FPlayer() {
2014-07-01 22:10:18 +02:00
this.resetFactionData(false);
this.power = Conf.powerPlayerStarting;
this.lastPowerUpdateTime = System.currentTimeMillis();
this.lastLoginTime = System.currentTimeMillis();
this.mapAutoUpdating = false;
this.autoClaimFor = null;
this.autoSafeZoneEnabled = false;
2014-04-04 20:55:21 +02:00
this.autoWarZoneEnabled = false;
2014-07-02 08:37:42 +02:00
this.loginPvpDisabled = Conf.noPVPDamageToOthersForXSecondsAfterLogin > 0;
2014-07-01 22:10:18 +02:00
this.deleteMe = false;
this.powerBoost = 0.0;
2014-04-04 20:55:21 +02:00
if (!Conf.newPlayerStartingFactionID.equals("0") && Factions.i.exists(Conf.newPlayerStartingFactionID)) {
this.factionId = Conf.newPlayerStartingFactionID;
}
}
public final void resetFactionData(boolean doSpoutUpdate) {
// clean up any territory ownership in old faction, if there is one
if (Factions.i.exists(this.getFactionId())) {
2014-07-01 22:10:18 +02:00
Faction currentFaction = this.getFaction();
currentFaction.removeFPlayer(this);
2014-04-04 20:55:21 +02:00
if (currentFaction.isNormal()) {
2014-04-17 03:10:12 +02:00
currentFaction.clearClaimOwnership(this);
2014-04-04 20:55:21 +02:00
}
}
this.factionId = "0"; // The default neutral faction
2014-07-01 22:10:18 +02:00
this.chatMode = ChatMode.PUBLIC;
this.role = Role.NORMAL;
this.title = "";
this.autoClaimFor = null;
2014-04-04 20:55:21 +02:00
}
public void resetFactionData() {
this.resetFactionData(true);
}
// -------------------------------------------- //
// Getters And Setters
// -------------------------------------------- //
public long getLastLoginTime() {
return lastLoginTime;
}
public void setLastLoginTime(long lastLoginTime) {
2014-07-01 22:10:18 +02:00
losePowerFromBeingOffline();
this.lastLoginTime = lastLoginTime;
this.lastPowerUpdateTime = lastLoginTime;
2014-04-04 20:55:21 +02:00
if (Conf.noPVPDamageToOthersForXSecondsAfterLogin > 0) {
this.loginPvpDisabled = true;
}
}
public boolean isMapAutoUpdating() {
return mapAutoUpdating;
}
public void setMapAutoUpdating(boolean mapAutoUpdating) {
this.mapAutoUpdating = mapAutoUpdating;
}
public boolean hasLoginPvpDisabled() {
if (!loginPvpDisabled) {
return false;
}
if (this.lastLoginTime + (Conf.noPVPDamageToOthersForXSecondsAfterLogin * 1000) < System.currentTimeMillis()) {
2014-07-01 22:10:18 +02:00
this.loginPvpDisabled = false;
return false;
}
return true;
2014-04-04 20:55:21 +02:00
}
public FLocation getLastStoodAt() {
return this.lastStoodAt;
}
public void setLastStoodAt(FLocation flocation) {
this.lastStoodAt = flocation;
}
public void markForDeletion(boolean delete) {
deleteMe = delete;
}
//----------------------------------------------//
// Title, Name, Faction Tag and Chat
//----------------------------------------------//
// Base:
public String getTitle() {
return this.hasFaction() ? title : "";
2014-04-04 20:55:21 +02:00
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
2014-05-19 18:45:45 +02:00
if (isOnline()) {
return getPlayer().getName();
2014-07-01 22:10:18 +02:00
}
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(getId()));
return player.getName() != null ? player.getName() : getId();
2014-04-04 20:55:21 +02:00
}
public String getTag() {
return this.hasFaction() ? this.getFaction().getTag() : "";
2014-04-04 20:55:21 +02:00
}
// Base concatenations:
public String getNameAndSomething(String something) {
2014-07-01 22:10:18 +02:00
String ret = this.role.getPrefix();
if (something.length() > 0) {
2014-04-04 20:55:21 +02:00
ret += something + " ";
2014-07-01 22:10:18 +02:00
}
ret += this.getName();
return ret;
2014-04-04 20:55:21 +02:00
}
public String getNameAndTitle() {
return this.getNameAndSomething(this.getTitle());
}
public String getNameAndTag() {
return this.getNameAndSomething(this.getTag());
}
// Colored concatenations:
// These are used in information messages
public String getNameAndTitle(Faction faction) {
return this.getColorTo(faction) + this.getNameAndTitle();
}
public String getNameAndTitle(FPlayer fplayer) {
return this.getColorTo(fplayer) + this.getNameAndTitle();
}
2011-10-21 19:20:33 +02:00
/*public String getNameAndTag(Faction faction)
{
return this.getRelationColor(faction)+this.getNameAndTag();
}
public String getNameAndTag(FPlayer fplayer)
{
2011-03-23 12:00:38 +01:00
return this.getRelationColor(fplayer)+this.getNameAndTag();
2011-10-21 19:20:33 +02:00
}*/
2014-04-04 20:55:21 +02:00
// TODO: Removed for refactoring.
2011-10-21 19:20:33 +02:00
/*public String getNameAndRelevant(Faction faction)
{
// Which relation?
2011-10-12 17:25:01 +02:00
Relation rel = this.getRelationTo(faction);
// For member we show title
if (rel == Relation.MEMBER) {
return rel.getColor() + this.getNameAndTitle();
}
// For non members we show tag
return rel.getColor() + this.getNameAndTag();
}
public String getNameAndRelevant(FPlayer fplayer)
{
2011-03-23 12:00:38 +01:00
return getNameAndRelevant(fplayer.getFaction());
2011-10-21 19:20:33 +02:00
}*/
2014-04-04 20:55:21 +02:00
// Chat Tag:
// These are injected into the format of global chat messages.
public String getChatTag() {
return this.hasFaction() ? String.format(Conf.chatTagFormat, this.role.getPrefix() + this.getTag()) : "";
2014-04-04 20:55:21 +02:00
}
// Colored Chat Tag
public String getChatTag(Faction faction) {
return this.hasFaction() ? this.getRelationTo(faction).getColor() + getChatTag() : "";
2014-04-04 20:55:21 +02:00
}
public String getChatTag(FPlayer fplayer) {
return this.hasFaction() ? this.getColorTo(fplayer) + getChatTag() : "";
2014-04-04 20:55:21 +02:00
}
// -------------------------------
// Relation and relation colors
// -------------------------------
@Override
public String describeTo(RelationParticipator that, boolean ucfirst) {
return RelationUtil.describeThatToMe(this, that, ucfirst);
}
@Override
public String describeTo(RelationParticipator that) {
return RelationUtil.describeThatToMe(this, that);
}
@Override
public Relation getRelationTo(RelationParticipator rp) {
return RelationUtil.getRelationTo(this, rp);
}
@Override
public Relation getRelationTo(RelationParticipator rp, boolean ignorePeaceful) {
return RelationUtil.getRelationTo(this, rp, ignorePeaceful);
}
public Relation getRelationToLocation() {
return Board.getFactionAt(new FLocation(this)).getRelationTo(this);
}
@Override
public ChatColor getColorTo(RelationParticipator rp) {
return RelationUtil.getColorOfThatToMe(this, rp);
}
//----------------------------------------------//
// Health
//----------------------------------------------//
public void heal(int amnt) {
2014-07-01 22:10:18 +02:00
Player player = this.getPlayer();
if (player == null) {
2014-04-04 20:55:21 +02:00
return;
2014-07-01 22:10:18 +02:00
}
player.setHealth(player.getHealth() + amnt);
2014-04-04 20:55:21 +02:00
}
//----------------------------------------------//
// Power
//----------------------------------------------//
public double getPower() {
2014-07-01 22:10:18 +02:00
this.updatePower();
return this.power;
2014-04-04 20:55:21 +02:00
}
protected void alterPower(double delta) {
2014-07-01 22:10:18 +02:00
this.power += delta;
if (this.power > this.getPowerMax()) {
2014-07-01 21:52:40 +02:00
this.power = this.getPowerMax();
2014-07-01 22:10:18 +02:00
} else if (this.power < this.getPowerMin()) {
this.power = this.getPowerMin();
}
2014-04-04 20:55:21 +02:00
}
public double getPowerMax() {
return Conf.powerPlayerMax + this.powerBoost;
}
public double getPowerMin() {
return Conf.powerPlayerMin + this.powerBoost;
}
public int getPowerRounded() {
return (int) Math.round(this.getPower());
}
public int getPowerMaxRounded() {
return (int) Math.round(this.getPowerMax());
}
public int getPowerMinRounded() {
return (int) Math.round(this.getPowerMin());
}
protected void updatePower() {
if (this.isOffline()) {
2014-07-01 22:10:18 +02:00
losePowerFromBeingOffline();
if (!Conf.powerRegenOffline) {
2014-04-04 20:55:21 +02:00
return;
}
2014-07-01 22:10:18 +02:00
}
long now = System.currentTimeMillis();
long millisPassed = now - this.lastPowerUpdateTime;
2014-04-04 20:55:21 +02:00
this.lastPowerUpdateTime = now;
2014-07-01 22:10:18 +02:00
Player thisPlayer = this.getPlayer();
if (thisPlayer != null && thisPlayer.isDead()) {
2014-04-04 20:55:21 +02:00
return; // don't let dead players regain power until they respawn
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
int millisPerMinute = 60 * 1000;
this.alterPower(millisPassed * Conf.powerPerMinute / millisPerMinute);
2014-04-04 20:55:21 +02:00
}
protected void losePowerFromBeingOffline() {
if (Conf.powerOfflineLossPerDay > 0.0 && this.power > Conf.powerOfflineLossLimit) {
2014-07-01 22:10:18 +02:00
long now = System.currentTimeMillis();
long millisPassed = now - this.lastPowerUpdateTime;
2014-04-04 20:55:21 +02:00
this.lastPowerUpdateTime = now;
double loss = millisPassed * Conf.powerOfflineLossPerDay / (24 * 60 * 60 * 1000);
if (this.power - loss < Conf.powerOfflineLossLimit) {
loss = this.power;
2014-07-01 22:10:18 +02:00
}
this.alterPower(-loss);
2014-04-04 20:55:21 +02:00
}
}
public void onDeath() {
2014-07-01 22:10:18 +02:00
this.updatePower();
this.alterPower(-Conf.powerPerDeath);
2014-04-04 20:55:21 +02:00
}
//----------------------------------------------//
// Territory
//----------------------------------------------//
public boolean isInOwnTerritory() {
return Board.getFactionAt(new FLocation(this)) == this.getFaction();
}
public boolean isInOthersTerritory() {
Faction factionHere = Board.getFactionAt(new FLocation(this));
return factionHere != null && factionHere.isNormal() && factionHere != this.getFaction();
}
public boolean isInAllyTerritory() {
return Board.getFactionAt(new FLocation(this)).getRelationTo(this).isAlly();
}
public boolean isInNeutralTerritory() {
return Board.getFactionAt(new FLocation(this)).getRelationTo(this).isNeutral();
}
public boolean isInEnemyTerritory() {
return Board.getFactionAt(new FLocation(this)).getRelationTo(this).isEnemy();
}
public void sendFactionHereMessage() {
Faction toShow = Board.getFactionAt(getLastStoodAt());
2014-09-01 21:56:32 +02:00
if (shouldShowScoreboard(toShow)) {
// Shows them the scoreboard instead of sending a message in chat. Will disappear after a few seconds.
FScoreboard.get(this).setTemporarySidebar(new FInfoSidebar(toShow));
} else {
String msg = P.p.txt.parse("<i>") + " ~ " + toShow.getTag(this);
if (toShow.getDescription().length() > 0) {
msg += " - " + toShow.getDescription();
}
this.sendMessage(msg);
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
}
2014-09-01 21:56:32 +02:00
/**
* Check if the scoreboard should be shown. Simple method to be used by above method.
*
2014-09-01 21:56:32 +02:00
* @param toShow Faction to be shown.
*
2014-09-01 21:56:32 +02:00
* @return true if should show, otherwise false.
*/
private boolean shouldShowScoreboard(Faction toShow) {
return !toShow.isWarZone() && !toShow.isNone() && !toShow.isSafeZone() && P.p.getConfig().contains("scoreboard.finfo") && P.p.getConfig().getBoolean("scoreboard.finfo-enabled", false) && P.p.cmdBase.cmdSB.showBoard(this);
}
2014-04-04 20:55:21 +02:00
// -------------------------------
// Actions
// -------------------------------
public void leave(boolean makePay) {
2014-07-01 22:10:18 +02:00
Faction myFaction = this.getFaction();
makePay = makePay && Econ.shouldBeUsed() && !this.isAdminBypassing();
2014-04-04 20:55:21 +02:00
if (myFaction == null) {
2014-07-01 22:10:18 +02:00
resetFactionData();
return;
2014-04-04 20:55:21 +02:00
}
boolean perm = myFaction.isPermanent();
if (!perm && this.getRole() == Role.ADMIN && myFaction.getFPlayers().size() > 1) {
2014-07-01 22:10:18 +02:00
msg("<b>You must give the admin role to someone else first.");
return;
2014-04-04 20:55:21 +02:00
}
if (!Conf.canLeaveWithNegativePower && this.getPower() < 0) {
2014-07-01 22:10:18 +02:00
msg("<b>You cannot leave until your power is positive.");
return;
2014-04-04 20:55:21 +02:00
}
// if economy is enabled and they're not on the bypass list, make sure they can pay
2014-07-01 22:10:18 +02:00
if (makePay && !Econ.hasAtLeast(this, Conf.econCostLeave, "to leave your faction.")) {
return;
}
2014-04-04 20:55:21 +02:00
FPlayerLeaveEvent leaveEvent = new FPlayerLeaveEvent(this, myFaction, FPlayerLeaveEvent.PlayerLeaveReason.LEAVE);
2014-07-01 22:10:18 +02:00
Bukkit.getServer().getPluginManager().callEvent(leaveEvent);
if (leaveEvent.isCancelled()) {
return;
}
2014-04-04 20:55:21 +02:00
// then make 'em pay (if applicable)
2014-07-01 21:49:42 +02:00
if (makePay && !Econ.modifyMoney(this, -Conf.econCostLeave, "to leave your faction.", "for leaving your faction.")) {
2014-04-04 20:55:21 +02:00
return;
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
// Am I the last one in the faction?
if (myFaction.getFPlayers().size() == 1) {
// Transfer all money
2014-07-01 21:49:42 +02:00
if (Econ.shouldBeUsed()) {
2014-04-04 20:55:21 +02:00
Econ.transferMoney(this, myFaction, this, Econ.getBalance(myFaction.getAccountId()));
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
}
if (myFaction.isNormal()) {
for (FPlayer fplayer : myFaction.getFPlayersWhereOnline(true)) {
fplayer.msg("%s<i> left %s<i>.", this.describeTo(fplayer, true), myFaction.describeTo(fplayer));
}
2014-07-01 22:10:18 +02:00
if (Conf.logFactionLeave) {
P.p.log(this.getName() + " left the faction: " + myFaction.getTag());
}
2014-04-04 20:55:21 +02:00
}
myFaction.removeAnnouncements(this);
2014-04-04 20:55:21 +02:00
this.resetFactionData();
if (myFaction.isNormal() && !perm && myFaction.getFPlayers().isEmpty()) {
// Remove this faction
for (FPlayer fplayer : FPlayers.i.getOnline()) {
fplayer.msg("<i>%s<i> was disbanded.", myFaction.describeTo(fplayer, true));
}
2014-07-01 22:10:18 +02:00
myFaction.detach();
if (Conf.logFactionDisband) {
2014-04-04 20:55:21 +02:00
P.p.log("The faction " + myFaction.getTag() + " (" + myFaction.getId() + ") was disbanded due to the last player (" + this.getName() + ") leaving.");
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
}
}
public boolean canClaimForFaction(Faction forFaction) {
2014-07-02 08:37:42 +02:00
return !forFaction.isNone() && (this.isAdminBypassing() || (forFaction == this.getFaction() && this.getRole().isAtLeast(Role.MODERATOR)) || (forFaction.isSafeZone() && Permission.MANAGE_SAFE_ZONE.has(getPlayer())) || (forFaction.isWarZone() && Permission.MANAGE_WAR_ZONE.has(getPlayer())));
2014-04-04 20:55:21 +02:00
}
public boolean canClaimForFactionAtLocation(Faction forFaction, Location location, boolean notifyFailure) {
2014-07-01 22:10:18 +02:00
String error = null;
FLocation flocation = new FLocation(location);
Faction myFaction = getFaction();
Faction currentFaction = Board.getFactionAt(flocation);
int ownedLand = forFaction.getLandRounded();
2014-04-04 20:55:21 +02:00
if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(location)) {
// Checks for WorldGuard regions in the chunk attempting to be claimed
error = P.p.txt.parse("<b>This land is protected");
2014-07-01 21:52:40 +02:00
} else if (Conf.worldsNoClaiming.contains(flocation.getWorldName())) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>Sorry, this world has land claiming disabled.");
2014-07-01 21:52:40 +02:00
} else if (this.isAdminBypassing()) {
2014-04-04 20:55:21 +02:00
return true;
2014-07-01 21:52:40 +02:00
} else if (forFaction.isSafeZone() && Permission.MANAGE_SAFE_ZONE.has(getPlayer())) {
2014-04-04 20:55:21 +02:00
return true;
2014-07-01 21:52:40 +02:00
} else if (forFaction.isWarZone() && Permission.MANAGE_WAR_ZONE.has(getPlayer())) {
2014-04-04 20:55:21 +02:00
return true;
2014-07-01 21:52:40 +02:00
} else if (myFaction != forFaction) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can't claim land for <h>%s<b>.", forFaction.describeTo(this));
2014-07-01 21:52:40 +02:00
} else if (forFaction == currentFaction) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("%s<i> already own this land.", forFaction.describeTo(this, true));
2014-07-01 21:52:40 +02:00
} else if (this.getRole().value < Role.MODERATOR.value) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You must be <h>%s<b> to claim land.", Role.MODERATOR.toString());
2014-07-01 21:52:40 +02:00
} else if (forFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("Factions must have at least <h>%s<b> members to claim land.", Conf.claimsRequireMinFactionMembers);
2014-07-01 21:52:40 +02:00
} else if (currentFaction.isSafeZone()) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can not claim a Safe Zone.");
2014-07-01 21:52:40 +02:00
} else if (currentFaction.isWarZone()) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can not claim a War Zone.");
2014-07-01 21:52:40 +02:00
} else if (ownedLand >= forFaction.getPowerRounded()) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can't claim more land! You need more power!");
2014-07-01 21:52:40 +02:00
} else if (Conf.claimedLandsMax != 0 && ownedLand >= Conf.claimedLandsMax && forFaction.isNormal()) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>Limit reached. You can't claim more land!");
2014-07-01 21:52:40 +02:00
} else if (currentFaction.getRelationTo(forFaction) == Relation.ALLY) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can't claim the land of your allies.");
2014-07-01 21:52:40 +02:00
} else if (Conf.claimsMustBeConnected && !this.isAdminBypassing() && myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0 && !Board.isConnectedLocation(flocation, myFaction) && (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal())) {
2014-07-01 21:49:42 +02:00
if (Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can only claim additional land which is connected to your first claim or controlled by another faction!");
2014-07-01 21:52:40 +02:00
} else {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You can only claim additional land which is connected to your first claim!");
2014-07-01 21:49:42 +02:00
}
2014-07-01 21:52:40 +02:00
} else if (currentFaction.isNormal()) {
2014-04-04 20:55:21 +02:00
if (myFaction.isPeaceful()) {
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));
2014-07-01 21:52:40 +02:00
} else if (currentFaction.isPeaceful()) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("%s<i> owns this land, and is a peaceful faction. You cannot claim land from them.", currentFaction.getTag(this));
2014-07-01 21:52:40 +02:00
} else if (!currentFaction.hasLandInflation()) {
2014-04-04 20:55:21 +02:00
// TODO more messages WARN current faction most importantly
error = P.p.txt.parse("%s<i> owns this land and is strong enough to keep it.", currentFaction.getTag(this));
2014-07-01 21:52:40 +02:00
} else if (!Board.isBorderLocation(flocation)) {
2014-04-04 20:55:21 +02:00
error = P.p.txt.parse("<b>You must start claiming land at the border of the territory.");
}
}
// TODO: Add more else if statements.
2014-04-04 20:55:21 +02:00
if (notifyFailure && error != null) {
msg(error);
2014-07-01 22:10:18 +02:00
}
return error == null;
2014-04-04 20:55:21 +02:00
}
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
2014-07-01 22:10:18 +02:00
FLocation flocation = new FLocation(location);
Faction currentFaction = Board.getFactionAt(flocation);
2014-04-04 20:55:21 +02:00
int ownedLand = forFaction.getLandRounded();
2014-07-01 22:10:18 +02:00
if (!this.canClaimForFactionAtLocation(forFaction, location, notifyFailure)) {
return false;
}
2014-04-04 20:55:21 +02:00
// if economy is enabled and they're not on the bypass list, make sure they can pay
boolean mustPay = Econ.shouldBeUsed() && !this.isAdminBypassing() && !forFaction.isSafeZone() && !forFaction.isWarZone();
2014-07-01 22:10:18 +02:00
double cost = 0.0;
EconomyParticipator payee = null;
if (mustPay) {
2014-04-04 20:55:21 +02:00
cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal());
2014-07-01 21:49:42 +02:00
if (Conf.econClaimUnconnectedFee != 0.0 && forFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0 && !Board.isConnectedLocation(flocation, forFaction)) {
2014-04-04 20:55:21 +02:00
cost += Conf.econClaimUnconnectedFee;
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
2014-07-01 21:52:40 +02:00
if (Conf.bankEnabled && Conf.bankFactionPaysLandCosts && this.hasFaction()) {
payee = this.getFaction();
2014-07-01 22:10:18 +02:00
} else {
payee = this;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if (!Econ.hasAtLeast(payee, cost, "to claim this land")) {
return false;
}
2014-04-04 20:55:21 +02:00
}
LandClaimEvent claimEvent = new LandClaimEvent(flocation, forFaction, this);
2014-07-01 22:10:18 +02:00
Bukkit.getServer().getPluginManager().callEvent(claimEvent);
if (claimEvent.isCancelled()) {
return false;
}
2014-04-04 20:55:21 +02:00
// then make 'em pay (if applicable)
2014-07-01 21:49:42 +02:00
if (mustPay && !Econ.modifyMoney(payee, -cost, "to claim this land", "for claiming this land")) {
return false;
}
2014-04-04 20:55:21 +02:00
// announce success
2014-07-01 22:10:18 +02:00
Set<FPlayer> informTheseFPlayers = new HashSet<FPlayer>();
informTheseFPlayers.add(this);
informTheseFPlayers.addAll(forFaction.getFPlayersWhereOnline(true));
for (FPlayer fp : informTheseFPlayers) {
2014-04-04 20:55:21 +02:00
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(forFaction, flocation);
2014-07-01 21:49:42 +02:00
if (Conf.logLandClaims) {
2014-04-04 20:55:21 +02:00
P.p.log(this.getName() + " claimed land at (" + flocation.getCoordString() + ") for the faction: " + forFaction.getTag());
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
return true;
}
// -------------------------------------------- //
// Persistence
2014-04-04 20:55:21 +02:00
// -------------------------------------------- //
@Override
public boolean shouldBeSaved() {
2014-07-01 21:49:42 +02:00
if (!this.hasFaction() && (this.getPowerRounded() == this.getPowerMaxRounded() || this.getPowerRounded() == (int) Math.round(Conf.powerPlayerStarting))) {
2014-04-04 20:55:21 +02:00
return false;
2014-07-01 22:10:18 +02:00
}
return !this.deleteMe;
2014-04-04 20:55:21 +02:00
}
public void msg(String str, Object... args) {
this.sendMessage(P.p.txt.parse(str, args));
}
2011-02-06 13:36:11 +01:00
}