2014-10-13 15:14:34 +02:00
|
|
|
package com.massivecraft.factions.cmd;
|
|
|
|
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import com.massivecraft.factions.Perm;
|
|
|
|
import com.massivecraft.factions.entity.BoardColl;
|
|
|
|
import com.massivecraft.factions.entity.Faction;
|
|
|
|
import com.massivecraft.factions.entity.MConf;
|
|
|
|
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
|
|
|
|
import com.massivecraft.massivecore.cmd.req.ReqIsPlayer;
|
|
|
|
import com.massivecraft.massivecore.ps.PS;
|
|
|
|
import com.massivecraft.massivecore.util.MUtil;
|
|
|
|
|
|
|
|
|
|
|
|
public class CmdFactionsSetFill extends CmdFactionsSetXSimple
|
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-11-19 10:30:44 +01:00
|
|
|
public CmdFactionsSetFill(boolean claim)
|
2014-10-13 15:14:34 +02:00
|
|
|
{
|
2014-11-19 10:30:44 +01:00
|
|
|
// Super
|
|
|
|
super(claim);
|
|
|
|
|
2014-10-13 15:14:34 +02:00
|
|
|
// Aliases
|
|
|
|
this.addAliases("f", "fill");
|
|
|
|
|
2014-10-14 08:56:07 +02:00
|
|
|
// Format
|
2014-10-14 09:41:20 +02:00
|
|
|
this.setFormatOne("<h>%s<i> %s <h>%d <i>chunk %s<i> using fill.");
|
|
|
|
this.setFormatMany("<h>%s<i> %s <h>%d <i>chunks near %s<i> using fill.");
|
2014-10-14 08:56:07 +02:00
|
|
|
|
2014-10-13 15:14:34 +02:00
|
|
|
// Requirements
|
|
|
|
this.addRequirements(ReqIsPlayer.get());
|
2014-11-19 10:30:44 +01:00
|
|
|
String node = claim ? Perm.CLAIM_FILL.node : Perm.UNCLAIM_FILL.node;
|
|
|
|
this.addRequirements(ReqHasPerm.get(node));
|
2014-10-13 15:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<PS> getChunks()
|
|
|
|
{
|
|
|
|
// Common Startup
|
2014-11-18 10:00:15 +01:00
|
|
|
final PS chunk = PS.valueOf(me.getLocation()).getChunk(true);
|
2014-10-13 15:14:34 +02:00
|
|
|
final Set<PS> chunks = new LinkedHashSet<PS>();
|
|
|
|
|
|
|
|
// What faction (aka color) resides there?
|
|
|
|
// NOTE: Wilderness/None is valid.
|
|
|
|
final Faction color = BoardColl.get().getFactionAt(chunk);
|
|
|
|
|
|
|
|
// We start where we are!
|
|
|
|
chunks.add(chunk);
|
|
|
|
|
|
|
|
// Flood!
|
|
|
|
int max = MConf.get().setFillMax;
|
|
|
|
floodSearch(chunks, color, max);
|
|
|
|
|
|
|
|
// Limit Reached?
|
|
|
|
if (chunks.size() >= max)
|
|
|
|
{
|
|
|
|
msg("<b>Fill limit of <h>%d <b>reached.", max);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK!
|
|
|
|
return chunks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// FLOOD FILL
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public static void floodSearch(Set<PS> set, Faction color, int max)
|
|
|
|
{
|
|
|
|
// Clean
|
|
|
|
if (set == null) throw new NullPointerException("set");
|
|
|
|
if (color == null) throw new NullPointerException("color");
|
|
|
|
|
|
|
|
// Expand
|
|
|
|
Set<PS> expansion = new LinkedHashSet<PS>();
|
|
|
|
for (PS chunk : set)
|
|
|
|
{
|
|
|
|
Set<PS> neighbours = MUtil.set(
|
|
|
|
chunk.withChunkX(chunk.getChunkX() + 1),
|
|
|
|
chunk.withChunkX(chunk.getChunkX() - 1),
|
|
|
|
chunk.withChunkZ(chunk.getChunkZ() + 1),
|
|
|
|
chunk.withChunkZ(chunk.getChunkZ() - 1)
|
|
|
|
);
|
|
|
|
|
|
|
|
for (PS neighbour : neighbours)
|
|
|
|
{
|
|
|
|
if (set.contains(neighbour)) continue;
|
|
|
|
Faction faction = BoardColl.get().getFactionAt(neighbour);
|
|
|
|
if (faction == null) continue;
|
|
|
|
if (faction != color) continue;
|
|
|
|
expansion.add(neighbour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set.addAll(expansion);
|
|
|
|
|
|
|
|
// No Expansion?
|
|
|
|
if (expansion.isEmpty()) return;
|
|
|
|
|
|
|
|
// Reached Max?
|
|
|
|
if (set.size() >= max) return;
|
|
|
|
|
|
|
|
// Recurse
|
|
|
|
floodSearch(set, color, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|