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;