Shynixn / StructureBlockLib

StructureBlockLib is a bukkit implementation for handling structures on spigot server.
https://shynixn.github.io/StructureBlockLib/apidocs/
MIT License
60 stars 5 forks source link

Block source location API #105

Closed SamB440 closed 2 years ago

SamB440 commented 2 years ago

Hi,

I was looking at a way to visualise the structure before pasting it within the world, and get locations after paste, but it seems like this is not possible with the current API.

I noticed entities have a getSourceLocation tied to them, however blocks do not, and using part.getTargetBlock().getLocation() always returns the origin location specified in the at method.

Attempting to get the location from the source block results in the following error:

java.util.concurrent.CompletionException: java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "net.minecraft.world.level.GeneratorAccess.getMinecraftWorld()" because "this.world" is null

It would be nice if there could be a method added similar to what entities have, or to fix the target block location, or the source location (so you can then do Location#add for offset).

SamB440 commented 2 years ago

I've taken a look at the code, and it seems the actual position is passed into the CraftBlock, but world is null.

However, it seems that you can directly access x/y/z from a Block using getX etc.

getLocation fails because every time you do that it is creating a new location object with the world.

Accessing the position directly works and can be used to apply the required offset:

final Block sourceBlock = part.getSourceBlock();
final int x = sourceBlock.getX();
final int y = sourceBlock.getY();
final int z = sourceBlock.getZ();
player.sendBlockChange(part.getTargetBlock().getLocation().add(x, y, z), part.getSourceBlock().getBlockData());

Closing as this fixes the issue - perhaps edge cases like this should be documented for future reference though?