summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsnowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb>2011-06-06 12:01:23 +0000
committersnowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb>2011-06-06 12:01:23 +0000
commit4bbc2aa8d538df2211466bae41220b8ee39a0aa9 (patch)
tree24557fb40ad753e293ba92c86fb6dc22f7e2bb3c
parentc038751c0b3e1630f935372aab33007bbf55b340 (diff)
downloadEssentials-4bbc2aa8d538df2211466bae41220b8ee39a0aa9.tar
Essentials-4bbc2aa8d538df2211466bae41220b8ee39a0aa9.tar.gz
Essentials-4bbc2aa8d538df2211466bae41220b8ee39a0aa9.tar.lz
Essentials-4bbc2aa8d538df2211466bae41220b8ee39a0aa9.tar.xz
Essentials-4bbc2aa8d538df2211466bae41220b8ee39a0aa9.zip
Rewrote TargetBlock
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1589 e251c2fe-e539-e718-e476-b85c1f46cddb
-rw-r--r--Essentials/src/com/earth2me/essentials/TargetBlock.java1105
1 files changed, 516 insertions, 589 deletions
diff --git a/Essentials/src/com/earth2me/essentials/TargetBlock.java b/Essentials/src/com/earth2me/essentials/TargetBlock.java
index ec9cd796e..d1f2e5cbb 100644
--- a/Essentials/src/com/earth2me/essentials/TargetBlock.java
+++ b/Essentials/src/com/earth2me/essentials/TargetBlock.java
@@ -1,599 +1,526 @@
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.
+ * Original authors: toi & Raphfrk
*/
-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();
-
- /**
- * Constructor requiring a player, uses default values
- *
- * @param player Player to work with
- */
- public TargetBlock(Player player)
- {
- this.setValues(player.getLocation(), 300, 1.65, 0.2, null);
- }
-
- /**
- * Constructor requiring a location, uses default values
- *
- * @param loc Location to work with
- */
- public TargetBlock(Location loc)
- {
- this.setValues(loc, 300, 0, 0.2, null);
- }
-
- /**
- * Constructor requiring a player, max distance and a checking distance
- *
- * @param player Player to work with
- * @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)
- {
- this.setValues(player.getLocation(), maxDistance, 1.65, checkDistance, null);
- }
-
- /**
- * Constructor requiring a location, max distance and a checking distance
- *
- * @param loc What location to work with
- * @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);
- }
-
- /**
- * Constructor requiring a player, max distance, checking distance and an array of blocks to ignore
- *
- * @param player What player to work with
- * @param maxDistance How far it checks for blocks
- * @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)
- {
- this.setValues(player.getLocation(), maxDistance, 1.65, checkDistance, blocksToIgnore);
- }
-
- /**
- * Constructor requiring a location, max distance, checking distance and an array of blocks to ignore
- *
- * @param loc What location to work with
- * @param maxDistance How far it checks for blocks
- * @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)
- {
- this.setValues(loc, maxDistance, 0, checkDistance, blocksToIgnore);
- }
-
- /**
- * Constructor requiring a player, max distance, checking distance and an array of blocks to ignore
- *
- * @param player What player to work with
- * @param maxDistance How far it checks for blocks
- * @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)
- {
- int[] bti = this.convertStringArraytoIntArray(blocksToIgnore);
- this.setValues(player.getLocation(), maxDistance, 1.65, checkDistance, bti);
- }
-
- /**
- * Constructor requiring a location, max distance, checking distance and an array of blocks to ignore
- *
- * @param loc What location to work with
- * @param maxDistance How far it checks for blocks
- * @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)
- {
- int[] bti = this.convertStringArraytoIntArray(blocksToIgnore);
- this.setValues(loc, maxDistance, 0, checkDistance, bti);
- }
-
- /**
- * Set the values, all constructors uses this function
- *
- * @param loc Location of the view
- * @param maxDistance How far it checks for blocks
- * @param viewPos Where the view is positioned in y-axis
- * @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)
- {
- this.loc = 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;
-
- 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))));
-
- targetPosDouble = new Vector(loc.getX(), loc.getY() + viewHeight, loc.getZ());
- targetPos = new Vector( targetPosDouble.getBlockX(), targetPosDouble.getBlockY(), targetPosDouble.getBlockZ());
- prevPos = targetPos.clone();
- }
-
- /**
- * Call this to reset checking position to allow you to check for a new target with the same TargetBlock instance.
- */
- public void reset()
- {
- targetPosDouble = new Vector(loc.getX(), loc.getY() + viewHeight, loc.getZ());
- targetPos = new Vector( targetPosDouble.getBlockX(), targetPosDouble.getBlockY(), targetPosDouble.getBlockZ());
- prevPos = targetPos.clone();
- this.curDistance = 0;
- }
-
- /**
- * Gets the distance to a block. Measures from the block underneath the player to the targetblock
- * Should only be used when passing player as an constructor parameter
- *
- * @return double
- */
- 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));
-
- Block blk = getTargetBlock();
- double x = blk.getX() - blockUnderPlayer.getBlockX();
- double y = blk.getY() - blockUnderPlayer.getBlockY();
- double z = blk.getZ() - blockUnderPlayer.getBlockZ();
-
- return Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)));
- }
-
- /**
- * Gets the rounded distance to a block. Measures from the block underneath the player to the targetblock
- * Should only be used when passing player as an constructor parameter
- *
- * @return int
- */
- 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)))));
- }
-
- /**
- * Gets the floored x distance to a block.
- *
- * @return int
- */
- public int getXDistanceToBlock()
- {
- this.reset();
- return (int) Math.floor(getTargetBlock().getX() - loc.getBlockX() + 0.5);
- }
-
- /**
- * Gets the floored y distance to a block
- *
- * @return int
- */
- public int getYDistanceToBlock()
- {
- this.reset();
- return (int) Math.floor(getTargetBlock().getY() - loc.getBlockY() + viewHeight);
- }
-
- /**
- * Gets the floored z distance to a block
- *
- * @return int
- */
- public int getZDistanceToBlock()
- {
- this.reset();
- return (int) Math.floor(getTargetBlock().getZ() - loc.getBlockZ() + 0.5);
- }
-
- /**
- * Returns the block at the sight. Returns null if out of range or if no viable target was found
- *
- * @return Block
- */
- @SuppressWarnings("empty-statement")
- public Block getTargetBlock()
- {
- this.reset();
- while ((getNextBlock() != null) && ((getCurrentBlock().getTypeId() == 0) || this.blockToIgnoreHasValue(getCurrentBlock().getTypeId())));
- return getCurrentBlock();
- }
-
- /**
- * Sets the type of the block at the sight. Returns false if the block wasn't set.
- *
- * @param typeID ID of type to set the block to
- * @return boolean
- */
- @SuppressWarnings("empty-statement")
- public boolean setTargetBlock(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;
- }
-
- /**
- * Sets the type of the block at the sight. Returns false if the block wasn't set.
- *
- * @param type Material to set the block to
- * @return boolean
- */
- @SuppressWarnings("empty-statement")
- public boolean setTargetBlock(Material type)
- {
- 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(type);
- return true;
- }
- return false;
- }
-
- /**
- * Sets the type of the block at the sight. Returns false if the block wasn't set.
- * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
- *
- * @param type Name of type to set the block to
- * @return boolean
- */
- @SuppressWarnings("empty-statement")
- public boolean setTargetBlock(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;
- }
-
- /**
- * Returns the block attached to the face at the sight. Returns null if out of range or if no viable target was found
- *
- * @return Block
- */
+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(final Player player)
+ {
+ this(player.getLocation(), 300, 1.65, 0.2, null);
+ }
+
+ /**
+ * Constructor requiring a location, uses default values
+ *
+ * @param loc Location to work with
+ */
+ public TargetBlock(final Location loc)
+ {
+ this(loc, 300, 0, 0.2, null);
+ }
+
+ /**
+ * Constructor requiring a player, max distance and a checking distance
+ *
+ * @param player Player to work with
+ * @param maxDistance How far it checks for blocks
+ * @param checkDistance How often to check for blocks, the smaller the more precise
+ */
+ public TargetBlock(final Player player, final int maxDistance, final double checkDistance)
+ {
+ this(player.getLocation(), maxDistance, 1.65, checkDistance, null);
+ }
+
+ /**
+ * Constructor requiring a location, max distance and a checking distance
+ *
+ * @param loc What location to work with
+ * @param maxDistance How far it checks for blocks
+ * @param checkDistance How often to check for blocks, the smaller the more precise
+ */
+ public TargetBlock(final Location loc, final int maxDistance, final double checkDistance)
+ {
+ this(loc, maxDistance, 0, checkDistance, null);
+ }
+
+ /**
+ * Constructor requiring a player, max distance, checking distance and an array of blocks to ignore
+ *
+ * @param player What player to work with
+ * @param maxDistance How far it checks for blocks
+ * @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(final Player player, final int maxDistance, final double checkDistance, final int[] blocksToIgnore)
+ {
+ this(player.getLocation(), maxDistance, 1.65, checkDistance, blocksToIgnore);
+ }
+
+ /**
+ * Constructor requiring a location, max distance, checking distance and an array of blocks to ignore
+ *
+ * @param loc What location to work with
+ * @param maxDistance How far it checks for blocks
+ * @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(final Location loc, final int maxDistance, final double checkDistance, final int[] blocksToIgnore)
+ {
+ this(loc, maxDistance, 0, checkDistance, blocksToIgnore);
+ }
+
+ /**
+ * Constructor requiring a player, max distance, checking distance and an array of blocks to ignore
+ *
+ * @param player What player to work with
+ * @param maxDistance How far it checks for blocks
+ * @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(final Player player, final int maxDistance, final double checkDistance, final List<String> blocksToIgnore)
+ {
+ this(player.getLocation(), maxDistance, 1.65, checkDistance, TargetBlock.convertStringArraytoIntArray(blocksToIgnore));
+ }
+
+ /**
+ * Constructor requiring a location, max distance, checking distance and an array of blocks to ignore
+ *
+ * @param loc What location to work with
+ * @param maxDistance How far it checks for blocks
+ * @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(final Location loc, final int maxDistance, final double checkDistance, final List<String> blocksToIgnore)
+ {
+ this(loc, maxDistance, 0, checkDistance, TargetBlock.convertStringArraytoIntArray(blocksToIgnore));
+ }
+
+ /**
+ * Set the values, all constructors uses this function
+ *
+ * @param loc Location of the view
+ * @param maxDistance How far it checks for blocks
+ * @param viewPos Where the view is positioned in y-axis
+ * @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 TargetBlock(final Location loc, final int maxDistance, final double viewHeight, final double checkDistance, final int[] blocksToIgnore)
+ {
+ this.location = loc;
+ this.maxDistance = maxDistance;
+ this.viewHeight = viewHeight;
+ this.checkDistance = checkDistance;
+ 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);
+ }
+
+ final double xRotation = (loc.getYaw() + 90) % 360;
+ final double yRotation = loc.getPitch() * -1;
+
+ 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 final void reset()
+ {
+ 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;
+ }
+
+ /**
+ * Gets the distance to a block. Measures from the block underneath the player to the targetblock
+ * Should only be used when passing player as an constructor parameter
+ *
+ * @return double
+ */
+ public double getDistanceToBlock()
+ {
+ 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);
+
+ 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(distX*distX + distY*distY + distZ*distZ);
+ }
+
+ /**
+ * Gets the rounded distance to a block. Measures from the block underneath the player to the targetblock
+ * Should only be used when passing player as an constructor parameter
+ *
+ * @return int
+ */
+ public int getDistanceToBlockRounded()
+ {
+ return (int)Math.round(getDistanceToBlock());
+ }
+
+ /**
+ * Gets the floored x distance to a block.
+ *
+ * @return int
+ */
+ public int getXDistanceToBlock()
+ {
+ return (int)Math.floor(getTargetBlock().getX() - location.getBlockX() + 0.5);
+ }
+
+ /**
+ * Gets the floored y distance to a block
+ *
+ * @return int
+ */
+ public int getYDistanceToBlock()
+ {
+ return (int)Math.floor(getTargetBlock().getY() - location.getBlockY() + viewHeight);
+ }
+
+ /**
+ * Gets the floored z distance to a block
+ *
+ * @return int
+ */
+ public int getZDistanceToBlock()
+ {
+ return (int)Math.floor(getTargetBlock().getZ() - location.getBlockZ() + 0.5);
+ }
+
+ /**
+ * Returns the block at the sight. Returns null if out of range or if no viable target was found
+ *
+ * @return Block
+ */
+ public Block getTargetBlock()
+ {
+ this.reset();
+ Block block;
+ do
+ {
+ block = getNextBlock();
+ }
+ while (block != null && ((block.getTypeId() == 0) || this.blockIsIgnored(block.getTypeId())));
+
+ return block;
+ }
+
+ /**
+ * Sets the type of the block at the sight. Returns false if the block wasn't set.
+ *
+ * @param typeID ID of type to set the block to
+ * @return boolean
+ */
+ public boolean setTargetBlock(final int typeID)
+ {
+ return setTargetBlock(Material.getMaterial(typeID));
+ }
+
+ /**
+ * Sets the type of the block at the sight. Returns false if the block wasn't set.
+ *
+ * @param type Material to set the block to
+ * @return boolean
+ */
@SuppressWarnings("empty-statement")
- public Block getFaceBlock()
- {
- while ((getNextBlock() != null) && ((getCurrentBlock().getTypeId() == 0) || this.blockToIgnoreHasValue(getCurrentBlock().getTypeId())));
- if (getCurrentBlock() != null)
- {
- return getPreviousBlock();
- }
- else
- {
- return null;
- }
- }
-
- /**
- * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set.
- *
- * @param typeID
- * @return boolean
- */
- public boolean setFaceBlock(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;
- }
-
- /**
- * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set.
- *
- * @param type
- * @return boolean
- */
- public boolean setFaceBlock(Material type)
- {
- if (getCurrentBlock() != null)
- {
- Block blk = loc.getWorld().getBlockAt(prevPos.getBlockX(), prevPos.getBlockY(), prevPos.getBlockZ());
- blk.setType(type);
- return true;
- }
- return false;
- }
-
- /**
- * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set.
- * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
- *
- * @param type
- * @return boolean
- */
- public boolean setFaceBlock(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;
- }
-
- /**
- * Get next block
- *
- * @return Block
- */
- public Block getNextBlock()
- {
- prevPos = targetPos.clone();
- 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());
- }
- while (curDistance <= maxDistance && targetPos.getBlockX() == prevPos.getBlockX() && targetPos.getBlockY() == prevPos.getBlockY() && targetPos.getBlockZ() == prevPos.getBlockZ());
- if (curDistance > maxDistance)
- {
- return null;
- }
-
- return this.loc.getWorld().getBlockAt(this.targetPos.getBlockX(), this.targetPos.getBlockY(), this.targetPos.getBlockZ());
- }
-
- /**
- * Returns the current block along the line of vision
- *
- * @return Block
- */
- public Block getCurrentBlock()
- {
- if (curDistance > maxDistance)
- {
- return null;
- }
- else
- {
- return this.loc.getWorld().getBlockAt(this.targetPos.getBlockX(), this.targetPos.getBlockY(), this.targetPos.getBlockZ());
- }
- }
-
- /**
- * Sets current block type. Returns false if the block wasn't set.
- *
- * @param typeID
- */
- public boolean setCurrentBlock(int typeID)
- {
- if (Material.getMaterial(typeID) != null)
- {
- Block blk = getCurrentBlock();
- if (blk != null)
- {
- blk.setTypeId(typeID);
- return true;
- }
- }
- return false;
- }
-
- /**
- * Sets current block type. Returns false if the block wasn't set.
- *
- * @param type
- */
- public boolean setCurrentBlock(Material type)
- {
- Block blk = getCurrentBlock();
- if (blk != null)
- {
- blk.setType(type);
- return true;
- }
- return false;
- }
-
- /**
- * Sets current block type. Returns false if the block wasn't set.
- * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
- *
- * @param type
- */
- public boolean setCurrentBlock(String type)
- {
- Material mat = Material.valueOf(type);
- if (mat != null)
- {
- Block blk = getCurrentBlock();
- if (blk != null)
- {
- blk.setType(mat);
- return true;
- }
- }
- return false;
- }
-
- /**
- * Returns the previous block in the aimed path
- *
- * @return Block
- */
- public Block getPreviousBlock()
- {
- return this.loc.getWorld().getBlockAt(prevPos.getBlockX(), prevPos.getBlockY(), prevPos.getBlockZ());
- }
-
- /**
- * Sets previous block type id. Returns false if the block wasn't set.
- *
- * @param typeID
- */
- public boolean setPreviousBlock(int typeID)
- {
- if (Material.getMaterial(typeID) != null)
- {
- Block blk = getPreviousBlock();
- if (blk != null)
- {
- blk.setTypeId(typeID);
- return true;
- }
- }
- return false;
- }
-
- /**
- * Sets previous block type id. Returns false if the block wasn't set.
- *
- * @param type
- */
- public boolean setPreviousBlock(Material type)
- {
- Block blk = getPreviousBlock();
- if (blk != null)
- {
- blk.setType(type);
- return true;
- }
- return false;
- }
-
- /**
- * Sets previous block type id. Returns false if the block wasn't set.
- * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
- *
- * @param type
- */
- public boolean setPreviousBlock(String type)
- {
- Material mat = Material.valueOf(type);
- if (mat != null)
- {
- Block blk = getPreviousBlock();
- if (blk != null)
- {
- blk.setType(mat);
- return true;
- }
- }
- return false;
- }
-
- private int[] convertStringArraytoIntArray(ArrayList<String> array)
- {
- if (array != null)
- {
- int intarray[] = new int[array.size()];
- for (int i = 0; i < array.size(); i++)
- {
- try
- {
- intarray[i] = Integer.parseInt(array.get(i));
- }
- catch (NumberFormatException nfe)
- {
- intarray[i] = 0;
- }
- }
- return intarray;
- }
- return null;
- }
-
- private boolean blockToIgnoreHasValue(int value)
- {
- if (this.blockToIgnore != null)
- {
- if (this.blockToIgnore.length > 0)
- {
- for (int i : this.blockToIgnore)
- {
- if (i == value)
- return true;
- }
- }
- }
- return false;
- }
+ public boolean setTargetBlock(final Material type)
+ {
+ if (type == null)
+ {
+ return false;
+ }
+ final Block block = getTargetBlock();
+ if (block != null)
+ {
+ block.setType(type);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Sets the type of the block at the sight. Returns false if the block wasn't set.
+ * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
+ *
+ * @param type Name of type to set the block to
+ * @return boolean
+ */
+ public boolean setTargetBlock(final String type)
+ {
+ return setTargetBlock(Material.valueOf(type));
+ }
+
+ /**
+ * Returns the block attached to the face at the sight. Returns null if out of range or if no viable target was found
+ *
+ * @return Block
+ */
+ public Block getFaceBlock()
+ {
+ final Block block = getTargetBlock();
+ if (block == null)
+ {
+ return null;
+ }
+ return getPreviousBlock();
+ }
+
+ /**
+ * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set.
+ *
+ * @param typeID
+ * @return boolean
+ */
+ public boolean setFaceBlock(final int typeID)
+ {
+ return setFaceBlock(Material.getMaterial(typeID));
+ }
+
+ /**
+ * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set.
+ *
+ * @param type
+ * @return boolean
+ */
+ public boolean setFaceBlock(final Material type)
+ {
+ if (type == null)
+ {
+ return false;
+ }
+ if (getCurrentBlock() != null)
+ {
+ final Block blk = location.getWorld().getBlockAt(prevPositionX, prevPositionY, prevPositionZ);
+ blk.setType(type);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Sets the type of the block attached to the face at the sight. Returns false if the block wasn't set.
+ * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
+ *
+ * @param type
+ * @return boolean
+ */
+ public boolean setFaceBlock(final String type)
+ {
+ return setFaceBlock(Material.valueOf(type));
+ }
+
+ /**
+ * Get next block
+ *
+ * @return Block
+ */
+ public Block getNextBlock()
+ {
+ prevPositionX = itargetPositionX;
+ prevPositionY = itargetPositionY;
+ prevPositionZ = itargetPositionZ;
+ do
+ {
+ curDistance += checkDistance;
+
+ targetPositionX += offsetX;
+ targetPositionY += offsetY;
+ targetPositionZ += offsetZ;
+ itargetPositionX = (int)Math.floor(targetPositionX);
+ itargetPositionY = (int)Math.floor(targetPositionY);
+ itargetPositionZ = (int)Math.floor(targetPositionZ);
+ }
+ while (curDistance <= maxDistance && itargetPositionX == prevPositionX && itargetPositionY == prevPositionY && itargetPositionZ == prevPositionZ);
+ if (curDistance > maxDistance)
+ {
+ return null;
+ }
+
+ return this.location.getWorld().getBlockAt(itargetPositionX, itargetPositionY, itargetPositionZ);
+ }
+
+ /**
+ * Returns the current block along the line of vision
+ *
+ * @return Block
+ */
+ public Block getCurrentBlock()
+ {
+ Block block;
+ if (curDistance <= maxDistance)
+ {
+ block = this.location.getWorld().getBlockAt(itargetPositionX, itargetPositionY, itargetPositionZ);
+ }
+ else
+ {
+ block = null;
+ }
+ return block;
+ }
+
+ /**
+ * Sets current block type. Returns false if the block wasn't set.
+ *
+ * @param typeID
+ */
+ public boolean setCurrentBlock(final int typeID)
+ {
+ return setCurrentBlock(Material.getMaterial(typeID));
+ }
+
+ /**
+ * Sets current block type. Returns false if the block wasn't set.
+ *
+ * @param type
+ */
+ public boolean setCurrentBlock(final Material type)
+ {
+ final Block blk = getCurrentBlock();
+ if (blk != null && type != null)
+ {
+ blk.setType(type);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Sets current block type. Returns false if the block wasn't set.
+ * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
+ *
+ * @param type
+ */
+ public boolean setCurrentBlock(final String type)
+ {
+ return setCurrentBlock(Material.valueOf(type));
+ }
+
+ /**
+ * Returns the previous block in the aimed path
+ *
+ * @return Block
+ */
+ public Block getPreviousBlock()
+ {
+ return this.location.getWorld().getBlockAt(prevPositionX, prevPositionY, prevPositionZ);
+ }
+
+ /**
+ * Sets previous block type id. Returns false if the block wasn't set.
+ *
+ * @param typeID
+ */
+ public boolean setPreviousBlock(final int typeID)
+ {
+ return setPreviousBlock(Material.getMaterial(typeID));
+ }
+
+ /**
+ * Sets previous block type id. Returns false if the block wasn't set.
+ *
+ * @param type
+ */
+ public boolean setPreviousBlock(final Material type)
+ {
+ final Block blk = getPreviousBlock();
+ if (blk != null && type != null)
+ {
+ blk.setType(type);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Sets previous block type id. Returns false if the block wasn't set.
+ * Observe! At the moment this function is using the built-in enumerator function .valueOf(String) but would preferably be changed to smarter function, when implemented
+ *
+ * @param type
+ */
+ public boolean setPreviousBlock(final String type)
+ {
+ return setPreviousBlock(Material.valueOf(type));
+ }
+
+ private static int[] convertStringArraytoIntArray(final List<String> array)
+ {
+ final int intarray[] = new int[array == null ? 0 : array.size()];
+ for (int i = 0; i < intarray.length; i++)
+ {
+ try
+ {
+ intarray[i] = Integer.parseInt(array.get(i));
+ }
+ catch (NumberFormatException nfe)
+ {
+ }
+ }
+ return intarray;
+ }
+
+ private boolean blockIsIgnored(final int value)
+ {
+ for (int i : this.blockToIgnore)
+ {
+ if (i == value)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
} \ No newline at end of file