From aaefd4ecb5d688827e5b0999abad0bc4abfb2cb6 Mon Sep 17 00:00:00 2001 From: EvilSeph Date: Sat, 1 Oct 2011 16:18:17 -0400 Subject: [PATCH] Added callback line of sight methods. Thanks xZise! --- .../craftbukkit/entity/CraftLivingEntity.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java index 63e1d1b959..6d5558d1fc 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -18,6 +18,7 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Snowball; import org.bukkit.entity.Vehicle; import org.bukkit.util.BlockIterator; +import org.bukkit.util.Callback; import java.util.List; import java.util.HashSet; @@ -86,6 +87,45 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { return getEyeHeight(); } + private List getLineOfSight(Callback callback, int maxDistance, int maxLength) { + if (maxDistance > 120) { + maxDistance = 120; + } + ArrayList blocks = new ArrayList(maxLength == 0 ? 10 : maxLength); + Iterator itr = new BlockIterator(this, maxDistance); + while (itr.hasNext()) { + Block block = itr.next(); + if (maxLength != 0 && blocks.size() + 1 > maxLength) { + blocks.remove(0); + } + blocks.add(block); + if (callback == null) { + if (block.getTypeId() != 0) { + break; + } + } else { + Boolean result = callback.call(block); + if (result != null && !result) { + break; + } + } + } + return blocks; + } + + public List getLineOfSight(Callback callback, int maxDistance) { + return getLineOfSight(callback, maxDistance, 0); + } + + public Block getTargetBlock(Callback callback, int maxDistance) { + List blocks = getLineOfSight(callback, maxDistance, 1); + return blocks.get(0); + } + + public List getLastTwoTargetBlocks(Callback callback, int maxDistance) { + return getLineOfSight(callback, maxDistance, 2); + } + private List getLineOfSight(HashSet transparent, int maxDistance, int maxLength) { if (maxDistance > 120) { maxDistance = 120;