Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
org.bukkit.util.BlockIterator Class Reference
Inheritance diagram for org.bukkit.util.BlockIterator:

Public Member Functions

 BlockIterator (World world, Vector start, Vector direction, double yOffset, int maxDistance)
 
 BlockIterator (Location loc, double yOffset, int maxDistance)
 
 BlockIterator (Location loc, double yOffset)
 
 BlockIterator (Location loc)
 
 BlockIterator (LivingEntity entity, int maxDistance)
 
 BlockIterator (LivingEntity entity)
 
boolean hasNext ()
 
Block next ()
 
void remove ()
 

Detailed Description

This class performs ray tracing and iterates along blocks on a line

Definition at line 17 of file BlockIterator.java.

Constructor & Destructor Documentation

org.bukkit.util.BlockIterator.BlockIterator ( World  world,
Vector  start,
Vector  direction,
double  yOffset,
int  maxDistance 
)

Constructs the BlockIterator

Parameters
worldThe world to use for tracing
startA Vector giving the initial location for the trace
directionA Vector pointing in the direction for the trace
yOffsetThe trace begins vertically offset from the start vector by this value
maxDistanceThis is the maximum distance in blocks for the trace. Setting this value above 140 may lead to problems with unloaded chunks. A value of 0 indicates no limit

Definition at line 54 of file BlockIterator.java.

References org.bukkit.util.Vector.clone(), org.bukkit.World.getBlockAt(), org.bukkit.block.Block.getRelative(), org.bukkit.util.Vector.getX(), org.bukkit.util.Vector.getY(), org.bukkit.util.Vector.getZ(), and org.bukkit.util.Vector.setY().

54  {
55  this.world = world;
56  this.maxDistance = maxDistance;
57 
58  Vector startClone = start.clone();
59 
60  startClone.setY(startClone.getY() + yOffset);
61 
62  currentDistance = 0;
63 
64  double mainDirection = 0;
65  double secondDirection = 0;
66  double thirdDirection = 0;
67 
68  double mainPosition = 0;
69  double secondPosition = 0;
70  double thirdPosition = 0;
71 
72  Block startBlock = this.world.getBlockAt(floor(startClone.getX()), floor(startClone.getY()), floor(startClone.getZ()));
73 
74  if (getXLength(direction) > mainDirection) {
75  mainFace = getXFace(direction);
76  mainDirection = getXLength(direction);
77  mainPosition = getXPosition(direction, startClone, startBlock);
78 
79  secondFace = getYFace(direction);
80  secondDirection = getYLength(direction);
81  secondPosition = getYPosition(direction, startClone, startBlock);
82 
83  thirdFace = getZFace(direction);
84  thirdDirection = getZLength(direction);
85  thirdPosition = getZPosition(direction, startClone, startBlock);
86  }
87  if (getYLength(direction) > mainDirection) {
88  mainFace = getYFace(direction);
89  mainDirection = getYLength(direction);
90  mainPosition = getYPosition(direction, startClone, startBlock);
91 
92  secondFace = getZFace(direction);
93  secondDirection = getZLength(direction);
94  secondPosition = getZPosition(direction, startClone, startBlock);
95 
96  thirdFace = getXFace(direction);
97  thirdDirection = getXLength(direction);
98  thirdPosition = getXPosition(direction, startClone, startBlock);
99  }
100  if (getZLength(direction) > mainDirection) {
101  mainFace = getZFace(direction);
102  mainDirection = getZLength(direction);
103  mainPosition = getZPosition(direction, startClone, startBlock);
104 
105  secondFace = getXFace(direction);
106  secondDirection = getXLength(direction);
107  secondPosition = getXPosition(direction, startClone, startBlock);
108 
109  thirdFace = getYFace(direction);
110  thirdDirection = getYLength(direction);
111  thirdPosition = getYPosition(direction, startClone, startBlock);
112  }
113 
114  // trace line backwards to find intercept with plane perpendicular to the main axis
115 
116  double d = mainPosition / mainDirection; // how far to hit face behind
117  double secondd = secondPosition - secondDirection * d;
118  double thirdd = thirdPosition - thirdDirection * d;
119 
120  // Guarantee that the ray will pass though the start block.
121  // It is possible that it would miss due to rounding
122  // This should only move the ray by 1 grid position
123  secondError = floor(secondd * gridSize);
124  secondStep = round(secondDirection / mainDirection * gridSize);
125  thirdError = floor(thirdd * gridSize);
126  thirdStep = round(thirdDirection / mainDirection * gridSize);
127 
128  if (secondError + secondStep <= 0) {
129  secondError = -secondStep + 1;
130  }
131 
132  if (thirdError + thirdStep <= 0) {
133  thirdError = -thirdStep + 1;
134  }
135 
136  Block lastBlock;
137 
138  lastBlock = startBlock.getRelative(mainFace.getOppositeFace());
139 
140  if (secondError < 0) {
141  secondError += gridSize;
142  lastBlock = lastBlock.getRelative(secondFace.getOppositeFace());
143  }
144 
145  if (thirdError < 0) {
146  thirdError += gridSize;
147  lastBlock = lastBlock.getRelative(thirdFace.getOppositeFace());
148  }
149 
150  // This means that when the variables are positive, it means that the coord=1 boundary has been crossed
151  secondError -= gridSize;
152  thirdError -= gridSize;
153 
154  blockQueue[0] = lastBlock;
155  currentBlock = -1;
156 
157  scan();
158 
159  boolean startBlockFound = false;
160 
161  for (int cnt = currentBlock; cnt >= 0; cnt--) {
162  if (blockEquals(blockQueue[cnt], startBlock)) {
163  currentBlock = cnt;
164  startBlockFound = true;
165  break;
166  }
167  }
168 
169  if (!startBlockFound) {
170  throw new IllegalStateException("Start block missed in BlockIterator");
171  }
172 
173  // Calculate the number of planes passed to give max distance
174  maxDistanceInt = round(maxDistance / (Math.sqrt(mainDirection * mainDirection + secondDirection * secondDirection + thirdDirection * thirdDirection) / mainDirection));
175 
176  }
Block getBlockAt(int x, int y, int z)
org.bukkit.util.BlockIterator.BlockIterator ( Location  loc,
double  yOffset,
int  maxDistance 
)

