Make claimsMustBeConnected enforcement stronger. Closes #35

It is made stronger only when an option is toggled in the config.
This commit is contained in:
Magnus Ulf 2020-01-20 00:50:34 +01:00
parent fd865ca037
commit b6212d77ec
3 changed files with 75 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -204,6 +205,32 @@ public class EngineChunkChange extends Engine
}
}
// Ensure claims are properly connected when unclaiming
if (newFaction.isNone() && MConf.get().claimsMustBeConnected && MConf.get().claimsMustBeConnectedStrict)
{
for (Entry<Faction, Set<PS>> entry : currentFactionChunks.entrySet())
{
Faction faction = entry.getKey();
Set<PS> factionRemovedChunks = entry.getValue();
Set<PS> pssBefore = BoardColl.get().getChunks(faction);
// Get how many "forests" of claims there are right now
List<Collection<PS>> forestsBefore = BoardColl.getForests(pssBefore);
Set<PS> pssAfter = new MassiveSet<>(pssBefore);
pssAfter.removeAll(factionRemovedChunks);
List<Collection<PS>> forestsAfter = BoardColl.getForests(pssAfter);
if (forestsAfter.size() > forestsBefore.size())
{
mplayer.msg("<b>Claims must be connected. You can't make them unconnected by unclaiming.");
event.setCancelled(true);
return;
}
}
}
// For each of the old factions ...
for (Entry<Faction, Set<PS>> entry : currentFactionChunks.entrySet())
{

View File

@ -1,6 +1,7 @@
package com.massivecraft.factions.entity;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.entity.MassiveCoreMConf;
@ -9,9 +10,11 @@ import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.util.MUtil;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;
public class BoardColl extends Coll<Board> implements BoardInterface
@ -391,4 +394,43 @@ public class BoardColl extends Coll<Board> implements BoardInterface
return ret;
}
public static List<Collection<PS>> getForests(Collection<PS> pss)
{
List<Collection<PS>> forests = new MassiveList<>();
List<PS> discovered = new MassiveList<>();
outer:
for (PS ps : pss)
{
if (discovered.contains(ps)) continue outer;
List<PS> forest = new MassiveList<>();
forests.add(forest);
Stack<PS> stack = new Stack<>();
stack.push(ps);
inner:
while (!stack.empty())
{
PS elm = stack.pop();
if (discovered.contains(elm)) continue inner;
System.out.println(elm);
discovered.add(elm);
forest.add(elm);
addIfInSource(elm.withChunkX(elm.getChunkX() + 1), stack, pss);
addIfInSource(elm.withChunkX(elm.getChunkX() - 1), stack, pss);
addIfInSource(elm.withChunkZ(elm.getChunkZ() + 1), stack, pss);
addIfInSource(elm.withChunkZ(elm.getChunkZ() - 1), stack, pss);
}
}
return forests;
}
private static void addIfInSource(PS ps, Stack<PS> stack, Collection<PS> source)
{
if (source.contains(ps)) stack.push(ps);
}
}

View File

@ -208,6 +208,11 @@ public class MConf extends Entity<MConf>
// That would makes outposts possible but also potentially ugly weird claims messing up your Dynmap and ingame experiance.
public boolean claimsMustBeConnected = true;
// Must claims be connected to each other enforced strictly?
// If this is enabled there is also done a check on
// unclaim which makes sure you can't make two different bases by unclaiming land.
public boolean claimsMustBeConnectedStrict = false;
// Would you like to allow unconnected claims when conquering land from another faction?
// Setting this to true would allow taking over someone elses base even if claims normally have to be connected.
// Note that even without this you can pillage/unclaim another factions territory in war.