diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsWarpGo.java b/src/com/massivecraft/factions/cmd/CmdFactionsWarpGo.java index 48aebd1a..5b98e568 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsWarpGo.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsWarpGo.java @@ -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; diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index ff8635ff..424ac616 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -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 implements BoardInterface { @@ -82,7 +81,8 @@ public class Board extends Entity 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 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 implements BoardInterface @Override public void removeAll(Faction faction) { - String factionId = faction.getId(); - - for (Entry 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 implements BoardInterface @Override public Set getChunks(String factionId) { - Set ret = new HashSet<>(); - for (Entry 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> getFactionToChunks() + { + return this.getFactionToChunks(true); } @Override - public Map> getFactionToChunks() + public Map> getFactionToChunks(boolean withWorld) { - Map> ret = new MassiveMap<>(); - - for (Entry entry : this.map.entrySet()) - { - // Get Faction - TerritoryAccess ta = entry.getValue(); - Faction faction = ta.getHostFaction(); - if (faction == null) continue; - - // Get Chunks - Set 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, 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>> getWorldToFactionToChunks(boolean withWorld) + { + return Collections.singletonMap(this.getId(), this.getFactionToChunks(withWorld)); } // COUNT @@ -208,45 +187,30 @@ public class Board extends Entity 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 getFactionToCount() + public Map getFactionToCount() { - Map ret = new MassiveMap<>(); - - for (Entry 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 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 implements BoardInterface @Override public boolean isAnyBorderPs(Set 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 implements BoardInterface @Override public boolean isAnyConnectedPs(Set 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)); } } diff --git a/src/com/massivecraft/factions/entity/BoardColl.java b/src/com/massivecraft/factions/entity/BoardColl.java index d03a948f..ae18d63e 100644 --- a/src/com/massivecraft/factions/entity/BoardColl.java +++ b/src/com/massivecraft/factions/entity/BoardColl.java @@ -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 implements BoardInterface { @@ -60,7 +58,7 @@ public class BoardColl extends Coll 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 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 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 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 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 implements BoardInterface @Override public Set getChunks(Faction faction) { - // Create - Set 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 getChunks(String factionId) { - // Create - Set 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> getFactionToChunks() + { + return this.getFactionToChunks(false); } @Override - public Map> getFactionToChunks() + public Map> getFactionToChunks(boolean withWorld) { // Create Map> ret = null; @@ -159,7 +148,7 @@ public class BoardColl extends Coll implements BoardInterface for (Board board : this.getAll()) { // Use the first board directly - Map> factionToChunks = board.getFactionToChunks(); + Map> factionToChunks = board.getFactionToChunks(withWorld); if (ret == null) { ret = factionToChunks; @@ -188,6 +177,13 @@ public class BoardColl extends Coll implements BoardInterface // Return return ret; } + + @Override + public Map>> 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 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 getFactionToCount() + public Map getFactionToCount() { - Map ret = null; - for (Board board : this.getAll()) - { - // Use the first board directly - Map factionToCount = board.getFactionToCount(); - if (ret == null) - { - ret = factionToCount; - continue; - } - - // Merge the following boards - for (Entry 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 by summing the values + .collect(Collectors.groupingBy( + Entry::getKey, + Collectors.summingLong(Entry::getValue) + )) + ; } // COUNT @@ -253,11 +228,8 @@ public class BoardColl extends Coll 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 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 implements BoardInterface @Override public boolean isAnyBorderPs(Set 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 implements BoardInterface public Set getClaimedWorlds(String factionId) { - // Create - Set 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()); } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/BoardInterface.java b/src/com/massivecraft/factions/entity/BoardInterface.java index 5ce05c04..4c54de68 100644 --- a/src/com/massivecraft/factions/entity/BoardInterface.java +++ b/src/com/massivecraft/factions/entity/BoardInterface.java @@ -23,12 +23,14 @@ public interface BoardInterface // CHUNKS Set getChunks(Faction faction); Set getChunks(String factionId); - Map> getFactionToChunks(); + @Deprecated Map> getFactionToChunks(); + Map> getFactionToChunks(boolean withWorld); + Map>> getWorldToFactionToChunks(boolean withWorld); // COUNT int getCount(Faction faction); int getCount(String factionId); - Map getFactionToCount(); + Map getFactionToCount(); // CLAIMED boolean hasClaimed(Faction faction); diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index d8c6b10c..695d98c5 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -1091,7 +1091,12 @@ public class Faction extends Entity implements FactionsParticipator, MP if (ret.size() == 0) return null; return ret.get(0); } - + + public Set getMPlayerIds() + { + return this.getMPlayers().stream().map(MPlayer::getId).collect(Collectors.toSet()); + } + public List getOnlineCommandSenders() { // Create Ret @@ -1141,7 +1146,7 @@ public class Faction extends Entity implements FactionsParticipator, MP MPlayer oldLeader = this.getLeader(); Rank leaderRank = oldLeader.getRank(); - List replacements = Collections.emptyList(); + List replacements = Collections.emptyList(); for (Rank rank = leaderRank; rank != null; rank = rank.getRankBelow()) { //Skip first