Constructs the BlockIterator

Parameters
locThe location for the start of the ray trace
yOffsetThe trace begins vertically offset from the start vector by this value
maxDistanceThis is the maximum distance in blocks for the trace. Setting this value above 140 may lead to problems with unloaded chunks. A value of 0 indicates no limit

Definition at line 232 of file BlockIterator.java.

References org.bukkit.Location.getDirection(), org.bukkit.Location.getWorld(), and org.bukkit.Location.toVector().

232  {
233  this(loc.getWorld(), loc.toVector(), loc.getDirection(), yOffset, maxDistance);
234  }
org.bukkit.util.BlockIterator.BlockIterator ( Location  loc,
double  yOffset 
)

Constructs the BlockIterator.

Parameters
locThe location for the start of the ray trace
yOffsetThe trace begins vertically offset from the start vector by this value

Definition at line 244 of file BlockIterator.java.

References org.bukkit.Location.getDirection(), org.bukkit.Location.getWorld(), and org.bukkit.Location.toVector().

244  {
245  this(loc.getWorld(), loc.toVector(), loc.getDirection(), yOffset, 0);
246  }
org.bukkit.util.BlockIterator.BlockIterator ( Location  loc)

Constructs the BlockIterator.

Parameters
locThe location for the start of the ray trace

Definition at line 254 of file BlockIterator.java.

254  {
255  this(loc, 0D);
256  }
org.bukkit.util.BlockIterator.BlockIterator ( LivingEntity  entity,
int  maxDistance 
)

Constructs the BlockIterator.

Parameters
entityInformation from the entity is used to set up the trace
maxDistanceThis is the maximum distance in blocks for the trace. Setting this value above 140 may lead to problems with unloaded chunks. A value of 0 indicates no limit

Definition at line 267 of file BlockIterator.java.

References org.bukkit.entity.LivingEntity.getEyeHeight(), and org.bukkit.entity.Entity.getLocation().

267  {
268  this(entity.getLocation(), entity.getEyeHeight(), maxDistance);
269  }
org.bukkit.util.BlockIterator.BlockIterator ( LivingEntity  entity)

Constructs the BlockIterator.

Parameters
entityInformation from the entity is used to set up the trace

Definition at line 277 of file BlockIterator.java.

277  {
278  this(entity, 0);
279  }

Member Function Documentation

boolean org.bukkit.util.BlockIterator.hasNext ( )

Returns true if the iteration has more elements

Definition at line 285 of file BlockIterator.java.

285  {
286  scan();
287  return currentBlock != -1;
288  }
Block org.bukkit.util.BlockIterator.next ( )

Returns the next Block in the trace

Returns
the next Block in the trace

Definition at line 296 of file BlockIterator.java.

296  {
297  scan();
298  if (currentBlock <= -1) {
299  throw new NoSuchElementException();
300  } else {
301  return blockQueue[currentBlock--];
302  }
303  }

The documentation for this class was generated from the following file: