Getting rid of FLocation all together.

This commit is contained in:
Olof Larsson 2013-04-11 11:18:04 +02:00
parent 4a63d50cbb
commit a34e2be362
4 changed files with 14 additions and 252 deletions

View File

@ -1,225 +0,0 @@
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;
import com.massivecraft.factions.util.MiscUtil;
public class FLocation
{
private String worldName = "world";
private int x = 0;
private int z = 0;
// TODO: It would be wise to ask yourself is there is a better solution to creating loads of new object
// This object is create many times.
//----------------------------------------------//
// Constructors
//----------------------------------------------//
public FLocation()
{
}
public FLocation(String worldName, int x, int z)
{
this.worldName = worldName;
this.x = x;
this.z = z;
}
public FLocation(Location location)
{
this( location.getWorld().getName(), blockToChunk(location.getBlockX()), blockToChunk(location.getBlockZ()) );
}
public FLocation(Player player)
{
this(player.getLocation());
}
public FLocation(FPlayer fplayer)
{
this(fplayer.getPlayer());
}
public FLocation(Block block)
{
this(block.getLocation());
}
//----------------------------------------------//
// Getters and Setters
//----------------------------------------------//
public String getWorldName()
{
return worldName;
}
public World getWorld()
{
return Bukkit.getWorld(worldName);
}
public void setWorldName(String worldName)
{
this.worldName = worldName;
}
public long getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public long getZ()
{
return z;
}
public void setZ(int z)
{
this.z = z;
}
public String getCoordString()
{
return ""+x+","+z;
}
@Override
public String toString() {
return "["+this.getWorldName()+","+this.getCoordString()+"]";
}
//----------------------------------------------//
// Block/Chunk/Region Value Transformation
//----------------------------------------------//
// bit-shifting is used because it's much faster than standard division and multiplication
public static int blockToChunk(int blockVal)
{ // 1 chunk is 16x16 blocks
return blockVal >> 4; // ">> 4" == "/ 16"
}
public static int blockToRegion(int blockVal)
{ // 1 region is 512x512 blocks
return blockVal >> 9; // ">> 9" == "/ 512"
}
public static int chunkToRegion(int chunkVal)
{ // 1 region is 32x32 chunks
return chunkVal >> 5; // ">> 5" == "/ 32"
}
public static int chunkToBlock(int chunkVal)
{
return chunkVal << 4; // "<< 4" == "* 16"
}
public static int regionToBlock(int regionVal)
{
return regionVal << 9; // "<< 9" == "* 512"
}
public static int regionToChunk(int regionVal)
{
return regionVal << 5; // "<< 5" == "* 32"
}
//----------------------------------------------//
// Misc Geometry
//----------------------------------------------//
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<FLocation> getCircle(double radius)
{
Set<FLocation> ret = new LinkedHashSet<FLocation>();
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<FLocation> getArea(FLocation from, FLocation to)
{
HashSet<FLocation> ret = new HashSet<FLocation>();
for (long x : MiscUtil.range(from.getX(), to.getX()))
{
for (long z : MiscUtil.range(from.getZ(), to.getZ()))
{
ret.add(new FLocation(from.getWorldName(), (int)x, (int)z));
}
}
return ret;
}
//----------------------------------------------//
// Comparison
//----------------------------------------------//
@Override
public int hashCode()
{
// should be fast, with good range and few hash collisions: (x * 512) + z + worldName.hashCode
return (this.x << 9) + this.z + (this.worldName != null ? this.worldName.hashCode() : 0);
}
@Override
public boolean equals(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof FLocation))
return false;
FLocation that = (FLocation) obj;
return this.x == that.x && this.z == that.z && ( this.worldName==null ? that.worldName==null : this.worldName.equals(that.worldName) );
}
}

View File

