Solved Get Block Player is Standing On

Discussion in 'Spigot Plugin Development' started by Leniency, Mar 16, 2016.

  1. 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.
     
  2. MiniDigger

    Supporter

    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
     
  3. 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.
     
  4. I think that he wants to kill the player if he is on a certain material of block.
     
    • Agree Agree x 1
  5. This is what I was planning to do.
    I want to kill the player, not detect if he has killed a player.
     
  6. Yes, thanks for the help :D
     
  7. MiniDigger

    Supporter

    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.
     
  8. 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
     
    • Like Like x 1
  9. 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);

            }
    }
     
     
    • Agree Agree x 1
  10. 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?
     
  11. Try to make an itemstack of wool with the byte of 15 and detect if it equals that itemstack?
     
  12. if itemstack... getData() == 15 -> then kill or whatever
     
  13. So i have to check if the byte = 15?
     
  14. I solved the problem. The finished code i used was:
    Code (Text):
    if (b.getType() == Material.WOOL && b.getData() == 15) {
                e.getPlayer().sendMessage("test");
            }
     
  15. MiniDigger

    Supporter

    @MeowmersSQL I am fast but not that fast ^^
    (And I am at work ;))