How to get block position from BlockEvent in Minecraft - Spigot

Ok, so when building a plugin for Minecraft, I encountered a problem that I can't seem to get past. So the problem is that I want to get the position of the placed Diamond Block, so I can check this block for other blocks. I want to create an automated farm plugin.
"So far I have tried to do this with

public void OnDiaBlockPlace(BlockPlaceEvent e){
    Block b = e.getBlock();
    b.getPosition();
}

      

and with

public void onPlantGrow(BlockGrowEvent e){
    Block b = e.getBlock();
    b.getPosition();
}

      


But b.getPosition();

doesn't exist in the spruot / craftbukkit API.
I just want to know if there is a way to solve this problem and how I can solve it.

TL / DR: How can I get block position from BlockPlaceEvent or BlockGrowEvent in x, y, z format?

Thanks everyone in advance.

EDIT: Working code snippet:

public void onPlantGrow(BlockGrowEvent e){
    Block b = e.getBlock();
    int x = b.getLocation().getBlockX();
    int y = b.getLocation().getBlockY();
    int z = b.getLocation().getBlockZ();
}

      

+3


source to share


1 answer


This will give you Location

this block:

Location blockLocation = b.getLocation();

      

The class Location

allows you to access coordinates using getBlockX()

, getBlockY()

and getBlockZ()

, as well as getWorld()

others ( Reference ).



And if you want to work with RegionCoordinates

, you can use this on yours blockLocation

:

RegionCoordinates regionCoords = RegionCoordinates.fromLocation(blockLocation);

      

+2


source







All Articles