PlayerMoveEvent not working properly

Discussion in 'Plugin Development' started by qqifford, Oct 26, 2020.

  1. Offline

    qqifford

    I have this code:
    Code:
    @EventHandler
        public void moveEvent(PlayerMoveEvent e)
        {
            Block wasOn;
            wasOn = e.getFrom().getBlock().getRelative(BlockFace.DOWN);
            if (wasOn.getType() != Material.AIR)
            {
                wasOn.setType(Material.AIR);
            }
        }
    It is supposed to make the block that you just moved from disappear. However, it makes the block that I move to disappear. Why?
     
  2. Offline

    timtower Moderator Moderator

    @qqifford Aren't they both disappearing then?
     
  3. Offline

    skyland1a

    I think this is because moveEvent() called every tiny bit a player moves, not every full block a player moves.
    For example, if a player's last location (x,y,z) was 0,65,0 and they move to o,65,0.1 then the block at their previous location (0,65,0) is still the block they are standing on, which leads to the block directly under them being removed.

    Try adding a check to see if the previous block that the player was standing on is different to the one the player is currently standing on, and only replace the previous block with air if that condition is true.
     
    Last edited: Oct 26, 2020
    KarimAKL likes this.
  4. Offline

    KarimAKL

    @qqifford You have to make sure the from & to blocks are not the same, since the location can be a different float on the same block.
     
    skyland1a likes this.
  5. Offline

    qqifford

    This explanation made perfect sense, but it didn't work

    Although there is probably an easier way to accomplish my goal. I'm trying to make a tnt-run like plugin that makes every block you touch disappear. However, I couldn't find an event trigger for a player touching a block, which is why I have been experimenting with the code above. Does such a trigger exist?
     
  6. Offline

    timtower Moderator Moderator

  7. Offline

    fishbucket

    you could have a loop check if the block under the player is a non air block, then if it is make the block disappear
     
  8. Offline

    KarimAKL

    It should work; i have used it myself multiple times. Could you post your code?
     
  9. Offline

    qqifford

    Code:
        @EventHandler
        public void moveEvent(PlayerMoveEvent e)
        {
            Block wasOn;
            wasOn = e.getFrom().getBlock();
            if (wasOn.getRelative(BlockFace.DOWN).getType() != Material.AIR && wasOn != e.getTo().getBlock())
            {
                    wasOn.getRelative(BlockFace.DOWN).setType(Material.AIR);
            }
        }
     
  10. Offline

    KarimAKL

    @qqifford You are comparing blocks with "!=", compare the locations using "equals()". Only use "==" and "!=" for comparing constants.
     
  11. Offline

    qqifford

    You fucking genius. It works now thank you so much haha
     
    Last edited: Oct 28, 2020
    KarimAKL likes this.

Share This Page