2013-04-22 09:37:53 +02:00
|
|
|
package com.massivecraft.factions.entity;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
2014-06-04 14:02:23 +02:00
|
|
|
import com.massivecraft.massivecore.store.Coll;
|
|
|
|
import com.massivecraft.massivecore.store.MStore;
|
|
|
|
import com.massivecraft.massivecore.util.Txt;
|
2014-09-17 13:17:33 +02:00
|
|
|
import com.massivecraft.factions.Const;
|
2013-04-22 09:37:53 +02:00
|
|
|
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
|
|
|
// -------------------------------------------- //
|
2014-09-17 13:17:33 +02:00
|
|
|
// INSTANCE & CONSTRUCT
|
2013-04-10 10:10:28 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
private static FactionColl i = new FactionColl();
|
|
|
|
public static FactionColl get() { return i; }
|
|
|
|
private FactionColl()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
super(Const.COLLECTION_FACTION, Faction.class, MStore.getDb(), 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-26 10:32:02 +02:00
|
|
|
@Override
|
|
|
|
public Faction get(Object oid)
|
|
|
|
{
|
|
|
|
Faction ret = super.get(oid);
|
|
|
|
|
2013-06-27 10:06:15 +02:00
|
|
|
// We should only trigger automatic clean if the whole database system is initialized.
|
|
|
|
// A cleaning can only be successful if all data is available.
|
|
|
|
// Example Reason: When creating the special factions for the first time "createSpecialFactions" a clean would be triggered otherwise.
|
|
|
|
if (ret == null && Factions.get().isDatabaseInitialized())
|
2013-04-26 10:32:02 +02:00
|
|
|
{
|
2014-09-17 13:29:58 +02:00
|
|
|
String message = Txt.parse("<b>Non existing factionId <h>%s <b>requested. <i>Cleaning all boards and mplayers.", this.fixId(oid));
|
2013-04-26 10:32:02 +02:00
|
|
|
Factions.get().log(message);
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
BoardColl.get().clean();
|
|
|
|
MPlayerColl.get().clean();
|
2013-04-26 10:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-22 15:05:00 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// INDEX
|
|
|
|
// -------------------------------------------- //
|
2011-10-24 02:33:30 +02:00
|
|
|
|
2014-09-17 13:29:58 +02:00
|
|
|
public void reindexMPlayers()
|
2013-04-17 14:44:08 +02:00
|
|
|
{
|
|
|
|
for (Faction faction : this.getAll())
|
|
|
|
{
|
2014-09-17 13:29:58 +02:00
|
|
|
faction.reindexMPlayers();
|
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()
|
|
|
|
{
|
2014-10-01 22:00:08 +02:00
|
|
|
String id = Factions.ID_NONE;
|
2013-04-24 13:50:02 +02:00
|
|
|
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
|
|
|
|
2014-11-13 11:41:21 +01:00
|
|
|
faction.setName(Factions.NAME_NONE_DEFAULT);
|
2013-04-24 13:50:02 +02:00
|
|
|
faction.setDescription(null);
|
2014-10-02 11:45:06 +02:00
|
|
|
|
2014-10-07 12:30:44 +02:00
|
|
|
faction.setFlag(MFlag.getFlagOpen(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagPermanent(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPeaceful(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagInfpower(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPowerloss(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPvp(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagFriendlyire(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagMonsters(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagExplosions(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagOfflineexplosions(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagFirespread(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagEndergrief(), true);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
2014-10-03 09:01:36 +02:00
|
|
|
faction.setPermittedRelations(MPerm.getPermBuild(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermDoor(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermContainer(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermButton(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermLever(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
2014-10-07 12:30:44 +02:00
|
|
|
faction.setPermittedRelations(MPerm.getPermDeposit(), Rel.LEADER, Rel.OFFICER); // Wilderness deposit should be limited as an anti spam meassure.
|
2013-04-24 13:50:02 +02:00
|
|
|
|
|
|
|
return faction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getSafezone()
|
|
|
|
{
|
2014-10-01 22:00:08 +02:00
|
|
|
String id = Factions.ID_SAFEZONE;
|
2013-04-24 13:50:02 +02:00
|
|
|
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
|
|
|
|
2014-11-13 11:41:21 +01:00
|
|
|
faction.setName(Factions.NAME_SAFEZONE_DEFAULT);
|
2013-04-24 13:50:02 +02:00
|
|
|
faction.setDescription("Free from PVP and monsters");
|
2014-10-02 11:45:06 +02:00
|
|
|
|
2014-10-07 12:30:44 +02:00
|
|
|
faction.setFlag(MFlag.getFlagOpen(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagPermanent(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPeaceful(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagInfpower(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPowerloss(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagPvp(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagFriendlyire(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagMonsters(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagExplosions(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagOfflineexplosions(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagFirespread(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagEndergrief(), false);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
2014-10-03 09:01:36 +02:00
|
|
|
faction.setPermittedRelations(MPerm.getPermDoor(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermContainer(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermButton(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermLever(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermTerritory(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
|
|
|
return faction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Faction getWarzone()
|
|
|
|
{
|
2014-10-01 22:00:08 +02:00
|
|
|
String id = Factions.ID_WARZONE;
|
2013-04-24 13:50:02 +02:00
|
|
|
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
|
|
|
|
2014-11-13 11:41:21 +01:00
|
|
|
faction.setName(Factions.NAME_WARZONE_DEFAULT);
|
2013-04-24 13:50:02 +02:00
|
|
|
faction.setDescription("Not the safest place to be");
|
2014-10-02 11:45:06 +02:00
|
|
|
|
2014-10-07 12:30:44 +02:00
|
|
|
faction.setFlag(MFlag.getFlagOpen(), false);
|
|
|
|
faction.setFlag(MFlag.getFlagPermanent(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPeaceful(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagInfpower(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPowerloss(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagPvp(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagFriendlyire(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagMonsters(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagExplosions(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagOfflineexplosions(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagFirespread(), true);
|
|
|
|
faction.setFlag(MFlag.getFlagEndergrief(), true);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
2014-10-03 09:01:36 +02:00
|
|
|
faction.setPermittedRelations(MPerm.getPermDoor(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermContainer(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermButton(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermLever(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY, Rel.TRUCE, Rel.NEUTRAL, Rel.ENEMY);
|
|
|
|
faction.setPermittedRelations(MPerm.getPermTerritory(), Rel.LEADER, Rel.OFFICER, Rel.MEMBER);
|
2013-04-24 13:50:02 +02:00
|
|
|
|
|
|
|
return faction;
|
2013-04-12 09:47:43 +02:00
|
|
|
}
|
|
|
|
|
2013-04-24 08:39:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// LAND REWARD
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public void econLandRewardRoutine()
|
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
if (!Econ.isEnabled()) return;
|
2013-04-24 08:39:26 +02:00
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
double econLandReward = MConf.get().econLandReward;
|
2013-04-24 08:39:26 +02:00
|
|
|
if (econLandReward == 0.0) return;
|
|
|
|
|
|
|
|
Factions.get().log("Running econLandRewardRoutine...");
|
|
|
|
for (Faction faction : this.getAll())
|
|
|
|
{
|
|
|
|
int landCount = faction.getLandCount();
|
2014-10-07 12:30:44 +02:00
|
|
|
if (!faction.getFlag(MFlag.getFlagPeaceful()) && landCount > 0)
|
2013-04-24 08:39:26 +02:00
|
|
|
{
|
2014-09-17 13:29:58 +02:00
|
|
|
List<MPlayer> players = faction.getMPlayers();
|
2013-04-24 08:39:26 +02:00
|
|
|
int playerCount = players.size();
|
|
|
|
double reward = econLandReward * landCount / playerCount;
|
2014-09-17 13:17:33 +02:00
|
|
|
for (MPlayer player : players)
|
2013-04-24 08:39:26 +02:00
|
|
|
{
|
|
|
|
Econ.modifyMoney(player, reward, "own " + landCount + " faction land divided among " + playerCount + " members");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2013-04-24 19:01:17 +02:00
|
|
|
// FACTION NAME
|
2013-04-10 09:40:39 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-24 19:01:17 +02:00
|
|
|
public ArrayList<String> validateName(String str)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
if (MiscUtil.getComparisonString(str).length() < MConf.get().factionNameLengthMin)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
errors.add(Txt.parse("<i>The faction name can't be shorter than <h>%s<i> chars.", MConf.get().factionNameLengthMin));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
if (str.length() > MConf.get().factionNameLengthMax)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
errors.add(Txt.parse("<i>The faction name can't be longer than <h>%s<i> chars.", MConf.get().factionNameLengthMax));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (char c : str.toCharArray())
|
|
|
|
{
|
|
|
|
if ( ! MiscUtil.substanceChars.contains(String.valueOf(c)))
|
|
|
|
{
|
2013-04-24 19:01:17 +02:00
|
|
|
errors.add(Txt.parse("<i>Faction name must be alphanumeric. \"<h>%s<i>\" is not allowed.", c));
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2014-10-13 08:11:00 +02:00
|
|
|
public Faction getByName(String name)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2014-10-13 08:11:00 +02:00
|
|
|
String compStr = MiscUtil.getComparisonString(name);
|
2013-04-12 09:47:43 +02:00
|
|
|
for (Faction faction : this.getAll())
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-24 19:01:17 +02:00
|
|
|
if (faction.getComparisonName().equals(compStr))
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
return faction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-04-24 19:01:17 +02:00
|
|
|
public boolean isNameTaken(String str)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-24 19:01:17 +02:00
|
|
|
return this.getByName(str) != null;
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2014-11-14 08:41:58 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// OLD MIGRATION COMMENT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
/*
|
|
|
|
@Override
|
|
|
|
public void init()
|
|
|
|
{
|
|
|
|
super.init();
|
|
|
|
|
|
|
|
this.migrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method is for the 1.8.X --> 2.0.0 migration
|
|
|
|
public void migrate()
|
|
|
|
{
|
|
|
|
// Create file objects
|
|
|
|
File oldFile = new File(Factions.get().getDataFolder(), "factions.json");
|
|
|
|
File newFile = new File(Factions.get().getDataFolder(), "factions.json.migrated");
|
|
|
|
|
|
|
|
// Already migrated?
|
|
|
|
if ( ! oldFile.exists()) return;
|
|
|
|
|
|
|
|
// Faction ids /delete
|
|
|
|
// For simplicity we just drop the old special factions.
|
|
|
|
// They will be replaced with new autogenerated ones per universe.
|
|
|
|
Set<String> factionIdsToDelete = MUtil.set("0", "-1", "-2");
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// The Coll
|
|
|
|
FactionColl coll = this.getForUniverse(MassiveCore.DEFAULT);
|
|
|
|
|
|
|
|
// Set the data
|
|
|
|
for (Entry<String, Faction> entry : id2faction.entrySet())
|
|
|
|
{
|
|
|
|
String factionId = entry.getKey();
|
|
|
|
if (factionIdsToDelete.contains(factionId)) continue;
|
|
|
|
Faction faction = entry.getValue();
|
|
|
|
coll.attach(faction, factionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as migrated
|
|
|
|
oldFile.renameTo(newFile);
|
|
|
|
}
|
2014-11-14 08:41:58 +01:00
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
*/
|
2012-11-28 06:51:37 +01:00
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|