Refactor boards
This commit is contained in:
parent
b936923bb1
commit
de9c4e273a
@ -96,7 +96,7 @@ public class CmdFactionsWarpGo extends FactionsCommandWarp
|
||||
{
|
||||
if (MUtil.isntPlayer(p)) continue;
|
||||
|
||||
if (p == null || !p.isOnline() || p.isDead() || p == me || p.getWorld() != w) continue;
|
||||
if (!p.isOnline() || p.isDead() || p == me || p.getWorld() != w) continue;
|
||||
|
||||
MPlayer fp = MPlayer.get(p);
|
||||
if (msender.getRelationTo(fp) != Rel.ENEMY) continue;
|
||||
|
@ -3,18 +3,17 @@ package com.massivecraft.factions.entity;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.massivecraft.factions.Factions;
|
||||
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.Entity;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Board extends Entity<Board> implements BoardInterface
|
||||
{
|
||||
@ -82,7 +81,8 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public TerritoryAccess getTerritoryAccessAt(PS ps)
|
||||
{
|
||||
if (ps == null) return null;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
|
||||
ps = ps.getChunkCoords(true);
|
||||
TerritoryAccess ret = this.map.get(ps);
|
||||
if (ret == null || ret.getHostFaction() == null) ret = TerritoryAccess.valueOf(Factions.ID_NONE);
|
||||
@ -92,9 +92,7 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public Faction getFactionAt(PS ps)
|
||||
{
|
||||
if (ps == null) return null;
|
||||
TerritoryAccess ta = this.getTerritoryAccessAt(ps);
|
||||
return ta.getHostFaction();
|
||||
return this.getTerritoryAccessAt(ps).getHostFaction();
|
||||
}
|
||||
|
||||
// SET
|
||||
@ -138,16 +136,7 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@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);
|
||||
}
|
||||
this.getChunks(faction).forEach(this::removeAt);
|
||||
}
|
||||
|
||||
// CHUNKS
|
||||
@ -161,46 +150,36 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public Set<PS> getChunks(String factionId)
|
||||
{
|
||||
Set<PS> ret = new HashSet<>();
|
||||
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
|
||||
{
|
||||
TerritoryAccess ta = entry.getValue();
|
||||
if (!ta.getHostFactionId().equals(factionId)) continue;
|
||||
|
||||
PS ps = entry.getKey();
|
||||
ps = ps.withWorld(this.getId());
|
||||
ret.add(ps);
|
||||
}
|
||||
return ret;
|
||||
return this.map.entrySet().stream()
|
||||
.filter(e -> e.getValue().getHostFactionId().equals(factionId))
|
||||
.map(Entry::getKey)
|
||||
.map(ps -> ps.withWorld(this.getId()))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Map<Faction, Set<PS>> getFactionToChunks()
|
||||
{
|
||||
return this.getFactionToChunks(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Faction, Set<PS>> getFactionToChunks()
|
||||
public Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld)
|
||||
{
|
||||
Map<Faction, Set<PS>> ret = new MassiveMap<>();
|
||||
|
||||
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
|
||||
{
|
||||
// Get Faction
|
||||
TerritoryAccess ta = entry.getValue();
|
||||
Faction faction = ta.getHostFaction();
|
||||
if (faction == null) continue;
|
||||
|
||||
// Get Chunks
|
||||
Set<PS> chunks = ret.get(faction);
|
||||
if (chunks == null)
|
||||
{
|
||||
chunks = new MassiveSet<>();
|
||||
ret.put(faction, chunks);
|
||||
}
|
||||
|
||||
// Add Chunk
|
||||
PS chunk = entry.getKey();
|
||||
chunk = chunk.withWorld(this.getId());
|
||||
chunks.add(chunk);
|
||||
}
|
||||
|
||||
return ret;
|
||||
Function<Entry<PS, TerritoryAccess>, PS> mapper = Entry::getKey;
|
||||
if (withWorld) mapper = mapper.andThen(ps -> ps.withWorld(this.getId()));
|
||||
|
||||
return map.entrySet().stream().collect(Collectors.groupingBy(
|
||||
entry -> entry.getValue().getHostFaction(), // This specifies how to get the key
|
||||
Collectors.mapping(mapper, Collectors.toSet()) // This maps the entries and puts them in the collection
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld)
|
||||
{
|
||||
return Collections.singletonMap(this.getId(), this.getFactionToChunks(withWorld));
|
||||
}
|
||||
|
||||
// COUNT
|
||||
@ -208,45 +187,30 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public int getCount(Faction faction)
|
||||
{
|
||||
if (faction == null) throw new NullPointerException("faction");
|
||||
|
||||
return this.getCount(faction.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount(String factionId)
|
||||
{
|
||||
int ret = 0;
|
||||
for (TerritoryAccess ta : this.map.values())
|
||||
{
|
||||
if (!ta.getHostFactionId().equals(factionId)) continue;
|
||||
ret += 1;
|
||||
}
|
||||
return ret;
|
||||
if (factionId == null) throw new NullPointerException("factionId");
|
||||
|
||||
return (int) this.map.values().stream()
|
||||
.map(TerritoryAccess::getHostFactionId)
|
||||
.filter(factionId::equals)
|
||||
.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Faction, Integer> getFactionToCount()
|
||||
public Map<Faction, Long> getFactionToCount()
|
||||
{
|
||||
Map<Faction, Integer> ret = new MassiveMap<>();
|
||||
|
||||
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
|
||||
{
|
||||
// Get Faction
|
||||
TerritoryAccess ta = entry.getValue();
|
||||
Faction faction = ta.getHostFaction();
|
||||
if (faction == null) continue;
|
||||
|
||||
// Get Count
|
||||
Integer count = ret.get(faction);
|
||||
if (count == null)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
// Add Chunk
|
||||
ret.put(faction, count + 1);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return this.map.entrySet().stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
e -> e.getValue().getHostFaction(),
|
||||
Collectors.counting()
|
||||
));
|
||||
}
|
||||
|
||||
// CLAIMED
|
||||
@ -260,12 +224,9 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public boolean hasClaimed(String factionId)
|
||||
{
|
||||
for (TerritoryAccess ta : this.map.values())
|
||||
{
|
||||
if ( ! ta.getHostFactionId().equals(factionId)) continue;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return this.map.values().stream()
|
||||
.map(TerritoryAccess::getHostFactionId)
|
||||
.anyMatch(factionId::equals);
|
||||
}
|
||||
|
||||
// NEARBY DETECTION
|
||||
@ -298,11 +259,7 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public boolean isAnyBorderPs(Set<PS> pss)
|
||||
{
|
||||
for (PS ps : pss)
|
||||
{
|
||||
if (this.isBorderPs(ps)) return true;
|
||||
}
|
||||
return false;
|
||||
return pss.stream().anyMatch(this::isBorderPs);
|
||||
}
|
||||
|
||||
// Is this coord connected to any coord claimed by the specified faction?
|
||||
@ -331,11 +288,7 @@ public class Board extends Entity<Board> implements BoardInterface
|
||||
@Override
|
||||
public boolean isAnyConnectedPs(Set<PS> pss, Faction faction)
|
||||
{
|
||||
for (PS ps : pss)
|
||||
{
|
||||
if (this.isConnectedPs(ps, faction)) return true;
|
||||
}
|
||||
return false;
|
||||
return pss.stream().anyMatch(ps -> this.isConnectedPs(ps, faction));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,12 +8,10 @@ import com.massivecraft.massivecore.store.Coll;
|
||||
import com.massivecraft.massivecore.util.MUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
{
|
||||
@ -60,7 +58,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public TerritoryAccess getTerritoryAccessAt(PS ps)
|
||||
{
|
||||
if (ps == null) return null;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return null;
|
||||
return board.getTerritoryAccessAt(ps);
|
||||
@ -69,7 +67,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public Faction getFactionAt(PS ps)
|
||||
{
|
||||
if (ps == null) return null;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return null;
|
||||
return board.getFactionAt(ps);
|
||||
@ -80,7 +78,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
|
||||
{
|
||||
if (ps == null) return;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return;
|
||||
board.setTerritoryAccessAt(ps, territoryAccess);
|
||||
@ -89,7 +87,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public void setFactionAt(PS ps, Faction faction)
|
||||
{
|
||||
if (ps == null) return;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return;
|
||||
board.setFactionAt(ps, faction);
|
||||
@ -100,7 +98,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public void removeAt(PS ps)
|
||||
{
|
||||
if (ps == null) return;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return;
|
||||
board.removeAt(ps);
|
||||
@ -120,37 +118,28 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public Set<PS> getChunks(Faction faction)
|
||||
{
|
||||
// Create
|
||||
Set<PS> ret = new HashSet<>();
|
||||
|
||||
// Fill
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
ret.addAll(board.getChunks(faction));
|
||||
}
|
||||
|
||||
// Return
|
||||
return ret;
|
||||
return this.getAll().stream()
|
||||
.flatMap(board -> board.getChunks(faction).stream())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<PS> getChunks(String factionId)
|
||||
{
|
||||
// Create
|
||||
Set<PS> ret = new HashSet<>();
|
||||
|
||||
// Fill
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
ret.addAll(board.getChunks(factionId));
|
||||
}
|
||||
|
||||
// Return
|
||||
return ret;
|
||||
return this.getAll().stream()
|
||||
.flatMap(board -> board.getChunks(factionId).stream())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Map<Faction, Set<PS>> getFactionToChunks()
|
||||
{
|
||||
return this.getFactionToChunks(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Faction, Set<PS>> getFactionToChunks()
|
||||
public Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld)
|
||||
{
|
||||
// Create
|
||||
Map<Faction, Set<PS>> ret = null;
|
||||
@ -159,7 +148,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
// Use the first board directly
|
||||
Map<Faction, Set<PS>> factionToChunks = board.getFactionToChunks();
|
||||
Map<Faction, Set<PS>> factionToChunks = board.getFactionToChunks(withWorld);
|
||||
if (ret == null)
|
||||
{
|
||||
ret = factionToChunks;
|
||||
@ -188,6 +177,13 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
// Return
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld)
|
||||
{
|
||||
return this.getAll().stream()
|
||||
.collect(Collectors.toMap(Board::getId, board -> board.getFactionToChunks(withWorld)));
|
||||
}
|
||||
|
||||
// COUNT
|
||||
|
||||
@ -200,46 +196,25 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public int getCount(String factionId)
|
||||
{
|
||||
int ret = 0;
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
ret += board.getCount(factionId);
|
||||
}
|
||||
return ret;
|
||||
return this.getAll().stream()
|
||||
.mapToInt(board -> board.getCount(factionId))
|
||||
.sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Faction, Integer> getFactionToCount()
|
||||
public Map<Faction, Long> getFactionToCount()
|
||||
{
|
||||
Map<Faction, Integer> ret = null;
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
// Use the first board directly
|
||||
Map<Faction, Integer> factionToCount = board.getFactionToCount();
|
||||
if (ret == null)
|
||||
{
|
||||
ret = factionToCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Merge the following boards
|
||||
for (Entry<Faction, Integer> entry : factionToCount.entrySet())
|
||||
{
|
||||
Faction faction = entry.getKey();
|
||||
Integer count = ret.get(faction);
|
||||
if (count == null)
|
||||
{
|
||||
ret.put(faction, entry.getValue());
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.put(faction, count + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == null) ret = new MassiveMap<>();
|
||||
return ret;
|
||||
// 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)
|
||||
))
|
||||
;
|
||||
}
|
||||
|
||||
// COUNT
|
||||
@ -253,11 +228,8 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public boolean hasClaimed(String factionId)
|
||||
{
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
if (board.hasClaimed(factionId)) return true;
|
||||
}
|
||||
return false;
|
||||
return this.getAll().stream()
|
||||
.anyMatch(board -> board.hasClaimed(factionId));
|
||||
}
|
||||
|
||||
// NEARBY DETECTION
|
||||
@ -265,7 +237,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public boolean isBorderPs(PS ps)
|
||||
{
|
||||
if (ps == null) return false;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return false;
|
||||
return board.isBorderPs(ps);
|
||||
@ -274,17 +246,13 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
@Override
|
||||
public boolean isAnyBorderPs(Set<PS> pss)
|
||||
{
|
||||
for (PS ps : pss)
|
||||
{
|
||||
if (this.isBorderPs(ps)) return true;
|
||||
}
|
||||
return false;
|
||||
return pss.stream().anyMatch(this::isBorderPs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnectedPs(PS ps, Faction faction)
|
||||
{
|
||||
if (ps == null) return false;
|
||||
if (ps == null) throw new NullPointerException("ps");
|
||||
Board board = this.get(ps.getWorld());
|
||||
if (board == null) return false;
|
||||
return board.isConnectedPs(ps, faction);
|
||||
@ -311,17 +279,11 @@ public class BoardColl extends Coll<Board> implements BoardInterface
|
||||
|
||||
public Set<String> getClaimedWorlds(String factionId)
|
||||
{
|
||||
// Create
|
||||
Set<String> ret = new MassiveSet<>();
|
||||
|
||||
// Fill
|
||||
for (Board board : this.getAll())
|
||||
{
|
||||
if (board.hasClaimed(factionId)) ret.add(board.getId());
|
||||
}
|
||||
|
||||
// Return
|
||||
return ret;
|
||||
if (factionId == null) throw new NullPointerException("factionId");
|
||||
return this.getAll().stream()
|
||||
.filter(board -> board.hasClaimed(factionId))
|
||||
.map(Board::getId)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -23,12 +23,14 @@ public interface BoardInterface
|
||||
// CHUNKS
|
||||
Set<PS> getChunks(Faction faction);
|
||||
Set<PS> getChunks(String factionId);
|
||||
Map<Faction, Set<PS>> getFactionToChunks();
|
||||
@Deprecated Map<Faction, Set<PS>> getFactionToChunks();
|
||||
Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld);
|
||||
Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld);
|
||||
|
||||
// COUNT
|
||||
int getCount(Faction faction);
|
||||
int getCount(String factionId);
|
||||
Map<Faction, Integer> getFactionToCount();
|
||||
Map<Faction, Long> getFactionToCount();
|
||||
|
||||
// CLAIMED
|
||||
boolean hasClaimed(Faction faction);
|
||||
|
@ -1091,7 +1091,12 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
|
||||
if (ret.size() == 0) return null;
|
||||
return ret.get(0);
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getMPlayerIds()
|
||||
{
|
||||
return this.getMPlayers().stream().map(MPlayer::getId).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public List<CommandSender> getOnlineCommandSenders()
|
||||
{
|
||||
// Create Ret
|
||||
@ -1141,7 +1146,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
|
||||
MPlayer oldLeader = this.getLeader();
|
||||
Rank leaderRank = oldLeader.getRank();
|
||||
|
||||
List<MPlayer> replacements = Collections.<MPlayer>emptyList();
|
||||
List<MPlayer> replacements = Collections.emptyList();
|
||||
for (Rank rank = leaderRank; rank != null; rank = rank.getRankBelow())
|
||||
{
|
||||
//Skip first
|
||||
|
Loading…
Reference in New Issue
Block a user