To get the block underneath a player, you could use:
Block block = player.getLocation().subtract(0, 1, 0).getBlock();
Which gets the player's location, subtracts 1 from the Y-axis, and then gets the block (hence getting the block under the player).
Then to get the type, you could use block.getType()
:
Material type = block.getType();
To set the block to glowstone, you could use block.setType(Material)
:
block.setType(Material.GLOWSTONE);
So, if you wanted to set the block under a player to glowstone, then immediately turn it back to the original block, you could use:
Block block = player.getLocation().subtract(0, 1, 0).getBlock(); //get the block
Material type = block.getType(); //get the block's type
block.setType(Material.GLOWSTONE); //set the block's material to glowstone
block.setType(type); //set the block's material back to the original material
But to have a delay between setting the block to glowstone then setting it back to the original block (in your case, the delay is 10 ticks) you could use a Runnable
task:
final Block block = player.getLocation().subtract(0, 1, 0).getBlock(); //get the block
final Material type = block.getType(); //get the block's type
block.setType(Material.GLOWSTONE); //set the block's material to glowstone
Bukkit.getScheduler().runTaskLater(Magic.getInstance(), new Runnable(){
public void run(){
block.setType(type); //set the block back to the original block
}
},10L);
We would want to make sure that the block beneath the player is not already glowstone, to insure that the block change does not become permanent. This could be done using simply:
if(!type.equals(Material.GLOWSTONE))
If we did not check for this, then a player could move with glowstone in their hand, hence setting the block underneath them to glowstone, and then starting a timer to set it back to the original block. But, if the player moves while the timer is in session (for example, 5 ticks after they last moved), the block underneath them would permanently change to glowstone.
So, your code could look something like this:
@EventHandler
public void onStep(PlayerMoveEvent pme) {
Player player = pme.getPlayer(); //get the player in the event
final Block block = player.getLocation().subtract(0, 1, 0).getBlock(); //get the block
final Material type = block.getState().getType(); //get the block's material
if(!type.equals(Material.GLOWSTONE)){//don't change the block if it's already glowstone
if(player.getItemInHand() != null){//make sure the item in the player's hand is not null, to avoid a null pointer
Material m = player.getItemInHand().getType(); //get the item in the player's hand
if(m == Material.GLOWSTONE){ //check if the item in the player's hand is glowstone
if(type != Material.AIR && type != Material.WATER && type != Material.STATIONARY_WATER && type != Material.LAVA && type != Material.STATIONARY_LAVA && type != Material.REDSTONE_WIRE && type != Material.REDSTONE_COMPARATOR && type != Material.REDSTONE_TORCH_ON && type != Material.REDSTONE_TORCH_OFF){
block.setType(Material.GLOWSTONE);//set the block type to glowstone
Bukkit.getScheduler().runTaskLater(Magic.getInstance(), new Runnable() {
public void run(){
block.setType(type); //set the block type back to the original type
}
},10L);
}
}
}
}
}
Also, you could reduce this if
statement:
if(type != Material.AIR && type != Material.WATER && type != Material.STATIONARY_WATER && type != Material.LAVA && type != Material.STATIONARY_LAVA && type != Material.REDSTONE_WIRE && type != Material.REDSTONE_COMPARATOR && type != Material.REDSTONE_TORCH_ON && type != Material.REDSTONE_TORCH_OFF)
into simply type.isSolid()
:
if(type.isSolid())