Moving files around

This commit is contained in:
Olof Larsson
2013-04-22 09:37:53 +02:00
parent ae61ef4c37
commit 41bb3f6e05
92 changed files with 367 additions and 344 deletions

View File

@@ -0,0 +1,320 @@
package com.massivecraft.factions.entity;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap;
import org.bukkit.ChatColor;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.integration.LWCFeatures;
import com.massivecraft.factions.util.AsciiCompass;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.Txt;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class Board extends Entity<Board> implements BoardInterface
{
public static final transient Type MAP_TYPE = new TypeToken<Map<PS, TerritoryAccess>>(){}.getType();
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static Board get(Object oid)
{
return BoardColl.get().get(oid);
}
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
@Override
public Board load(Board that)
{
this.map = that.map;
return this;
}
@Override
public boolean isDefault()
{
if (this.map == null) return true;
if (this.map.isEmpty()) return true;
return false;
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// TODO: Make TerritoryAccess immutable.
private ConcurrentSkipListMap<PS, TerritoryAccess> map;
public Map<PS, TerritoryAccess> getMap() { return Collections.unmodifiableMap(this.map); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public Board()
{
this.map = new ConcurrentSkipListMap<PS, TerritoryAccess>();
}
public Board(Map<PS, TerritoryAccess> map)
{
this.map = new ConcurrentSkipListMap<PS, TerritoryAccess>(map);
}
// -------------------------------------------- //
// OVERRIDE: BOARD
// -------------------------------------------- //
// GET
@Override
public TerritoryAccess getTerritoryAccessAt(PS ps)
{
if (ps == null) return null;
ps = ps.getChunkCoords(true);
TerritoryAccess ret = this.map.get(ps);
if (ret == null) ret = new TerritoryAccess(Const.FACTIONID_NONE);
return ret;
}
@Override
public Faction getFactionAt(PS ps)
{
if (ps == null) return null;
return this.getTerritoryAccessAt(ps).getHostFaction();
}
// SET
@Override
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
{
ps = ps.getChunkCoords(true);
if (territoryAccess == null || (territoryAccess.getHostFactionId().equals(Const.FACTIONID_NONE) && territoryAccess.isDefault()))
{
// TODO: Listen to an event instead!
// NOTE: And this is probably the place where the event should be triggered!
if (ConfServer.onUnclaimResetLwcLocks && LWCFeatures.getEnabled())
{
LWCFeatures.clearAllProtections(ps);
}
this.map.remove(ps);
}
else
{
this.map.put(ps, territoryAccess);
}
this.changed();
}
@Override
public void setFactionAt(PS ps, Faction faction)
{
TerritoryAccess territoryAccess = null;
if (faction != null)
{
territoryAccess = new TerritoryAccess(faction.getId());
}
this.setTerritoryAccessAt(ps, territoryAccess);
}
// REMOVE
@Override
public void removeAt(PS ps)
{
this.setTerritoryAccessAt(ps, null);
}
@Override
public void removeAll(Faction faction)
{
String factionId = faction.getId();
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
{
TerritoryAccess territoryAccess = entry.getValue();
if ( ! territoryAccess.getHostFactionId().equals(factionId)) continue;
PS ps = entry.getKey();
this.removeAt(ps);
}
}
// Removes orphaned foreign keys
@Override
public void clean()
{
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
{
TerritoryAccess territoryAccess = entry.getValue();
if (FactionColl.get().containsId(territoryAccess.getHostFactionId())) continue;
PS ps = entry.getKey();
this.removeAt(ps);
Factions.get().log("Board cleaner removed "+territoryAccess.getHostFactionId()+" from "+entry.getKey());
}
}
// COUNT
@Override
public int getCount(Faction faction)
{
return this.getCount(faction.getId());
}
public int getCount(String factionId)
{
int ret = 0;
for (TerritoryAccess territoryAccess : this.map.values())
{
if(territoryAccess.getHostFactionId().equals(factionId)) ret += 1;
}
return ret;
}
// NEARBY DETECTION
// Is this coord NOT completely surrounded by coords claimed by the same faction?
// Simpler: Is there any nearby coord with a faction other than the faction here?
@Override
public boolean isBorderPs(PS ps)
{
PS nearby = null;
Faction faction = this.getFactionAt(ps);
nearby = ps.withChunkX(ps.getChunkX() +1);
if (faction != this.getFactionAt(nearby)) return true;
nearby = ps.withChunkX(ps.getChunkX() -1);
if (faction != this.getFactionAt(nearby)) return true;
nearby = ps.withChunkZ(ps.getChunkZ() +1);
if (faction != this.getFactionAt(nearby)) return true;
nearby = ps.withChunkZ(ps.getChunkZ() -1);
if (faction != this.getFactionAt(nearby)) return true;
return false;
}
// Is this coord connected to any coord claimed by the specified faction?
@Override
public boolean isConnectedPs(PS ps, Faction faction)
{
PS nearby = null;
nearby = ps.withChunkX(ps.getChunkX() +1);
if (faction == this.getFactionAt(nearby)) return true;
nearby = ps.withChunkX(ps.getChunkX() -1);
if (faction == this.getFactionAt(nearby)) return true;
nearby = ps.withChunkZ(ps.getChunkZ() +1);
if (faction == this.getFactionAt(nearby)) return true;
nearby = ps.withChunkZ(ps.getChunkZ() -1);
if (faction == this.getFactionAt(nearby)) return true;
return false;
}
// MAP GENERATION
@Override
public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees)
{
centerPs = centerPs.getChunkCoords(true);
ArrayList<String> ret = new ArrayList<String>();
Faction centerFaction = this.getFactionAt(centerPs);
ret.add(Txt.titleize("("+centerPs.getChunkX() + "," + centerPs.getChunkZ()+") "+centerFaction.getTag(observer)));
int halfWidth = Const.MAP_WIDTH / 2;
int halfHeight = Const.MAP_HEIGHT / 2;
PS topLeftPs = centerPs.plusChunkCoords(-halfWidth, -halfHeight);
int width = halfWidth * 2 + 1;
int height = halfHeight * 2 + 1;
//Make room for the list of tags
height--;
Map<Faction, Character> fList = new HashMap<Faction, Character>();
int chrIdx = 0;
// For each row
for (int dz = 0; dz < height; dz++)
{
// Draw and add that row
String row = "";
for (int dx = 0; dx < width; dx++)
{
if(dx == halfWidth && dz == halfHeight)
{
row += ChatColor.AQUA+"+";
continue;
}
PS herePs = topLeftPs.plusChunkCoords(dx, dz);
Faction hereFaction = this.getFactionAt(herePs);
if (hereFaction.isNone())
{
row += ChatColor.GRAY+"-";
}
else
{
if (!fList.containsKey(hereFaction))
fList.put(hereFaction, Const.MAP_KEY_CHARS[chrIdx++]);
char fchar = fList.get(hereFaction);
row += hereFaction.getColorTo(observer) + "" + fchar;
}
}
ret.add(row);
}
// Get the compass
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Txt.parse("<a>"));
// Add the compass
ret.set(1, asciiCompass.get(0)+ret.get(1).substring(3*3));
ret.set(2, asciiCompass.get(1)+ret.get(2).substring(3*3));
ret.set(3, asciiCompass.get(2)+ret.get(3).substring(3*3));
String fRow = "";
for (Faction keyfaction : fList.keySet())
{
fRow += ""+keyfaction.getColorTo(observer) + fList.get(keyfaction) + ": " + keyfaction.getTag() + " ";
}
fRow = fRow.trim();
ret.add(fRow);
return ret;
}
}

View File

