How would I create a code which detects the block a player is standing is on and then kills the player? I am thinking about using playermoveevent and looking at the block under them, but i am not sure if there is a more efficient way.
You are only interessted which block the player is standing on if they kill another player, right? Just listen to the entitydeathevent, check if the entity which died was a player, check if there was a killer and the killer was a player too and then you can get the location of the killer, the location of the block the players is standing on, the block itselfes and then the material of that block
Not going to spoonfeed, so basically you could use the player move event, or you could use a runnable either way, you want to get the players location, clone, subtract 1 in the y value then you get the block at that location, and then you can do a check if that is the block you are looking for, then if it is, do something.
Oh sorry I understood that wrong ^^ But you should not clone the loc but use getPlayer.getLocation.getBlock.getRelative(BlockFace.DOWN) to get the block below the player also when using the player move event make sure the player actually moved (changed his x y or z coordinates) and not only moved his head.
Code (Text): @EventHandler public void onMove(PlayerMoveEvent e){ Location loc = e.getPlayer().getLocation().clone().subtract(0, 1, 0); Block b = loc.getBlock(); //Whatever Material you want if(b.getType() == Material.DIRT){ e.getPlayer().setHealth(0); } } Quick code, compare if you want, haven't tested it but in theory should work
Good point, I would do that, that is also why a runnable would work, it doesn't really matter about the move event though, if it gets fired 3 times, meh it doesn't hurt. Code (Text): @EventHandler public void onMove(PlayerMoveEvent e){ Location loc = e.getPlayer().getLocation.getBlock.getRelative(BlockFace.DOWN); Block b = loc.getBlock(); //Whatever Material you want if(b.getType() == Material.DIRT){ e.getPlayer().setHealth(0); } }
Thanks this works, but one issue I am having is, I want to check if the block is black wool. The code you gave me only checks if it is wool, and would be triggered on any type of wool. How would i check if it is black wool specifically?
I solved the problem. The finished code i used was: Code (Text): if (b.getType() == Material.WOOL && b.getData() == 15) { e.getPlayer().sendMessage("test"); }
Ihhh magic numbers ^^ Bukkit has a api for this: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/material/Wool.html#getColor()