Factions3/src/com/massivecraft/factions/entity/UPlayer.java

671 lines
18 KiB
Java
Raw Normal View History

2013-04-22 09:37:53 +02:00
package com.massivecraft.factions.entity;
2011-02-06 13:36:11 +01:00
2011-10-22 16:00:24 +02:00
import java.util.HashSet;
import java.util.Set;
2011-02-06 13:36:11 +01:00
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
2011-07-18 22:06:02 +02:00
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.Const;
import com.massivecraft.factions.EconomyParticipator;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Lang;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.event.FactionsEventChunkChange;
2013-04-19 14:08:45 +02:00
import com.massivecraft.factions.event.FactionsEventMembershipChange;
import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.Worldguard;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.util.RelationUtil;
2013-04-18 10:35:40 +02:00
import com.massivecraft.mcore.mixin.Mixin;
2013-04-22 09:37:53 +02:00
import com.massivecraft.mcore.money.Money;
import com.massivecraft.mcore.ps.PS;
2013-04-12 08:56:26 +02:00
import com.massivecraft.mcore.store.SenderEntity;
2013-04-23 14:00:18 +02:00
import com.massivecraft.mcore.util.MUtil;
2013-04-10 10:32:04 +02:00
import com.massivecraft.mcore.util.Txt;
2011-02-06 13:36:11 +01:00
public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipator
{
// -------------------------------------------- //
2013-04-12 08:56:26 +02:00
// META
// -------------------------------------------- //
public static UPlayer get(Object oid)
{
return UPlayerColls.get().get2(oid);
}
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
2013-04-12 08:56:26 +02:00
@Override
public UPlayer load(UPlayer that)
{
this.setFactionId(that.factionId);
2013-04-17 15:30:21 +02:00
this.setRole(that.role);
this.setTitle(that.title);
2013-04-23 12:14:36 +02:00
this.setPower(that.power);
return this;
2013-04-12 08:56:26 +02:00
}
@Override
public boolean isDefault()
{
2013-04-23 12:14:36 +02:00
if (this.hasFaction()) return false;
// Role means nothing without a faction.
// Title means nothing without a faction.
2013-04-23 14:00:18 +02:00
if (this.getPowerRounded() != (int) Math.round(UConf.get(this).defaultPlayerPower)) return false;
2013-04-17 15:30:21 +02:00
2013-04-12 08:56:26 +02:00
return true;
}
// -------------------------------------------- //
2013-04-17 15:30:21 +02:00
// FIELDS: RAW
// -------------------------------------------- //
2013-04-17 15:30:21 +02:00
// In this section of the source code we place the field declarations only.
// Each field has it's own section further down since just the getter and setter logic takes up quite some place.
2013-04-17 15:30:21 +02:00
// This is a foreign key.
2013-04-23 12:14:36 +02:00
// Each player belong to a faction.
// Null means default which is the no-faction faction called Wilderness.
private String factionId = null;
2013-04-17 15:30:21 +02:00
// What role does the player have in the faction?
2013-04-23 12:14:36 +02:00
// Null means default which is the default value for the universe.
2013-04-17 15:30:21 +02:00
private Rel role = null;
// What title does the player have in the faction?
2013-04-23 12:14:36 +02:00
// The title is just for fun. It's not connected to any game mechanic.
2013-04-17 15:30:21 +02:00
// The default case is no title since it's what you start with and also the most common case.
// The player title is similar to the faction description.
//
// Question: Can the title contain chat colors?
// Answer: Yes but in such case the policy is that they already must be parsed using Txt.parse.
2013-04-23 12:14:36 +02:00
// If the title contains raw markup, such as "<white>" instead of "§f" it will not be parsed and "<white>" will be displayed.
2013-04-17 15:30:21 +02:00
private String title = null;
2013-04-23 12:14:36 +02:00
// Each player has an individual power level.
// The power level for online players is occasionally updated by a recurring task and the power should stay the same for offline players.
// For that reason the value is to be considered correct when you pick it. Do not call the power update method.
// Null means default which is the default value for the universe.
private Double power = null;
2013-04-17 15:30:21 +02:00
// -------------------------------------------- //
// FIELDS: RAW TRANSIENT
// -------------------------------------------- //
// FIELD: mapAutoUpdating
// TODO: Move this to the MPlayer
2013-04-17 15:30:21 +02:00
private transient boolean mapAutoUpdating = false;
public void setMapAutoUpdating(boolean mapAutoUpdating) { this.mapAutoUpdating = mapAutoUpdating; }
public boolean isMapAutoUpdating() { return mapAutoUpdating; }
// FIELD: autoClaimEnabled
private transient Faction autoClaimFor = null;
public Faction getAutoClaimFor() { return autoClaimFor; }
public void setAutoClaimFor(Faction faction) { this.autoClaimFor = faction; }
private transient boolean usingAdminMode = false;
public boolean isUsingAdminMode() { return this.usingAdminMode; }
public void setUsingAdminMode(boolean val) { this.usingAdminMode = val; }
// FIELD: loginPvpDisabled
//private transient boolean loginPvpDisabled;
2013-04-17 15:30:21 +02:00
// FIELD: account
public String getAccountId() { return this.getId(); }
// -------------------------------------------- //
2013-04-23 12:14:36 +02:00
// CORE UTILITIES
2013-04-17 15:30:21 +02:00
// -------------------------------------------- //
public void resetFactionData()
2013-04-17 15:30:21 +02:00
{
// The default neutral faction
this.setFactionId(null);
this.setRole(null);
this.setTitle(null);
this.autoClaimFor = null;
}
2013-04-23 12:14:36 +02:00
/*
public boolean isPresent(boolean requireFetchable)
{
if (!this.isOnline()) return false;
if (requireFetchable)
{
}
else
{
}
PS ps = Mixin.getSenderPs(this.getId());
if (ps == null) return false;
String psUniverse = Factions.get().getMultiverse().getUniverseForWorldName(ps.getWorld());
if (!psUniverse.equals(this.getUniverse())) return false;
if (!requireFetchable) return true;
Player player = this.getPlayer();
if (player == null) return false;
if (player.isDead()) return false;
return true;
}
*/
2013-04-17 15:30:21 +02:00
// -------------------------------------------- //
// FIELD: factionId
// -------------------------------------------- //
2013-04-23 14:00:18 +02:00
public String getDefaultFactionId()
{
return UConf.get(this).defaultPlayerFactionId;
}
2013-04-17 15:30:21 +02:00
// This method never returns null
public String getFactionId()
2011-10-08 23:22:02 +02:00
{
2013-04-23 14:00:18 +02:00
if (this.factionId == null) return this.getDefaultFactionId();
return this.factionId;
}
2013-04-17 15:30:21 +02:00
// This method never returns null
public Faction getFaction()
{
Faction ret = FactionColls.get().get(this).get(this.getFactionId());
2013-04-23 14:00:18 +02:00
if (ret == null) ret = FactionColls.get().get(this).get(UConf.get(this).defaultPlayerFactionId);
return ret;
}
public boolean hasFaction()
{
return !this.getFactionId().equals(Const.FACTIONID_NONE);
}
// This setter is so long because it search for default/null case and takes care of updating the faction member index
public void setFactionId(String factionId)
{
// Avoid null input
2013-04-23 14:00:18 +02:00
if (factionId == null) factionId = this.getDefaultFactionId();
// Get the old value
String oldFactionId = this.getFactionId();
// Ignore nochange
if (factionId.equals(oldFactionId)) return;
// Apply change
2013-04-23 14:00:18 +02:00
if (factionId.equals(this.getDefaultFactionId())) factionId = null;
this.factionId = factionId;
// Next we must be attached and inited
if (!this.attached()) return;
if (!this.getColl().inited()) return;
2013-04-22 15:05:00 +02:00
if (!Factions.get().isDatabaseInitialized()) return;
// Update index
Faction oldFaction = FactionColls.get().get(this).get(oldFactionId);
Faction faction = FactionColls.get().get(this).get(factionId);
oldFaction.uplayers.remove(this);
faction.uplayers.add(this);
// Mark as changed
this.changed();
}
public void setFaction(Faction faction)
{
this.setFactionId(faction.getId());
2011-10-08 23:22:02 +02:00
}
2013-04-17 15:30:21 +02:00
// -------------------------------------------- //
2011-10-08 23:22:02 +02:00
// FIELD: role
2013-04-17 15:30:21 +02:00
// -------------------------------------------- //
2011-10-08 23:22:02 +02:00
2013-04-23 14:00:18 +02:00
public Rel getDefaultRole()
{
return UConf.get(this).defaultPlayerRole;
}
2013-04-17 15:30:21 +02:00
public Rel getRole()
{
2013-04-23 14:00:18 +02:00
if (this.role == null) return this.getDefaultRole();
2013-04-17 15:30:21 +02:00
return this.role;
}
2011-10-08 23:22:02 +02:00
2013-04-17 15:30:21 +02:00
public void setRole(Rel role)
{
2013-04-23 14:00:18 +02:00
if (role == null || MUtil.equals(role, this.getDefaultRole())) role = null;
this.role = role;
2013-04-17 15:30:21 +02:00
this.changed();
}
2011-10-08 23:22:02 +02:00
2013-04-12 08:11:11 +02:00
// -------------------------------------------- //
2013-04-17 15:30:21 +02:00
// FIELD: title
2013-04-12 08:11:11 +02:00
// -------------------------------------------- //
2013-04-17 15:30:21 +02:00
public boolean hasTitle()
{
return this.title != null;
}
2011-10-09 18:35:39 +02:00
2013-04-17 15:30:21 +02:00
public String getTitle()
{
if (this.hasTitle()) return this.title;
return Lang.PLAYER_NOTITLE;
}
2011-10-08 23:22:02 +02:00
2013-04-17 15:30:21 +02:00
public void setTitle(String title)
{
if (title != null)
{
title = title.trim();
if (title.length() == 0)
{
title = null;
}
}
this.title = title;
this.changed();
}
2011-10-12 17:25:01 +02:00
2013-04-18 10:35:40 +02:00
// -------------------------------------------- //
// FIELD: power
2011-03-22 17:20:21 +01:00
// -------------------------------------------- //
// MIXIN: RAW
public double getPowerMaxUniversal()
{
return Factions.get().getPowerMixin().getMaxUniversal(this);
}
public double getPowerMax()
{
return Factions.get().getPowerMixin().getMax(this);
}
public double getPowerMin()
{
return Factions.get().getPowerMixin().getMin(this);
}
public double getPowerPerHour()
{
return Factions.get().getPowerMixin().getPerHour(this);
}
public double getPowerPerDeath()
{
return Factions.get().getPowerMixin().getPerDeath(this);
}
// MIXIN: FINER
public double getLimitedPower(double power)
{
power = Math.max(power, this.getPowerMin());
power = Math.min(power, this.getPowerMax());
return power;
}
2013-04-18 10:35:40 +02:00
// RAW
2013-04-23 14:00:18 +02:00
public double getDefaultPower()
{
return UConf.get(this).defaultPlayerPower;
}
2013-04-18 10:35:40 +02:00
public double getPower()
{
Double ret = this.power;
2013-04-23 14:00:18 +02:00
if (ret == null) ret = this.getDefaultPower();
ret = this.getLimitedPower(ret);
return ret;
2013-04-18 10:35:40 +02:00
}
2013-04-23 12:14:36 +02:00
public void setPower(Double power)
2013-04-18 10:35:40 +02:00
{
2013-04-23 14:00:18 +02:00
if (power == null || MUtil.equals(power, this.getDefaultPower())) power = null;
power = this.getLimitedPower(power);
2013-04-23 14:00:18 +02:00
this.power = power;
2013-04-23 12:14:36 +02:00
this.changed();
2013-04-18 10:35:40 +02:00
}
// FINER
public int getPowerRounded()
{
return (int) Math.round(this.getPower());
}
// -------------------------------------------- //
// TITLE, NAME, FACTION TAG AND CHAT
// -------------------------------------------- //
public String getName()
{
return this.getFixedId();
}
public String getTag()
{
2013-04-23 14:00:18 +02:00
Faction faction = this.getFaction();
if (faction.isNone()) return "";
return faction.getTag();
}
// Base concatenations:
public String getNameAndSomething(String something)
{
String ret = this.role.getPrefix();
2013-04-17 15:30:21 +02:00
if (something.length() > 0)
{
ret += something+" ";
}
ret += this.getName();
return ret;
}
public String getNameAndTitle()
{
2013-04-17 15:30:21 +02:00
if (this.hasTitle())
{
return this.getNameAndSomething(this.getTitle());
}
else
{
return this.getName();
}
}
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(UPlayer uplayer)
{
return this.getColorTo(uplayer)+this.getNameAndTitle();
}
// -------------------------------------------- //
// RELATION AND RELATION COLORS
// -------------------------------------------- //
2011-10-12 17:25:01 +02:00
@Override
2011-10-24 11:07:06 +02:00
public String describeTo(RelationParticipator observer, boolean ucfirst)
{
2011-10-24 11:07:06 +02:00
return RelationUtil.describeThatToMe(this, observer, ucfirst);
}
2011-10-12 17:25:01 +02:00
@Override
2011-10-24 11:07:06 +02:00
public String describeTo(RelationParticipator observer)
{
2011-10-24 11:07:06 +02:00
return RelationUtil.describeThatToMe(this, observer);
}
2011-10-12 17:25:01 +02:00
@Override
2011-10-24 11:07:06 +02:00
public Rel getRelationTo(RelationParticipator observer)
{
2011-10-24 11:07:06 +02:00
return RelationUtil.getRelationOfThatToMe(this, observer);
}
2011-10-12 17:25:01 +02:00
@Override
2011-10-24 11:07:06 +02:00
public Rel getRelationTo(RelationParticipator observer, boolean ignorePeaceful)
{
2011-10-24 11:07:06 +02:00
return RelationUtil.getRelationOfThatToMe(this, observer, ignorePeaceful);
}
public Rel getRelationToLocation()
{
// TODO: Use some built in system to get sender
return BoardColls.get().getFactionAt(PS.valueOf(this.getPlayer())).getRelationTo(this);
}
2011-10-12 17:25:01 +02:00
@Override
2011-10-24 11:07:06 +02:00
public ChatColor getColorTo(RelationParticipator observer)
2011-10-12 17:25:01 +02:00
{
2011-10-24 11:07:06 +02:00
return RelationUtil.getColorOfThatToMe(this, observer);
2011-10-12 17:25:01 +02:00
}
// -------------------------------------------- //
// HEALTH
// -------------------------------------------- //
public void heal(int amnt)
{
2011-02-06 13:36:11 +01:00
Player player = this.getPlayer();
if (player == null)
{
2011-02-06 13:36:11 +01:00
return;
}
player.setHealth(player.getHealth() + amnt);
}
// -------------------------------------------- //
// TERRITORY
// -------------------------------------------- //
public boolean isInOwnTerritory()
{
// TODO: Use Mixin to get this PS instead
return BoardColls.get().getFactionAt(Mixin.getSenderPs(this.getId())) == this.getFaction();
2011-02-06 13:36:11 +01:00
}
public boolean isInEnemyTerritory()
{
// TODO: Use Mixin to get this PS instead
return BoardColls.get().getFactionAt(Mixin.getSenderPs(this.getId())).getRelationTo(this) == Rel.ENEMY;
2011-02-06 13:36:11 +01:00
}
// -------------------------------------------- //
// ACTIONS
// -------------------------------------------- //
2011-03-22 20:36:33 +01:00
public void leave()
{
2011-03-22 20:36:33 +01:00
Faction myFaction = this.getFaction();
2013-04-19 14:08:45 +02:00
boolean permanent = myFaction.getFlag(FFlag.PERMANENT);
2011-03-22 20:36:33 +01:00
if (!permanent && this.getRole() == Rel.LEADER && myFaction.getUPlayers().size() > 1)
{
msg("<b>You must give the leader role to someone else first.");
2011-03-22 20:36:33 +01:00
return;
}
if (!UConf.get(myFaction).canLeaveWithNegativePower && this.getPower() < 0)
{
2011-10-10 13:40:24 +02:00
msg("<b>You cannot leave until your power is positive.");
return;
}
2013-04-19 14:08:45 +02:00
// Event
FactionsEventMembershipChange membershipChangeEvent = new FactionsEventMembershipChange(sender, this, myFaction, MembershipChangeReason.LEAVE);
membershipChangeEvent.run();
if (membershipChangeEvent.isCancelled()) return;
// Am I the last one in the faction?
if (myFaction.getUPlayers().size() == 1)
{
// Transfer all money
2013-04-22 09:37:53 +02:00
if (Econ.isEnabled(this))
{
Econ.transferMoney(this, myFaction, this, Money.get(this));
}
}
if (myFaction.isNormal())
{
for (UPlayer uplayer : myFaction.getUPlayersWhereOnline(true))
{
uplayer.msg("%s<i> left %s<i>.", this.describeTo(uplayer, true), myFaction.describeTo(uplayer));
}
if (MConf.get().logFactionLeave)
{
Factions.get().log(this.getName()+" left the faction: "+myFaction.getTag());
}
}
this.resetFactionData();
if (myFaction.isNormal() && !permanent && myFaction.getUPlayers().isEmpty())
{
2011-03-22 20:36:33 +01:00
// Remove this faction
for (UPlayer uplayer : UPlayerColls.get().get(this).getAllOnline())
{
uplayer.msg("<i>%s<i> was disbanded.", myFaction.describeTo(uplayer, true));
2011-03-22 20:36:33 +01:00
}
2011-10-10 01:21:05 +02:00
myFaction.detach();
if (MConf.get().logFactionDisband)
{
Factions.get().log("The faction "+myFaction.getTag()+" ("+myFaction.getId()+") was disbanded due to the last player ("+this.getName()+") leaving.");
}
2011-03-22 20:36:33 +01:00
}
}
2013-04-19 14:24:35 +02:00
public boolean canClaimForFactionAtLocation(Faction forFaction, PS ps, boolean notifyFailure)
{
2011-10-22 16:00:24 +02:00
String error = null;
Faction myFaction = this.getFaction();
Faction currentFaction = BoardColls.get().getFactionAt(ps);
int ownedLand = forFaction.getLandCount();
2011-10-22 16:00:24 +02:00
UConf uconf = UConf.get(ps);
if (uconf.worldGuardChecking && Worldguard.checkForRegionsInChunk(ps))
{
// Checks for WorldGuard regions in the chunk attempting to be claimed
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>This land is protected");
}
else if (MConf.get().worldsNoClaiming.contains(ps.getWorld()))
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>Sorry, this world has land claiming disabled.");
}
2013-04-16 11:27:03 +02:00
else if (this.isUsingAdminMode())
{
2011-10-22 16:00:24 +02:00
return true;
}
2011-10-22 16:00:24 +02:00
else if (forFaction == currentFaction)
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("%s<i> already own this land.", forFaction.describeTo(this, true));
}
else if ( ! FPerm.TERRITORY.has(this, forFaction, true))
{
return false;
}
else if (forFaction.getUPlayers().size() < uconf.claimsRequireMinFactionMembers)
{
error = Txt.parse("Factions must have at least <h>%s<b> members to claim land.", uconf.claimsRequireMinFactionMembers);
}
2011-10-22 16:00:24 +02:00
else if (ownedLand >= forFaction.getPowerRounded())
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>You can't claim more land! You need more power!");
2011-10-22 16:00:24 +02:00
}
else if (uconf.claimedLandsMax != 0 && ownedLand >= uconf.claimedLandsMax && ! forFaction.getFlag(FFlag.INFPOWER))
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>Limit reached. You can't claim more land!");
}
else if ( ! uconf.claimingFromOthersAllowed && currentFaction.isNormal())
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>You may not claim land from others.");
}
2011-10-25 22:18:54 +02:00
else if (currentFaction.getRelationTo(forFaction).isAtLeast(Rel.TRUCE) && ! currentFaction.isNone())
2011-10-22 16:00:24 +02:00
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>You can't claim this land due to your relation with the current owner.");
2011-10-22 16:00:24 +02:00
}
else if
(
uconf.claimsMustBeConnected
2013-04-16 11:27:03 +02:00
&& ! this.isUsingAdminMode()
&& myFaction.getLandCountInWorld(ps.getWorld()) > 0
&& !BoardColls.get().isConnectedPs(ps, myFaction)
&& (!uconf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal())
)
{
if (uconf.claimsCanBeUnconnectedIfOwnedByOtherFaction)
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>You can only claim additional land which is connected to your first claim or controlled by another faction!");
else
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>You can only claim additional land which is connected to your first claim!");
}
2011-10-22 16:00:24 +02:00
else if (currentFaction.isNormal())
{
2011-10-23 22:08:57 +02:00
if ( ! currentFaction.hasLandInflation())
{
// TODO more messages WARN current faction most importantly
2013-04-10 10:32:04 +02:00
error = Txt.parse("%s<i> owns this land and is strong enough to keep it.", currentFaction.getTag(this));
}
else if ( ! BoardColls.get().isBorderPs(ps))
{
2013-04-10 10:32:04 +02:00
error = Txt.parse("<b>You must start claiming land at the border of the territory.");
}
}
2011-10-22 16:00:24 +02:00
if (notifyFailure && error != null)
{
msg(error);
}
return error == null;
}
2013-04-19 14:24:35 +02:00
// 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
public boolean attemptClaim(Faction forFaction, PS psChunk, boolean notifyFailure)
2011-10-22 16:00:24 +02:00
{
2013-04-19 14:24:35 +02:00
psChunk = psChunk.getChunk(true);
Faction currentFaction = BoardColls.get().getFactionAt(psChunk);
2011-10-22 16:00:24 +02:00
2013-04-19 14:24:35 +02:00
if ( ! this.canClaimForFactionAtLocation(forFaction, psChunk, notifyFailure)) return false;
// Event
FactionsEventChunkChange event = new FactionsEventChunkChange(sender, psChunk, forFaction);
event.run();
if (event.isCancelled()) return false;
// announce success
Set<UPlayer> informTheseUPlayers = new HashSet<UPlayer>();
informTheseUPlayers.add(this);
informTheseUPlayers.addAll(forFaction.getUPlayersWhereOnline(true));
for (UPlayer fp : informTheseUPlayers)
{
2011-10-22 16:00:24 +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));
}
2011-10-22 16:00:24 +02:00
BoardColls.get().setFactionAt(psChunk, forFaction);
if (MConf.get().logLandClaims)
{
2013-04-19 14:24:35 +02:00
Factions.get().log(this.getName()+" claimed land at ("+psChunk.getChunkX()+","+psChunk.getChunkZ()+") for the faction: "+forFaction.getTag());
}
return true;
}
2011-03-19 13:00:03 +01:00
}