Search code examples
minecraftbukkit

Does Material material = (new Location(w,x,y,z)).getBlock().getType() work?


I want to define a ton of block materials, but I don't want it to take up so much space in my code. So, I thought that maybe this would work?

Material material = (new Location(w,x,y,z)).getBlock().getType()

Does that get the material of the position w, x, y, z(w is world)?


Solution

  • The code you provided in the question is the same; although more concise, as:

    Location location = new Location(w, x, y, z);
    Block block = location.getBlock();
    Material material = block.getType();
    

    Where new Location(... creates a new instance of the Location class which has a getBlock() method. This method will return an instance of the Block interface which from there you can use getType() which returns an instance of Material.

    So yes, as long as your world and the coordinates of the block exist, it will get the material at the specified position.

    You can read more on the new keyword here.