From 32db800fe1aa89f016a74a6eb56743699d1f87ed Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 19 Apr 2016 17:57:28 +0200 Subject: [PATCH] Max world claim count --- .../factions/engine/EngineMain.java | 51 ++++++++++++++++--- .../massivecraft/factions/entity/Board.java | 21 +++++++- .../factions/entity/BoardColl.java | 42 +++++++++++++++ .../factions/entity/BoardInterface.java | 4 ++ .../massivecraft/factions/entity/Faction.java | 9 ++++ .../massivecraft/factions/entity/MConf.java | 3 ++ 6 files changed, 122 insertions(+), 8 deletions(-) diff --git a/src/com/massivecraft/factions/engine/EngineMain.java b/src/com/massivecraft/factions/engine/EngineMain.java index b569a2da..8c90370b 100644 --- a/src/com/massivecraft/factions/engine/EngineMain.java +++ b/src/com/massivecraft/factions/engine/EngineMain.java @@ -15,6 +15,7 @@ import java.util.Map.Entry; import java.util.Set; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -93,9 +94,11 @@ import com.massivecraft.factions.integration.spigot.IntegrationSpigot; import com.massivecraft.factions.util.VisualizeUtil; import com.massivecraft.massivecore.Engine; import com.massivecraft.massivecore.PriorityLines; +import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.event.EventMassiveCorePlayerLeave; import com.massivecraft.massivecore.mixin.Mixin; +import com.massivecraft.massivecore.mixin.MixinWorld; import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.util.MUtil; @@ -520,17 +523,51 @@ public class EngineMain extends Engine return; } - // ... ensure the claim would not bypass the global max limit ... - int ownedLand = newFaction.getLandCount(); - if (MConf.get().claimedLandsMax != 0 && ownedLand + chunks.size() > MConf.get().claimedLandsMax && ! newFaction.getFlag(MFlag.getFlagInfpower())) + int claimedLandCount = newFaction.getLandCount(); + if ( ! newFaction.getFlag(MFlag.getFlagInfpower())) { - mplayer.msg("Limit reached. You can't claim more land."); - event.setCancelled(true); - return; + // ... ensure the claim would not bypass the global max limit ... + if (MConf.get().claimedLandsMax != 0 && claimedLandCount + chunks.size() > MConf.get().claimedLandsMax) + { + mplayer.msg("Limit reached. You can't claim more land."); + event.setCancelled(true); + return; + } + + // ... ensure the claim would not bypass the global max limit ... + if (MConf.get().claimedWorldsMax >= 0) + { + Set oldWorlds = newFaction.getClaimedWorlds(); + Set newWorlds = PS.getDistinctWorlds(chunks); + + Set worlds = new MassiveSet<>(); + worlds.addAll(oldWorlds); + worlds.addAll(newWorlds); + + if (worlds.size() > MConf.get().claimedWorldsMax) + { + List worldNames = new MassiveList<>(); + for (String world : oldWorlds) + { + worldNames.add(MixinWorld.get().getWorldDisplayName(world)); + } + + String worldsMax = MConf.get().claimedWorldsMax == 1 ? "world" : "worlds"; + String worldsAlready = oldWorlds.size() == 1 ? "world" : "worlds"; + mplayer.msg("A faction may only be present on %d different %s.", MConf.get().claimedWorldsMax, worldsMax); + mplayer.msg("%s is already present on %d %s:", newFaction.describeTo(mplayer), oldWorlds.size(), worldsAlready); + mplayer.message(Txt.implodeCommaAndDot(worldNames, ChatColor.YELLOW.toString())); + mplayer.msg("Please unclaim bases on other worlds to claim here."); + + event.setCancelled(true); + return; + } + } + } // ... ensure the claim would not bypass the faction power ... - if (ownedLand + chunks.size() > newFaction.getPowerRounded()) + if (claimedLandCount + chunks.size() > newFaction.getPowerRounded()) { mplayer.msg("You don't have enough power to claim that land."); event.setCancelled(true); diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index e2d7d4c2..3efc9449 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -276,6 +276,25 @@ public class Board extends Entity implements BoardInterface return ret; } + // CLAIMED + + @Override + public boolean hasClaimed(Faction faction) + { + return this.hasClaimed(faction.getId()); + } + + @Override + public boolean hasClaimed(String factionId) + { + for (TerritoryAccess ta : this.map.values()) + { + if ( ! ta.getHostFactionId().equals(factionId)) continue; + return true; + } + return false; + } + // NEARBY DETECTION // Is this coord NOT completely surrounded by coords claimed by the same faction? @@ -302,7 +321,7 @@ public class Board extends Entity implements BoardInterface return false; } - + @Override public boolean isAnyBorderPs(Set pss) { diff --git a/src/com/massivecraft/factions/entity/BoardColl.java b/src/com/massivecraft/factions/entity/BoardColl.java index e38b02d7..6312782d 100644 --- a/src/com/massivecraft/factions/entity/BoardColl.java +++ b/src/com/massivecraft/factions/entity/BoardColl.java @@ -14,6 +14,7 @@ import com.massivecraft.factions.Factions; import com.massivecraft.factions.RelationParticipator; 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.store.MStore; @@ -240,6 +241,25 @@ public class BoardColl extends Coll implements BoardInterface return ret; } + // COUNT + + @Override + public boolean hasClaimed(Faction faction) + { + return this.hasClaimed(faction.getId()); + } + + @Override + public boolean hasClaimed(String factionId) + { + for (Board board : this.getAll()) + { + if ( ! board.hasClaimed(factionId)) continue; + return true; + } + return false; + } + // NEARBY DETECTION @Override @@ -291,6 +311,28 @@ public class BoardColl extends Coll implements BoardInterface return board.getMap(observer, centerPs, inDegrees, width, height); } + // -------------------------------------------- // + // WORLDS + // -------------------------------------------- // + + public Set getClaimedWorlds(Faction faction) + { + return getClaimedWorlds(faction.getId()); + } + + public Set getClaimedWorlds(String factionId) + { + Set ret = new MassiveSet<>(); + + for (Board board : this.getAll()) + { + if ( ! board.hasClaimed(factionId)) continue; + ret.add(board.getId()); + } + + return ret; + } + // -------------------------------------------- // // UTIL // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/BoardInterface.java b/src/com/massivecraft/factions/entity/BoardInterface.java index 9e4b101d..6be177cb 100644 --- a/src/com/massivecraft/factions/entity/BoardInterface.java +++ b/src/com/massivecraft/factions/entity/BoardInterface.java @@ -33,6 +33,10 @@ public interface BoardInterface public int getCount(String factionId); public Map getFactionToCount(); + // CLAIMED + public boolean hasClaimed(Faction faction); + public boolean hasClaimed(String factionId); + // NEARBY DETECTION public boolean isBorderPs(PS ps); public boolean isAnyBorderPs(Set pss); diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 57a7921e..5b94213e 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -995,6 +995,15 @@ public class Faction extends Entity implements EconomyParticipator, Nam return this.getLandCount() > this.getPowerRounded(); } + // -------------------------------------------- // + // WORLDS + // -------------------------------------------- // + + public Set getClaimedWorlds() + { + return BoardColl.get().getClaimedWorlds(this); + } + // -------------------------------------------- // // FOREIGN KEY: MPLAYER // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index 8f5e6741..407436df 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -235,6 +235,9 @@ public class MConf extends Entity // 0 means there isn't. public int claimedLandsMax = 0; + // The max amount of worlds in which a player can have claims in. + public int claimedWorldsMax = -1; + // -------------------------------------------- // // PROTECTION // -------------------------------------------- //