@@ -0,0 +1,208 @@
package com.massivecraft.factions.entity;
import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.ps.PSBuilder;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.MStore;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class BoardColl extends Coll<Board> implements BoardInterface
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static BoardColl i = new BoardColl();
public static BoardColl get() { return i; }
private BoardColl()
{
super(Const.COLLECTION_BASENAME_BOARD, Board.class, MStore.getDb(ConfServer.dburi), Factions.get(), true, true);
}
// -------------------------------------------- //
// OVERRIDE: COLL
// -------------------------------------------- //
@Override
public String fixId(Object oid)
{
if (oid == null) return null;
if (oid instanceof String) return (String)oid;
if (oid instanceof Board) return this.getId(oid);
return MUtil.extract(String.class, "worldName", oid);
}
@Override
public void init()
{
super.init();
this.migrate();
}
public void migrate()
{
// Create file objects
File oldFile = new File(Factions.get().getDataFolder(), "board.json");
File newFile = new File(Factions.get().getDataFolder(), "board.json.migrated");
// Already migrated?
if ( ! oldFile.exists()) return;
// Read the file content through GSON.
Type type = new TypeToken<Map<String,Map<String,TerritoryAccess>>>(){}.getType();
Map<String,Map<String,TerritoryAccess>> worldCoordIds = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
// Set the data
for (Entry<String,Map<String,TerritoryAccess>> entry : worldCoordIds.entrySet())
{
String worldName = entry.getKey();
Board board = this.get(worldName);
for (Entry<String,TerritoryAccess> entry2 : entry.getValue().entrySet())
{
String[] ChunkCoordParts = entry2.getKey().trim().split("[,\\s]+");
int chunkX = Integer.parseInt(ChunkCoordParts[0]);
int chunkZ = Integer.parseInt(ChunkCoordParts[1]);
PS ps = new PSBuilder().chunkX(chunkX).chunkZ(chunkZ).build();
TerritoryAccess territoryAccess = entry2.getValue();
board.setTerritoryAccessAt(ps, territoryAccess);
}
}
// Mark as migrated
oldFile.renameTo(newFile);
}
// -------------------------------------------- //
// OVERRIDE: BOARD
// -------------------------------------------- //
@Override
public TerritoryAccess getTerritoryAccessAt(PS ps)
{
if (ps == null) return null;
Board board = this.get(ps.getWorld());
if (board == null) return null;
return board.getTerritoryAccessAt(ps);
}
@Override
public Faction getFactionAt(PS ps)
{
if (ps == null) return null;
Board board = this.get(ps.getWorld());
if (board == null) return null;
return board.getFactionAt(ps);
}
// SET
@Override
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
{
if (ps == null) return;
Board board = this.get(ps.getWorld());
if (board == null) return;
board.setTerritoryAccessAt(ps, territoryAccess);
}
@Override
public void setFactionAt(PS ps, Faction faction)
{
if (ps == null) return;
Board board = this.get(ps.getWorld());
if (board == null) return;
board.setFactionAt(ps, faction);
}
// REMOVE
@Override
public void removeAt(PS ps)
{
if (ps == null) return;
Board board = this.get(ps.getWorld());
if (board == null) return;
board.removeAt(ps);
}
@Override
public void removeAll(Faction faction)
{
for (Board board : this.getAll())
{
board.removeAll(faction);
}
}
@Override
public void clean()
{
for (Board board : this.getAll())
{
board.clean();
}
}
// COUNT
@Override
public int getCount(Faction faction)
{
int ret = 0;
for (Board board : this.getAll())
{
ret += board.getCount(faction);
}
return ret;
}
// NEARBY DETECTION
@Override
public boolean isBorderPs(PS ps)
{
if (ps == null) return false;
Board board = this.get(ps.getWorld());
if (board == null) return false;
return board.isBorderPs(ps);
}
@Override
public boolean isConnectedPs(PS ps, Faction faction)
{
if (ps == null) return false;
Board board = this.get(ps.getWorld());
if (board == null) return false;
return board.isConnectedPs(ps, faction);
}
// MAP GENERATION
@Override
public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees)
{
if (centerPs == null) return null;
Board board = this.get(centerPs.getWorld());
if (board == null) return null;
return board.getMap(observer, centerPs, inDegrees);
}
}

View File

@@ -0,0 +1,35 @@
package com.massivecraft.factions.entity;
import java.util.ArrayList;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.mcore.ps.PS;
public interface BoardInterface
{
// GET
public TerritoryAccess getTerritoryAccessAt(PS ps);
public Faction getFactionAt(PS ps);
// SET
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess);
public void setFactionAt(PS ps, Faction faction);
// REMOVE
public void removeAt(PS ps);
public void removeAll(Faction faction);
public void clean();
// COUNT
public int getCount(Faction faction);
// NEARBY DETECTION
public boolean isBorderPs(PS ps);
public boolean isConnectedPs(PS ps, Faction faction);
// MAP
// TODO: Could the degrees be embedded in centerPs yaw instead?
public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees);
}

View File

