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
12 changes: 7 additions & 5 deletions src/main/java/ch/njol/skript/util/BlockLineIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package ch.njol.skript.util;

import ch.njol.skript.bukkitutil.WorldUtils;
import ch.njol.util.Math2;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.util.BlockIterator;
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 +37,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 +61,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
17 changes: 17 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprBlocks.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "blocks void":
set {_loc} to location(0.5, 320.5, 0.5)
set {_blocks::*} to blocks between {_loc} and ({_loc} ~ vector(10,0,0))
assert size of {_blocks::*} is 11 with "Blocks between loc and (loc~vector(10,0,0)) is not 11"
assert blocks at {_blocks::*} is void air with "Blocks can be set in the void?"
set blocks at {_blocks::*} to stone
assert blocks at {_blocks::*} is void air with "Blocks can be set in the void?"

test "blocks vector direction":
set {_loc} to (spawn of world "world") ~ vector(10,10,0)
set {_blocks::*} to blocks vector(10,0,0) {_loc}
assert size of {_blocks::*} is 10 with "Blocks vector(10,0,0) loc is not 10"
set blocks at {_blocks::*} to stone
assert blocks at {_blocks::*} is stone with "1 or more blocks were not set to stone"
set blocks at {_blocks::*} to air
assert blocks at {_blocks::*} is air with "1 or more blocks were not set to stone"