2011-03-18 17:33:23 +01:00
|
|
|
package com.bukkit.mcteam.factions;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Type;
|
2011-02-06 13:36:11 +01:00
|
|
|
import java.util.*;
|
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-03-19 13:00:03 +01:00
|
|
|
import com.bukkit.mcteam.factions.struct.Relation;
|
|
|
|
import com.bukkit.mcteam.factions.struct.Role;
|
2011-03-18 17:33:23 +01:00
|
|
|
import com.bukkit.mcteam.gson.reflect.TypeToken;
|
|
|
|
import com.bukkit.mcteam.util.DiscUtil;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public class FPlayer {
|
|
|
|
public static transient Map<String, FPlayer> instances = new HashMap<String, FPlayer>();
|
|
|
|
public static transient File file = new File(Factions.instance.getDataFolder(), "players.json");
|
|
|
|
|
|
|
|
public transient String playername;
|
2011-03-19 13:00:03 +01:00
|
|
|
public transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked?
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
public int factionId;
|
|
|
|
public Role role;
|
|
|
|
private String title;
|
|
|
|
private double power;
|
|
|
|
private long lastPowerUpdateTime;
|
2011-02-13 09:08:20 +01:00
|
|
|
private boolean mapAutoUpdating;
|
|
|
|
private boolean factionChatting;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public FPlayer(Player player) {
|
|
|
|
this.playername = player.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
public FPlayer(String playername) {
|
|
|
|
this.playername = playername;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
this.mapAutoUpdating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void resetFactionData() {
|
|
|
|
this.factionId = 0; // The default neutral faction
|
|
|
|
this.factionChatting = false;
|
|
|
|
this.role = Role.NORMAL;
|
|
|
|
this.title = "";
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Player getPlayer() {
|
|
|
|
return Factions.instance.getServer().getPlayer(playername);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPlayerName() {
|
|
|
|
return this.playername;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// Online / Offline State Checking
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public boolean isOnline() {
|
|
|
|
return Factions.instance.getServer().getPlayer(playername) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOffline() {
|
|
|
|
return ! isOnline();
|
|
|
|
}
|
|
|
|
|
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-03-19 13:00:03 +01:00
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
public boolean isMapAutoUpdating() {
|
|
|
|
return mapAutoUpdating;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMapAutoUpdating(boolean mapAutoUpdating) {
|
|
|
|
this.mapAutoUpdating = mapAutoUpdating;
|
|
|
|
}
|
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() {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
2011-03-19 13:00:03 +01:00
|
|
|
save();
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-02-12 18:05:05 +01:00
|
|
|
public String getName() {
|
2011-03-19 13:00:03 +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-18 17:33:23 +01:00
|
|
|
public String getNameAndTitle(FPlayer follower) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return this.getRelationColor(follower)+this.getNameAndTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNameAndTag(Faction faction) {
|
|
|
|
return this.getRelationColor(faction)+this.getNameAndTag();
|
|
|
|
}
|
2011-03-18 17:33:23 +01:00
|
|
|
public String getNameAndTag(FPlayer follower) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return this.getRelationColor(follower)+this.getNameAndTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 17:33:23 +01:00
|
|
|
public String getNameAndRelevant(FPlayer follower) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return getNameAndRelevant(follower.getFaction());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-18 17:33:23 +01:00
|
|
|
public String getChatTag(FPlayer follower) {
|
2011-03-19 13:00:03 +01:00
|
|
|
if ( ! this.hasFaction()) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getRelation(follower).getColor()+getChatTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------
|
|
|
|
// Relation and relation colors
|
|
|
|
// -------------------------------
|
|
|
|
|
|
|
|
public Relation getRelation(Faction faction) {
|
|
|
|
return faction.getRelation(this);
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public Relation getRelation(FPlayer follower) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return this.getFaction().getRelation(follower);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChatColor getRelationColor(Faction faction) {
|
|
|
|
return faction.getRelationColor(this);
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public ChatColor getRelationColor(FPlayer follower) {
|
2011-02-12 18:05:05 +01:00
|
|
|
return this.getRelation(follower).getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
long millisPassed = now - this.lastPowerUpdateTime;
|
|
|
|
this.lastPowerUpdateTime = now;
|
|
|
|
|
|
|
|
int millisPerMinute = 60*1000;
|
|
|
|
this.alterPower(millisPassed * Conf.powerPerMinute / millisPerMinute);
|
|
|
|
//this.save(); // This would save to often. So we save this on player quit instead.
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onDeath() {
|
|
|
|
this.updatePower();
|
|
|
|
this.alterPower(-Conf.powerPerDeath);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Territory
|
|
|
|
//----------------------------------------------//
|
|
|
|
public boolean isInOwnTerritory() {
|
2011-03-19 13:00:03 +01:00
|
|
|
return Board.getIdAt(new FLocation(this)) == this.factionId;
|
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));
|
|
|
|
return idHere != 0 && idHere != this.factionId;
|
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);
|
|
|
|
if (factionHere.id != 0) {
|
|
|
|
msg += " - "+factionHere.getDescription();
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
this.sendMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Faction management
|
|
|
|
//----------------------------------------------//
|
|
|
|
public Faction getFaction() {
|
2011-03-19 13:00:03 +01:00
|
|
|
return Faction.get(factionId);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-02-12 18:05:05 +01:00
|
|
|
public boolean hasFaction() {
|
|
|
|
return factionId != 0;
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public ArrayList<String> invite(FPlayer follower) {
|
2011-02-06 13:36:11 +01:00
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
|
|
|
//Log.debug("this.role: "+this.role);
|
|
|
|
//Log.debug("this.role.value: "+this.role.value);
|
|
|
|
//Log.debug("FactionRole.MODERATOR.value: "+FactionRole.MODERATOR.value);
|
|
|
|
|
|
|
|
if (this.role.value < Role.MODERATOR.value) {
|
2011-03-11 05:14:39 +01:00
|
|
|
errors.add(Conf.colorSystem+"You must be a moderator to invite.");
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(errors.size() > 0) {
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getFaction().invite(follower);
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public ArrayList<String> deinvite(FPlayer follower) {
|
2011-02-06 13:36:11 +01:00
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
|
|
|
if (this.role.value < Role.MODERATOR.value) {
|
2011-03-11 05:14:39 +01:00
|
|
|
errors.add(Conf.colorSystem+"You must be a moderator to deinvite.");
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(errors.size() > 0) {
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2011-02-06 16:07:10 +01:00
|
|
|
return this.getFaction().deinvite(follower);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public ArrayList<String> kick(FPlayer follower) {
|
2011-02-06 13:36:11 +01:00
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
|
|
|
if ( ! follower.getFaction().equals(this.getFaction())) {
|
2011-02-12 18:05:05 +01:00
|
|
|
errors.add(follower.getNameAndRelevant(this)+Conf.colorSystem+" is not a member of "+Conf.colorMember+this.getFaction().getTag());
|
2011-02-06 13:36:11 +01:00
|
|
|
} else if (follower.equals(this)) {
|
2011-03-11 05:14:39 +01:00
|
|
|
errors.add(Conf.colorSystem+"You cannot kick yourself.");
|
2011-02-06 13:36:11 +01:00
|
|
|
errors.add(Conf.colorSystem+"You might want to "+Conf.colorCommand+Conf.aliasBase.get(0)+" "+Conf.aliasLeave.get(0));
|
|
|
|
} else if (follower.role.value >= this.role.value) { // TODO add more informative messages.
|
2011-03-11 05:14:39 +01:00
|
|
|
errors.add(Conf.colorSystem+"Your rank is too low to kick this player.");
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(errors.size() > 0) {
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
return follower.getFaction().kick(follower);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// You can only get a "skin" for online players.
|
|
|
|
// The same object is always returned for the same player.
|
|
|
|
// This means you can use the == operator. No .equals method necessary.
|
|
|
|
// -------------------------------------------- //
|
|
|
|
public static FPlayer get(String playername) {
|
|
|
|
if (instances.containsKey(playername)) {
|
|
|
|
return instances.get(playername);
|
|
|
|
}
|
|
|
|
|
|
|
|
FPlayer vplayer = new FPlayer(playername);
|
|
|
|
instances.put(playername, vplayer);
|
|
|
|
return vplayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
if (entry.getKey().equalsIgnoreCase(playername)) {
|
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Messages
|
|
|
|
// -------------------------------------------- //
|
|
|
|
public void sendMessage(String message) {
|
|
|
|
this.getPlayer().sendMessage(Conf.colorSystem + message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(List<String> messages) {
|
|
|
|
for(String message : messages) {
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// Persistance
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public boolean shouldBeSaved() {
|
|
|
|
return this.factionId != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean save() {
|
|
|
|
Factions.log("Saving players to disk");
|
|
|
|
|
|
|
|
// We only wan't to save the vplayers with non default values
|
|
|
|
Map<String, FPlayer> vplayersToSave = new HashMap<String, FPlayer>();
|
|
|
|
for (Entry<String, FPlayer> entry : instances.entrySet()) {
|
|
|
|
if (entry.getValue().shouldBeSaved()) {
|
|
|
|
vplayersToSave.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
DiscUtil.write(file, Factions.gson.toJson(vplayersToSave));
|
|
|
|
} catch (IOException e) {
|
|
|
|
Factions.log("Failed to save the players to disk.");
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean load() {
|
|
|
|
if ( ! file.exists()) {
|
|
|
|
Factions.log("No players to load from disk. Creating new file.");
|
|
|
|
save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Type type = new TypeToken<Map<String, FPlayer>>(){}.getType();
|
|
|
|
instances = Factions.gson.fromJson(DiscUtil.read(file), type);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fillPlayernames();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void fillPlayernames() {
|
|
|
|
for(Entry<String, FPlayer> entry : instances.entrySet()) {
|
|
|
|
entry.getValue().playername = entry.getKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|