2011-07-18 22:06:02 +02:00
|
|
|
package com.massivecraft.factions;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-26 15:01:48 +01:00
|
|
|
import java.io.*;
|
2011-03-18 17:33:23 +01:00
|
|
|
import java.lang.reflect.Type;
|
2011-02-06 13:36:11 +01:00
|
|
|
import java.util.*;
|
2011-03-26 15:01:48 +01:00
|
|
|
import java.util.logging.Level;
|
2011-03-18 17:33:23 +01:00
|
|
|
import java.util.Map.Entry;
|
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
|
|
|
|
2011-07-27 22:56:45 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
2011-07-18 22:06:02 +02:00
|
|
|
import com.massivecraft.factions.struct.Relation;
|
|
|
|
import com.massivecraft.factions.struct.Role;
|
|
|
|
import com.massivecraft.factions.util.DiscUtil;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
|
2011-03-22 15:45:41 +01: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).
|
|
|
|
*
|
2011-03-23 12:00:38 +01:00
|
|
|
* The FPlayer is linked to a minecraft player using the player name.
|
2011-03-22 15:45:41 +01:00
|
|
|
*
|
|
|
|
* The same instance is always returned for the same player.
|
|
|
|
* This means you can use the == operator. No .equals method necessary.
|
|
|
|
*/
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public class FPlayer {
|
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Fields
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2011-03-23 12:00:38 +01:00
|
|
|
private static transient TreeMap<String, FPlayer> instances = new TreeMap<String, FPlayer>(String.CASE_INSENSITIVE_ORDER);
|
2011-03-22 17:20:21 +01:00
|
|
|
private static transient File file = new File(Factions.instance.getDataFolder(), "players.json");
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
private transient String playerName;
|
|
|
|
private transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked?
|
|
|
|
|
|
|
|
private int factionId;
|
|
|
|
private Role role;
|
2011-02-06 13:36:11 +01:00
|
|
|
private String title;
|
|
|
|
private double power;
|
|
|
|
private long lastPowerUpdateTime;
|
2011-03-22 20:36:33 +01:00
|
|
|
private long lastLoginTime;
|
2011-03-22 15:45:41 +01:00
|
|
|
private transient boolean mapAutoUpdating;
|
2011-06-10 21:26:12 +02:00
|
|
|
private transient boolean autoClaimEnabled;
|
|
|
|
private transient boolean autoSafeZoneEnabled;
|
|
|
|
private transient boolean autoWarZoneEnabled;
|
2011-06-23 03:10:42 +02:00
|
|
|
private transient boolean loginPvpDisabled;
|
2011-02-13 09:08:20 +01:00
|
|
|
private boolean factionChatting;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Construct
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
// GSON need this noarg constructor.
|
|
|
|
public FPlayer() {
|
2011-03-19 13:00:03 +01:00
|
|
|
this.resetFactionData();
|
|
|
|
this.power = this.getPowerMax();
|
|
|
|
this.lastPowerUpdateTime = System.currentTimeMillis();
|
2011-03-22 20:36:33 +01:00
|
|
|
this.lastLoginTime = System.currentTimeMillis();
|
2011-03-19 13:00:03 +01:00
|
|
|
this.mapAutoUpdating = false;
|
2011-06-21 07:38:31 +02:00
|
|
|
this.autoClaimEnabled = false;
|
|
|
|
this.autoSafeZoneEnabled = false;
|
|
|
|
this.autoWarZoneEnabled = false;
|
2011-06-23 03:10:42 +02:00
|
|
|
this.loginPvpDisabled = (Conf.noPVPDamageToOthersForXSecondsAfterLogin > 0) ? true : false;
|
2011-03-19 13:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void resetFactionData() {
|
Faction admins can now mark already claimed areas as owned by specific faction members. Ownership can include multiple members. New command /f owner *[player name], to set/remove ownership. This command is only available to the faction admin and optionally the faction moderators. If no player name is specified, it will either set ownership to the player running the command (if no owner is currently set) or completely clear ownership of the territory. New command /f ownerlist, to view a list of owners for the current area. Only works inside your own faction's territory. New conf.json options "ownedAreasEnabled", "ownedAreasModeratorsCanSet", "ownedAreaModeratorsBypass", "ownedAreaDenyBuild", "ownedAreaProtectMaterials", and "ownedAreaDenyUseage" (all defaulting to true) to determine whether faction moderators can set or bypass ownership (faction admin always can), and what sort of protection these owned areas have against normal members of the faction (members other than the owner(s), faction admin, and probably faction moderators). New conf.json option "ownedAreasLimitPerFaction" to limit how many owned areas can be set. New permission node "factions.ownershipBypass" which allows a player to bypass ownership protection, but only within the person's own faction.
various little tweaks and improvements to other code
moderate speed boost to FLocation code
made commandDisable permissions work for any command alias of a command, instead of just the first one
2011-07-31 03:17:00 +02:00
|
|
|
// clean up any territory ownership in old faction, if there is one
|
|
|
|
if (this.factionId > 0 && Faction.exists(this.factionId)) {
|
|
|
|
Faction.get(factionId).clearClaimOwnership(playerName);
|
|
|
|
}
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
this.factionId = 0; // The default neutral faction
|
|
|
|
this.factionChatting = false;
|
|
|
|
this.role = Role.NORMAL;
|
|
|
|
this.title = "";
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Minecraft Player
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public Player getPlayer() {
|
2011-03-22 15:45:41 +01:00
|
|
|
return Factions.instance.getServer().getPlayer(playerName);
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPlayerName() {
|
2011-03-22 15:45:41 +01:00
|
|
|
return this.playerName;
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOnline() {
|
2011-03-22 15:45:41 +01:00
|
|
|
return Factions.instance.getServer().getPlayer(playerName) != null;
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOffline() {
|
|
|
|
return ! isOnline();
|
|
|
|
}
|
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Getters And Setters
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public Faction getFaction() {
|
|
|
|
return Faction.get(factionId);
|
|
|
|
}
|
|
|
|
|
Faction admins can now mark already claimed areas as owned by specific faction members. Ownership can include multiple members. New command /f owner *[player name], to set/remove ownership. This command is only available to the faction admin and optionally the faction moderators. If no player name is specified, it will either set ownership to the player running the command (if no owner is currently set) or completely clear ownership of the territory. New command /f ownerlist, to view a list of owners for the current area. Only works inside your own faction's territory. New conf.json options "ownedAreasEnabled", "ownedAreasModeratorsCanSet", "ownedAreaModeratorsBypass", "ownedAreaDenyBuild", "ownedAreaProtectMaterials", and "ownedAreaDenyUseage" (all defaulting to true) to determine whether faction moderators can set or bypass ownership (faction admin always can), and what sort of protection these owned areas have against normal members of the faction (members other than the owner(s), faction admin, and probably faction moderators). New conf.json option "ownedAreasLimitPerFaction" to limit how many owned areas can be set. New permission node "factions.ownershipBypass" which allows a player to bypass ownership protection, but only within the person's own faction.
various little tweaks and improvements to other code
moderate speed boost to FLocation code
made commandDisable permissions work for any command alias of a command, instead of just the first one
2011-07-31 03:17:00 +02:00
|
|
|
public int getFactionId() {
|
2011-03-22 19:25:11 +01:00
|
|
|
return factionId;
|
|
|
|
}
|
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
public void setFaction(Faction faction) {
|
|
|
|
this.factionId = faction.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasFaction() {
|
|
|
|
return factionId != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Role getRole() {
|
|
|
|
return this.role;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRole(Role role) {
|
|
|
|
this.role = role;
|
|
|
|
}
|
|
|
|
|
2011-02-13 09:08:20 +01:00
|
|
|
public boolean isFactionChatting() {
|
|
|
|
if (this.factionId == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return factionChatting;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFactionChatting(boolean factionChatting) {
|
|
|
|
this.factionChatting = factionChatting;
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-22 20:36:33 +01:00
|
|
|
public long getLastLoginTime() {
|
|
|
|
return lastLoginTime;
|
|
|
|
}
|
|
|
|
|
2011-06-10 21:26:12 +02:00
|
|
|
public boolean autoClaimEnabled() {
|
|
|
|
if (this.factionId == 0)
|
|
|
|
return false;
|
|
|
|
return autoClaimEnabled;
|
|
|
|
}
|
|
|
|
public void enableAutoClaim(boolean enabled) {
|
|
|
|
this.autoClaimEnabled = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
this.autoSafeZoneEnabled = false;
|
|
|
|
this.autoWarZoneEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean autoSafeZoneEnabled() {
|
|
|
|
return autoSafeZoneEnabled;
|
|
|
|
}
|
|
|
|
public void enableAutoSafeZone(boolean enabled) {
|
|
|
|
this.autoSafeZoneEnabled = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
this.autoClaimEnabled = false;
|
|
|
|
this.autoWarZoneEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean autoWarZoneEnabled() {
|
|
|
|
return autoWarZoneEnabled;
|
|
|
|
}
|
|
|
|
public void enableAutoWarZone(boolean enabled) {
|
|
|
|
this.autoWarZoneEnabled = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
this.autoClaimEnabled = false;
|
|
|
|
this.autoSafeZoneEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:36:33 +01:00
|
|
|
public void setLastLoginTime(long lastLoginTime) {
|
|
|
|
this.lastLoginTime = lastLoginTime;
|
2011-05-29 23:28:29 +02:00
|
|
|
this.lastPowerUpdateTime = lastLoginTime;
|
2011-06-23 03:10:42 +02:00
|
|
|
if (Conf.noPVPDamageToOthersForXSecondsAfterLogin > 0) {
|
|
|
|
this.loginPvpDisabled = true;
|
|
|
|
}
|
2011-03-22 20:36:33 +01:00
|
|
|
}
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
public boolean isMapAutoUpdating() {
|
|
|
|
return mapAutoUpdating;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMapAutoUpdating(boolean mapAutoUpdating) {
|
|
|
|
this.mapAutoUpdating = mapAutoUpdating;
|
|
|
|
}
|
2011-06-23 03:10:42 +02:00
|
|
|
|
|
|
|
public boolean hasLoginPvpDisabled() {
|
|
|
|
if (!loginPvpDisabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.lastLoginTime + (Conf.noPVPDamageToOthersForXSecondsAfterLogin * 1000) < System.currentTimeMillis()) {
|
|
|
|
this.loginPvpDisabled = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-12 18:05:05 +01:00
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
public FLocation getLastStoodAt() {
|
|
|
|
return this.lastStoodAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLastStoodAt(FLocation flocation) {
|
|
|
|
this.lastStoodAt = flocation;
|
|
|
|
}
|
|
|
|
|
2011-02-12 18:05:05 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Title, Name, Faction Tag and Chat
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
// Base:
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
public String getTitle() {
|
2011-03-22 19:25:11 +01:00
|
|
|
return this.title;
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
2011-02-12 18:05:05 +01:00
|
|
|
public String getName() {
|
2011-03-22 15:45:41 +01:00
|
|
|
return this.playerName;
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getTag() {
|
2011-03-19 13:00:03 +01:00
|
|
|
if ( ! this.hasFaction()) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return this.getFaction().getTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base concatenations:
|
|
|
|
|
|
|
|
public String getNameAndSomething(String something) {
|
|
|
|
String ret = this.role.getPrefix();
|
|
|
|
if (something.length() > 0) {
|
|
|
|
ret += something+" ";
|
|
|
|
}
|
|
|
|
ret += this.getName();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.getRelationColor(faction)+this.getNameAndTitle();
|
|
|
|
}
|
2011-03-23 12:00:38 +01:00
|
|
|
public String getNameAndTitle(FPlayer fplayer) {
|
|
|
|
return this.getRelationColor(fplayer)+this.getNameAndTitle();
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getNameAndTag(Faction faction) {
|
|
|
|
return this.getRelationColor(faction)+this.getNameAndTag();
|
|
|
|
}
|
2011-03-23 12:00:38 +01:00
|
|
|
public String getNameAndTag(FPlayer fplayer) {
|
|
|
|
return this.getRelationColor(fplayer)+this.getNameAndTag();
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getNameAndRelevant(Faction faction) {
|
|
|
|
// Which relation?
|
|
|
|
Relation rel = this.getRelation(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();
|
|
|
|
}
|
2011-03-23 12:00:38 +01:00
|
|
|
public String getNameAndRelevant(FPlayer fplayer) {
|
|
|
|
return getNameAndRelevant(fplayer.getFaction());
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chat Tag:
|
|
|
|
// These are injected into the format of global chat messages.
|
|
|
|
|
|
|
|
public String getChatTag() {
|
2011-03-19 13:00:03 +01:00
|
|
|
if ( ! this.hasFaction()) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return String.format(Conf.chatTagFormat, this.role.getPrefix()+this.getTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Colored Chat Tag
|
|
|
|
public String getChatTag(Faction faction) {
|
2011-03-19 13:00:03 +01:00
|
|
|
if ( ! this.hasFaction()) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getRelation(faction).getColor()+getChatTag();
|
|
|
|
}
|
2011-03-23 12:00:38 +01:00
|
|
|
public String getChatTag(FPlayer fplayer) {
|
2011-03-19 13:00:03 +01:00
|
|
|
if ( ! this.hasFaction()) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2011-03-23 12:00:38 +01:00
|
|
|
return this.getRelation(fplayer).getColor()+getChatTag();
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------
|
|
|
|
// Relation and relation colors
|
|
|
|
// -------------------------------
|
|
|
|
|
|
|
|
public Relation getRelation(Faction faction) {
|
|
|
|
return faction.getRelation(this);
|
|
|
|
}
|
|
|
|
|
2011-03-23 12:00:38 +01:00
|
|
|
public Relation getRelation(FPlayer fplayer) {
|
|
|
|
return this.getFaction().getRelation(fplayer);
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChatColor getRelationColor(Faction faction) {
|
|
|
|
return faction.getRelationColor(this);
|
|
|
|
}
|
|
|
|
|
2011-03-23 12:00:38 +01:00
|
|
|
public ChatColor getRelationColor(FPlayer fplayer) {
|
|
|
|
return this.getRelation(fplayer).getColor();
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Health
|
|
|
|
//----------------------------------------------//
|
|
|
|
public void heal(int amnt) {
|
|
|
|
Player player = this.getPlayer();
|
|
|
|
if (player == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.setHealth(player.getHealth() + amnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Power
|
|
|
|
//----------------------------------------------//
|
|
|
|
public double getPower() {
|
|
|
|
this.updatePower();
|
|
|
|
return this.power;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void alterPower(double delta) {
|
|
|
|
this.power += delta;
|
|
|
|
if (this.power > this.getPowerMax()) {
|
|
|
|
this.power = this.getPowerMax();
|
|
|
|
} else if (this.power < this.getPowerMin()) {
|
|
|
|
this.power = this.getPowerMin();
|
|
|
|
}
|
2011-02-13 17:02:51 +01:00
|
|
|
//Log.debug("Power of "+this.getName()+" is now: "+this.power);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public double getPowerMax() {
|
2011-02-12 18:05:05 +01:00
|
|
|
return Conf.powerPlayerMax;
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public double getPowerMin() {
|
2011-02-12 18:05:05 +01:00
|
|
|
return Conf.powerPlayerMin;
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2011-06-01 13:54:00 +02:00
|
|
|
if (this.isOffline() && !Conf.powerRegenOffline) {
|
2011-05-29 23:28:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
long millisPassed = now - this.lastPowerUpdateTime;
|
|
|
|
this.lastPowerUpdateTime = now;
|
|
|
|
|
|
|
|
int millisPerMinute = 60*1000;
|
|
|
|
this.alterPower(millisPassed * Conf.powerPerMinute / millisPerMinute);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onDeath() {
|
|
|
|
this.updatePower();
|
|
|
|
this.alterPower(-Conf.powerPerDeath);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Territory
|
|
|
|
//----------------------------------------------//
|
|
|
|
public boolean isInOwnTerritory() {
|
2011-03-22 17:20:21 +01:00
|
|
|
return Board.getFactionAt(new FLocation(this)) == this.getFaction();
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isInOthersTerritory() {
|
2011-03-19 13:00:03 +01:00
|
|
|
int idHere = Board.getIdAt(new FLocation(this));
|
2011-06-19 10:56:21 +02:00
|
|
|
return idHere > 0 && idHere != this.factionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isInEnemyTerritory() {
|
|
|
|
return Board.getFactionAt(new FLocation(this)).getRelation(this) == Relation.ENEMY;
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendFactionHereMessage() {
|
2011-03-19 13:00:03 +01:00
|
|
|
Faction factionHere = Board.getFactionAt(new FLocation(this));
|
2011-02-12 18:05:05 +01:00
|
|
|
String msg = Conf.colorSystem+" ~ "+factionHere.getTag(this);
|
2011-03-23 17:39:56 +01:00
|
|
|
if (factionHere.getDescription().length() > 0) {
|
2011-02-12 18:05:05 +01:00
|
|
|
msg += " - "+factionHere.getDescription();
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
this.sendMessage(msg);
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:36:33 +01:00
|
|
|
// -------------------------------
|
|
|
|
// Actions
|
|
|
|
// -------------------------------
|
|
|
|
|
|
|
|
public void leave() {
|
|
|
|
Faction myFaction = this.getFaction();
|
|
|
|
|
|
|
|
if (this.getRole() == Role.ADMIN && myFaction.getFPlayers().size() > 1) {
|
|
|
|
sendMessage("You must give the admin role to someone else first.");
|
|
|
|
return;
|
|
|
|
}
|
2011-04-28 22:45:43 +02:00
|
|
|
|
2011-05-29 23:41:50 +02:00
|
|
|
if (!Conf.CanLeaveWithNegativePower && this.getPower() < 0) {
|
|
|
|
sendMessage("You cannot leave until your power is positive.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-28 22:45:43 +02:00
|
|
|
if (myFaction.isNormal()) {
|
|
|
|
myFaction.sendMessage(this.getNameAndRelevant(myFaction) + Conf.colorSystem + " left your faction.");
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:36:33 +01:00
|
|
|
this.resetFactionData();
|
2011-04-28 22:45:43 +02:00
|
|
|
|
|
|
|
if (myFaction.isNormal() && myFaction.getFPlayers().size() == 0) {
|
2011-03-22 20:36:33 +01:00
|
|
|
// Remove this faction
|
|
|
|
for (FPlayer fplayer : FPlayer.getAllOnline()) {
|
|
|
|
fplayer.sendMessage("The faction "+myFaction.getTag(fplayer)+Conf.colorSystem+" was disbanded.");
|
|
|
|
}
|
|
|
|
Faction.delete(myFaction.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-10 21:26:12 +02:00
|
|
|
public boolean attemptClaim(boolean notifyFailure) {
|
2011-07-24 13:12:01 +02:00
|
|
|
// notifyFailure is false if called by auto-claim; no need to notify on every failure for it
|
2011-06-10 21:26:12 +02:00
|
|
|
// return value is false on failure, true on success
|
|
|
|
|
|
|
|
Faction myFaction = getFaction();
|
|
|
|
FLocation flocation = new FLocation(this);
|
|
|
|
Faction otherFaction = Board.getFactionAt(flocation);
|
|
|
|
|
|
|
|
if (myFaction == otherFaction) {
|
|
|
|
if (notifyFailure)
|
|
|
|
sendMessage("You already own this land.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.getRole().value < Role.MODERATOR.value) {
|
|
|
|
sendMessage("You must be "+Role.MODERATOR+" to claim land.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-25 20:16:32 +02:00
|
|
|
if (myFaction.getFPlayers().size() < Conf.claimsRequireMinFactionMembers && !Conf.adminBypassPlayers.contains(this.playerName)) {
|
|
|
|
sendMessage("Your faction must have at least "+Conf.claimsRequireMinFactionMembers+" members to claim land.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-10 21:26:12 +02:00
|
|
|
if (Conf.worldsNoClaiming.contains(flocation.getWorldName())) {
|
|
|
|
sendMessage("Sorry, this world has land claiming disabled.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (otherFaction.isSafeZone()) {
|
|
|
|
if (notifyFailure)
|
|
|
|
sendMessage("You can not claim a Safe Zone.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (otherFaction.isWarZone()) {
|
|
|
|
if (notifyFailure)
|
|
|
|
sendMessage("You can not claim a War Zone.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myFaction.getLandRounded() >= myFaction.getPowerRounded()) {
|
|
|
|
sendMessage("You can't claim more land! You need more power!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (otherFaction.getRelation(this) == Relation.ALLY) {
|
|
|
|
if (notifyFailure)
|
|
|
|
sendMessage("You can't claim the land of your allies.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-20 21:45:18 +02:00
|
|
|
if (
|
|
|
|
Conf.claimsMustBeConnected
|
2011-07-24 13:12:01 +02:00
|
|
|
&& !Conf.adminBypassPlayers.contains(this.playerName)
|
2011-07-20 21:45:18 +02:00
|
|
|
&& myFaction.getLandRoundedInWorld(flocation.getWorldName()) > 0
|
|
|
|
&& !Board.isConnectedLocation(flocation, myFaction)
|
|
|
|
&& (!Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction || !otherFaction.isNormal())
|
|
|
|
) {
|
|
|
|
if (Conf.claimsCanBeUnconnectedIfOwnedByOtherFaction)
|
|
|
|
sendMessage("You can only claim additional land which is connected to your first claim or controlled by another faction!");
|
|
|
|
else
|
|
|
|
sendMessage("You can only claim additional land which is connected to your first claim!");
|
2011-06-21 04:15:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-10 21:26:12 +02:00
|
|
|
if (otherFaction.isNone()) {
|
|
|
|
myFaction.sendMessage(this.getNameAndRelevant(myFaction)+Conf.colorSystem+" claimed some new land :D");
|
|
|
|
} else { //if (otherFaction.isNormal()) {
|
|
|
|
|
|
|
|
if ( ! otherFaction.hasLandInflation()) {
|
|
|
|
// TODO more messages WARN current faction most importantly
|
|
|
|
sendMessage(this.getRelationColor(otherFaction)+otherFaction.getTag()+Conf.colorSystem+" owns this land and is strong enough to keep it.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! Board.isBorderLocation(flocation)) {
|
|
|
|
sendMessage("You must start claiming land at the border of the territory.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASDF claimed some of your land 450 blocks NNW of you.
|
|
|
|
// ASDf claimed some land from FACTION NAME
|
|
|
|
otherFaction.sendMessage(this.getNameAndRelevant(otherFaction)+Conf.colorSystem+" stole some of your land :O");
|
|
|
|
myFaction.sendMessage(this.getNameAndRelevant(myFaction)+Conf.colorSystem+" claimed some land from "+otherFaction.getTag(myFaction));
|
|
|
|
}
|
|
|
|
|
|
|
|
Board.setFactionAt(myFaction, flocation);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Messages
|
|
|
|
// -------------------------------------------- //
|
|
|
|
public void sendMessage(String message) {
|
2011-03-30 11:20:24 +02:00
|
|
|
if (this.getPlayer() != null)
|
|
|
|
this.getPlayer().sendMessage(Conf.colorSystem + message);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 17:20:21 +01:00
|
|
|
public void sendMessage(List<String> messages) {
|
|
|
|
for(String message : messages) {
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
2011-02-12 18:05:05 +01:00
|
|
|
}
|
2011-03-22 17:20:21 +01:00
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
// -------------------------------------------- //
|
2011-03-19 13:00:03 +01:00
|
|
|
// Get and search
|
2011-03-18 17:33:23 +01:00
|
|
|
// -------------------------------------------- //
|
2011-03-23 12:00:38 +01:00
|
|
|
|
|
|
|
// You should use this one to be sure you do not spell the player name wrong.
|
|
|
|
public static FPlayer get(Player player) {
|
|
|
|
return get(player.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static FPlayer get(String playerName) {
|
2011-03-22 15:45:41 +01:00
|
|
|
if (instances.containsKey(playerName)) {
|
|
|
|
return instances.get(playerName);
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 19:25:11 +01:00
|
|
|
FPlayer vplayer = new FPlayer();
|
|
|
|
vplayer.playerName = playerName;
|
|
|
|
|
2011-03-22 15:45:41 +01:00
|
|
|
instances.put(playerName, vplayer);
|
2011-03-18 17:33:23 +01:00
|
|
|
return vplayer;
|
|
|
|
}
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static Set<FPlayer> getAllOnline() {
|
|
|
|
Set<FPlayer> fplayers = new HashSet<FPlayer>();
|
|
|
|
for (Player player : Factions.instance.getServer().getOnlinePlayers()) {
|
|
|
|
fplayers.add(FPlayer.get(player));
|
|
|
|
}
|
|
|
|
return fplayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Collection<FPlayer> getAll() {
|
|
|
|
return instances.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FPlayer find(String playername) {
|
|
|
|
for (Entry<String, FPlayer> entry : instances.entrySet()) {
|
2011-07-09 05:06:55 +02:00
|
|
|
if (entry.getKey().equalsIgnoreCase(playername) || entry.getKey().startsWith(playername)) {
|
2011-03-19 13:00:03 +01:00
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Persistance
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public boolean shouldBeSaved() {
|
|
|
|
return this.factionId != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean save() {
|
2011-03-22 22:31:04 +01:00
|
|
|
//Factions.log("Saving players to disk");
|
2011-03-18 17:33:23 +01:00
|
|
|
|
2011-03-23 12:00:38 +01:00
|
|
|
// We only wan't to save the players with non default values
|
|
|
|
Map<String, FPlayer> playersToSave = new HashMap<String, FPlayer>();
|
2011-03-18 17:33:23 +01:00
|
|
|
for (Entry<String, FPlayer> entry : instances.entrySet()) {
|
|
|
|
if (entry.getValue().shouldBeSaved()) {
|
2011-03-23 12:00:38 +01:00
|
|
|
playersToSave.put(entry.getKey(), entry.getValue());
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2011-07-27 22:56:45 +02:00
|
|
|
DiscUtil.write(file, Factions.instance.gson.toJson(playersToSave));
|
2011-04-06 11:08:08 +02:00
|
|
|
} catch (Exception e) {
|
2011-03-18 17:33:23 +01:00
|
|
|
e.printStackTrace();
|
2011-04-06 11:08:08 +02:00
|
|
|
Factions.log("Failed to save the players to disk.");
|
2011-03-18 17:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean load() {
|
2011-03-23 12:00:38 +01:00
|
|
|
Factions.log("Loading players from disk");
|
2011-03-18 17:33:23 +01:00
|
|
|
if ( ! file.exists()) {
|
2011-03-26 15:01:48 +01:00
|
|
|
if ( ! loadOld())
|
|
|
|
Factions.log("No players to load from disk. Creating new file.");
|
2011-03-18 17:33:23 +01:00
|
|
|
save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Type type = new TypeToken<Map<String, FPlayer>>(){}.getType();
|
2011-07-27 22:56:45 +02:00
|
|
|
Map<String, FPlayer> instancesFromFile = Factions.instance.gson.fromJson(DiscUtil.read(file), type);
|
2011-03-23 12:00:38 +01:00
|
|
|
instances.clear();
|
|
|
|
instances.putAll(instancesFromFile);
|
2011-04-06 11:08:08 +02:00
|
|
|
} catch (Exception e) {
|
2011-03-18 17:33:23 +01:00
|
|
|
e.printStackTrace();
|
2011-04-06 11:08:08 +02:00
|
|
|
Factions.log("Failed to load the players from disk.");
|
2011-03-18 17:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fillPlayernames();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void fillPlayernames() {
|
|
|
|
for(Entry<String, FPlayer> entry : instances.entrySet()) {
|
2011-03-22 15:45:41 +01:00
|
|
|
entry.getValue().playerName = entry.getKey();
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 19:25:11 +01:00
|
|
|
public static void clean() {
|
|
|
|
for (FPlayer fplayer : instances.values()) {
|
|
|
|
if ( ! Faction.exists(fplayer.getFactionId())) {
|
|
|
|
Factions.log("Reset faction data (invalid faction) for player "+fplayer.getName());
|
|
|
|
fplayer.resetFactionData();
|
|
|
|
}
|
|
|
|
}
|
2011-03-22 20:36:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void autoLeaveOnInactivityRoutine() {
|
2011-07-29 12:12:14 +02:00
|
|
|
if (Conf.autoLeaveAfterDaysOfInactivity <= 0.0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:36:33 +01:00
|
|
|
long now = System.currentTimeMillis();
|
2011-03-23 17:39:56 +01:00
|
|
|
double toleranceMillis = Conf.autoLeaveAfterDaysOfInactivity * 24 * 60 * 60 * 1000;
|
2011-03-22 20:36:33 +01:00
|
|
|
|
|
|
|
for (FPlayer fplayer : FPlayer.getAll()) {
|
|
|
|
if (now - fplayer.getLastLoginTime() > toleranceMillis) {
|
|
|
|
fplayer.leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-26 15:01:48 +01:00
|
|
|
|
|
|
|
private static boolean loadOld() {
|
|
|
|
File folderFollower = new File(Factions.instance.getDataFolder(), "follower");
|
|
|
|
|
|
|
|
if ( ! folderFollower.isDirectory())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Factions.log("Players file doesn't exist, attempting to load old pre-1.1 data.");
|
|
|
|
|
|
|
|
String ext = ".json";
|
|
|
|
|
|
|
|
class jsonFileFilter implements FileFilter {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return (file.getName().toLowerCase().endsWith(".json") && file.isFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
File[] jsonFiles = folderFollower.listFiles(new jsonFileFilter());
|
|
|
|
|
|
|
|
for (File jsonFile : jsonFiles) {
|
|
|
|
// Extract the name from the filename. The name is filename minus ".json"
|
|
|
|
String name = jsonFile.getName();
|
|
|
|
name = name.substring(0, name.length() - ext.length());
|
|
|
|
try {
|
2011-07-27 22:56:45 +02:00
|
|
|
FPlayer follower = Factions.instance.gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class);
|
2011-03-26 15:01:48 +01:00
|
|
|
follower.playerName = name;
|
|
|
|
follower.lastLoginTime = System.currentTimeMillis();
|
|
|
|
instances.put(follower.playerName, follower);
|
|
|
|
Factions.log("loaded pre-1.1 follower "+name);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Factions.log(Level.WARNING, "failed to load follower "+name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|