Factions3/src/main/java/com/massivecraft/factions/entity/BoardColl.java

233 lines
5.5 KiB
Java
Raw Normal View History

2013-04-22 09:37:53 +02:00
package com.massivecraft.factions.entity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
2013-04-11 09:50:48 +02:00
import com.massivecraft.factions.Const;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.TerritoryAccess;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.store.MStore;
import com.massivecraft.massivecore.util.MUtil;
2013-04-12 15:10:11 +02:00
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_BOARD, Board.class, MStore.getDb(), Factions.get(), false, 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: 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();
}
}
// CHUNKS
@Override
public Set<PS> getChunks(Faction faction)
{
Set<PS> ret = new HashSet<PS>();
for (Board board : this.getAll())
{
ret.addAll(board.getChunks(faction));
}
return ret;
}
// 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);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
// Distance -1 returns 0 chunks always.
// Distance 0 returns 1 chunk only (the one supplied).
// Distance 1 returns 3x3 = 9 chunks.
public static Set<PS> getNearbyChunks(PS chunk, int distance, boolean includeSelf)
{
// Fix Args
if (chunk == null) throw new NullPointerException("chunk");
chunk = chunk.getChunk(true);
// Create Ret
Set<PS> ret = new LinkedHashSet<PS>();
if (distance < 0) return ret;
// if (distance == 0 && ! includeSelf) return ret; // This will be done by the code below.
// Main
int xmin = chunk.getChunkX() - distance;
int xmax = chunk.getChunkX() + distance;
int zmin = chunk.getChunkZ() - distance;
int zmax = chunk.getChunkZ() + distance;
for (int x = xmin; x <= xmax; x++)
{
for (int z = zmin; z <= zmax; z++)
{
if ( ! includeSelf && x == chunk.getChunkX() && z == chunk.getChunkZ()) continue;
ret.add(chunk.withChunkX(x).withChunkZ(z));
}
}
// Return Ret
return ret;
}
public static Set<Faction> getDistinctFactions(Collection<PS> chunks)
{
// Fix Args
if (chunks == null) throw new NullPointerException("chunks");
// Create Ret
Set<Faction> ret = new LinkedHashSet<Faction>();
// Main
for (PS chunk : chunks)
{
Faction faction = BoardColl.get().getFactionAt(chunk);
if (faction == null) continue;
ret.add(faction);
}
// Return Ret
return ret;
}
}