Solved Send message when player goes through lava help!

Discussion in 'Plugin Development' started by Geekhellmc, Jul 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Geekhellmc

    When a player goes through a block of lava, a message appears but I can't snap it out! Here is my code:

    Code:java
    1. public void onPlayerTouchLava(PlayerMoveEvent e){
    2. Player p = e.getPlayer();
    3. Location loc = e.getPlayer().getLocation();
    4. loc.setZ(loc.getZ() - 1);
    5. loc.setX(loc.getZ() - 1);
    6. Block b = loc.getBlock();
    7. if(b.getType() == Material.LAVA) {
    8. p.sendMessage(ChatColor.GOLD + "[ITL] You gained 1 point!");
    9. TokenAPI.addToken(p, 1); //This is just one of my private plugins API
    10. }
    11. }
     
  2. Firstly, you should use the event locations and not the player locations, and secondly you must check if the player has changed blocks, and not just moved, since otherwise every time the player moves a pixel, it'll call that event.

    In the following code, I didn't check if they changed blocks in the way I normally do which is casting all locations to ints and checking if the From X doesn't equal To X, or From Y doesn't equal to To Y, or From Z doesn't equal to To Z.
    Code:
    public void onPlayerTouchLava(PlayerMoveEvent event) {
        if (event.getFrom().getBlock().getType() == Material.LAVA && event.getTo().getBlock().getType() != Material.LAVA) {
            // Add points.
        }
    }
    
     
  3. Offline

    Geekhellmc

    Thank you!
     
Thread Status:
Not open for further replies.

Share This Page