Solved Get Block beneath player once...

Discussion in 'Plugin Development' started by ziimzy, May 27, 2013.

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

    ziimzy

    Hey so I'm using this code to check the block beneath the player:

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerMoveEvent(PlayerMoveEvent event) {
            Player player = (Player) event.getPlayer();
            if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getTypeId() == 41) {
                player.sendMessage("You are on a gold block!");
            }
            }
    And it works, but it fires every time I move at all. Is it possible to make it fire once? The first time you move on it? Then also tell me when I step off it? Thanks,

    Ziimzy.
     
  2. Offline

    ZeusAllMighty11

    You can add the player to a list, or only check XYZ
     
  3. Offline

    ziimzy

    Ill try the list now. Thanks didn't think of that!

    How do I get when they step off of a gold block?

    I now have this for when they step on it:

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onPlayerMoveEvent(PlayerMoveEvent event) {
    Player player = (Player) event.getPlayer();
    String playername = player.getName();
    if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getTypeId() == 41) {
    if(!(ongoldblock.contains(playername))){
    ongoldblock.add(playername);
    player.sendMessage("You entered a gold block!");
    }
               
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    Technius

    ziimzy
    Compare the event's to and the event's from. Here are the methods:
    Code:
    event.getTo();
    event.getFrom();
    
     
    ziimzy likes this.
  5. Offline

    ziimzy

    Okay I will try that out :)
     
  6. Offline

    TheDeathMachine

    ziimzy or you could do :

    if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getTypeId() != 41) {
     
  7. Offline

    ziimzy

    Okay I did it! :) I used this code in the end for anyone that wants to know:

    Code:
        Set<String> ongoldblock = new HashSet<String>();
     
       
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerMoveEvent(PlayerMoveEvent event) {
            final Player player = (Player) event.getPlayer();
            final String playername = player.getName();
            if(event.getTo().getBlock().getRelative(BlockFace.DOWN).getTypeId() == 41 && event.getFrom().getBlock().getRelative(BlockFace.DOWN).getTypeId() != 41) {
                    if(!(ongoldblock.contains(playername))){
                        ongoldblock.add(playername);
                        player.sendMessage("You entered a gold block!");
                    }
            }
                if(event.getFrom().getBlock().getRelative(BlockFace.DOWN).getTypeId() == 41 && event.getTo().getBlock().getRelative(BlockFace.DOWN).getTypeId() != 41) {   
                    if(ongoldblock.contains(playername)){
                        ongoldblock.remove(playername);
                        player.sendMessage("You left a gold block!");
                    }
                }
               
            }
    But one last thing, when I jump I'm not on a gold block, is there anyway to get the block beneath what I'm standing on? So even when I jump the block that's 2 blocks below me is a gold block? Thanks Technius you helped alot ;)
     
  8. Offline

    Garris0n

    Use the getRelative(BlockFace.DOWN) again on the block that's already down.
     
  9. Offline

    ziimzy

    Okay it's all done. :) thanks!
     
Thread Status:
Not open for further replies.

Share This Page