Halfway through adding in universe support

This commit is contained in:
Olof Larsson
2013-04-22 12:26:13 +02:00
parent de703d3461
commit 9fc75b1fcf
25 changed files with 593 additions and 270 deletions

View File

@ -32,7 +32,7 @@ public class Board extends Entity<Board> implements BoardInterface
public static Board get(Object oid)
{
return BoardColl.get().get(oid);
return BoardColls.get().get2(oid);
}
// -------------------------------------------- //
@ -169,7 +169,7 @@ public class Board extends Entity<Board> implements BoardInterface
{
TerritoryAccess territoryAccess = entry.getValue();
if (FactionColl.get().containsId(territoryAccess.getHostFactionId())) continue;
if (FactionColls.get().get(this).containsId(territoryAccess.getHostFactionId())) continue;
PS ps = entry.getKey();
this.removeAt(ps);

View File

@ -1,35 +1,25 @@
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
// CONSTRUCT
// -------------------------------------------- //
private static BoardColl i = new BoardColl();
public static BoardColl get() { return i; }
private BoardColl()
public BoardColl(String name)
{
super(Const.COLLECTION_BASENAME_BOARD, Board.class, MStore.getDb(ConfServer.dburi), Factions.get(), true, true);
super(name, Board.class, MStore.getDb(ConfServer.dburi), Factions.get(), true, true);
}
// -------------------------------------------- //
@ -46,50 +36,6 @@ public class BoardColl extends Coll<Board> implements BoardInterface
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
// -------------------------------------------- //

View File

@ -0,0 +1,233 @@
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.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.Colls;
import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.usys.Aspect;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class BoardColls extends Colls<BoardColl, Board> implements BoardInterface
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static BoardColls i = new BoardColls();
public static BoardColls get() { return i; }
// -------------------------------------------- //
// OVERRIDE: COLLS
// -------------------------------------------- //
@Override
public BoardColl createColl(String collName)
{
return new BoardColl(collName);
}
@Override
public Aspect getAspect()
{
return Factions.get().getAspect();
}
@Override
public String getBasename()
{
return Const.COLLECTION_BASENAME_UCONF;
}
@Override
public BoardColl get(Object o)
{
if (o == null) return null;
if (o instanceof Entity)
{
return this.getForUniverse(((Entity<?>)o).getUniverse());
}
if (o instanceof Coll)
{
return this.getForUniverse(((Coll<?>)o).getUniverse());
}
String worldName = MUtil.extract(String.class, "worldName", o);
if (worldName == null) return null;
return this.getForWorld(worldName);
}
@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();
BoardColl boardColl = this.getForWorld(worldName);
Board board = boardColl.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;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return null;
return coll.getTerritoryAccessAt(ps);
}
@Override
public Faction getFactionAt(PS ps)
{
if (ps == null) return null;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return null;
return coll.getFactionAt(ps);
}
// SET
@Override
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
{
if (ps == null) return;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return;
coll.setTerritoryAccessAt(ps, territoryAccess);
}
@Override
public void setFactionAt(PS ps, Faction faction)
{
if (ps == null) return;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return;
coll.setFactionAt(ps, faction);
}
// REMOVE
@Override
public void removeAt(PS ps)
{
if (ps == null) return;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return;
coll.removeAt(ps);
}
@Override
public void removeAll(Faction faction)
{
for (BoardColl coll : this.getColls())
{
coll.removeAll(faction);
}
}
@Override
public void clean()
{
for (BoardColl coll : this.getColls())
{
coll.clean();
}
}
// COUNT
@Override
public int getCount(Faction faction)
{
int ret = 0;
for (BoardColl coll : this.getColls())
{
ret += coll.getCount(faction);
}
return ret;
}
// NEARBY DETECTION
@Override
public boolean isBorderPs(PS ps)
{
if (ps == null) return false;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return false;
return coll.isBorderPs(ps);
}
@Override
public boolean isConnectedPs(PS ps, Faction faction)
{
if (ps == null) return false;
BoardColl coll = this.getForWorld(ps.getWorld());
if (coll == null) return false;
return coll.isConnectedPs(ps, faction);
}
// MAP GENERATION
@Override
public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees)
{
if (centerPs == null) return null;
BoardColl coll = this.getForWorld(centerPs.getWorld());
if (coll == null) return null;
return coll.getMap(observer, centerPs, inDegrees);
}
}

