2011-10-08 22:03:44 +02:00
|
|
|
package com.massivecraft.factions;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2011-10-24 02:33:30 +02:00
|
|
|
import com.massivecraft.factions.struct.FFlag;
|
|
|
|
import com.massivecraft.factions.struct.FPerm;
|
|
|
|
import com.massivecraft.factions.struct.Rel;
|
2011-10-08 22:03:44 +02:00
|
|
|
import com.massivecraft.factions.util.MiscUtil;
|
|
|
|
import com.massivecraft.factions.zcore.persist.EntityCollection;
|
2011-10-22 14:39:01 +02:00
|
|
|
import com.massivecraft.factions.zcore.util.TextUtil;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
public class Factions extends EntityCollection<Faction>
|
|
|
|
{
|
|
|
|
public static Factions i = new Factions();
|
|
|
|
|
|
|
|
P p = P.p;
|
|
|
|
|
|
|
|
private Factions()
|
|
|
|
{
|
|
|
|
super
|
|
|
|
(
|
|
|
|
Faction.class,
|
|
|
|
new CopyOnWriteArrayList<Faction>(),
|
|
|
|
new ConcurrentHashMap<String, Faction>(),
|
|
|
|
new File(P.p.getDataFolder(), "factions.json"),
|
|
|
|
P.p.gson
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Type getMapType()
|
|
|
|
{
|
|
|
|
return new TypeToken<Map<String, Faction>>(){}.getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean loadFromDisc()
|
|
|
|
{
|
|
|
|
if ( ! super.loadFromDisc()) return false;
|
|
|
|
|
2011-10-24 02:33:30 +02:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Create Default Special Factions
|
|
|
|
//----------------------------------------------//
|
2011-10-08 22:03:44 +02:00
|
|
|
if ( ! this.exists("0"))
|
|
|
|
{
|
|
|
|
Faction faction = this.create("0");
|
|
|
|
faction.setTag(ChatColor.DARK_GREEN+"Wilderness");
|
|
|
|
faction.setDescription("");
|
2011-10-24 02:33:30 +02:00
|
|
|
this.setFlagsForWilderness(faction);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
if ( ! this.exists("-1"))
|
|
|
|
{
|
|
|
|
Faction faction = this.create("-1");
|
2011-10-22 17:03:49 +02:00
|
|
|
faction.setTag("SafeZone");
|
2011-10-08 22:03:44 +02:00
|
|
|
faction.setDescription("Free from PVP and monsters");
|
2011-10-24 02:33:30 +02:00
|
|
|
|
|
|
|
this.setFlagsForSafeZone(faction);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
if ( ! this.exists("-2"))
|
|
|
|
{
|
|
|
|
Faction faction = this.create("-2");
|
2011-10-22 17:03:49 +02:00
|
|
|
faction.setTag("WarZone");
|
2011-10-08 22:03:44 +02:00
|
|
|
faction.setDescription("Not the safest place to be");
|
2011-10-24 02:33:30 +02:00
|
|
|
this.setFlagsForWarZone(faction);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
2011-10-24 02:33:30 +02:00
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Fix From Old Formats
|
|
|
|
//----------------------------------------------//
|
|
|
|
Faction wild = this.get("0");
|
|
|
|
Faction safeZone = this.get("-1");
|
|
|
|
Faction warZone = this.get("-2");
|
|
|
|
|
|
|
|
// Remove troublesome " " from old pre-1.6.0 names
|
|
|
|
if (safeZone != null && safeZone.getTag().contains(" "))
|
|
|
|
safeZone.setTag("SafeZone");
|
|
|
|
if (warZone != null && warZone.getTag().contains(" "))
|
|
|
|
warZone.setTag("WarZone");
|
|
|
|
|
|
|
|
// Set Flags if they are not set already.
|
|
|
|
if (wild != null && ! wild.getFlag(FFlag.PERMANENT))
|
|
|
|
setFlagsForWilderness(wild);
|
|
|
|
if (safeZone != null && ! safeZone.getFlag(FFlag.PERMANENT))
|
|
|
|
setFlagsForSafeZone(safeZone);
|
|
|
|
if (warZone != null && ! warZone.getFlag(FFlag.PERMANENT))
|
|
|
|
setFlagsForWarZone(warZone);
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-24 02:33:30 +02:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Flag Setters
|
|
|
|
//----------------------------------------------//
|
|
|
|
public void setFlagsForWilderness(Faction faction)
|
|
|
|
{
|
|
|
|
faction.setFlag(FFlag.PERMANENT, true);
|
|
|
|
faction.setFlag(FFlag.PEACEFUL, true);
|
|
|
|
faction.setFlag(FFlag.INFPOWER, true);
|
|
|
|
faction.setFlag(FFlag.POWERLOSS, true);
|
|
|
|
faction.setFlag(FFlag.PVP, true);
|
|
|
|
faction.setFlag(FFlag.FRIENDLYFIRE, false);
|
|
|
|
faction.setFlag(FFlag.MONSTERS, true);
|
|
|
|
faction.setFlag(FFlag.EXPLOSIONS, true);
|
|
|
|
faction.setFlag(FFlag.FIRESPREAD, true);
|
|
|
|
faction.setFlag(FFlag.LIGHTNING, true);
|
|
|
|
faction.setFlag(FFlag.ENDERGRIEF, true);
|
|
|
|
|
|
|
|
faction.setPermittedRelations(FPerm.BUILD, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.DOOR, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.CONTAINER, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.BUTTON, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.LEVER, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlagsForSafeZone(Faction faction)
|
|
|
|
{
|
|
|
|
faction.setFlag(FFlag.PERMANENT, true);
|
|
|
|
faction.setFlag(FFlag.PEACEFUL, true);
|
|
|
|
faction.setFlag(FFlag.INFPOWER, true);
|
|
|
|
faction.setFlag(FFlag.POWERLOSS, false);
|
|
|
|
faction.setFlag(FFlag.PVP, false);
|
|
|
|
faction.setFlag(FFlag.FRIENDLYFIRE, false);
|
|
|
|
faction.setFlag(FFlag.MONSTERS, false);
|
|
|
|
faction.setFlag(FFlag.EXPLOSIONS, false);
|
|
|
|
faction.setFlag(FFlag.FIRESPREAD, false);
|
|
|
|
faction.setFlag(FFlag.LIGHTNING, false);
|
|
|
|
faction.setFlag(FFlag.ENDERGRIEF, false);
|
|
|
|
|
|
|
|
faction.setPermittedRelations(FPerm.BUILD, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.DOOR, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.CONTAINER, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.BUTTON, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.LEVER, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlagsForWarZone(Faction faction)
|
|
|
|
{
|
|
|
|
faction.setFlag(FFlag.PERMANENT, true);
|
|
|
|
faction.setFlag(FFlag.PEACEFUL, true);
|
|
|
|
faction.setFlag(FFlag.INFPOWER, true);
|
|
|
|
faction.setFlag(FFlag.POWERLOSS, true);
|
|
|
|
faction.setFlag(FFlag.PVP, true);
|
|
|
|
faction.setFlag(FFlag.FRIENDLYFIRE, true);
|
|
|
|
faction.setFlag(FFlag.MONSTERS, true);
|
|
|
|
faction.setFlag(FFlag.EXPLOSIONS, true);
|
|
|
|
faction.setFlag(FFlag.FIRESPREAD, true);
|
|
|
|
faction.setFlag(FFlag.LIGHTNING, true);
|
|
|
|
faction.setFlag(FFlag.ENDERGRIEF, true);
|
|
|
|
|
|
|
|
faction.setPermittedRelations(FPerm.BUILD, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.DOOR, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.CONTAINER, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.BUTTON, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
faction.setPermittedRelations(FPerm.LEVER, Rel.ENEMY, Rel.NEUTRAL, Rel.TRUCE, Rel.ALLY, Rel.MEMBER);
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// GET
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Faction get(String id)
|
|
|
|
{
|
|
|
|
if ( ! this.exists(id))
|
|
|
|
{
|
|
|
|
p.log(Level.WARNING, "Non existing factionId "+id+" requested! Issuing cleaning!");
|
|
|
|
Board.clean();
|
|
|
|
FPlayers.i.clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getNone()
|
|
|
|
{
|
|
|
|
return this.get("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Faction tag
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public static ArrayList<String> validateTag(String str)
|
|
|
|
{
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
|
|
|
if(MiscUtil.getComparisonString(str).length() < Conf.factionTagLengthMin)
|
|
|
|
{
|
2011-10-21 19:20:33 +02:00
|
|
|
errors.add(P.p.txt.parse("<i>The faction tag can't be shorter than <h>%s<i> chars.", Conf.factionTagLengthMin));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(str.length() > Conf.factionTagLengthMax)
|
|
|
|
{
|
2011-10-21 19:20:33 +02:00
|
|
|
errors.add(P.p.txt.parse("<i>The faction tag can't be longer than <h>%s<i> chars.", Conf.factionTagLengthMax));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (char c : str.toCharArray())
|
|
|
|
{
|
|
|
|
if ( ! MiscUtil.substanceChars.contains(String.valueOf(c)))
|
|
|
|
{
|
2011-10-21 19:20:33 +02:00
|
|
|
errors.add(P.p.txt.parse("<i>Faction tag must be alphanumeric. \"<h>%s<i>\" is not allowed.", c));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2011-10-22 14:39:01 +02:00
|
|
|
public Faction getByTag(String str)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
String compStr = MiscUtil.getComparisonString(str);
|
|
|
|
for (Faction faction : this.get())
|
|
|
|
{
|
|
|
|
if (faction.getComparisonTag().equals(compStr))
|
|
|
|
{
|
|
|
|
return faction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-10-23 12:07:20 +02:00
|
|
|
public Faction getBestTagMatch(String searchFor)
|
2011-10-22 14:39:01 +02:00
|
|
|
{
|
|
|
|
Map<String, Faction> tag2faction = new HashMap<String, Faction>();
|
|
|
|
|
|
|
|
// TODO: Slow index building
|
|
|
|
for (Faction faction : this.get())
|
|
|
|
{
|
2011-10-22 17:03:49 +02:00
|
|
|
tag2faction.put(ChatColor.stripColor(faction.getTag()), faction);
|
2011-10-22 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 12:07:20 +02:00
|
|
|
String tag = TextUtil.getBestStartWithCI(tag2faction.keySet(), searchFor);
|
2011-10-22 14:39:01 +02:00
|
|
|
if (tag == null) return null;
|
|
|
|
return tag2faction.get(tag);
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
public boolean isTagTaken(String str)
|
|
|
|
{
|
2011-10-22 14:39:01 +02:00
|
|
|
return this.getByTag(str) != null;
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|