2013-04-22 09:37:53 +02:00
|
|
|
package com.massivecraft.factions.entity;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
2013-04-19 18:34:21 +02:00
|
|
|
import com.massivecraft.mcore.money.Money;
|
2013-04-12 09:47:43 +02:00
|
|
|
import com.massivecraft.mcore.store.Coll;
|
|
|
|
import com.massivecraft.mcore.store.MStore;
|
2013-04-10 10:32:04 +02:00
|
|
|
import com.massivecraft.mcore.util.Txt;
|
2012-11-28 06:51:37 +01:00
|
|
|
|
2013-04-22 09:37:53 +02:00
|
|
|
import com.massivecraft.factions.ConfServer;
|
|
|
|
import com.massivecraft.factions.FFlag;
|
|
|
|
import com.massivecraft.factions.FPerm;
|
|
|
|
import com.massivecraft.factions.Factions;
|
|
|
|
import com.massivecraft.factions.Rel;
|
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
|
|
|
// -------------------------------------------- //
|
2013-04-22 12:26:13 +02:00
|
|
|
// CONSTRUCT
|
2013-04-10 10:10:28 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-22 12:26:13 +02:00
|
|
|
public FactionColl(String name)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 12:26:13 +02:00
|
|
|
super(name, Faction.class, MStore.getDb(ConfServer.dburi), Factions.get());
|
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-24 13:50:02 +02:00
|
|
|
this.createSpecialFactions();
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:05:00 +02:00
|
|
|
/*
|
2013-04-12 09:47:43 +02:00
|
|
|
@Override
|
2013-04-19 18:34:21 +02:00
|
|
|
protected synchronized String attach(Faction faction, Object oid, boolean noteChange)
|
2011-10-24 02:33:30 +02:00
|
|
|
{
|
2013-04-19 18:34:21 +02:00
|
|
|
String ret = super.attach(faction, oid, noteChange);
|
2011-11-05 11:15:37 +01:00
|
|
|
|
2013-04-19 18:34:21 +02:00
|
|
|
// Factions start with 0 money.
|
2013-04-22 09:37:53 +02:00
|
|
|
// TODO: Can this be done here?
|
|
|
|
// TODO: Or will it be a to heavy operation to do this often?
|
|
|
|
|
2013-04-22 15:05:00 +02:00
|
|
|
//System.out.println("faction "+faction);
|
|
|
|
//System.out.println("faction.getId() "+faction.getId());
|
|
|
|
// TODO: Why does this happen for Wilderness?
|
|
|
|
if (faction.getId() == null) return ret;
|
|
|
|
|
2013-04-22 09:37:53 +02:00
|
|
|
if (!Money.exists(faction))
|
2013-04-19 18:34:21 +02:00
|
|
|
{
|
2013-04-22 09:37:53 +02:00
|
|
|
Money.set(faction, 0);
|
2013-04-19 18:34:21 +02:00
|
|
|
}
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-19 18:34:21 +02:00
|
|
|
return ret;
|
2013-04-22 15:05:00 +02:00
|
|
|
}*/
|
2013-04-19 18:34:21 +02:00
|
|
|
|
2013-04-24 13:50:02 +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.
|
|
|
|
/*
|
|
|
|
@Override
|
|
|
|
public Faction get(String id)
|
|
|
|
{
|
|
|
|
if ( ! this.exists(id))
|
|
|
|
{
|
|
|
|
Factions.get().log(Level.WARNING, "Non existing factionId "+id+" requested! Issuing cleaning!");
|
|
|
|
BoardColl.get().clean();
|
|
|
|
FPlayerColl.get().clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.get(id);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2013-04-19 18:34:21 +02:00
|
|
|
@Override
|
|
|
|
public Faction detachId(Object oid)
|
|
|
|
{
|
|
|
|
Faction faction = this.get(oid);
|
2013-04-22 12:26:13 +02:00
|
|
|
Money.set(faction, 0);
|
|
|
|
String universe = faction.getUniverse();
|
2013-04-12 09:47:43 +02:00
|
|
|
|
2013-04-19 18:34:21 +02:00
|
|
|
Faction ret = super.detachId(oid);
|
|
|
|
|
2013-04-12 09:47:43 +02:00
|
|
|
// Clean the board
|
2013-04-22 12:26:13 +02:00
|
|
|
BoardColls.get().getForUniverse(universe).clean();
|
2013-04-12 09:47:43 +02:00
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
// Clean the uplayers
|
|
|
|
UPlayerColls.get().getForUniverse(universe).clean();
|
2013-04-12 09:47:43 +02:00
|
|
|
|
|
|
|
return ret;
|
2011-10-24 02:33:30 +02:00
|
|
|
}
|
2013-04-22 15:05:00 +02:00
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// INDEX
|
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2013-04-22 19:57:11 +02:00
|
|
|
public void reindexUPlayers()
|
2013-04-17 14:44:08 +02:00
|
|
|
{
|
|
|
|
for (Faction faction : this.getAll())
|
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
faction.reindexUPlayers();
|
2013-04-17 14:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2013-04-24 13:50:02 +02:00
|
|
|
// SPECIAL FACTIONS
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-24 13:50:02 +02:00
|
|
|
public void createSpecialFactions()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-24 13:50:02 +02:00
|
|
|
this.getNone();
|
|
|
|
this.getSafezone();
|
|
|
|
this.getWarzone();
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getNone()
|
|
|
|
{
|
2013-04-24 13:50:02 +02:00
|
|
|
String id = UConf.get(this).factionIdNone;
|
|
|
|
Faction faction = this.get(id);
|
|
|
|
if (faction != null) return faction;
|
|
|
|
|
2013-04-24 16:40:35 +02:00
|
|
|
faction = this.create(id);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
|
|
|
faction.setTag(ChatColor.DARK_GREEN+"Wilderness");
|
|
|
|
faction.setDescription(null);
|
|
|
|
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.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);
|
|
|
|
|
|
|
|
return faction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getSafezone()
|
|
|
|
{
|
|
|
|
String id = UConf.get(this).factionIdSafezone;
|
|
|
|
Faction faction = this.get(id);
|
|
|
|
if (faction != null) return faction;
|
|
|
|
|
2013-04-24 16:40:35 +02:00
|
|
|
faction = this.create(id);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
|
|
|
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.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);
|
|
|
|
|
|
|
|
return faction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getWarzone()
|
|
|
|
{
|
|
|
|
String id = UConf.get(this).factionIdWarzone;
|
|
|
|
Faction faction = this.get(id);
|
|
|
|
if (faction != null) return faction;
|
|
|
|
|
2013-04-24 16:40:35 +02:00
|
|
|
faction = this.create(id);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
|
|
|
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.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);
|
|
|
|
|
|
|
|
return faction;
|
2013-04-12 09:47:43 +02:00
|
|
|
}
|
|
|
|
|
2013-04-24 08:39:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// LAND REWARD
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public void econLandRewardRoutine()
|
|
|
|
{
|
|
|
|
if (!Econ.isEnabled(this.getUniverse())) return;
|
|
|
|
|
|
|
|
double econLandReward = UConf.get(this).econLandReward;
|
|
|
|
if (econLandReward == 0.0) return;
|
|
|
|
|
|
|
|
Factions.get().log("Running econLandRewardRoutine...");
|
|
|
|
for (Faction faction : this.getAll())
|
|
|
|
{
|
|
|
|
int landCount = faction.getLandCount();
|
|
|
|
if (!faction.getFlag(FFlag.PEACEFUL) && landCount > 0)
|
|
|
|
{
|
|
|
|
List<UPlayer> players = faction.getUPlayers();
|
|
|
|
int playerCount = players.size();
|
|
|
|
double reward = econLandReward * landCount / playerCount;
|
|
|
|
for (UPlayer player : players)
|
|
|
|
{
|
|
|
|
Econ.modifyMoney(player, reward, "own " + landCount + " faction land divided among " + playerCount + " members");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-04-22 16:58:22 +02:00
|
|
|
public ArrayList<String> validateTag(String str)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
2013-04-22 16:58:22 +02:00
|
|
|
if (MiscUtil.getComparisonString(str).length() < UConf.get(this).factionTagLengthMin)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 16:58:22 +02:00
|
|
|
errors.add(Txt.parse("<i>The faction tag can't be shorter than <h>%s<i> chars.", UConf.get(this).factionTagLengthMin));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 16:58:22 +02:00
|
|
|
if (str.length() > UConf.get(this).factionTagLengthMax)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 16:58:22 +02:00
|
|
|
errors.add(Txt.parse("<i>The faction tag can't be longer than <h>%s<i> chars.", UConf.get(this).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
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|