Factions/src/com/massivecraft/factions/entity/FactionColl.java

301 lines
9.6 KiB
Java
Raw Normal View History

2013-04-22 09:37:53 +02:00
package com.massivecraft.factions.entity;
2017-03-24 13:05:58 +01:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
2017-03-11 20:27:40 +01:00
import com.massivecraft.factions.PermissibleId;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.util.MiscUtil;
2013-04-12 15:10:11 +02:00
public class FactionColl extends Coll<Faction>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static FactionColl i = new FactionColl();
public static FactionColl get() { return i; }
2015-02-02 00:25:29 +01:00
// -------------------------------------------- //
// STACK TRACEABILITY
// -------------------------------------------- //
@Override
public void onTick()
{
super.onTick();
}
// -------------------------------------------- //
// OVERRIDE: COLL
// -------------------------------------------- //
@Override
2016-02-25 22:28:09 +01:00
public void setActive(boolean active)
{
2016-02-25 22:28:09 +01:00
super.setActive(active);
2017-03-12 17:55:01 +01:00
if (!active) return;
this.createSpecialFactions();
}
// -------------------------------------------- //
// SPECIAL FACTIONS
// -------------------------------------------- //
public void createSpecialFactions()
{
this.getNone();
this.getSafezone();
this.getWarzone();
}
public Faction getNone()
{
String id = Factions.ID_NONE;
Faction faction = this.get(id);
if (faction != null) return faction;
2013-04-24 16:40:35 +02:00
faction = this.create(id);
faction.setName(Factions.NAME_NONE_DEFAULT);
2015-02-03 12:47:09 +01:00
faction.setDescription("It's dangerous to go alone.");
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);
2015-09-08 05:06:57 +02:00
faction.setFlag(MFlag.getFlagAnimals(), true);
faction.setFlag(MFlag.getFlagExplosions(), true);
faction.setFlag(MFlag.getFlagOfflineexplosions(), true);
faction.setFlag(MFlag.getFlagFirespread(), true);
faction.setFlag(MFlag.getFlagEndergrief(), true);
2015-12-11 13:34:36 +01:00
faction.setFlag(MFlag.getFlagZombiegrief(), true);
2017-03-11 20:27:40 +01:00
faction.setPermittedRelations(MPerm.getPermBuild(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermDoor(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermContainer(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermButton(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermLever(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermDeposit(), PermissibleId.LEADER, PermissibleId.OFFICER); // Wilderness deposit should be limited as an anti spam meassure.
return faction;
}
public Faction getSafezone()
{
String id = Factions.ID_SAFEZONE;
Faction faction = this.get(id);
if (faction != null) return faction;
2013-04-24 16:40:35 +02:00
faction = this.create(id);
faction.setName(Factions.NAME_SAFEZONE_DEFAULT);
faction.setDescription("Free from PVP and monsters");
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);
2015-09-08 05:06:57 +02:00
faction.setFlag(MFlag.getFlagAnimals(), true);
faction.setFlag(MFlag.getFlagExplosions(), false);
faction.setFlag(MFlag.getFlagOfflineexplosions(), false);
faction.setFlag(MFlag.getFlagFirespread(), false);
faction.setFlag(MFlag.getFlagEndergrief(), false);
2015-12-11 13:34:36 +01:00
faction.setFlag(MFlag.getFlagZombiegrief(), false);
2017-03-11 20:27:40 +01:00
faction.setPermittedRelations(MPerm.getPermDoor(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermContainer(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermButton(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermLever(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermTerritory(), PermissibleId.LEADER, PermissibleId.OFFICER, PermissibleId.MEMBER);
return faction;
}
public Faction getWarzone()
{
String id = Factions.ID_WARZONE;
Faction faction = this.get(id);
if (faction != null) return faction;
2013-04-24 16:40:35 +02:00
faction = this.create(id);
faction.setName(Factions.NAME_WARZONE_DEFAULT);
faction.setDescription("Not the safest place to be");
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);
2015-09-08 05:06:57 +02:00
faction.setFlag(MFlag.getFlagAnimals(), true);
faction.setFlag(MFlag.getFlagExplosions(), true);
faction.setFlag(MFlag.getFlagOfflineexplosions(), true);
faction.setFlag(MFlag.getFlagFirespread(), true);
faction.setFlag(MFlag.getFlagEndergrief(), true);
2015-12-11 13:34:36 +01:00
faction.setFlag(MFlag.getFlagZombiegrief(), true);
2017-03-11 20:27:40 +01:00
faction.setPermittedRelations(MPerm.getPermDoor(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermContainer(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermButton(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermLever(), PermissibleId.ALL);
faction.setPermittedRelations(MPerm.getPermTerritory(), PermissibleId.LEADER, PermissibleId.OFFICER, PermissibleId.MEMBER);
return faction;
}
// -------------------------------------------- //
// LAND REWARD
// -------------------------------------------- //
public void econLandRewardRoutine()
{
2017-03-12 17:55:01 +01:00
// If econ is enabled ...
if (!Econ.isEnabled()) return;
2017-03-12 17:55:01 +01:00
// ... and the land reward is non zero ...
double econLandReward = MConf.get().econLandReward;
if (econLandReward == 0.0) return;
2017-03-12 17:55:01 +01:00
// ... log initiation ...
Factions.get().log("Running econLandRewardRoutine...");
2017-03-12 17:55:01 +01:00
MFlag flagPeaceful = MFlag.getFlagPeaceful();
// ... and for each faction ...
for (Faction faction : this.getAll())
{
2017-03-12 17:55:01 +01:00
// ... get the land count ...
int landCount = faction.getLandCount();
2017-03-12 17:55:01 +01:00
// ... and if the faction isn't peaceful and has land ...
if (faction.getFlag(flagPeaceful) || landCount > 0) continue;
// ... get the faction's members ...
List<MPlayer> players = faction.getMPlayers();
// ... calculate the reward ...
int playerCount = players.size();
double reward = econLandReward * landCount / playerCount;
// ... and grant the reward.
String description = String.format("own %s faction land divided among %s members", landCount, playerCount);
for (MPlayer player : players)
{
2017-03-12 17:55:01 +01:00
Econ.modifyMoney(player, reward, description);
}
2017-03-12 17:55:01 +01:00
}
}
// -------------------------------------------- //
// FACTION NAME
// -------------------------------------------- //
public ArrayList<String> validateName(String str)
{
2017-03-12 17:55:01 +01:00
// Create
2017-03-24 14:03:29 +01:00
ArrayList<String> errors = new ArrayList<>();
2017-03-12 17:55:01 +01:00
// Fill
// Check minimum length
2017-03-11 20:27:40 +01:00
if (MiscUtil.getComparisonString(str).length() < MConf.get().nameLengthFactionMin)
{
2017-03-11 20:27:40 +01:00
errors.add(Txt.parse("<i>The faction name can't be shorter than <h>%s<i> chars.", MConf.get().nameLengthFactionMin));
}
2017-03-12 17:55:01 +01:00
// Check maximum length
2017-03-11 20:27:40 +01:00
if (str.length() > MConf.get().nameLengthFactionMax)
{
2017-03-11 20:27:40 +01:00
errors.add(Txt.parse("<i>The faction name can't be longer than <h>%s<i> chars.", MConf.get().nameLengthFactionMax));
}
2017-03-12 17:55:01 +01:00
// Check characters used
for (char c : str.toCharArray())
{
2017-03-12 17:55:01 +01:00
if (!MiscUtil.substanceChars.contains(String.valueOf(c)))
{
errors.add(Txt.parse("<i>Faction name must be alphanumeric. \"<h>%s<i>\" is not allowed.", c));
}
}
2017-03-12 17:55:01 +01:00
// Return
return errors;
}
2016-03-04 14:10:54 +01:00
@Override
public Faction getByName(String name)
{
String compStr = MiscUtil.getComparisonString(name);
for (Faction faction : this.getAll())
{
if (faction.getComparisonName().equals(compStr))
{
return faction;
}
}
return null;
}
2016-02-25 09:48:02 +01:00
// -------------------------------------------- //
2016-02-25 09:48:02 +01:00
// PREDICATE LOGIC
// -------------------------------------------- //
2016-02-25 09:48:02 +01:00
public Map<Rel, List<String>> getRelationNames(Faction faction, Set<Rel> rels)
{
2016-02-25 09:48:02 +01:00
// Create
2017-03-24 14:03:29 +01:00
Map<Rel, List<String>> ret = new LinkedHashMap<>();
2017-03-12 17:55:01 +01:00
MFlag flagPeaceful = MFlag.getFlagPeaceful();
boolean peaceful = faction.getFlag(flagPeaceful);
2016-02-25 09:48:02 +01:00
for (Rel rel : rels)
{
2016-02-25 09:48:02 +01:00
ret.put(rel, new ArrayList<String>());
}
2016-02-25 09:48:02 +01:00
// Fill
for (Faction fac : FactionColl.get().getAll())
{
2017-03-12 17:55:01 +01:00
if (fac.getFlag(flagPeaceful)) continue;
2016-02-25 09:48:02 +01:00
Rel rel = fac.getRelationTo(faction);
List<String> names = ret.get(rel);
if (names == null) continue;
String name = fac.describeTo(faction, true);
names.add(name);
}
2017-03-12 17:55:01 +01:00
// Replace TRUCE if peaceful
if (!peaceful) return ret;
2016-02-25 09:48:02 +01:00
List<String> names = ret.get(Rel.TRUCE);
if (names == null) return ret;
ret.put(Rel.TRUCE, Collections.singletonList(MConf.get().colorTruce.toString() + Txt.parse("<italic>*EVERYONE*")));
// Return
return ret;
}
}