From 5db20e96252dadfa96f2f41903542594f636d8c9 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 27 Nov 2011 22:47:40 +0100 Subject: [PATCH] Added radius claim --- src/com/massivecraft/factions/FLocation.java | 47 ++++++++++++++++++- .../massivecraft/factions/cmd/CmdClaim.java | 23 ++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/com/massivecraft/factions/FLocation.java b/src/com/massivecraft/factions/FLocation.java index 48a2fe64..856d0e03 100644 --- a/src/com/massivecraft/factions/FLocation.java +++ b/src/com/massivecraft/factions/FLocation.java @@ -1,8 +1,12 @@ package com.massivecraft.factions; import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -64,6 +68,11 @@ public class FLocation { return worldName; } + + public World getWorld() + { + return Bukkit.getWorld(worldName); + } public void setWorldName(String worldName) { @@ -104,10 +113,46 @@ public class FLocation // Misc Geometry //----------------------------------------------// - public FLocation getRelative(int dx, int dz) { + public FLocation getRelative(int dx, int dz) + { return new FLocation(this.worldName, this.x + dx, this.z + dz); } + public double getDistanceTo(FLocation that) + { + double dx = that.x - this.x; + double dz = that.z - this.z; + return Math.sqrt(dx*dx+dz*dz); + } + + //----------------------------------------------// + // Some Geometry + //----------------------------------------------// + public Set getCircle(double radius) + { + Set ret = new LinkedHashSet(); + if (radius <= 0) return ret; + + int xfrom = (int) Math.floor(this.x - radius); + int xto = (int) Math.ceil(this.x + radius); + int zfrom = (int) Math.floor(this.z - radius); + int zto = (int) Math.ceil(this.z + radius); + + for (int x=xfrom; x<=xto; x++) + { + for (int z=zfrom; z<=zto; z++) + { + FLocation potential = new FLocation(this.worldName, x, z); + if (this.getDistanceTo(potential) <= radius) + { + ret.add(potential); + } + } + } + + return ret; + } + public static HashSet getArea(FLocation from, FLocation to) { HashSet ret = new HashSet(); diff --git a/src/com/massivecraft/factions/cmd/CmdClaim.java b/src/com/massivecraft/factions/cmd/CmdClaim.java index 5f97105d..f4fd63d0 100644 --- a/src/com/massivecraft/factions/cmd/CmdClaim.java +++ b/src/com/massivecraft/factions/cmd/CmdClaim.java @@ -1,5 +1,10 @@ package com.massivecraft.factions.cmd; +import java.util.Set; + +import org.bukkit.Location; + +import com.massivecraft.factions.FLocation; import com.massivecraft.factions.Faction; import com.massivecraft.factions.struct.Permission; @@ -13,6 +18,7 @@ public class CmdClaim extends FCommand //this.requiredArgs.add(""); this.optionalArgs.put("faction", "your"); + this.optionalArgs.put("radius", "1"); this.permission = Permission.CLAIM.node; this.disableOnLock = true; @@ -28,8 +34,23 @@ public class CmdClaim extends FCommand @Override public void perform() { + // Read and validate input Faction forFaction = this.argAsFaction(0, myFaction); - fme.attemptClaim(forFaction, me.getLocation(), true); + double radius = this.argAsDouble(1, 1d); + radius -= 0.5; + if (radius <= 0) + { + msg("That radius is to small."); + return; + } + + // Get the FLocations + Set flocs = new FLocation(me).getCircle(radius); + p.log(flocs); + for (FLocation floc : flocs) + { + fme.attemptClaim(forFaction, new Location(floc.getWorld(), floc.getX()*16, 1, floc.getZ()*16), true); + } } }