View File

@ -41,7 +41,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
public static FPlayer get(Object oid)
{
return FPlayerColl.get().get(oid);
return FPlayerColls.get().get2(oid);
}
// -------------------------------------------- //
@ -200,8 +200,8 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
// 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);
Faction ret = FactionColls.get().get(this).get(this.getFactionId());
if (ret == null) ret = FactionColls.get().get(this).get(Const.FACTIONID_NONE);
return ret;
}
@ -235,15 +235,15 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
// Next we must be attached and inited
if (!this.attached()) return;
if (!this.getColl().inited()) return;
if (!FactionColl.get().inited()) return;
if (!FactionColls.get().get(this).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);
Faction oldFaction = FactionColls.get().get(this).get(oldFactionId);
Faction faction = FactionColls.get().get(this).get(factionId);
oldFaction.fplayers.remove(this);
faction.fplayers.add(this);
@ -596,7 +596,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
public Rel getRelationToLocation()
{
// TODO: Use some built in system to get sender
return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())).getRelationTo(this);
return BoardColls.get().getFactionAt(PS.valueOf(this.getPlayer())).getRelationTo(this);
}
@Override
@ -626,13 +626,13 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
public boolean isInOwnTerritory()
{
// TODO: Use Mixin to get this PS instead
return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())) == this.getFaction();
return BoardColls.get().getFactionAt(Mixin.getSenderPs(this.getId())) == 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;
return BoardColls.get().getFactionAt(Mixin.getSenderPs(this.getId())).getRelationTo(this) == Rel.ENEMY;
}
public void sendFactionHereMessage()
@ -641,7 +641,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
{
return;
}
Faction factionHere = BoardColl.get().getFactionAt(this.getCurrentChunk());
Faction factionHere = BoardColls.get().getFactionAt(this.getCurrentChunk());
String msg = Txt.parse("<i>")+" ~ "+factionHere.getTag(this);
if (factionHere.hasDescription())
{
@ -705,7 +705,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
if (myFaction.isNormal() && !permanent && myFaction.getFPlayers().isEmpty())
{
// Remove this faction
for (FPlayer fplayer : FPlayerColl.get().getAllOnline())
for (FPlayer fplayer : FPlayerColls.get().get(this).getAllOnline())
{
fplayer.msg("<i>%s<i> was disbanded.", myFaction.describeTo(fplayer, true));
}
@ -723,7 +723,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
String error = null;
Faction myFaction = this.getFaction();
Faction currentFaction = BoardColl.get().getFactionAt(ps);
Faction currentFaction = BoardColls.get().getFactionAt(ps);
int ownedLand = forFaction.getLandCount();
if (ConfServer.worldGuardChecking && Worldguard.checkForRegionsInChunk(ps))
@ -772,7 +772,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
ConfServer.claimsMustBeConnected
&& ! this.isUsingAdminMode()
&& myFaction.getLandCountInWorld(ps.getWorld()) > 0
&& !BoardColl.get().isConnectedPs(ps, myFaction)
&& !BoardColls.get().isConnectedPs(ps, myFaction)
&& (!ConfServer.claimsCanBeUnconnectedIfOwnedByOtherFaction || !currentFaction.isNormal())
)
{
@ -788,7 +788,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
// 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))
else if ( ! BoardColls.get().isBorderPs(ps))
{
error = Txt.parse("<b>You must start claiming land at the border of the territory.");
}
@ -806,7 +806,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
public boolean attemptClaim(Faction forFaction, PS psChunk, boolean notifyFailure)
{
psChunk = psChunk.getChunk(true);
Faction currentFaction = BoardColl.get().getFactionAt(psChunk);
Faction currentFaction = BoardColls.get().getFactionAt(psChunk);
int ownedLand = forFaction.getLandCount();
if ( ! this.canClaimForFactionAtLocation(forFaction, psChunk, notifyFailure)) return false;
@ -820,7 +820,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
// 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))
if (ConfServer.econClaimUnconnectedFee != 0.0 && forFaction.getLandCountInWorld(psChunk.getWorld()) > 0 && !BoardColls.get().isConnectedPs(psChunk, forFaction))
{
cost += ConfServer.econClaimUnconnectedFee;
}
@ -841,7 +841,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
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);
BoardColls.get().setFactionAt(psChunk, forFaction);
SpoutFeatures.updateTerritoryDisplayLoc(psChunk);
if (MConf.get().logLandClaims)

View File

@ -1,73 +1,28 @@
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
// CONSTRUCT
// -------------------------------------------- //
private static FPlayerColl i = new FPlayerColl();
public static FPlayerColl get() { return i; }
private FPlayerColl()
public FPlayerColl(String name)
{
super(Const.COLLECTION_BASENAME_PLAYER, FPlayer.class, MStore.getDb(ConfServer.dburi), Factions.get());
super(name, 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)
{
@ -75,7 +30,7 @@ public class FPlayerColl extends SenderColl<FPlayer>
// If inited ...
if (!this.inited()) return ret;
if (!FactionColl.get().inited()) return ret;
if (!FactionColls.get().getForUniverse(this.getUniverse()).inited()) return ret;
// ... update the index.
Faction faction = entity.getFaction();
@ -108,7 +63,7 @@ public class FPlayerColl extends SenderColl<FPlayer>
{
for (FPlayer fplayer : this.getAll())
{
if (FactionColl.get().containsId(fplayer.getFactionId())) continue;
if (FactionColls.get().get(this).containsId(fplayer.getFactionId())) continue;
Factions.get().log("Reset faction data (invalid faction) for player "+fplayer.getName());
fplayer.resetFactionData(false);

View File

@ -0,0 +1,103 @@
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.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.Colls;
import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.usys.Aspect;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class FPlayerColls extends Colls<FPlayerColl, FPlayer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static FPlayerColls i = new FPlayerColls();
public static FPlayerColls get() { return i; }
// -------------------------------------------- //
// OVERRIDE: COLLS
// -------------------------------------------- //
@Override
public FPlayerColl createColl(String collName)
{
return new FPlayerColl(collName);
}
@Override
public Aspect getAspect()
{
return Factions.get().getAspect();
}
@Override
public String getBasename()
{
return Const.COLLECTION_BASENAME_PLAYER;
}
@Override
public FPlayerColl get(Object o)
{
if (o == null) return null;
if (o instanceof Entity)
{
return this.getForUniverse(((Entity<?>)o).getUniverse());
}
if (o instanceof Coll)
{
return this.getForUniverse(((Coll<?>)o).getUniverse());
}
String worldName = MUtil.extract(String.class, "worldName", o);
if (worldName == null) return null;
return this.getForWorld(worldName);
}
@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();
FPlayerColls.get().getForUniverse(MCore.DEFAULT).create(playerId).load(fplayer);
}
// Mark as migrated
oldFile.renameTo(newFile);
}
}

View File

@ -36,7 +36,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public static Faction get(Object oid)
{
return FactionColl.get().get(oid);
return FactionColls.get().get2(oid);
}
// -------------------------------------------- //
@ -238,7 +238,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
{
if (ps == null) return true;
if (!ConfServer.homesMustBeInClaimedTerritory) return true;
if (BoardColl.get().getFactionAt(ps) == this) return true;
if (BoardColls.get().getFactionAt(ps) == this) return true;
return false;
}
@ -451,7 +451,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
{
ret.put(rel, new ArrayList<String>());
}
for (Faction faction : FactionColl.get().getAll())
for (Faction faction : FactionColls.get().get(this).getAll())
{
Rel relation = faction.getRelationTo(this);
if (onlyNonNeutral && relation == Rel.NEUTRAL) continue;
@ -715,11 +715,11 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public int getLandCount()
{
return BoardColl.get().getCount(this);
return BoardColls.get().getCount(this);
}
public int getLandCountInWorld(String worldName)
{
return BoardColl.get().get(worldName).getCount(this);
return BoardColls.get().get(worldName).getCount(this);
}
public boolean hasLandInflation()
@ -743,7 +743,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
String factionId = this.getId();
if (factionId == null) return;
for (FPlayer fplayer : FPlayerColl.get().getAll())
for (FPlayer fplayer : FPlayerColls.get().get(this).getAll())
{
if (!MUtil.equals(factionId, fplayer.getFactionId())) continue;
this.fplayers.add(fplayer);
@ -805,7 +805,8 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
List<CommandSender> ret = new ArrayList<CommandSender>();
for (CommandSender player : SenderUtil.getOnlineSenders())
{
FPlayer fplayer = FPlayerColl.get().get(player);
FPlayer fplayer = FPlayer.get(player);
if (!MUtil.equals(fplayer.getUniverse(), this.getUniverse())) continue;
if (fplayer.getFaction() != this) continue;
ret.add(player);
}
@ -817,7 +818,8 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
List<Player> ret = new ArrayList<Player>();
for (Player player : Bukkit.getOnlinePlayers())
{
FPlayer fplayer = FPlayerColl.get().get(player);
FPlayer fplayer = FPlayer.get(player);
if (!MUtil.equals(fplayer.getUniverse(), this.getUniverse())) continue;
if (fplayer.getFaction() != this) continue;
ret.add(player);
}
@ -856,7 +858,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
Factions.get().log("The faction "+this.getTag()+" ("+this.getId()+") has been disbanded since it has no members left.");
}
for (FPlayer fplayer : FPlayerColl.get().getAllOnline())
for (FPlayer fplayer : FPlayerColls.get().get(this).getAllOnline())
{
fplayer.msg("The faction %s<i> was disbanded.", this.getTag(fplayer));
}

View File

@ -1,18 +1,13 @@
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;
@ -26,14 +21,12 @@ import com.massivecraft.factions.util.MiscUtil;
public class FactionColl extends Coll<Faction>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// CONSTRUCT
// -------------------------------------------- //
private static FactionColl i = new FactionColl();
public static FactionColl get() { return i; }
private FactionColl()
public FactionColl(String name)
{
super(Const.COLLECTION_BASENAME_FACTION, Faction.class, MStore.getDb(ConfServer.dburi), Factions.get());
super(name, Faction.class, MStore.getDb(ConfServer.dburi), Factions.get());
}
// -------------------------------------------- //
@ -45,37 +38,10 @@ public class FactionColl extends Coll<Faction>
{
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)
{
@ -97,19 +63,16 @@ public class FactionColl extends Coll<Faction>
public Faction detachId(Object oid)
{
Faction faction = this.get(oid);
if (faction != null)
{
Money.set(faction, faction, 0);
}
Money.set(faction, 0);
String universe = faction.getUniverse();
Faction ret = super.detachId(oid);
// Clean the board
// TODO: Use events for this instead?
BoardColl.get().clean();
BoardColls.get().getForUniverse(universe).clean();
// Clean the fplayers
FPlayerColl.get().clean();
FPlayerColls.get().getForUniverse(universe).clean();
return ret;
}

View File

@ -0,0 +1,104 @@
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.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.Colls;
import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.usys.Aspect;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class FactionColls extends Colls<FactionColl, Faction>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static FactionColls i = new FactionColls();
public static FactionColls get() { return i; }
// -------------------------------------------- //
// OVERRIDE: COLLS
// -------------------------------------------- //
@Override
public FactionColl createColl(String collName)
{
return new FactionColl(collName);
}
@Override
public Aspect getAspect()
{
return Factions.get().getAspect();
}
@Override
public String getBasename()
{
return Const.COLLECTION_BASENAME_FACTION;
}
@Override
public FactionColl get(Object o)
{
if (o == null) return null;
if (o instanceof Entity)
{
return this.getForUniverse(((Entity<?>)o).getUniverse());
}
if (o instanceof Coll)
{
return this.getForUniverse(((Coll<?>)o).getUniverse());
}
String worldName = MUtil.extract(String.class, "worldName", o);
if (worldName == null) return null;
return this.getForWorld(worldName);
}
@Override
public void init()
{
super.init();
this.migrate();
}
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();
this.getForUniverse(MCore.DEFAULT).create(factionId).load(faction);
}
// Mark as migrated
oldFile.renameTo(newFile);
}
}