2011-10-08 22:03:44 +02:00
|
|
|
package com.massivecraft.factions;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.*;
|
2013-04-12 09:47:43 +02:00
|
|
|
import java.util.Map.Entry;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
import com.massivecraft.mcore.store.Coll;
|
|
|
|
import com.massivecraft.mcore.store.MStore;
|
|
|
|
import com.massivecraft.mcore.util.DiscUtil;
|
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;
|
|
|
|
|
2013-04-12 15:10:11 +02:00
|
|
|
public class FactionColl extends Coll<Faction>
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-10 10:10:28 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
private static FactionColl i = new FactionColl();
|
|
|
|
public static FactionColl get() { return i; }
|
2013-04-09 12:58:39 +02:00
|
|
|
private FactionColl()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-12 15:10:11 +02:00
|
|
|
super(MStore.getDb(ConfServer.dburi), Factions.get(), "uuid", Const.COLLECTION_BASENAME_FACTION, Faction.class, false);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE: COLL
|
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
@Override
|
2013-04-12 09:47:43 +02:00
|
|
|
public void init()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-12 09:47:43 +02:00
|
|
|
super.init();
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
this.migrate();
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
this.createDefaultFactions();
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// TODO: Refactor and fix with the member-index.
|
2012-01-13 10:46:31 +01:00
|
|
|
// populate all faction player lists
|
2013-04-12 09:47:43 +02:00
|
|
|
// or as you also could describe it: Reindex of Members
|
|
|
|
for (Faction faction : this.getAll())
|
2012-01-13 10:46:31 +01:00
|
|
|
{
|
|
|
|
faction.refreshFPlayers();
|
2013-04-12 09:47:43 +02:00
|
|
|
}
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
public void migrate()
|
2011-10-24 02:33:30 +02:00
|
|
|
{
|
2013-04-12 09:47:43 +02:00
|
|
|
// Create file objects
|
|
|
|
File oldFile = new File(Factions.get().getDataFolder(), "factions.json");
|
|
|
|
File newFile = new File(Factions.get().getDataFolder(), "factions.json.migrated");
|
2011-11-05 11:15:37 +01:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// Already migrated?
|
|
|
|
if ( ! oldFile.exists()) return;
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// Read the file content through GSON.
|
|
|
|
Type type = new TypeToken<Map<String, Faction>>(){}.getType();
|
|
|
|
Map<String, Faction> id2faction = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
|
2011-11-05 11:15:37 +01:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// Set the data
|
|
|
|
for (Entry<String, Faction> entry : id2faction.entrySet())
|
|
|
|
{
|
|
|
|
String factionId = entry.getKey();
|
|
|
|
Faction faction = entry.getValue();
|
2013-04-12 10:42:37 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
FactionColl.get().create(factionId).load(faction);
|
|
|
|
}
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// Mark as migrated
|
|
|
|
oldFile.renameTo(newFile);
|
2011-10-24 02:33:30 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
@Override
|
|
|
|
public Faction detachId(Object oid)
|
2011-10-24 02:33:30 +02:00
|
|
|
{
|
2013-04-12 09:47:43 +02:00
|
|
|
String accountId = this.get(oid).getAccountId();
|
2011-11-05 11:15:37 +01:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
Faction ret = super.detachId(oid);
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
if (Econ.shouldBeUsed())
|
|
|
|
{
|
|
|
|
Econ.setBalance(accountId, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean the board
|
|
|
|
// TODO: Use events for this instead?
|
|
|
|
BoardColl.get().clean();
|
|
|
|
|
|
|
|
// Clean the fplayers
|
|
|
|
FPlayerColl.get().clean();
|
|
|
|
|
|
|
|
return ret;
|
2011-10-24 02:33:30 +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
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// TODO: I hope this one is not crucial anymore.
|
|
|
|
// If it turns out to be I will just have to recreate the feature in the proper place.
|
|
|
|
/*
|
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!");
|
2013-04-11 11:11:31 +02:00
|
|
|
BoardColl.get().clean();
|
2013-04-12 08:56:26 +02:00
|
|
|
FPlayerColl.get().clean();
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.get(id);
|
|
|
|
}
|
2013-04-12 09:47:43 +02:00
|
|
|
*/
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
public Faction getNone()
|
|
|
|
{
|
2013-04-12 09:47:43 +02:00
|
|
|
return this.get(Const.FACTIONID_NONE);
|
|
|
|
}
|
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2013-04-12 09:47:43 +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);
|
2013-04-12 09:47:43 +02:00
|
|
|
for (Faction faction : this.getAll())
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
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
|
2013-04-12 09:47:43 +02:00
|
|
|
for (Faction faction : this.getAll())
|
2011-10-22 14:39:01 +02:00
|
|
|
{
|
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...");
|
2013-04-12 09:47:43 +02:00
|
|
|
for (Faction faction : this.getAll())
|
2012-11-28 06:51:37 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 09:47:43 +02:00
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// CREATE DEFAULT FACTIONS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public void createDefaultFactions()
|
|
|
|
{
|
|
|
|
this.createNoneFaction();
|
|
|
|
this.createSafeZoneFaction();
|
|
|
|
this.createWarZoneFaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createNoneFaction()
|
|
|
|
{
|
|
|
|
if (this.containsId(Const.FACTIONID_NONE)) return;
|
|
|
|
|
|
|
|
Faction faction = this.create(Const.FACTIONID_NONE);
|
|
|
|
|
|
|
|
faction.setTag(ChatColor.DARK_GREEN+"Wilderness");
|
|
|
|
faction.setDescription("");
|
|
|
|
faction.setOpen(false);
|
|
|
|
|
|
|
|
faction.setFlag(FFlag.PERMANENT, true);
|
|
|
|
faction.setFlag(FFlag.PEACEFUL, false);
|
|
|
|
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.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createSafeZoneFaction()
|
|
|
|
{
|
|
|
|
if (this.containsId(Const.FACTIONID_SAFEZONE)) return;
|
|
|
|
|
|
|
|
Faction faction = this.create(Const.FACTIONID_SAFEZONE);
|
|
|
|
|
|
|
|
faction.setTag("SafeZone");
|
|
|
|
faction.setDescription("Free from PVP and monsters");
|
|
|
|
faction.setOpen(false);
|
|
|
|
|
|
|
|
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.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createWarZoneFaction()
|
|
|
|
{
|
|
|
|
if (this.containsId(Const.FACTIONID_WARZONE)) return;
|
|
|
|
|
|
|
|
Faction faction = this.create(Const.FACTIONID_WARZONE);
|
|
|
|
|
|
|
|
faction.setTag("WarZone");
|
|
|
|
faction.setDescription("Not the safest place to be");
|
|
|
|
faction.setOpen(false);
|
|
|
|
|
|
|
|
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.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);
|
|
|
|
}
|
2012-11-28 06:51:37 +01:00
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|