@@ -0,0 +1,881 @@
package com.massivecraft.factions.entity;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.EconomyParticipator;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Lang;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.event.FactionsEventLandClaim;
import com.massivecraft.factions.event.FactionsEventMembershipChange;
import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason;
import com.massivecraft.factions.event.FactionsEventPowerChange;
import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.LWCFeatures;
import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.integration.Worldguard;
import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.money.Money;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.SenderEntity;
import com.massivecraft.mcore.util.TimeUnit;
import com.massivecraft.mcore.util.Txt;
public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipator
{
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static FPlayer get(Object oid)
{
return FPlayerColl.get().get(oid);
}
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
@Override
public FPlayer load(FPlayer that)
{
this.setFactionId(that.factionId);
this.setRole(that.role);
this.setTitle(that.title);
this.setPowerBoost(that.powerBoost);
this.power = that.power;
this.lastPowerUpdateTime = that.lastPowerUpdateTime;
return this;
}
@Override
public boolean isDefault()
{
if (this.hasFaction()) return false;
// Note: we do not check role or title here since they mean nothing without a faction.
// TODO: This line looks obnoxious, investigate it.
if (this.getPowerRounded() != this.getPowerMaxRounded() && this.getPowerRounded() != (int) Math.round(ConfServer.powerStarting)) return false;
if (this.hasPowerBoost()) return false;
return true;
}
// -------------------------------------------- //
// FIELDS: RAW
// -------------------------------------------- //
// In this section of the source code we place the field declarations only.
// Each field has it's own section further down since just the getter and setter logic takes up quite some place.
// This is a foreign key.
// A players always belongs to a faction.
// If null the player belongs to the no-faction faction called Wilderness.
private String factionId = null;
// What role does the player have in the faction?
// The default value here is MEMBER since that one would be one of the most common ones and our goal is to save database space.
// A note to self is that we can not change it from member to anything else just because we feel like it, that would corrupt database content.
private Rel role = null;
// What title does the player have in the faction?
// The title is just for fun. It's completely meaningless.
// The default case is no title since it's what you start with and also the most common case.
// The player title is similar to the faction description.
//
// Question: Can the title contain chat colors?
// Answer: Yes but in such case the policy is that they already must be parsed using Txt.parse.
// If they contain markup it should not be parsed in case we coded the system correctly.
private String title = null;
// Player usually do not have a powerboost. It defaults to 0.
// The powerBoost is a custom increase/decrease to default and maximum power.
// Note that player powerBoost and faction powerBoost are very similar.
private Double powerBoost = null;
// This field contains the last calculated value of the players power.
// The power calculation is lazy which means that the power is calculated first when you try to view the value.
private double power;
// This is the timestamp for the last calculation of the power.
// The value is used for the lazy calculation described above.
private long lastPowerUpdateTime;
// -------------------------------------------- //
// FIELDS: RAW TRANSIENT
// -------------------------------------------- //
// Where did this player stand the last time we checked?
private transient PS currentChunk = null;
public PS getCurrentChunk() { return this.currentChunk; }
public void setCurrentChunk(PS currentChunk) { this.currentChunk = currentChunk.getChunk(true); }
// FIELD: mapAutoUpdating
private transient boolean mapAutoUpdating = false;
public void setMapAutoUpdating(boolean mapAutoUpdating) { this.mapAutoUpdating = mapAutoUpdating; }
public boolean isMapAutoUpdating() { return mapAutoUpdating; }
// FIELD: autoClaimEnabled
private transient Faction autoClaimFor = null;
public Faction getAutoClaimFor() { return autoClaimFor; }
public void setAutoClaimFor(Faction faction) { this.autoClaimFor = faction; }
private transient boolean usingAdminMode = false;
public boolean isUsingAdminMode() { return this.usingAdminMode; }
public void setUsingAdminMode(boolean val) { this.usingAdminMode = val; }
// FIELD: loginPvpDisabled
//private transient boolean loginPvpDisabled;
// FIELD: account
public String getAccountId() { return this.getId(); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
// GSON need this noarg constructor.
public FPlayer()
{
this.resetFactionData(false);
this.power = ConfServer.powerStarting;
this.lastPowerUpdateTime = System.currentTimeMillis();
if ( ! ConfServer.newPlayerStartingFactionID.equals(Const.FACTIONID_NONE) && FactionColl.get().containsId(ConfServer.newPlayerStartingFactionID))
{
this.factionId = ConfServer.newPlayerStartingFactionID;
}
}
public final void resetFactionData(boolean doSpoutUpdate)
{
// TODO: Should we not rather use ConfServer.newPlayerStartingFactionID here?
// The default neutral faction
this.setFactionId(null);
this.setRole(null);
this.setTitle(null);
this.autoClaimFor = null;
if (doSpoutUpdate)
{
SpoutFeatures.updateTitle(this, null);
SpoutFeatures.updateTitle(null, this);
SpoutFeatures.updateCape(this.getPlayer(), null);
}
}
public void resetFactionData()
{
this.resetFactionData(true);
}
// -------------------------------------------- //
// FIELD: factionId
// -------------------------------------------- //
// This method never returns null
public String getFactionId()
{
if (this.factionId == null) return Const.FACTIONID_NONE;
return this.factionId;
}
// This method never returns null
public Faction getFaction()
{
Faction ret = FactionColl.get().get(this.getFactionId());
if (ret == null) ret = FactionColl.get().get(Const.FACTIONID_NONE);
return ret;
}
public boolean hasFaction()
{
return !this.getFactionId().equals(Const.FACTIONID_NONE);
}
// This setter is so long because it search for default/null case and takes care of updating the faction member index
public void setFactionId(String factionId)
{
// Avoid null input
if (factionId == null) factionId = Const.FACTIONID_NONE;
// Get the old value
String oldFactionId = this.getFactionId();
// Ignore nochange
if (factionId.equals(oldFactionId)) return;
// Apply change
if (factionId.equals(Const.FACTIONID_NONE))
{
this.factionId = null;
}
else
{
this.factionId = factionId;
}
// Next we must be attached and inited
if (!this.attached()) return;
if (!this.getColl().inited()) return;
if (!FactionColl.get().inited()) return;
// Spout Derp
SpoutFeatures.updateTitle(this, null);
SpoutFeatures.updateTitle(null, this);
// Update index
Faction oldFaction = FactionColl.get().get(oldFactionId);
Faction faction = FactionColl.get().get(factionId);
oldFaction.fplayers.remove(this);
faction.fplayers.add(this);
// Mark as changed
this.changed();
}
public void setFaction(Faction faction)
{
this.setFactionId(faction.getId());
}
// -------------------------------------------- //
// FIELD: role
// -------------------------------------------- //
public Rel getRole()
{
if (this.role == null) return Rel.MEMBER;
return this.role;
}
public void setRole(Rel role)
{
if (role == null || role == Rel.MEMBER)
{
this.role = null;
}
else
{
this.role = role;
}
SpoutFeatures.updateTitle(this, null);
this.changed();
}
// -------------------------------------------- //
// FIELD: title
// -------------------------------------------- //
public boolean hasTitle()
{
return this.title != null;
}
public String getTitle()
{
if (this.hasTitle()) return this.title;
return Lang.PLAYER_NOTITLE;
}
public void setTitle(String title)
{
if (title != null)
{
title = title.trim();
if (title.length() == 0)
{
title = null;
}
}
this.title = title;
this.changed();
}
// -------------------------------------------- //
// FIELD: powerBoost
// -------------------------------------------- //
public double getPowerBoost()
{
Double ret = this.powerBoost;
if (ret == null) ret = 0D;
return ret;
}
public void setPowerBoost(Double powerBoost)
{
if (powerBoost == null || powerBoost == 0)
{
powerBoost = null;
}
this.powerBoost = powerBoost;
this.changed();
}
public boolean hasPowerBoost()
{
return this.getPowerBoost() != 0D;
}
// -------------------------------------------- //
// FIELD: lastPowerUpdateTime
// -------------------------------------------- //
// RAW
public long getLastPowerUpdateTime()
{
return this.lastPowerUpdateTime;
}
public void setLastPowerUpdateTime(long lastPowerUpdateTime)
{
this.lastPowerUpdateTime = lastPowerUpdateTime;
this.changed();
}
// -------------------------------------------- //
// FIELD: power
// -------------------------------------------- //
// RAW
public double getPower()
{
this.recalculatePower();
return this.power;
}
public void setPower(double power)
{
this.setPower(power, System.currentTimeMillis());
}
public void setPower(double power, long now)
{
power = Math.min(power, this.getPowerMax());
power = Math.max(power, this.getPowerMin());
// Nochange
if (this.power == power) return;
this.power = power;
this.setLastPowerUpdateTime(now);
this.changed();
}
public double getPowerMax()
{
return ConfServer.powerMax + this.getPowerBoost();
}
public double getPowerMin()
{
return ConfServer.powerMin + this.getPowerBoost();
}
public void recalculatePower()
{
this.recalculatePower(this.isOnline());
}
private static final transient long POWER_RECALCULATION_MINIMUM_WAIT_MILLIS = 10 * TimeUnit.MILLIS_PER_SECOND;
public void recalculatePower(boolean online)
{
// Is the player really on this server?
// We use the sender ps mixin to fetch the current player location.
// If the PS is null it's OK. We assume the player is here if we do not know.
PS ps = Mixin.getSenderPs(this.getId());
if (ps != null && !ps.isWorldLoadedOnThisServer()) return;
// Get the now
long now = System.currentTimeMillis();
// We will only update if a certain amount of time has passed.
if (this.getLastPowerUpdateTime() + POWER_RECALCULATION_MINIMUM_WAIT_MILLIS >= now) return;
// Calculate millis passed
long millisPassed = now - this.getLastPowerUpdateTime();
// Note that we updated
this.setLastPowerUpdateTime(now);
// We consider dead players to be offline.
if (online)
{
Player thisPlayer = this.getPlayer();
if (thisPlayer != null && thisPlayer.isDead())
{
online = false;
}
}
// Depending on online state pick the config values
double powerPerHour = online ? ConfServer.powerPerHourOnline : ConfServer.powerPerHourOffline;
double powerLimitGain = online ? ConfServer.powerLimitGainOnline : ConfServer.powerLimitGainOffline;
double powerLimitLoss = online ? ConfServer.powerLimitLossOnline : ConfServer.powerLimitLossOffline;
// Apply the negative divisor thingy
if (ConfServer.scaleNegativePower && this.power < 0)
{
powerPerHour += (Math.sqrt(Math.abs(this.power)) * Math.abs(this.power)) / ConfServer.scaleNegativeDivisor;
}
// Calculate delta and target
double powerDelta = powerPerHour * millisPassed / TimeUnit.MILLIS_PER_HOUR;
double powerTarget = this.power + powerDelta;
// Check Gain and Loss limits
if (powerDelta >= 0)
{
// Gain
if (powerTarget > powerLimitGain)
{
if (this.power > powerLimitGain)
{
// Did already cross --> Just freeze
powerTarget = this.power;
}
else
{
// Crossing right now --> Snap to limit
powerTarget = powerLimitGain;
}
}
}
else
{
// Loss
if (powerTarget < powerLimitLoss)
{
if (this.power < powerLimitLoss)
{
// Did already cross --> Just freeze
powerTarget = this.power;
}
else
{
// Crossing right now --> Snap to limit
powerTarget = powerLimitLoss;
}
}
}
FactionsEventPowerChange event = new FactionsEventPowerChange(null, this, PowerChangeReason.TIME, powerTarget);
event.run();
if (event.isCancelled()) return;
powerTarget = event.getNewPower();
this.setPower(powerTarget, now);
}
// FINER
public int getPowerRounded()
{
return (int) Math.round(this.getPower());
}
public int getPowerMaxRounded()
{
return (int) Math.round(this.getPowerMax());
}
public int getPowerMinRounded()
{
return (int) Math.round(this.getPowerMin());
}
// -------------------------------------------- //
// TITLE, NAME, FACTION TAG AND CHAT
// -------------------------------------------- //
public String getName()
{
return this.getFixedId();
}
public String getTag()
{
if ( ! this.hasFaction())
{
return "";
}
return this.getFaction().getTag();
}
// Base concatenations:
public String getNameAndSomething(String something)
{
String ret = this.role.getPrefix();
if (something.length() > 0)
{
ret += something+" ";
}
ret += this.getName();
return ret;
}
public String getNameAndTitle()
{
if (this.hasTitle())
{
return this.getNameAndSomething(this.getTitle());
}
else
{
return this.getName();
}
}
public String getNameAndTag()
{
return this.getNameAndSomething(this.getTag());
}
// Colored concatenations:
// These are used in information messages
public String getNameAndTitle(Faction faction)
{
return this.getColorTo(faction)+this.getNameAndTitle();
}
public String getNameAndTitle(FPlayer fplayer)
{
return this.getColorTo(fplayer)+this.getNameAndTitle();
}
// Chat Tag:
// These are injected into the format of global chat messages.
public String getChatTag()
{
if ( ! this.hasFaction()) {
return "";
}
return String.format(ConfServer.chatTagFormat, this.role.getPrefix()+this.getTag());
}
// Colored Chat Tag
public String getChatTag(Faction faction)
{
if ( ! this.hasFaction()) {
return "";
}
return this.getRelationTo(faction).getColor()+getChatTag();
}
public String getChatTag(FPlayer fplayer)
{
if ( ! this.hasFaction())
{
return "";
}
return this.getColorTo(fplayer)+getChatTag();
}
// -------------------------------------------- //
// RELATION AND RELATION COLORS
// -------------------------------------------- //
@Override
public String describeTo(RelationParticipator observer, boolean ucfirst)
{
return RelationUtil.describeThatToMe(this, observer, ucfirst);
}
@Override
public String describeTo(RelationParticipator observer)
{
return RelationUtil.describeThatToMe(this, observer);
}
@Override
public Rel getRelationTo(RelationParticipator observer)
{
return RelationUtil.getRelationOfThatToMe(this, observer);
}
@Override
public Rel getRelationTo(RelationParticipator observer, boolean ignorePeaceful)
{
return RelationUtil.getRelationOfThatToMe(this, observer, ignorePeaceful);
}
public Rel getRelationToLocation()
{
// TODO: Use some built in system to get sender
return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())).getRelationTo(this);
}
@Override
public ChatColor getColorTo(RelationParticipator observer)
{
return RelationUtil.getColorOfThatToMe(this, observer);
}
// -------------------------------------------- //
// HEALTH
// -------------------------------------------- //
public void heal(int amnt)
{
Player player = this.getPlayer();
if (player == null)
{
return;
}
player.setHealth(player.getHealth() + amnt);
}
// -------------------------------------------- //
// TERRITORY
// -------------------------------------------- //
public boolean isInOwnTerritory()
{
// TODO: Use Mixin to get this PS instead
return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())) == this.getFaction();
}
public boolean isInEnemyTerritory()
{
// TODO: Use Mixin to get this PS instead
return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())).getRelationTo(this) == Rel.ENEMY;
}
public void sendFactionHereMessage()
{
if (SpoutFeatures.updateTerritoryDisplay(this))
{
return;
}
Faction factionHere = BoardColl.get().getFactionAt(this.getCurrentChunk());
String msg = Txt.parse("<i>")+" ~ "+factionHere.getTag(this);
if (factionHere.hasDescription())
{
msg += " - "+factionHere.getDescription();
}
this.sendMessage(msg);
}
// -------------------------------------------- //
// ACTIONS
// -------------------------------------------- //
public void leave(boolean makePay)
{
Faction myFaction = this.getFaction();
boolean permanent = myFaction.getFlag(FFlag.PERMANENT);
if (!permanent && this.getRole() == Rel.LEADER && myFaction.getFPlayers().size() > 1)
{
msg("<b>You must give the leader role to someone else first.");
return;
}
if (!ConfServer.canLeaveWithNegativePower && this.getPower() < 0)
{
msg("<b>You cannot leave until your power is positive.");
return;
}
// Event
FactionsEventMembershipChange membershipChangeEvent = new FactionsEventMembershipChange(sender, this, myFaction, MembershipChangeReason.LEAVE);
membershipChangeEvent.run();
if (membershipChangeEvent.isCancelled()) return;
// Am I the last one in the faction?
if (myFaction.getFPlayers().size() == 1)
{
// Transfer all money
if (Econ.isEnabled(this))
{
Econ.transferMoney(this, myFaction, this, Money.get(this));
}
}
if (myFaction.isNormal())
{
for (FPlayer fplayer : myFaction.getFPlayersWhereOnline(true))
{
fplayer.msg("%s<i> left %s<i>.", this.describeTo(fplayer, true), myFaction.describeTo(fplayer));
}
if (ConfServer.logFactionLeave)
Factions.get().log(this.getName()+" left the faction: "+myFaction.getTag());
}
this.resetFactionData();
if (myFaction.isNormal() && !permanent && myFaction.getFPlayers().isEmpty())
{
// Remove this faction
for (FPlayer fplayer : FPlayerColl.get().getAllOnline())
{
fplayer.msg("<i>%s<i> was disbanded.", myFaction.describeTo(fplayer, true));
}
myFaction.detach();
if (ConfServer.logFactionDisband)
Factions.get().log("The faction "+myFaction.getTag()+" ("+myFaction.getId()+") was disbanded due to the last player ("+this.getName()+") leaving.");
}
}
public boolean canClaimForFactionAtLocation(Faction forFaction, PS ps, boolean notifyFailure)
{
String error = null;
Faction myFaction = this.getFaction();
Faction currentFaction = BoardColl.get().getFactionAt(ps);
int ownedLand = forFaction.getLandCount();
if (ConfServer.worldGuardChecking && Worldguard.checkForRegionsInChunk(ps))
{
// Checks for WorldGuard regions in the chunk attempting to be claimed
error = Txt.parse("<b>This land is protected");
}
else if (ConfServer.worldsNoClaiming.contains(ps.getWorld()))
{
error = Txt.parse("<b>Sorry, this world has land claiming disabled.");
}
else if (this.isUsingAdminMode())
{
return true;
}
else if (forFaction == currentFaction)
{
error = Txt.parse("%s<i> already own this land.", forFaction.describeTo(this, true));
}
else if ( ! FPerm.TERRITORY.has(this, forFaction, true))
{
return false;
}
else if (forFaction.getFPlayers().size() < ConfServer.claimsRequireMinFactionMembers)
{
error = Txt.parse("Factions must have at least <h>%s<b> members to claim land.", ConfServer.claimsRequireMinFactionMembers);
}
else if (ownedLand >= forFaction.getPowerRounded())
{
error = Txt.parse("<b>You can't claim more land! You need more power!");
}
else if (ConfServer.claimedLandsMax != 0 && ownedLand >= ConfServer.claimedLandsMax && ! forFaction.getFlag(FFlag.INFPOWER))
{
error = Txt.parse("<b>Limit reached. You can't claim more land!");
}
else if ( ! ConfServer.claimingFromOthersAllowed && currentFaction.isNormal())
{
error = Txt.parse("<b>You may not claim land from others.");
}
else if (currentFaction.getRelationTo(forFaction).isAtLeast(Rel.TRUCE) && ! currentFaction.isNone())
{
error = Txt.parse("<b>You can't claim this land due to your relation with the current owner.");
}
else if
(
ConfServer.claimsMustBeConnected
&& ! this.isUsingAdminMode()
&& myFaction.getLandCountInWorld(ps.getWorld()) > 0
&& !BoardColl.get().isConnectedPs(ps, myFaction)
&& (!ConfServer.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal())
)
{
if (ConfServer.claimsCanBeUnconnectedIfOwnedByOtherFaction)
error = Txt.parse("<b>You can only claim additional land which is connected to your first claim or controlled by another faction!");
else
error = Txt.parse("<b>You can only claim additional land which is connected to your first claim!");
}
else if (currentFaction.isNormal())
{
if ( ! currentFaction.hasLandInflation())
{
// TODO more messages WARN current faction most importantly
error = Txt.parse("%s<i> owns this land and is strong enough to keep it.", currentFaction.getTag(this));
}
else if ( ! BoardColl.get().isBorderPs(ps))
{
error = Txt.parse("<b>You must start claiming land at the border of the territory.");
}
}
if (notifyFailure && error != null)
{
msg(error);
}
return error == null;
}
// notifyFailure is false if called by auto-claim; no need to notify on every failure for it
// return value is false on failure, true on success
public boolean attemptClaim(Faction forFaction, PS psChunk, boolean notifyFailure)
{
psChunk = psChunk.getChunk(true);
Faction currentFaction = BoardColl.get().getFactionAt(psChunk);
int ownedLand = forFaction.getLandCount();
if ( ! this.canClaimForFactionAtLocation(forFaction, psChunk, notifyFailure)) return false;
// Event
FactionsEventLandClaim event = new FactionsEventLandClaim(sender, forFaction, psChunk);
event.run();
if (event.isCancelled()) return false;
// then make 'em pay (if applicable)
// TODO: The economy integration should cancel the event above!
// Calculate the cost to claim the area
double cost = Econ.calculateClaimCost(ownedLand, currentFaction.isNormal());
if (ConfServer.econClaimUnconnectedFee != 0.0 && forFaction.getLandCountInWorld(psChunk.getWorld()) > 0 && !BoardColl.get().isConnectedPs(psChunk, forFaction))
{
cost += ConfServer.econClaimUnconnectedFee;
}
if (Econ.payForAction(cost, this, "claim this land")) return false;
// TODO: The LWC integration should listen to Monitor for the claim event.
if (LWCFeatures.getEnabled() && forFaction.isNormal() && ConfServer.onCaptureResetLwcLocks)
{
LWCFeatures.clearOtherProtections(psChunk, this.getFaction());
}
// announce success
Set<FPlayer> informTheseFPlayers = new HashSet<FPlayer>();
informTheseFPlayers.add(this);
informTheseFPlayers.addAll(forFaction.getFPlayersWhereOnline(true));
for (FPlayer fp : informTheseFPlayers)
{
fp.msg("<h>%s<i> claimed land for <h>%s<i> from <h>%s<i>.", this.describeTo(fp, true), forFaction.describeTo(fp), currentFaction.describeTo(fp));
}
BoardColl.get().setFactionAt(psChunk, forFaction);
SpoutFeatures.updateTerritoryDisplayLoc(psChunk);
if (ConfServer.logLandClaims)
Factions.get().log(this.getName()+" claimed land at ("+psChunk.getChunkX()+","+psChunk.getChunkZ()+") for the faction: "+forFaction.getTag());
return true;
}
}