@ -428,22 +428,6 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
// TODO: Use Mixin to get this PS instead // TODO: Use Mixin to get this PS instead
return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())) == this.getFaction(); return BoardColl.get().getFactionAt(PS.valueOf(this.getPlayer())) == this.getFaction();
} }
/*public boolean isInOthersTerritory()
{
Faction factionHere = Board.getFactionAt(new FLocation(this));
return factionHere != null && factionHere.isNormal() && factionHere != this.getFaction();
}*/
/*public boolean isInAllyTerritory()
{
return Board.getFactionAt(new FLocation(this)).getRelationTo(this) == Rel.ALLY;
}*/
/*public boolean isInNeutralTerritory()
{
return Board.getFactionAt(new FLocation(this)).getRelationTo(this) == Rel.NEUTRAL;
}*/
public boolean isInEnemyTerritory() public boolean isInEnemyTerritory()
{ {

View File

@ -1,10 +1,10 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Perm; import com.massivecraft.factions.Perm;
import com.massivecraft.factions.util.SpiralTask; import com.massivecraft.factions.util.SpiralTask;
import com.massivecraft.mcore.ps.PS;
public class CmdFactionsClaim extends FCommand public class CmdFactionsClaim extends FCommand
@ -55,7 +55,7 @@ public class CmdFactionsClaim extends FCommand
return; return;
} }
new SpiralTask(new FLocation(me), radius) new SpiralTask(PS.valueOf(me), radius)
{ {
private int failCount = 0; private int failCount = 0;
private final int limit = ConfServer.radiusClaimFailureLimit - 1; private final int limit = ConfServer.radiusClaimFailureLimit - 1;

View File

@ -6,8 +6,8 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.mcore.ps.PS;
/* /*
@ -40,12 +40,14 @@ public abstract class SpiralTask implements Runnable
private transient int current = 0; private transient int current = 0;
// @SuppressWarnings("LeakingThisInConstructor") This actually triggers a warning in Eclipse xD Could we find another way to suppress the error please? :) // @SuppressWarnings("LeakingThisInConstructor") This actually triggers a warning in Eclipse xD Could we find another way to suppress the error please? :)
public SpiralTask(FLocation fLocation, int radius) public SpiralTask(PS chunk, int radius)
{ {
chunk = chunk.getChunk(true);
// limit is determined based on spiral leg length for given radius; see insideRadius() // limit is determined based on spiral leg length for given radius; see insideRadius()
this.limit = (radius - 1) * 2; this.limit = (radius - 1) * 2;
this.world = Bukkit.getWorld(fLocation.getWorldName()); this.world = Bukkit.getWorld(chunk.getWorld());
if (this.world == null) if (this.world == null)
{ {
Factions.get().log(Level.WARNING, "[SpiralTask] A valid world must be specified!"); Factions.get().log(Level.WARNING, "[SpiralTask] A valid world must be specified!");
@ -53,8 +55,8 @@ public abstract class SpiralTask implements Runnable
return; return;
} }
this.x = (int)fLocation.getX(); this.x = (int)chunk.getChunkX();
this.z = (int)fLocation.getZ(); this.z = (int)chunk.getChunkZ();
this.readyToGo = true; this.readyToGo = true;
@ -70,11 +72,11 @@ public abstract class SpiralTask implements Runnable
public abstract boolean work(); public abstract boolean work();
/* /*
* Returns an FLocation pointing at the current chunk X and Z values. * Returns a PS pointing at the current chunk X and Z values.
*/ */
public final FLocation currentFLocation() public final PS currentChunk()
{ {
return new FLocation(world.getName(), x, z); return PS.valueOf(this.world.getName(), null, null, null, null, null, null, this.x, this.z, null, null, null, null, null);
} }
/* /*
* Returns a Location pointing at the current chunk X and Z values. * Returns a Location pointing at the current chunk X and Z values.
@ -82,7 +84,8 @@ public abstract class SpiralTask implements Runnable
*/ */
public final Location currentLocation() public final Location currentLocation()
{ {
return new Location(world, FLocation.chunkToBlock(x), 65.0, FLocation.chunkToBlock(z));
return new Location(world, this.x * 16, 65.0, this.z * 16);
} }
/* /*
* Returns current chunk X and Z values. * Returns current chunk X and Z values.