Rewrote TargetBlock

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1589 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-06-06 12:01:23 +00:00
parent c038751c0b
commit 4bbc2aa8d5

View File

@ -1,36 +1,44 @@
package com.earth2me.essentials;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.block.Block;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
/**
* @author toi
* Thanks to Raphfrk for optimization of this class.
*/
public class TargetBlock {
private Location loc;
private double viewHeight;
private int maxDistance;
private int[] blockToIgnore;
private double checkDistance, curDistance;
private double xRotation, yRotation;
private Vector targetPos = new Vector();
private Vector targetPosDouble = new Vector();
private Vector prevPos = new Vector();
private final Vector offset = new Vector();
/**
* Original authors: toi & Raphfrk
*/
public class TargetBlock
{
private transient final Location location;
private transient final double viewHeight;
private transient final int maxDistance;
private transient final int[] blockToIgnore;
private transient final double checkDistance;
private transient double curDistance;
private transient double targetPositionX;
private transient double targetPositionY;
private transient double targetPositionZ;
private transient int itargetPositionX;
private transient int itargetPositionY;
private transient int itargetPositionZ;
private transient int prevPositionX;
private transient int prevPositionY;
private transient int prevPositionZ;
private transient final double offsetX;
private transient final double offsetY;
private transient final double offsetZ;
/**
* Constructor requiring a player, uses default values
*
* @param player Player to work with
*/
public TargetBlock(Player player)
public TargetBlock(final Player player)
{
this.setValues(player.getLocation(), 300, 1.65, 0.2, null);
this(player.getLocation(), 300, 1.65, 0.2, null);
}
/**
@ -38,9 +46,9 @@ public class TargetBlock {
*
* @param loc Location to work with
*/
public TargetBlock(Location loc)
public TargetBlock(final Location loc)
{
this.setValues(loc, 300, 0, 0.2, null);
this(loc, 300, 0, 0.2, null);
}
/**
@ -50,9 +58,9 @@ public class TargetBlock {
* @param maxDistance How far it checks for blocks
* @param checkDistance How often to check for blocks, the smaller the more precise
*/
public TargetBlock(Player player, int maxDistance, double checkDistance)
public TargetBlock(final Player player, final int maxDistance, final double checkDistance)
{
this.setValues(player.getLocation(), maxDistance, 1.65, checkDistance, null);
this(player.getLocation(), maxDistance, 1.65, checkDistance, null);
}
/**
@ -62,8 +70,9 @@ public class TargetBlock {
* @param maxDistance How far it checks for blocks
* @param checkDistance How often to check for blocks, the smaller the more precise
*/
public TargetBlock(Location loc, int maxDistance, double checkDistance) {
this.setValues(loc, maxDistance, 0, checkDistance, null);
public TargetBlock(final Location loc, final int maxDistance, final double checkDistance)
{
this(loc, maxDistance, 0, checkDistance, null);
}
/**
@ -74,9 +83,9 @@ public class TargetBlock {
* @param checkDistance How often to check for blocks, the smaller the more precise
* @param blocksToIgnore Integer array of what block ids to ignore while checking for viable targets
*/
public TargetBlock (Player player, int maxDistance, double checkDistance, int[] blocksToIgnore)
public TargetBlock(final Player player, final int maxDistance, final double checkDistance, final int[] blocksToIgnore)
{
this.setValues(player.getLocation(), maxDistance, 1.65, checkDistance, blocksToIgnore);
this(player.getLocation(), maxDistance, 1.65, checkDistance, blocksToIgnore);
}
/**
@ -87,9 +96,9 @@ public class TargetBlock {
* @param checkDistance How often to check for blocks, the smaller the more precise
* @param blocksToIgnore Array of what block ids to ignore while checking for viable targets
*/
public TargetBlock (Location loc, int maxDistance, double checkDistance, int[] blocksToIgnore)
public TargetBlock(final Location loc, final int maxDistance, final double checkDistance, final int[] blocksToIgnore)
{
this.setValues(loc, maxDistance, 0, checkDistance, blocksToIgnore);
this(loc, maxDistance, 0, checkDistance, blocksToIgnore);
}
/**
@ -100,10 +109,9 @@ public class TargetBlock {
* @param checkDistance How often to check for blocks, the smaller the more precise
* @param blocksToIgnore String ArrayList of what block ids to ignore while checking for viable targets
*/
public TargetBlock (Player player, int maxDistance, double checkDistance, ArrayList<String> blocksToIgnore)
public TargetBlock(final Player player, final int maxDistance, final double checkDistance, final List<String> blocksToIgnore)
{
int[] bti = this.convertStringArraytoIntArray(blocksToIgnore);
this.setValues(player.getLocation(), maxDistance, 1.65, checkDistance, bti);
this(player.getLocation(), maxDistance, 1.65, checkDistance, TargetBlock.convertStringArraytoIntArray(blocksToIgnore));
}
/**
@ -114,10 +122,9 @@ public class TargetBlock {
* @param checkDistance How often to check for blocks, the smaller the more precise
* @param blocksToIgnore String ArrayList of what block ids to ignore while checking for viable targets
*/
public TargetBlock (Location loc, int maxDistance, double checkDistance, ArrayList<String> blocksToIgnore)
public TargetBlock(final Location loc, final int maxDistance, final double checkDistance, final List<String> blocksToIgnore)
{
int[] bti = this.convertStringArraytoIntArray(blocksToIgnore);
this.setValues(loc, maxDistance, 0, checkDistance, bti);
this(loc, maxDistance, 0, checkDistance, TargetBlock.convertStringArraytoIntArray(blocksToIgnore));
}
/**
@ -129,35 +136,47 @@ public class TargetBlock {
* @param checkDistance How often to check for blocks, the smaller the more precise
* @param blocksToIgnore Ids of blocks to ignore while checking for viable targets
*/
private void setValues(Location loc, int maxDistance, double viewHeight, double checkDistance, int[] blocksToIgnore)
private TargetBlock(final Location loc, final int maxDistance, final double viewHeight, final double checkDistance, final int[] blocksToIgnore)
{
this.loc = loc;
this.location = loc;
this.maxDistance = maxDistance;
this.viewHeight = viewHeight;
this.checkDistance = checkDistance;
this.blockToIgnore = blocksToIgnore;
this.curDistance = 0;
xRotation = (loc.getYaw() + 90) % 360;
yRotation = loc.getPitch() * -1;
if (blocksToIgnore == null || blocksToIgnore.length == 0)
{
this.blockToIgnore = new int[0];
}
else
{
this.blockToIgnore = new int[blocksToIgnore.length];
System.arraycopy(blocksToIgnore, 0, this.blockToIgnore, 0, this.blockToIgnore.length);
}
double h = (checkDistance * Math.cos(Math.toRadians(yRotation)));
offset.setY((checkDistance * Math.sin(Math.toRadians(yRotation))));
offset.setX((h * Math.cos(Math.toRadians(xRotation))));
offset.setZ((h * Math.sin(Math.toRadians(xRotation))));
final double xRotation = (loc.getYaw() + 90) % 360;
final double yRotation = loc.getPitch() * -1;
targetPosDouble = new Vector(loc.getX(), loc.getY() + viewHeight, loc.getZ());
targetPos = new Vector( targetPosDouble.getBlockX(), targetPosDouble.getBlockY(), targetPosDouble.getBlockZ());
prevPos = targetPos.clone();
final double hypotenuse = (checkDistance * Math.cos(Math.toRadians(yRotation)));
offsetX = hypotenuse * Math.cos(Math.toRadians(xRotation));
offsetY = checkDistance * Math.sin(Math.toRadians(yRotation));
offsetZ = hypotenuse * Math.sin(Math.toRadians(xRotation));
reset();
}
/**
* Call this to reset checking position to allow you to check for a new target with the same TargetBlock instance.
*/
public void reset()
public final void reset()
{
targetPosDouble = new Vector(loc.getX(), loc.getY() + viewHeight, loc.getZ());
targetPos = new Vector( targetPosDouble.getBlockX(), targetPosDouble.getBlockY(), targetPosDouble.getBlockZ());
prevPos = targetPos.clone();
targetPositionX = location.getX();
targetPositionY = location.getY() + viewHeight;
targetPositionZ = location.getZ();
itargetPositionX = (int)Math.floor(targetPositionX);
itargetPositionY = (int)Math.floor(targetPositionY);
itargetPositionZ = (int)Math.floor(targetPositionZ);
prevPositionX = itargetPositionX;
prevPositionY = itargetPositionY;
prevPositionZ = itargetPositionZ;
this.curDistance = 0;
}
@ -169,17 +188,16 @@ public class TargetBlock {
*/
public double getDistanceToBlock()
{
Vector blockUnderPlayer = new Vector(
(int) Math.floor(loc.getX() + 0.5),
(int) Math.floor(loc.getY() - 0.5),
(int) Math.floor(loc.getZ() + 0.5));
final double blockUnderPlayerX = Math.floor(location.getX() + 0.5);
final double blockUnderPlayerY = Math.floor(location.getY() - 0.5);
final double blockUnderPlayerZ = Math.floor(location.getZ() + 0.5);
Block blk = getTargetBlock();
double x = blk.getX() - blockUnderPlayer.getBlockX();
double y = blk.getY() - blockUnderPlayer.getBlockY();
double z = blk.getZ() - blockUnderPlayer.getBlockZ();
final Block block = getTargetBlock();
final double distX = block.getX() - blockUnderPlayerX;
final double distY = block.getY() - blockUnderPlayerY;
final double distZ = block.getZ() - blockUnderPlayerZ;
return Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)));
return Math.sqrt(distX*distX + distY*distY + distZ*distZ);
}
/**
@ -190,17 +208,7 @@ public class TargetBlock {
*/
public int getDistanceToBlockRounded()
{
Vector blockUnderPlayer = new Vector(
(int) Math.floor(loc.getX() + 0.5),
(int) Math.floor(loc.getY() - 0.5),
(int) Math.floor(loc.getZ() + 0.5));
Block blk = getTargetBlock();
double x = blk.getX() - blockUnderPlayer.getBlockX();
double y = blk.getY() - blockUnderPlayer.getBlockY();
double z = blk.getZ() - blockUnderPlayer.getBlockZ();
return (int) Math.round((Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)))));
return (int)Math.round(getDistanceToBlock());
}
/**
@ -210,8 +218,7 @@ public class TargetBlock {
*/
public int getXDistanceToBlock()
{
this.reset();
return (int) Math.floor(getTargetBlock().getX() - loc.getBlockX() + 0.5);
return (int)Math.floor(getTargetBlock().getX() - location.getBlockX() + 0.5);
}
/**
@ -221,8 +228,7 @@ public class TargetBlock {
*/
public int getYDistanceToBlock()
{
this.reset();
return (int) Math.floor(getTargetBlock().getY() - loc.getBlockY() + viewHeight);
return (int)Math.floor(getTargetBlock().getY() - location.getBlockY() + viewHeight);
}
/**
@ -232,8 +238,7 @@ public class TargetBlock {
*/
public int getZDistanceToBlock()
{
this.reset();
return (int) Math.floor(getTargetBlock().getZ() - loc.getBlockZ() + 0.5);
return (int)Math.floor(getTargetBlock().getZ() - location.getBlockZ() + 0.5);
}
/**
@ -241,12 +246,17 @@ public class TargetBlock {
*
* @return Block
*/
@SuppressWarnings("empty-statement")
public Block getTargetBlock()
{
this.reset();
while ((getNextBlock() != null) && ((getCurrentBlock().getTypeId() == 0) || this.blockToIgnoreHasValue(getCurrentBlock().getTypeId())));
return getCurrentBlock();
Block block;
do
{
block = getNextBlock();
}
while (block != null && ((block.getTypeId() == 0) || this.blockIsIgnored(block.getTypeId())));
return block;
}
/**
@ -255,21 +265,9 @@ public class TargetBlock {
* @param typeID ID of type to set the block to
* @return boolean
*/
@SuppressWarnings("empty-statement")
public boolean setTargetBlock(int typeID)
public boolean setTargetBlock(final int typeID)
{
if (Material.getMaterial(typeID) != null)
{
this.reset();
while (getNextBlock() != null && getCurrentBlock().getTypeId() == 0);
if (getCurrentBlock() != null)
{
Block blk = loc.getWorld().getBlockAt(targetPos.getBlockX(), targetPos.getBlockY(), targetPos.getBlockZ());
blk.setTypeId(typeID);
return true;
}
}
return false;
return setTargetBlock(Material.getMaterial(typeID));
}
/**
@ -279,14 +277,16 @@ public class TargetBlock {
* @return boolean
*/
@SuppressWarnings("empty-statement")
public boolean setTargetBlock(Material type)
public boolean setTargetBlock(final Material type)
{
this.reset();
while ((getNextBlock() != null) && ((getCurrentBlock().getTypeId() == 0) || this.blockToIgnoreHasValue(getCurrentBlock().getTypeId())));
if (getCurrentBlock() != null)
if (type == null)
{
Block blk = loc.getWorld().getBlockAt(targetPos.getBlockX(), targetPos.getBlockY(), targetPos.getBlockZ());
blk.setType(type);
return false;
}
final Block block = getTargetBlock();
if (block != null)
{
block.setType(type);
return true;
}
return false;
@ -299,22 +299,9 @@ public class TargetBlock {
* @param type Name of type to set the block to
* @return boolean
*/
@SuppressWarnings("empty-statement")
public boolean setTargetBlock(String type)
public boolean setTargetBlock(final String type)
{
Material mat = Material.valueOf(type);
if (mat != null)
{
this.reset();
while ((getNextBlock() != null) && ((getCurrentBlock().getTypeId() == 0) || this.blockToIgnoreHasValue(getCurrentBlock().getTypeId())));
if (getCurrentBlock() != null)
{
Block blk = loc.getWorld().getBlockAt(targetPos.getBlockX(), targetPos.getBlockY(), targetPos.getBlockZ());
blk.setType(mat);
return true;
}
}
return false;
return setTargetBlock(Material.valueOf(type));
}
/**
@ -322,18 +309,14 @@ public class TargetBlock {
*
* @return Block
*/
@SuppressWarnings("empty-statement")
public Block getFaceBlock()
{
while ((getNextBlock() != null) && ((getCurrentBlock().getTypeId() == 0) || this.blockToIgnoreHasValue(getCurrentBlock().getTypeId())));
if (getCurrentBlock() != null)
{
return getPreviousBlock();
}
else
final Block block = getTargetBlock();
if (block == null)
{
return null;
}
return getPreviousBlock();
}
/**
@ -342,18 +325,9 @@ public class TargetBlock {
* @param typeID
* @return boolean
*/
public boolean setFaceBlock(int typeID)
public boolean setFaceBlock(final int typeID)
{
if (Material.getMaterial(typeID) != null)
{
if (getCurrentBlock() != null)
{
Block blk = loc.getWorld().getBlockAt(prevPos.getBlockX(), prevPos.getBlockY(), prevPos.getBlockZ());
blk.setTypeId(typeID);
return true;
}
}
return false;
return setFaceBlock(Material.getMaterial(typeID));
}
/**
@ -362,11 +336,15 @@ public class TargetBlock {
* @param type
* @return boolean
*/
public boolean setFaceBlock(Material type)
public boolean setFaceBlock(final Material type)
{
if (type == null)
{
return false;
}
if (getCurrentBlock() != null)
{
Block blk = loc.getWorld().getBlockAt(prevPos.getBlockX(), prevPos.getBlockY(), prevPos.getBlockZ());
final Block blk = location.getWorld().getBlockAt(prevPositionX, prevPositionY, prevPositionZ);
blk.setType(type);
return true;
}
@ -380,19 +358,9 @@ public class TargetBlock {
* @param type
* @return boolean
*/
public boolean setFaceBlock(String type)
public boolean setFaceBlock(final String type)
{
Material mat = Material.valueOf(type);
if (mat != null)
{
if (getCurrentBlock() != null)
{
Block blk = loc.getWorld().getBlockAt(prevPos.getBlockX(), prevPos.getBlockY(), prevPos.getBlockZ());
blk.setType(mat);
return true;
}
}
return false;
return setFaceBlock(Material.valueOf(type));
}
/**
@ -402,23 +370,27 @@ public class TargetBlock {
*/
public Block getNextBlock()
{
prevPos = targetPos.clone();
prevPositionX = itargetPositionX;
prevPositionY = itargetPositionY;
prevPositionZ = itargetPositionZ;
do
{
curDistance += checkDistance;
targetPosDouble.setX(offset.getX() + targetPosDouble.getX());
targetPosDouble.setY(offset.getY() + targetPosDouble.getY());
targetPosDouble.setZ(offset.getZ() + targetPosDouble.getZ());
targetPos = new Vector( targetPosDouble.getBlockX(), targetPosDouble.getBlockY(), targetPosDouble.getBlockZ());
targetPositionX += offsetX;
targetPositionY += offsetY;
targetPositionZ += offsetZ;
itargetPositionX = (int)Math.floor(targetPositionX);
itargetPositionY = (int)Math.floor(targetPositionY);
itargetPositionZ = (int)Math.floor(targetPositionZ);
}
while (curDistance <= maxDistance && targetPos.getBlockX() == prevPos.getBlockX() && targetPos.getBlockY() == prevPos.getBlockY() && targetPos.getBlockZ() == prevPos.getBlockZ());
while (curDistance <= maxDistance && itargetPositionX == prevPositionX && itargetPositionY == prevPositionY && itargetPositionZ == prevPositionZ);
if (curDistance > maxDistance)
{
return null;
}
return this.loc.getWorld().getBlockAt(this.targetPos.getBlockX(), this.targetPos.getBlockY(), this.targetPos.getBlockZ());
return this.location.getWorld().getBlockAt(itargetPositionX, itargetPositionY, itargetPositionZ);
}
/**
@ -428,14 +400,16 @@ public class TargetBlock {
*/
public Block getCurrentBlock()
{
if (curDistance > maxDistance)
Block block;
if (curDistance <= maxDistance)
{
return null;
block = this.location.getWorld().getBlockAt(itargetPositionX, itargetPositionY, itargetPositionZ);
}
else
{
return this.loc.getWorld().getBlockAt(this.targetPos.getBlockX(), this.targetPos.getBlockY(), this.targetPos.getBlockZ());
block = null;
}
return block;
}
/**
@ -443,18 +417,9 @@ public class TargetBlock {
*
* @param typeID
*/
public boolean setCurrentBlock(int typeID)
public boolean setCurrentBlock(final int typeID)
{
if (Material.getMaterial(typeID) != null)
{
Block blk = getCurrentBlock();
if (blk != null)
{
blk.setTypeId(typeID);
return true;
}
}
return false;
return setCurrentBlock(Material.getMaterial(typeID));
}
/**
@ -462,10 +427,10 @@ public class TargetBlock {
*
* @param type
*/
public boolean setCurrentBlock(Material type)
public boolean setCurrentBlock(final Material type)
{
Block blk = getCurrentBlock();
if (blk != null)
final Block blk = getCurrentBlock();
if (blk != null && type != null)
{
blk.setType(type);
return true;
@ -479,19 +444,9 @@ public class TargetBlock {
*
* @param type
*/
public boolean setCurrentBlock(String type)
public boolean setCurrentBlock(final String type)
{
Material mat = Material.valueOf(type);
if (mat != null)
{
Block blk = getCurrentBlock();
if (blk != null)
{
blk.setType(mat);
return true;
}
}
return false;
return setCurrentBlock(Material.valueOf(type));
}
/**
@ -501,7 +456,7 @@ public class TargetBlock {
*/
public Block getPreviousBlock()
{
return this.loc.getWorld().getBlockAt(prevPos.getBlockX(), prevPos.getBlockY(), prevPos.getBlockZ());
return this.location.getWorld().getBlockAt(prevPositionX, prevPositionY, prevPositionZ);
}
/**
@ -509,18 +464,9 @@ public class TargetBlock {
*
* @param typeID
*/
public boolean setPreviousBlock(int typeID)
public boolean setPreviousBlock(final int typeID)
{
if (Material.getMaterial(typeID) != null)
{
Block blk = getPreviousBlock();
if (blk != null)
{
blk.setTypeId(typeID);
return true;
}
}
return false;
return setPreviousBlock(Material.getMaterial(typeID));
}
/**
@ -528,10 +474,10 @@ public class TargetBlock {
*
* @param type
*/
public boolean setPreviousBlock(Material type)
public boolean setPreviousBlock(final Material type)
{
Block blk = getPreviousBlock();
if (blk != null)
final Block blk = getPreviousBlock();
if (blk != null && type != null)
{
blk.setType(type);
return true;
@ -545,27 +491,15 @@ public class TargetBlock {
*
* @param type
*/
public boolean setPreviousBlock(String type)
public boolean setPreviousBlock(final String type)
{
Material mat = Material.valueOf(type);
if (mat != null)
{
Block blk = getPreviousBlock();
if (blk != null)
{
blk.setType(mat);
return true;
}
}
return false;
return setPreviousBlock(Material.valueOf(type));
}
private int[] convertStringArraytoIntArray(ArrayList<String> array)
private static int[] convertStringArraytoIntArray(final List<String> array)
{
if (array != null)
{
int intarray[] = new int[array.size()];
for (int i = 0; i < array.size(); i++)
final int intarray[] = new int[array == null ? 0 : array.size()];
for (int i = 0; i < intarray.length; i++)
{
try
{
@ -573,27 +507,20 @@ public class TargetBlock {
}
catch (NumberFormatException nfe)
{
intarray[i] = 0;
}
}
return intarray;
}
return null;
}
private boolean blockToIgnoreHasValue(int value)
{
if (this.blockToIgnore != null)
{
if (this.blockToIgnore.length > 0)
private boolean blockIsIgnored(final int value)
{
for (int i : this.blockToIgnore)
{
if (i == value)
{
return true;
}
}
}
return false;
}
}