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 20 commits into
base: dev/patch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8105063
Starter Commit
TheAbsolutionism Sep 9, 2024
0967fda
Cleanup
TheAbsolutionism Sep 9, 2024
65f754d
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Sep 9, 2024
ff5c7ca
Test
TheAbsolutionism Sep 9, 2024
af1e4ef
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Sep 12, 2024
796533d
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Sep 18, 2024
44c75e4
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Sep 21, 2024
336dd49
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Sep 22, 2024
2173bdd
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Sep 22, 2024
bae602f
Revert
TheAbsolutionism Sep 24, 2024
af6036c
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Oct 1, 2024
3eff459
Remove check
TheAbsolutionism Oct 1, 2024
684b736
Fix Test
TheAbsolutionism Oct 1, 2024
92ce257
Removed TODO
TheAbsolutionism Oct 1, 2024
3bf72d9
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Oct 1, 2024
193f246
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Oct 1, 2024
db98d3e
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Oct 2, 2024
8248694
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Nov 1, 2024
8295f21
Fix 1.13.2
TheAbsolutionism Nov 1, 2024
68ab41b
Merge branch 'dev/patch' into dev/ExprBlocks-Void-to-Void
TheAbsolutionism Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/ch/njol/skript/expressions/ExprBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
@Since("1.0, 2.5.1 (within/cuboid/chunk)")
public class ExprBlocks extends SimpleExpression<Block> {

private static final boolean SUPPORTS_WORLD_LOADED = Skript.methodExists(Location.class, "isWorldLoaded");

static {
Skript.registerExpression(ExprBlocks.class, Block.class, ExpressionType.COMBINED,
"[(all [[of] the]|the)] blocks %direction% [%locations%]", // TODO doesn't loop all blocks?
"[(all [[of] the]|the)] blocks %direction% [%locations%]",
"[(all [[of] the]|the)] blocks from %location% [on] %direction%",
"[(all [[of] the]|the)] blocks from %location% to %location%",
"[(all [[of] the]|the)] blocks between %location% and %location%",
Expand Down Expand Up @@ -116,7 +118,11 @@ protected Block[] get(Event event) {
return from.stream(event)
.filter(Location.class::isInstance)
.map(Location.class::cast)
.filter(Location::isWorldLoaded)
.filter(location -> {
if (SUPPORTS_WORLD_LOADED)
return location.isWorldLoaded();
return location.getChunk().isLoaded();
})
.map(direction::getRelative)
.map(Location::getBlock)
.toArray(Block[]::new);
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")
set {_blocks::*} to blocks vector(1,0,0) {_loc}
assert size of {_blocks::*} is 100 with "Blocks vector(1,0,0) loc is not 100"
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"