Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExprBlocks - BlockLineIterator #7062

Open
wants to merge 10 commits into
base: dev/patch
Choose a base branch
from
3 changes: 3 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Iterator;

import ch.njol.skript.lang.function.ExprFunctionCall;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -157,6 +158,8 @@ public Iterator<Block> iterator(Event event) {
if (number != null)
distance = number.intValue();
}
} else if (this.direction instanceof ExprFunctionCall) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
distance = (int) this.direction.getSingle(event).getDirection().length();
}
return new BlockLineIterator(location, vector, distance);
} else {
Expand Down
19 changes: 6 additions & 13 deletions src/main/java/ch/njol/skript/util/BlockLineIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.bukkitutil.WorldUtils;
import ch.njol.util.Math2;
import ch.njol.util.NullableChecker;
import ch.njol.util.coll.iterator.StoppableIterator;

Expand All @@ -37,8 +35,10 @@ public class BlockLineIterator extends StoppableIterator<Block> {
* @throws IllegalStateException randomly (Bukkit bug)
*/
public BlockLineIterator(Block start, Block end) throws IllegalStateException {
super(new BlockIterator(start.getWorld(), fitInWorld(start.getLocation().add(0.5, 0.5, 0.5), end.getLocation().subtract(start.getLocation()).toVector()),
end.equals(start) ? new Vector(1, 0, 0) : end.getLocation().subtract(start.getLocation()).toVector(), 0, 0), // should prevent an error if start = end
super(new BlockIterator(start.getWorld(), start.getLocation().toVector(),
end.equals(start) ? new Vector(1, 0, 0) : end.getLocation().subtract(start.getLocation()).toVector(),
0, 0
), // should prevent an error if start = end
new NullableChecker<Block>() {
private final double overshotSq = Math.pow(start.getLocation().distance(end.getLocation()) + 2, 2);

Expand All @@ -59,7 +59,7 @@ public boolean check(@Nullable Block block) {
* @throws IllegalStateException randomly (Bukkit bug)
*/
public BlockLineIterator(Location start, Vector direction, double distance) throws IllegalStateException {
super(new BlockIterator(start.getWorld(), fitInWorld(start, direction), direction, 0, 0), new NullableChecker<Block>() {
super(new BlockIterator(start.getWorld(), start.toVector(), direction, 0, 0), new NullableChecker<Block>() {
private final double distSq = distance * distance;

@Override
Expand Down Expand Up @@ -87,14 +87,7 @@ public BlockLineIterator(Block start, Vector direction, double distance) throws
* @return The newly modified Vector if needed.
*/
private static Vector fitInWorld(Location location, Vector direction) {
int lowest = WorldUtils.getWorldMinHeight(location.getWorld());
int highest = location.getWorld().getMaxHeight();
Vector vector = location.toVector();
int y = location.getBlockY();
if (y >= lowest && y <= highest)
return vector;
double newY = Math2.fit(lowest, location.getY(), highest);
return new Vector(location.getX(), newY, location.getZ());
return location.toVector();
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

}