2011-10-08 22:03:44 +02:00
|
|
|
package com.massivecraft.factions;
|
|
|
|
|
2014-10-25 20:22:41 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.massivecraft.factions.util.MiscUtil;
|
|
|
|
import com.massivecraft.factions.zcore.persist.EntityCollection;
|
2014-07-01 20:19:36 +02:00
|
|
|
import com.massivecraft.factions.zcore.util.TL;
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.massivecraft.factions.zcore.util.TextUtil;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Type;
|
2014-04-04 20:55:21 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2011-10-08 22:03:44 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public class Factions extends EntityCollection<Faction> {
|
2014-08-05 17:17:27 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public static Factions i = new Factions();
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
P p = P.p;
|
|
|
|
|
|
|
|
private Factions() {
|
2014-07-01 20:19:36 +02:00
|
|
|
super(Faction.class, new CopyOnWriteArrayList<Faction>(), new ConcurrentHashMap<String, Faction>(), new File(P.p.getDataFolder(), "factions.json"), P.p.gson);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Type getMapType() {
|
2014-07-01 22:10:18 +02:00
|
|
|
return new TypeToken<Map<String, Faction>>() {
|
|
|
|
}.getType();
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean loadFromDisc() {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (!super.loadFromDisc()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
// Make sure the default neutral faction exists
|
|
|
|
if (!this.exists("0")) {
|
2014-07-01 22:10:18 +02:00
|
|
|
Faction faction = this.create("0");
|
|
|
|
faction.setTag(TL.WILDERNESS.toString());
|
2014-07-01 20:19:36 +02:00
|
|
|
faction.setDescription(TL.WILDERNESS_DESCRIPTION.toString());
|
2014-07-01 21:52:40 +02:00
|
|
|
} else {
|
2014-07-01 20:46:30 +02:00
|
|
|
if (!this.get("0").getTag().equalsIgnoreCase(TL.WILDERNESS.toString())) {
|
|
|
|
get("0").setTag(TL.WILDERNESS.toString());
|
2014-07-01 22:10:18 +02:00
|
|
|
}
|
|
|
|
if (!this.get("0").getDescription().equalsIgnoreCase(TL.WILDERNESS_DESCRIPTION.toString())) {
|
2014-07-01 20:46:30 +02:00
|
|
|
get("0").setDescription(TL.WILDERNESS_DESCRIPTION.toString());
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the safe zone faction exists
|
|
|
|
if (!this.exists("-1")) {
|
2014-07-01 22:10:18 +02:00
|
|
|
Faction faction = this.create("-1");
|
|
|
|
faction.setTag(TL.SAFEZONE.toString());
|
2014-07-01 20:19:36 +02:00
|
|
|
faction.setDescription(TL.SAFEZONE_DESCRIPTION.toString());
|
2014-07-01 21:52:40 +02:00
|
|
|
} else {
|
2014-07-01 20:46:30 +02:00
|
|
|
if (!getSafeZone().getTag().equalsIgnoreCase(TL.SAFEZONE.toString())) {
|
|
|
|
getSafeZone().setTag(TL.SAFEZONE.toString());
|
2014-07-01 22:10:18 +02:00
|
|
|
}
|
|
|
|
if (!getSafeZone().getDescription().equalsIgnoreCase(TL.SAFEZONE_DESCRIPTION.toString())) {
|
2014-07-01 20:46:30 +02:00
|
|
|
getSafeZone().setDescription(TL.SAFEZONE_DESCRIPTION.toString());
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
// if SafeZone has old pre-1.6.0 name, rename it to remove troublesome " "
|
|
|
|
Faction faction = this.getSafeZone();
|
2014-07-01 22:10:18 +02:00
|
|
|
if (faction.getTag().contains(" ")) {
|
|
|
|
faction.setTag(TL.SAFEZONE.toString());
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the war zone faction exists
|
|
|
|
if (!this.exists("-2")) {
|
2014-07-01 22:10:18 +02:00
|
|
|
Faction faction = this.create("-2");
|
|
|
|
faction.setTag(TL.WARZONE.toString());
|
2014-07-01 20:19:36 +02:00
|
|
|
faction.setDescription(TL.WARZONE_DESCRIPTION.toString());
|
2014-07-01 21:52:40 +02:00
|
|
|
} else {
|
2014-07-01 20:46:30 +02:00
|
|
|
if (!getWarZone().getTag().equalsIgnoreCase(TL.WARZONE.toString())) {
|
|
|
|
getWarZone().setTag(TL.WARZONE.toString());
|
2014-07-01 22:10:18 +02:00
|
|
|
}
|
|
|
|
if (!getWarZone().getDescription().equalsIgnoreCase(TL.WARZONE_DESCRIPTION.toString())) {
|
2014-07-01 20:46:30 +02:00
|
|
|
getWarZone().setDescription(TL.WARZONE_DESCRIPTION.toString());
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
// if WarZone has old pre-1.6.0 name, rename it to remove troublesome " "
|
|
|
|
Faction faction = this.getWarZone();
|
2014-07-01 22:10:18 +02:00
|
|
|
if (faction.getTag().contains(" ")) {
|
|
|
|
faction.setTag(TL.WARZONE.toString());
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// populate all faction player lists
|
|
|
|
for (Faction faction : i.get()) {
|
|
|
|
faction.refreshFPlayers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// GET
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Faction get(String id) {
|
|
|
|
if (!this.exists(id)) {
|
2014-07-01 22:10:18 +02:00
|
|
|
p.log(Level.WARNING, "Non existing factionId " + id + " requested! Issuing cleaning!");
|
|
|
|
Board.clean();
|
2014-04-04 20:55:21 +02:00
|
|
|
FPlayers.i.clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getNone() {
|
|
|
|
return this.get("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getSafeZone() {
|
|
|
|
return this.get("-1");
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getWarZone() {
|
|
|
|
return this.get("-2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Faction tag
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public static ArrayList<String> validateTag(String str) {
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
|
|
|
if (MiscUtil.getComparisonString(str).length() < Conf.factionTagLengthMin) {
|
|
|
|
errors.add(P.p.txt.parse("<i>The faction tag can't be shorter than <h>%s<i> chars.", Conf.factionTagLengthMin));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str.length() > Conf.factionTagLengthMax) {
|
|
|
|
errors.add(P.p.txt.parse("<i>The faction tag can't be longer than <h>%s<i> chars.", Conf.factionTagLengthMax));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (char c : str.toCharArray()) {
|
|
|
|
if (!MiscUtil.substanceChars.contains(String.valueOf(c))) {
|
|
|
|
errors.add(P.p.txt.parse("<i>Faction tag must be alphanumeric. \"<h>%s<i>\" is not allowed.", c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2014-05-19 20:03:26 +02:00
|
|
|
// Loops through all faction tags. Case and color insensitive.
|
2014-04-04 20:55:21 +02:00
|
|
|
public Faction getByTag(String str) {
|
2014-07-01 22:10:18 +02:00
|
|
|
String compStr = MiscUtil.getComparisonString(str);
|
|
|
|
for (Faction faction : this.get()) {
|
2014-04-04 20:55:21 +02:00
|
|
|
if (faction.getComparisonTag().equals(compStr)) {
|
|
|
|
return faction;
|
|
|
|
}
|
2014-07-01 22:10:18 +02:00
|
|
|
}
|
|
|
|
return null;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getBestTagMatch(String searchFor) {
|
|
|
|
Map<String, Faction> tag2faction = new HashMap<String, Faction>();
|
|
|
|
|
|
|
|
// TODO: Slow index building
|
|
|
|
for (Faction faction : this.get()) {
|
|
|
|
tag2faction.put(ChatColor.stripColor(faction.getTag()), faction);
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
String tag = TextUtil.getBestStartWithCI(tag2faction.keySet(), searchFor);
|
|
|
|
if (tag == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
return tag2faction.get(tag);
|
|
|
|
}
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public boolean isTagTaken(String str) {
|
|
|
|
return this.getByTag(str) != null;
|
|
|
|
}
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
}
|