View File

@@ -0,0 +1,152 @@
package com.massivecraft.factions.entity;
import java.io.File;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Map.Entry;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.store.MStore;
import com.massivecraft.mcore.store.SenderColl;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.util.TimeUnit;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class FPlayerColl extends SenderColl<FPlayer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static FPlayerColl i = new FPlayerColl();
public static FPlayerColl get() { return i; }
private FPlayerColl()
{
super(Const.COLLECTION_BASENAME_PLAYER, FPlayer.class, MStore.getDb(ConfServer.dburi), Factions.get());
}
// -------------------------------------------- //
// OVERRIDE: COLL
// -------------------------------------------- //
// TODO: Init and migration routine!
@Override
public void init()
{
super.init();
this.migrate();
}
public void migrate()
{
// Create file objects
File oldFile = new File(Factions.get().getDataFolder(), "players.json");
File newFile = new File(Factions.get().getDataFolder(), "players.json.migrated");
// Already migrated?
if ( ! oldFile.exists()) return;
// Read the file content through GSON.
Type type = new TypeToken<Map<String, FPlayer>>(){}.getType();
Map<String, FPlayer> id2fplayer = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
// Set the data
for (Entry<String, FPlayer> entry : id2fplayer.entrySet())
{
String playerId = entry.getKey();
FPlayer fplayer = entry.getValue();
FPlayerColl.get().create(playerId).load(fplayer);
}
// Mark as migrated
oldFile.renameTo(newFile);
}
@Override
protected synchronized String attach(FPlayer entity, Object oid, boolean noteChange)
{
String ret = super.attach(entity, oid, noteChange);
// If inited ...
if (!this.inited()) return ret;
if (!FactionColl.get().inited()) return ret;
// ... update the index.
Faction faction = entity.getFaction();
faction.fplayers.add(entity);
return ret;
}
@Override
public FPlayer detachId(Object oid)
{
FPlayer ret = super.detachId(oid);
if (ret == null) return null;
// If inited ...
if (!this.inited()) return ret;
// ... update the index.
Faction faction = ret.getFaction();
faction.fplayers.remove(ret);
return ret;
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public void clean()
{
for (FPlayer fplayer : this.getAll())
{
if (FactionColl.get().containsId(fplayer.getFactionId())) continue;
Factions.get().log("Reset faction data (invalid faction) for player "+fplayer.getName());
fplayer.resetFactionData(false);
}
}
public void autoLeaveOnInactivityRoutine()
{
if (ConfServer.autoLeaveAfterDaysOfInactivity <= 0.0) return;
long now = System.currentTimeMillis();
double toleranceMillis = ConfServer.autoLeaveAfterDaysOfInactivity * TimeUnit.MILLIS_PER_DAY;
for (FPlayer fplayer : this.getAll())
{
Long lastPlayed = Mixin.getLastPlayed(fplayer.getId());
if (lastPlayed == null) continue;
if (fplayer.isOnline()) continue;
if (now - lastPlayed <= toleranceMillis) continue;
if (ConfServer.logFactionLeave || ConfServer.logFactionKick)
{
Factions.get().log("Player "+fplayer.getName()+" was auto-removed due to inactivity.");
}
// if player is faction leader, sort out the faction since he's going away
if (fplayer.getRole() == Rel.LEADER)
{
Faction faction = fplayer.getFaction();
if (faction != null)
{
fplayer.getFaction().promoteNewLeader();
}
}
fplayer.leave(false);
fplayer.detach();
}
}
}

