2013-04-22 09:37:53 +02:00
|
|
|
package com.massivecraft.factions.entity;
|
2013-04-11 08:49:14 +02:00
|
|
|
|
2017-03-24 13:05:58 +01:00
|
|
|
import com.massivecraft.factions.TerritoryAccess;
|
|
|
|
import com.massivecraft.massivecore.collections.MassiveMap;
|
|
|
|
import com.massivecraft.massivecore.collections.MassiveSet;
|
|
|
|
import com.massivecraft.massivecore.ps.PS;
|
|
|
|
import com.massivecraft.massivecore.store.Coll;
|
|
|
|
import com.massivecraft.massivecore.util.MUtil;
|
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
import java.util.Collection;
|
2014-10-13 11:42:40 +02:00
|
|
|
import java.util.Map;
|
2014-10-22 07:50:30 +02:00
|
|
|
import java.util.Map.Entry;
|
2013-04-24 11:16:37 +02:00
|
|
|
import java.util.Set;
|
2019-03-03 10:16:41 +01:00
|
|
|
import java.util.stream.Collectors;
|
2013-04-11 09:50:48 +02:00
|
|
|
|
2013-04-12 15:10:11 +02:00
|
|
|
public class BoardColl extends Coll<Board> implements BoardInterface
|
2013-04-11 08:49:14 +02:00
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
2014-09-17 13:17:33 +02:00
|
|
|
// INSTANCE & CONSTRUCT
|
2013-04-11 08:49:14 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
private static BoardColl i = new BoardColl();
|
|
|
|
public static BoardColl get() { return i; }
|
|
|
|
private BoardColl()
|
2013-04-11 08:49:14 +02:00
|
|
|
{
|
2015-12-01 11:23:47 +01:00
|
|
|
this.setCreative(true);
|
|
|
|
this.setLowercasing(true);
|
2013-04-11 08:49:14 +02:00
|
|
|
}
|
2015-02-02 00:25:29 +01:00
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// STACK TRACEABILITY
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTick()
|
|
|
|
{
|
|
|
|
super.onTick();
|
|
|
|
}
|
2013-04-11 08:49:14 +02:00
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE: COLL
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fixId(Object oid)
|
|
|
|
{
|
|
|
|
if (oid == null) return null;
|
|
|
|
if (oid instanceof String) return (String)oid;
|
2015-11-21 22:26:00 +01:00
|
|
|
if (oid instanceof Board) return ((Board)oid).getId();
|
2013-04-11 09:25:26 +02:00
|
|
|
|
|
|
|
return MUtil.extract(String.class, "worldName", oid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE: BOARD
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TerritoryAccess getTerritoryAccessAt(PS ps)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return null;
|
|
|
|
return board.getTerritoryAccessAt(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Faction getFactionAt(PS ps)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return null;
|
|
|
|
return board.getFactionAt(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SET
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return;
|
|
|
|
board.setTerritoryAccessAt(ps, territoryAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFactionAt(PS ps, Faction faction)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return;
|
|
|
|
board.setFactionAt(ps, faction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// REMOVE
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeAt(PS ps)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 11:16:37 +02:00
|
|
|
// CHUNKS
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<PS> getChunks(Faction faction)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
return this.getAll().stream()
|
|
|
|
.flatMap(board -> board.getChunks(faction).stream())
|
|
|
|
.collect(Collectors.toSet());
|
2013-04-24 11:16:37 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 07:50:30 +02:00
|
|
|
@Override
|
|
|
|
public Set<PS> getChunks(String factionId)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
return this.getAll().stream()
|
|
|
|
.flatMap(board -> board.getChunks(factionId).stream())
|
|
|
|
.collect(Collectors.toSet());
|
2014-10-22 07:50:30 +02:00
|
|
|
}
|
2019-03-03 10:16:41 +01:00
|
|
|
|
2014-10-22 07:50:30 +02:00
|
|
|
@Override
|
2019-03-03 10:16:41 +01:00
|
|
|
@Deprecated
|
2014-10-22 07:50:30 +02:00
|
|
|
public Map<Faction, Set<PS>> getFactionToChunks()
|
2019-03-03 10:16:41 +01:00
|
|
|
{
|
|
|
|
return this.getFactionToChunks(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld)
|
2014-10-22 07:50:30 +02:00
|
|
|
{
|
2017-03-12 17:55:01 +01:00
|
|
|
// Create
|
2014-10-22 07:50:30 +02:00
|
|
|
Map<Faction, Set<PS>> ret = null;
|
2017-03-12 17:55:01 +01:00
|
|
|
|
|
|
|
// Fill
|
2014-10-22 07:50:30 +02:00
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
// Use the first board directly
|
2019-03-03 10:16:41 +01:00
|
|
|
Map<Faction, Set<PS>> factionToChunks = board.getFactionToChunks(withWorld);
|
2014-10-22 07:50:30 +02:00
|
|
|
if (ret == null)
|
|
|
|
{
|
|
|
|
ret = factionToChunks;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the following boards
|
|
|
|
for (Entry<Faction, Set<PS>> entry : factionToChunks.entrySet())
|
|
|
|
{
|
|
|
|
Faction faction = entry.getKey();
|
|
|
|
Set<PS> chunks = ret.get(faction);
|
|
|
|
if (chunks == null)
|
|
|
|
{
|
|
|
|
ret.put(faction, entry.getValue());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunks.addAll(entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Enforce create
|
2017-03-24 14:03:29 +01:00
|
|
|
if (ret == null) ret = new MassiveMap<>();
|
2017-03-12 17:55:01 +01:00
|
|
|
|
|
|
|
// Return
|
2014-10-22 07:50:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2019-03-03 10:16:41 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld)
|
|
|
|
{
|
|
|
|
return this.getAll().stream()
|
|
|
|
.collect(Collectors.toMap(Board::getId, board -> board.getFactionToChunks(withWorld)));
|
|
|
|
}
|
2014-10-22 07:50:30 +02:00
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// COUNT
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount(Faction faction)
|
2014-10-18 18:30:13 +02:00
|
|
|
{
|
|
|
|
return this.getCount(faction.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount(String factionId)
|
2013-04-11 09:25:26 +02:00
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
return this.getAll().stream()
|
|
|
|
.mapToInt(board -> board.getCount(factionId))
|
|
|
|
.sum();
|
2013-04-11 09:25:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 07:50:30 +02:00
|
|
|
@Override
|
2019-03-03 10:16:41 +01:00
|
|
|
public Map<Faction, Long> getFactionToCount()
|
2014-10-22 07:50:30 +02:00
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
// Get them all and map them to sets of entries
|
|
|
|
return this.getAll().stream()
|
|
|
|
.map(Board::getFactionToCount)
|
|
|
|
.map(Map::entrySet)
|
|
|
|
.flatMap(Set::stream)
|
|
|
|
// Collect the entries in a map of <Faction, Long> by summing the values
|
|
|
|
.collect(Collectors.groupingBy(
|
|
|
|
Entry::getKey,
|
|
|
|
Collectors.summingLong(Entry::getValue)
|
|
|
|
))
|
|
|
|
;
|
2014-10-22 07:50:30 +02:00
|
|
|
}
|
|
|
|
|
2016-04-19 17:57:28 +02:00
|
|
|
// COUNT
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasClaimed(Faction faction)
|
|
|
|
{
|
|
|
|
return this.hasClaimed(faction.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasClaimed(String factionId)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
return this.getAll().stream()
|
|
|
|
.anyMatch(board -> board.hasClaimed(factionId));
|
2016-04-19 17:57:28 +02:00
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// NEARBY DETECTION
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isBorderPs(PS ps)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return false;
|
|
|
|
return board.isBorderPs(ps);
|
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean isAnyBorderPs(Set<PS> pss)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
return pss.stream().anyMatch(this::isBorderPs);
|
2014-10-13 11:42:40 +02:00
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean isConnectedPs(PS ps, Faction faction)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (ps == null) throw new NullPointerException("ps");
|
2013-04-11 09:25:26 +02:00
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return false;
|
|
|
|
return board.isConnectedPs(ps, faction);
|
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean isAnyConnectedPs(Set<PS> pss, Faction faction)
|
|
|
|
{
|
|
|
|
for (PS ps : pss)
|
|
|
|
{
|
|
|
|
if (this.isConnectedPs(ps, faction)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-19 17:57:28 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// WORLDS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public Set<String> getClaimedWorlds(Faction faction)
|
|
|
|
{
|
|
|
|
return getClaimedWorlds(faction.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<String> getClaimedWorlds(String factionId)
|
|
|
|
{
|
2019-03-03 10:16:41 +01:00
|
|
|
if (factionId == null) throw new NullPointerException("factionId");
|
|
|
|
return this.getAll().stream()
|
|
|
|
.filter(board -> board.hasClaimed(factionId))
|
|
|
|
.map(Board::getId)
|
|
|
|
.collect(Collectors.toSet());
|
2016-04-19 17:57:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// UTIL
|
|
|
|
// -------------------------------------------- //
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
// Distance -1 returns 0 chunks always.
|
|
|
|
// Distance 0 returns 1 chunk only (the one supplied).
|
|
|
|
// Distance 1 returns 3x3 = 9 chunks.
|
2017-03-12 17:55:01 +01:00
|
|
|
public static Set<PS> getNearbyChunks(PS psChunk, int distance)
|
2014-09-17 13:17:33 +02:00
|
|
|
{
|
2014-10-07 17:08:40 +02:00
|
|
|
// Fix Args
|
2017-03-12 17:55:01 +01:00
|
|
|
if (psChunk == null) throw new NullPointerException("psChunk");
|
|
|
|
psChunk = psChunk.getChunk(true);
|
2014-10-07 17:08:40 +02:00
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Create
|
2018-01-19 20:51:07 +01:00
|
|
|
Set<PS> ret = new MassiveSet<>();
|
2014-10-07 17:08:40 +02:00
|
|
|
if (distance < 0) return ret;
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Fill
|
|
|
|
int chunkX = psChunk.getChunkX();
|
|
|
|
int xmin = chunkX - distance;
|
|
|
|
int xmax = chunkX + distance;
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
int chunkZ = psChunk.getChunkZ();
|
|
|
|
int zmin = chunkZ - distance;
|
|
|
|
int zmax = chunkZ + distance;
|
2014-10-07 17:08:40 +02:00
|
|
|
|
|
|
|
for (int x = xmin; x <= xmax; x++)
|
2014-09-17 13:17:33 +02:00
|
|
|
{
|
2017-03-12 17:55:01 +01:00
|
|
|
PS psChunkX = psChunk.withChunkX(x);
|
2014-10-07 17:08:40 +02:00
|
|
|
for (int z = zmin; z <= zmax; z++)
|
2014-09-17 13:17:33 +02:00
|
|
|
{
|
2017-03-12 17:55:01 +01:00
|
|
|
ret.add(psChunkX.withChunkZ(z));
|
2014-09-17 13:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Return
|
2014-10-07 17:08:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
public static Set<PS> getNearbyChunks(Collection<PS> chunks, int distance)
|
|
|
|
{
|
|
|
|
// Fix Args
|
|
|
|
if (chunks == null) throw new NullPointerException("chunks");
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Create
|
2018-01-19 20:51:07 +01:00
|
|
|
Set<PS> ret = new MassiveSet<>();
|
2014-10-13 11:42:40 +02:00
|
|
|
|
|
|
|
if (distance < 0) return ret;
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Fill
|
2014-10-13 11:42:40 +02:00
|
|
|
for (PS chunk : chunks)
|
|
|
|
{
|
|
|
|
ret.addAll(getNearbyChunks(chunk, distance));
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Return
|
2014-10-13 11:42:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
public static Set<Faction> getDistinctFactions(Collection<PS> chunks)
|
|
|
|
{
|
|
|
|
// Fix Args
|
|
|
|
if (chunks == null) throw new NullPointerException("chunks");
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Create
|
2018-01-19 20:51:07 +01:00
|
|
|
Set<Faction> ret = new MassiveSet<>();
|
2014-10-07 17:08:40 +02:00
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Fill
|
2014-10-07 17:08:40 +02:00
|
|
|
for (PS chunk : chunks)
|
|
|
|
{
|
2017-03-12 17:55:01 +01:00
|
|
|
Faction faction = get().getFactionAt(chunk);
|
2014-10-07 17:08:40 +02:00
|
|
|
if (faction == null) continue;
|
|
|
|
ret.add(faction);
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Return
|
2014-10-07 17:08:40 +02:00
|
|
|
return ret;
|
2014-09-17 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
public static Map<PS, Faction> getChunkFaction(Collection<PS> chunks)
|
|
|
|
{
|
2017-03-12 17:55:01 +01:00
|
|
|
// Create
|
2018-01-19 20:51:07 +01:00
|
|
|
Map<PS, Faction> ret = new MassiveMap<>();
|
2014-10-13 11:42:40 +02:00
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Fill
|
|
|
|
Faction none = FactionColl.get().getNone();
|
2014-10-13 11:42:40 +02:00
|
|
|
for (PS chunk : chunks)
|
|
|
|
{
|
|
|
|
chunk = chunk.getChunk(true);
|
|
|
|
Faction faction = get().getFactionAt(chunk);
|
2017-03-12 17:55:01 +01:00
|
|
|
if (faction == null) faction = none;
|
2014-10-13 11:42:40 +02:00
|
|
|
ret.put(chunk, faction);
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:55:01 +01:00
|
|
|
// Return
|
2014-10-13 11:42:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-11 08:49:14 +02:00
|
|
|
}
|