1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-16 11:37:30 +02:00

Update LocationUtil to updated 2.9 code

This commit is contained in:
snowleo
2012-09-02 19:37:40 +02:00
parent 288604f673
commit 3676881efd

View File

@@ -1,6 +1,10 @@
package net.ess3.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static net.ess3.I18n._;
import org.bukkit.Location;
@@ -68,6 +72,47 @@ public class LocationUtil {
}
return block.getLocation();
}
public final static int RADIUS = 3;
public final static Vector3D[] VOLUME;
public static class Vector3D
{
public Vector3D(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public int x;
public int y;
public int z;
}
static
{
List<Vector3D> pos = new ArrayList<Vector3D>();
for (int x = -RADIUS; x <= RADIUS; x++)
{
for (int y = -RADIUS; y <= RADIUS; y++)
{
for (int z = -RADIUS; z <= RADIUS; z++)
{
pos.add(new Vector3D(x, y, z));
}
}
}
Collections.sort(pos, new Comparator<Vector3D>()
{
@Override
public int compare(Vector3D a, Vector3D b)
{
return (a.x * a.x + a.y * a.y + a.z * a.z) - (b.x * b.x + b.y * b.y + b.z * b.z);
}
});
VOLUME = pos.toArray(new Vector3D[0]);
}
public static Location getSafeDestination(final Location loc) throws Exception
{
@@ -79,25 +124,46 @@ public class LocationUtil {
int x = loc.getBlockX();
int y = (int)Math.round(loc.getY());
int z = loc.getBlockZ();
final int origX = x;
final int origY = y;
final int origZ = z;
while (isBlockAboveAir(world, x, y, z))
{
y -= 1;
if (y < 0)
{
y = origY;
break;
}
}
int i = 0;
while (isBlockUnsafe(world, x, y, z))
{
i++;
if (i >= VOLUME.length)
{
x = origX;
y = origY + RADIUS;
z = origZ;
break;
}
x = origX + VOLUME[i].x;
y = origY + VOLUME[i].y;
z = origZ + VOLUME[i].z;
}
while (isBlockUnsafe(world, x, y, z))
{
y += 1;
if (y >= world.getHighestBlockYAt(x, z))
if (y >= world.getMaxHeight())
{
x += 1;
break;
}
}
while (isBlockUnsafe(world, x, y, z))
{
y -= 1;
@@ -105,7 +171,7 @@ public class LocationUtil {
{
x += 1;
y = world.getHighestBlockYAt(x, z);
if (x - 32 > loc.getBlockX())
if (x - 48 > loc.getBlockX())
{
throw new Exception(_("holeInFloor"));
}
@@ -120,6 +186,15 @@ public class LocationUtil {
}
public static boolean isBlockUnsafe(final World world, final int x, final int y, final int z)
{
if (isBlockDamaging(world, x, y, z))
{
return true;
}
return isBlockAboveAir(world, x, y, z);
}
public static boolean isBlockDamaging(final World world, final int x, final int y, final int z)
{
final Block below = world.getBlockAt(x, y - 1, z);
if (below.getType() == Material.LAVA || below.getType() == Material.STATIONARY_LAVA)
@@ -132,11 +207,16 @@ public class LocationUtil {
return true;
}
if (below.getType() == Material.BED_BLOCK)
{
return true;
}
if ((!AIR_MATERIALS.contains(world.getBlockAt(x, y, z).getType().getId()))
|| (!AIR_MATERIALS.contains(world.getBlockAt(x, y + 1, z).getType().getId())))
{
return true;
}
return isBlockAboveAir(world, x, y, z);
return false;
}
}