View File

@@ -0,0 +1,918 @@
package com.massivecraft.factions.entity;
import java.util.*;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.EconomyParticipator;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.FactionEqualsPredictate;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Lang;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.util.*;
import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.SenderUtil;
import com.massivecraft.mcore.xlib.gson.annotations.SerializedName;
public class Faction extends Entity<Faction> implements EconomyParticipator
{
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static Faction get(Object oid)
{
return FactionColl.get().get(oid);
}
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
@Override
public Faction load(Faction that)
{
this.tag = that.tag;
this.setDescription(that.description);
this.home = that.home;
this.setPowerBoost(that.powerBoost);
this.cape = that.cape;
this.open = that.open;
this.setInvitedPlayerIds(that.invitedPlayerIds);
this.setRelationWishes(that.relationWish);
this.setFlags(that.flagOverrides);
this.setPerms(that.permOverrides);
return this;
}
// -------------------------------------------- //
// FIELDS: RAW
// -------------------------------------------- //
// In this section of the source code we place the field declarations only.
// Each field has it's own section further down since just the getter and setter logic takes up quite some place.
// TODO: The faction "tag" could/should also have been called "name".
// The actual faction id looks something like "54947df8-0e9e-4471-a2f9-9af509fb5889" and that is not too easy to remember for humans.
// Thus we make use of a name. Since the id is used in all foreign key situations changing the name is fine.
private String tag = null;
// Factions can optionally set a description for themselves.
// This description can for example be seen in territorial alerts.
private String description = null;
// Factions can optionally set a home location.
// If they do their members can teleport there using /f home
private PS home = null;
// Factions usually do not have a powerboost. It defaults to 0.
// The powerBoost is a custom increase/decrease to default and maximum power.
private Double powerBoost = null;
// The cape field is used by the Spout integration features.
// It's the URL to the faction cape.
private String cape = null;
// Can anyone join the Faction?
// If the faction is open they can.
// If the faction is closed an invite is required.
private Boolean open = null;
// This is the ids of the invited players.
// They are actually "senderIds" since you can invite "@console" to your faction.
@SerializedName("invites")
private Set<String> invitedPlayerIds = null;
// The keys in this map are factionIds.
private Map<String, Rel> relationWish = null;
// The flag overrides are modifications to the default values.
private Map<FFlag, Boolean> flagOverrides = null;
// The perm overrides are modifications to the default values.
private Map<FPerm, Set<Rel>> permOverrides = null;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public Faction()
{
}
// -------------------------------------------- //
// FIELD: id
// -------------------------------------------- //
// FINER
public boolean isNone()
{
return this.getId().equals(Const.FACTIONID_NONE);
}
public boolean isNormal()
{
return ! this.isNone();
}
// This is the bank account id used by external money-plugins
@Override
public String getAccountId()
{
String accountId = "faction-"+this.getId();
return accountId;
}
// -------------------------------------------- //
// FIELD: tag
// -------------------------------------------- //
// TODO: Rename tag --> name ?
// RAW
public String getTag()
{
String ret = this.tag;
if (ConfServer.factionTagForceUpperCase)
{
ret = ret.toUpperCase();
}
return ret;
}
public void setTag(String str)
{
if (ConfServer.factionTagForceUpperCase)
{
str = str.toUpperCase();
}
this.tag = str;
this.changed();
}
// FINER
public String getComparisonTag()
{
return MiscUtil.getComparisonString(this.getTag());
}
public String getTag(String prefix)
{
return prefix + this.getTag();
}
public String getTag(RelationParticipator observer)
{
if (observer == null) return getTag();
return this.getTag(this.getColorTo(observer).toString());
}
// -------------------------------------------- //
// FIELD: description
// -------------------------------------------- //
// RAW
public boolean hasDescription()
{
return this.description != null;
}
public String getDescription()
{
if (this.hasDescription()) return this.description;
return Lang.FACTION_NODESCRIPTION;
}
public void setDescription(String description)
{
if (description != null)
{
description = description.trim();
// This code should be kept for a while to clean out the previous default text that was actually stored in the database.
if (description.length() == 0 || description.equalsIgnoreCase("Default faction description :("))
{
description = null;
}
}
this.description = description;
this.changed();
}
// -------------------------------------------- //
// FIELD: home
// -------------------------------------------- //
public PS getHome()
{
this.verifyHomeIsValid();
return this.home;
}
public void verifyHomeIsValid()
{
if (this.isValidHome(this.home)) return;
this.home = null;
msg("<b>Your faction home has been un-set since it is no longer in your territory.");
}
public boolean isValidHome(PS ps)
{
if (ps == null) return true;
if (!ConfServer.homesMustBeInClaimedTerritory) return true;
if (BoardColl.get().getFactionAt(ps) == this) return true;
return false;
}
public boolean hasHome()
{
return this.getHome() != null;
}
public void setHome(PS home)
{
this.home = home;
}
// -------------------------------------------- //
// FIELD: powerBoost
// -------------------------------------------- //
// RAW
public double getPowerBoost()
{
Double ret = this.powerBoost;
if (ret == null) ret = 0D;
return ret;
}
public void setPowerBoost(Double powerBoost)
{
if (powerBoost == null || powerBoost == 0)
{
powerBoost = null;
}
this.powerBoost = powerBoost;
this.changed();
}
public boolean hasPowerBoost()
{
return this.getPowerBoost() != 0D;
}
// -------------------------------------------- //
// FIELD: cape
// -------------------------------------------- //
public String getCape()
{
return cape;
}
public void setCape(String cape)
{
this.cape = cape;
SpoutFeatures.updateCape(this, null);
}
// -------------------------------------------- //
// FIELD: open
// -------------------------------------------- //
public boolean isOpen()
{
Boolean ret = this.open;
if (ret == null) ret = ConfServer.newFactionsDefaultOpen;
return ret;
}
public void setOpen(Boolean open)
{
this.open = open;
this.changed();
}
// -------------------------------------------- //
// FIELD: invitedPlayerIds
// -------------------------------------------- //
// RAW
public TreeSet<String> getInvitedPlayerIds()
{
TreeSet<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
if (this.invitedPlayerIds != null) ret.addAll(this.invitedPlayerIds);
return ret;
}
public void setInvitedPlayerIds(Collection<String> invitedPlayerIds)
{
if (invitedPlayerIds == null || invitedPlayerIds.isEmpty())
{
this.invitedPlayerIds = null;
}
else
{
TreeSet<String> target = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String invitedPlayerId : invitedPlayerIds)
{
target.add(invitedPlayerId.toLowerCase());
}
this.invitedPlayerIds = target;
}
this.changed();
}
// FINER
public boolean isInvited(String playerId)
{
return this.getInvitedPlayerIds().contains(playerId);
}
public boolean isInvited(FPlayer fplayer)
{
return this.isInvited(fplayer.getId());
}
public boolean setInvited(String playerId, boolean invited)
{
TreeSet<String> invitedPlayerIds = this.getInvitedPlayerIds();
boolean ret;
if (invited)
{
ret = invitedPlayerIds.add(playerId.toLowerCase());
}
else
{
ret = invitedPlayerIds.remove(playerId.toLowerCase());
}
this.setInvitedPlayerIds(invitedPlayerIds);
return ret;
}
public void setInvited(FPlayer fplayer, boolean invited)
{
this.setInvited(fplayer.getId(), invited);
}
// -------------------------------------------- //
// FIELD: relationWish
// -------------------------------------------- //
// RAW
public Map<String, Rel> getRelationWishes()
{
Map<String, Rel> ret = new LinkedHashMap<String, Rel>();
if (this.relationWish != null) ret.putAll(this.relationWish);
return ret;
}
public void setRelationWishes(Map<String, Rel> relationWishes)
{
if (relationWishes == null || relationWishes.isEmpty())
{
this.relationWish = null;
}
else
{
this.relationWish = relationWishes;
}
this.changed();
}
// FINER
public Rel getRelationWish(String factionId)
{
Rel ret = this.getRelationWishes().get(factionId);
if (ret == null) ret = Rel.NEUTRAL;
return ret;
}
public Rel getRelationWish(Faction faction)
{
return this.getRelationWish(faction.getId());
}
public void setRelationWish(String factionId, Rel rel)
{
Map<String, Rel> relationWishes = this.getRelationWishes();
if (rel == null || rel == Rel.NEUTRAL)
{
relationWishes.remove(factionId);
}
else
{
relationWishes.put(factionId, rel);
}
this.setRelationWishes(relationWishes);
}
public void setRelationWish(Faction faction, Rel rel)
{
this.setRelationWish(faction.getId(), rel);
}
// TODO: What is this and where is it used?
public Map<Rel, List<String>> getFactionTagsPerRelation(RelationParticipator rp)
{
return getFactionTagsPerRelation(rp, false);
}
// onlyNonNeutral option provides substantial performance boost on large servers for listing only non-neutral factions
public Map<Rel, List<String>> getFactionTagsPerRelation(RelationParticipator rp, boolean onlyNonNeutral)
{
Map<Rel, List<String>> ret = new HashMap<Rel, List<String>>();
for (Rel rel : Rel.values())
{
ret.put(rel, new ArrayList<String>());
}
for (Faction faction : FactionColl.get().getAll())
{
Rel relation = faction.getRelationTo(this);
if (onlyNonNeutral && relation == Rel.NEUTRAL) continue;
ret.get(relation).add(faction.getTag(rp));
}
return ret;
}
// -------------------------------------------- //
// FIELD: flagOverrides
// -------------------------------------------- //
// RAW
public Map<FFlag, Boolean> getFlags()
{
Map<FFlag, Boolean> ret = new LinkedHashMap<FFlag, Boolean>();
for (FFlag fflag : FFlag.values())
{
ret.put(fflag, fflag.getDefault());
}
if (this.flagOverrides != null)
{
for (Entry<FFlag, Boolean> entry : this.flagOverrides.entrySet())
{
ret.put(entry.getKey(), entry.getValue());
}
}
return ret;
}
public void setFlags(Map<FFlag, Boolean> flags)
{
Map<FFlag, Boolean> target = new LinkedHashMap<FFlag, Boolean>();
if (flags != null)
{
target.putAll(flags);
}
Iterator<Entry<FFlag, Boolean>> iter = target.entrySet().iterator();
while (iter.hasNext())
{
Entry<FFlag, Boolean> entry = iter.next();
if (entry.getKey().getDefault() == entry.getValue())
{
iter.remove();
}
}
if (target == null || target.isEmpty())
{
this.flagOverrides = null;
}
else
{
this.flagOverrides = target;
}
this.changed();
}
// FINER
public boolean getFlag(FFlag flag)
{
return this.getFlags().get(flag);
}
public void setFlag(FFlag flag, boolean value)
{
Map<FFlag, Boolean> flags = this.getFlags();
flags.put(flag, value);
this.setFlags(flags);
}
// -------------------------------------------- //
// FIELD: permOverrides
// -------------------------------------------- //
// RAW
public Map<FPerm, Set<Rel>> getPerms()
{
Map<FPerm, Set<Rel>> ret = new LinkedHashMap<FPerm, Set<Rel>>();
for (FPerm fperm : FPerm.values())
{
ret.put(fperm, fperm.getDefault());
}
if (this.permOverrides != null)
{
for (Entry<FPerm, Set<Rel>> entry : this.permOverrides.entrySet())
{
ret.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue()));
}
}
return ret;
}
public void setPerms(Map<FPerm, Set<Rel>> perms)
{
Map<FPerm, Set<Rel>> target = new LinkedHashMap<FPerm, Set<Rel>>();
if (perms != null)
{
for (Entry<FPerm, Set<Rel>> entry : perms.entrySet())
{
target.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue()));
}
}
Iterator<Entry<FPerm, Set<Rel>>> iter = target.entrySet().iterator();
while (iter.hasNext())
{
Entry<FPerm, Set<Rel>> entry = iter.next();
if (entry.getKey().getDefault().equals(entry.getValue()))
{
iter.remove();
}
}
if (target == null || target.isEmpty())
{
this.permOverrides = null;
}
else
{
this.permOverrides = target;
}
this.changed();
}
// FINER
public Set<Rel> getPermittedRelations(FPerm perm)
{
return this.getPerms().get(perm);
}
public void setPermittedRelations(FPerm perm, Set<Rel> rels)
{
Map<FPerm, Set<Rel>> perms = this.getPerms();
perms.put(perm, rels);
this.setPerms(perms);
}
public void setPermittedRelations(FPerm perm, Rel... rels)
{
Set<Rel> temp = new HashSet<Rel>();
temp.addAll(Arrays.asList(rels));
this.setPermittedRelations(perm, temp);
}
public void setRelationPermitted(FPerm perm, Rel rel, boolean permitted)
{
Map<FPerm, Set<Rel>> perms = this.getPerms();
Set<Rel> rels = perms.get(perm);
if (permitted)
{
rels.add(rel);
}
else
{
rels.remove(rel);
}
this.setPerms(perms);
}
// -------------------------------------------- //
// OVERRIDE: RelationParticipator
// -------------------------------------------- //
@Override
public String describeTo(RelationParticipator observer, boolean ucfirst)
{
return RelationUtil.describeThatToMe(this, observer, ucfirst);
}
@Override
public String describeTo(RelationParticipator observer)
{
return RelationUtil.describeThatToMe(this, observer);
}
@Override
public Rel getRelationTo(RelationParticipator observer)
{
return RelationUtil.getRelationOfThatToMe(this, observer);
}
@Override
public Rel getRelationTo(RelationParticipator observer, boolean ignorePeaceful)
{
return RelationUtil.getRelationOfThatToMe(this, observer, ignorePeaceful);
}
@Override
public ChatColor getColorTo(RelationParticipator observer)
{
return RelationUtil.getColorOfThatToMe(this, observer);
}
// -------------------------------------------- //
// POWER
// -------------------------------------------- //
// TODO: Implement a has enough feature.
public double getPower()
{
if (this.getFlag(FFlag.INFPOWER))
{
return 999999;
}
double ret = 0;
for (FPlayer fplayer : this.getFPlayers())
{
ret += fplayer.getPower();
}
if (ConfServer.powerFactionMax > 0 && ret > ConfServer.powerFactionMax)
{
ret = ConfServer.powerFactionMax;
}
return ret + this.getPowerBoost();
}
public double getPowerMax()
{
if (this.getFlag(FFlag.INFPOWER))
{
return 999999;
}
double ret = 0;
for (FPlayer fplayer : this.getFPlayers())
{
ret += fplayer.getPowerMax();
}
if (ConfServer.powerFactionMax > 0 && ret > ConfServer.powerFactionMax)
{
ret = ConfServer.powerFactionMax;
}
return ret + this.getPowerBoost();
}
public int getPowerRounded()
{
return (int) Math.round(this.getPower());
}
public int getPowerMaxRounded()
{
return (int) Math.round(this.getPowerMax());
}
public int getLandCount()
{
return BoardColl.get().getCount(this);
}
public int getLandCountInWorld(String worldName)
{
return BoardColl.get().get(worldName).getCount(this);
}
public boolean hasLandInflation()
{
return this.getLandCount() > this.getPowerRounded();
}
// -------------------------------------------- //
// FOREIGN KEYS: FPLAYERS
// -------------------------------------------- //
// TODO: With this approach null must be used as default always.
// TODO: Take a moment and reflect upon the consequenses eeeeeeh...
// TODO: This one may be to slow after all :/ Thus I must maintain an index.
protected transient List<FPlayer> fplayers = null;
public void reindexFPlayers()
{
this.fplayers = new ArrayList<FPlayer>();
String factionId = this.getId();
if (factionId == null) return;
for (FPlayer fplayer : FPlayerColl.get().getAll())
{
if (!MUtil.equals(factionId, fplayer.getFactionId())) continue;
this.fplayers.add(fplayer);
}
}
public List<FPlayer> getFPlayers()
{
return new ArrayList<FPlayer>(this.fplayers);
}
public List<FPlayer> getFPlayersWhereOnline(boolean online)
{
List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
FPlayer fplayer = iter.next();
if (fplayer.isOnline() != online)
{
iter.remove();
}
}
return ret;
}
public List<FPlayer> getFPlayersWhereRole(Rel role)
{
List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
FPlayer fplayer = iter.next();
if (fplayer.getRole() != role)
{
iter.remove();
}
}
return ret;
}
public FPlayer getLeader()
{
List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
FPlayer fplayer = iter.next();
if (fplayer.getRole() == Rel.LEADER)
{
return fplayer;
}
}
return null;
}
public List<CommandSender> getOnlineCommandSenders()
{
List<CommandSender> ret = new ArrayList<CommandSender>();
for (CommandSender player : SenderUtil.getOnlineSenders())
{
FPlayer fplayer = FPlayerColl.get().get(player);
if (fplayer.getFaction() != this) continue;
ret.add(player);
}
return ret;
}
public List<Player> getOnlinePlayers()
{
List<Player> ret = new ArrayList<Player>();
for (Player player : Bukkit.getOnlinePlayers())
{
FPlayer fplayer = FPlayerColl.get().get(player);
if (fplayer.getFaction() != this) continue;
ret.add(player);
}
return ret;
}
// used when current leader is about to be removed from the faction; promotes new leader, or disbands faction if no other members left
public void promoteNewLeader()
{
if ( ! this.isNormal()) return;
if (this.getFlag(FFlag.PERMANENT) && ConfServer.permanentFactionsDisableLeaderPromotion) return;
FPlayer oldLeader = this.getLeader();
// get list of officers, or list of normal members if there are no officers
List<FPlayer> replacements = this.getFPlayersWhereRole(Rel.OFFICER);
if (replacements == null || replacements.isEmpty())
{
replacements = this.getFPlayersWhereRole(Rel.MEMBER);
}
if (replacements == null || replacements.isEmpty())
{ // faction leader is the only member; one-man faction
if (this.getFlag(FFlag.PERMANENT))
{
if (oldLeader != null)
{
oldLeader.setRole(Rel.MEMBER);
}
return;
}
// no members left and faction isn't permanent, so disband it
if (ConfServer.logFactionDisband)
{
Factions.get().log("The faction "+this.getTag()+" ("+this.getId()+") has been disbanded since it has no members left.");
}
for (FPlayer fplayer : FPlayerColl.get().getAllOnline())
{
fplayer.msg("The faction %s<i> was disbanded.", this.getTag(fplayer));
}
this.detach();
}
else
{ // promote new faction leader
if (oldLeader != null)
{
oldLeader.setRole(Rel.MEMBER);
}
replacements.get(0).setRole(Rel.LEADER);
this.msg("<i>Faction leader <h>%s<i> has been removed. %s<i> has been promoted as the new faction leader.", oldLeader == null ? "" : oldLeader.getName(), replacements.get(0).getName());
Factions.get().log("Faction "+this.getTag()+" ("+this.getId()+") leader was removed. Replacement leader: "+replacements.get(0).getName());
}
}
// -------------------------------------------- //
// MESSAGES
// -------------------------------------------- //
// These methods are simply proxied in from the Mixin.
// CONVENIENCE SEND MESSAGE
public boolean sendMessage(String message)
{
return Mixin.message(new FactionEqualsPredictate(this), message);
}
public boolean sendMessage(String... messages)
{
return Mixin.message(new FactionEqualsPredictate(this), messages);
}
public boolean sendMessage(Collection<String> messages)
{
return Mixin.message(new FactionEqualsPredictate(this), messages);
}
// CONVENIENCE MSG
public boolean msg(String msg)
{
return Mixin.msg(new FactionEqualsPredictate(this), msg);
}
public boolean msg(String msg, Object... args)
{
return Mixin.msg(new FactionEqualsPredictate(this), msg, args);
}
public boolean msg(Collection<String> msgs)
{
return Mixin.msg(new FactionEqualsPredictate(this), msgs);
}
}

