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;
|
|
|
|
|
2013-04-10 10:32:04 +02:00
|
|
|
import com.massivecraft.mcore.util.Txt;
|
2013-04-10 10:18:34 +02:00
|
|
|
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
|
2012-11-28 06:51:37 +01:00
|
|
|
|
|
|
|
import com.massivecraft.factions.integration.Econ;
|
2011-10-08 22:03:44 +02:00
|
|
|
import com.massivecraft.factions.util.MiscUtil;
|
|
|
|
import com.massivecraft.factions.zcore.persist.EntityCollection;
|
|
|
|
|
2013-04-09 12:58:39 +02:00
|
|
|
public class FactionColl extends EntityCollection<Faction>
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-10 10:10:28 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-09 12:58:39 +02:00
|
|
|
public static FactionColl i = new FactionColl();
|
2013-04-10 10:10:28 +02:00
|
|
|
// public static FactionColl get() { return i; }
|
|
|
|
// TODO: Crap, does not work because of the instance method colliding.
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-09 12:58:39 +02:00
|
|
|
private FactionColl()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
super
|
|
|
|
(
|
|
|
|
Faction.class,
|
|
|
|
new CopyOnWriteArrayList<Faction>(),
|
|
|
|
new ConcurrentHashMap<String, Faction>(),
|
2013-04-09 13:12:13 +02:00
|
|
|
new File(Factions.get().getDataFolder(), "factions.json"),
|
|
|
|
Factions.get().gson
|
2011-10-08 22:03:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Type getMapType()
|
|
|
|
{
|
|
|
|
return new TypeToken<Map<String, Faction>>(){}.getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean loadFromDisc()
|
|
|
|
{
|
|
|
|
if ( ! super.loadFromDisc()) return false;
|
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
// Create Default Special Factions
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
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
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
// Fix From Old Formats
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
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);
|
2012-01-13 10:46:31 +01:00
|
|
|
|
|
|
|
// populate all faction player lists
|
|
|
|
for (Faction faction : i.get())
|
|
|
|
{
|
|
|
|
faction.refreshFPlayers();
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
// Flag Setters
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
public void setFlagsForWilderness(Faction faction)
|
|
|
|
{
|
2011-11-05 11:15:37 +01:00
|
|
|
faction.setOpen(false);
|
|
|
|
|
2011-10-24 02:33:30 +02:00
|
|
|
faction.setFlag(FFlag.PERMANENT, true);
|
2011-10-25 22:18:54 +02:00
|
|
|
faction.setFlag(FFlag.PEACEFUL, false);
|
2011-10-24 02:33:30 +02:00
|
|
|
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);
|
2011-10-24 14:54:37 +02:00
|
|
|
//faction.setFlag(FFlag.LIGHTNING, true);
|
2011-10-24 02:33:30 +02:00
|
|
|
faction.setFlag(FFlag.ENDERGRIEF, true);
|
|
|
|
|
2013-01-06 21:44:29 +01:00
|
|
|
faction.setPermittedRelations(FPerm.BUILD, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.DOOR, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.CONTAINER, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.BUTTON, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.LEVER, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
2011-10-24 02:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlagsForSafeZone(Faction faction)
|
|
|
|
{
|
2011-11-05 11:15:37 +01:00
|
|
|
faction.setOpen(false);
|
|
|
|
|
2011-10-24 02:33:30 +02:00
|
|
|
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);
|
2011-10-24 14:54:37 +02:00
|
|
|
//faction.setFlag(FFlag.LIGHTNING, false);
|
2011-10-24 02:33:30 +02:00
|
|
|
faction.setFlag(FFlag.ENDERGRIEF, false);
|
|
|
|
|
2013-01-06 21:44:29 +01:00
|
|
|
faction.setPermittedRelations(FPerm.DOOR, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.CONTAINER, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.BUTTON, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.LEVER, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.TERRITORY, Rel.LEADER, Rel.OFFICER, Rel.MEMBER);
|
2011-10-24 02:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlagsForWarZone(Faction faction)
|
|
|
|
{
|
2011-11-05 11:15:37 +01:00
|
|
|
faction.setOpen(false);
|
|
|
|
|
2011-10-24 02:33:30 +02:00
|
|
|
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);
|
2011-10-24 14:54:37 +02:00
|
|
|
//faction.setFlag(FFlag.LIGHTNING, true);
|
2011-10-24 02:33:30 +02:00
|
|
|
faction.setFlag(FFlag.ENDERGRIEF, true);
|
|
|
|
|
2013-01-06 21:44:29 +01:00
|
|
|
faction.setPermittedRelations(FPerm.DOOR, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.CONTAINER, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.BUTTON, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(FPerm.LEVER, Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
2012-08-08 04:58:04 +02:00
|
|
|
faction.setPermittedRelations(FPerm.TERRITORY, Rel.LEADER, Rel.OFFICER, Rel.MEMBER);
|
2011-10-24 02:33:30 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
// GET
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Faction get(String id)
|
|
|
|
{
|
|
|
|
if ( ! this.exists(id))
|
|
|
|
{
|
2013-04-10 08:40:41 +02:00
|
|
|
Factions.get().log(Level.WARNING, "Non existing factionId "+id+" requested! Issuing cleaning!");
|
2011-10-08 22:03:44 +02:00
|
|
|
Board.clean();
|
2013-04-09 13:22:23 +02:00
|
|
|
FPlayerColl.i.clean();
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getNone()
|
|
|
|
{
|
|
|
|
return this.get("0");
|
|
|
|
}
|
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
// Faction tag
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
public static ArrayList<String> validateTag(String str)
|
|
|
|
{
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
2013-04-09 13:15:25 +02:00
|
|
|
if(MiscUtil.getComparisonString(str).length() < ConfServer.factionTagLengthMin)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-10 10:32:04 +02:00
|
|
|
errors.add(Txt.parse("<i>The faction tag can't be shorter than <h>%s<i> chars.", ConfServer.factionTagLengthMin));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-09 13:15:25 +02:00
|
|
|
if(str.length() > ConfServer.factionTagLengthMax)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-10 10:32:04 +02:00
|
|
|
errors.add(Txt.parse("<i>The faction tag can't be longer than <h>%s<i> chars.", ConfServer.factionTagLengthMax));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (char c : str.toCharArray())
|
|
|
|
{
|
|
|
|
if ( ! MiscUtil.substanceChars.contains(String.valueOf(c)))
|
|
|
|
{
|
2013-04-10 10:32:04 +02:00
|
|
|
errors.add(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
|
|
|
}
|
|
|
|
|
2013-04-10 10:45:47 +02:00
|
|
|
String tag = Txt.getBestCIStart(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
|
|
|
}
|
|
|
|
|
2012-11-28 06:51:37 +01:00
|
|
|
public void econLandRewardRoutine()
|
|
|
|
{
|
2013-01-27 02:59:00 +01:00
|
|
|
if ( ! Econ.shouldBeUsed()) return;
|
|
|
|
|
2013-04-09 13:12:13 +02:00
|
|
|
Factions.get().log("Running econLandRewardRoutine...");
|
2012-11-28 06:51:37 +01:00
|
|
|
for (Faction faction : this.get())
|
|
|
|
{
|
|
|
|
int landCount = faction.getLandRounded();
|
|
|
|
if (!faction.getFlag(FFlag.PEACEFUL) && landCount > 0)
|
|
|
|
{
|
|
|
|
Set<FPlayer> players = faction.getFPlayers();
|
|
|
|
int playerCount = players.size();
|
2013-04-09 13:15:25 +02:00
|
|
|
double reward = ConfServer.econLandReward * landCount / playerCount;
|
2012-11-28 06:51:37 +01:00
|
|
|
for (FPlayer player : players)
|
|
|
|
{
|
2012-11-30 21:02:43 +01:00
|
|
|
Econ.modifyMoney(player, reward, "to own faction land", "for faction owning " + landCount + " land divided among " + playerCount + " member(s)");
|
2012-11-28 06:51:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|