View File

@@ -0,0 +1,330 @@
package com.massivecraft.factions.entity;
import java.io.File;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
import com.massivecraft.mcore.money.Money;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.MStore;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.util.Txt;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.util.MiscUtil;
public class FactionColl extends Coll<Faction>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static FactionColl i = new FactionColl();
public static FactionColl get() { return i; }
private FactionColl()
{
super(Const.COLLECTION_BASENAME_FACTION, Faction.class, MStore.getDb(ConfServer.dburi), Factions.get());
}
// -------------------------------------------- //
// OVERRIDE: COLL
// -------------------------------------------- //
@Override
public void init()
{
super.init();
this.migrate();
this.createDefaultFactions();
this.reindexFPlayers();
}
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;
// 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);
// Set the data
for (Entry<String, Faction> entry : id2faction.entrySet())
{
String factionId = entry.getKey();
Faction faction = entry.getValue();
FactionColl.get().create(factionId).load(faction);
}
// Mark as migrated
oldFile.renameTo(newFile);
}
@Override
protected synchronized String attach(Faction faction, Object oid, boolean noteChange)
{
String ret = super.attach(faction, oid, noteChange);
// Factions start with 0 money.
// TODO: Can this be done here?
// TODO: Or will it be a to heavy operation to do this often?
if (!Money.exists(faction))
{
Money.set(faction, 0);
}
return ret;
}
@Override
public Faction detachId(Object oid)
{
Faction faction = this.get(oid);
if (faction != null)
{
Money.set(faction, faction, 0);
}
Faction ret = super.detachId(oid);
// Clean the board
// TODO: Use events for this instead?
BoardColl.get().clean();
// Clean the fplayers
FPlayerColl.get().clean();
return ret;
}
public void reindexFPlayers()
{
for (Faction faction : this.getAll())
{
faction.reindexFPlayers();
}
}
// -------------------------------------------- //
// GET
// -------------------------------------------- //
// 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);
}
*/
public Faction getNone()
{
return this.get(Const.FACTIONID_NONE);
}
// -------------------------------------------- //
// FACTION TAG
// -------------------------------------------- //
public static ArrayList<String> validateTag(String str)
{
ArrayList<String> errors = new ArrayList<String>();
if(MiscUtil.getComparisonString(str).length() < ConfServer.factionTagLengthMin)
{
errors.add(Txt.parse("<i>The faction tag can't be shorter than <h>%s<i> chars.", ConfServer.factionTagLengthMin));
}
if(str.length() > ConfServer.factionTagLengthMax)
{
errors.add(Txt.parse("<i>The faction tag can't be longer than <h>%s<i> chars.", ConfServer.factionTagLengthMax));
}
for (char c : str.toCharArray())
{
if ( ! MiscUtil.substanceChars.contains(String.valueOf(c)))
{
errors.add(Txt.parse("<i>Faction tag must be alphanumeric. \"<h>%s<i>\" is not allowed.", c));
}
}
return errors;
}
public Faction getByTag(String str)
{
String compStr = MiscUtil.getComparisonString(str);
for (Faction faction : this.getAll())
{
if (faction.getComparisonTag().equals(compStr))
{
return faction;
}
}
return null;
}
public Faction getBestTagMatch(String searchFor)
{
Map<String, Faction> tag2faction = new HashMap<String, Faction>();
// TODO: Slow index building
for (Faction faction : this.getAll())
{
tag2faction.put(ChatColor.stripColor(faction.getTag()), faction);
}
String tag = Txt.getBestCIStart(tag2faction.keySet(), searchFor);
if (tag == null) return null;
return tag2faction.get(tag);
}
public boolean isTagTaken(String str)
{
return this.getByTag(str) != null;
}
public void econLandRewardRoutine()
{
if (!Econ.isEnabled(this.getUniverse())) return;
if (ConfServer.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<FPlayer> players = faction.getFPlayers();
int playerCount = players.size();
double reward = ConfServer.econLandReward * landCount / playerCount;
for (FPlayer player : players)
{
Econ.modifyMoney(player, reward, "own " + landCount + " faction land divided among " + playerCount + " members");
}
}
}
}
// -------------------------------------------- //
// 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.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.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.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);
}
}

View File

@@ -0,0 +1,22 @@
package com.massivecraft.factions.entity;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.store.Entity;
public class MConf extends Entity<MConf>
{
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static MConf get()
{
return MConfColl.get().get(MCore.INSTANCE);
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
}

View File

@@ -0,0 +1,34 @@
package com.massivecraft.factions.entity;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.MStore;
public class MConfColl extends Coll<MConf>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static MConfColl i = new MConfColl();
public static MConfColl get() { return i; }
private MConfColl()
{
super(Const.COLLECTION_BASENAME_MCONF, MConf.class, MStore.getDb(ConfServer.dburi), Factions.get(), true, false);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void init()
{
super.init();
this.get(MCore.INSTANCE);
}
}