1.17.x Block.setType() method not working

Discussion in 'Spigot Plugin Development' started by Narae, Jul 18, 2021.

  1. Code (Text):

        @EventHandler
        public void RespawnAnchorToAir(PlayerInteractEvent e)
        {
            if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) return;
            if (!e.getClickedBlock().getType().equals(Material.RESPAWN_ANCHOR)) return;
       
            Bukkit.broadcastMessage(((RespawnAnchor) e.getClickedBlock().getBlockData()).getCharges() + ""); // print current charge level. (work)
       
            e.getClickedBlock().setType(Material.AIR); // RespawnAnchor block change to AIR (do not work)
        }
     
    In this code if someone interacts with respawnanchor block it turns the block into the air

    The BroadcastMessage method works fine.
    But the ClickedBlock().setType() method doesn't work.
    Why doesn't it work?
     
  2. It seems to be your code mistake. Try debugging with System.out.print first.
     
    • Like Like x 1
  3. setCancelled the event, then replace it with your block.
     
    • Like Like x 1
  4. Here's what actually happens in your code:
    You check the even, if it's a Respawn Anchor you change it to Air.
    Then the function ends, and the outside function that called that even to check if it's cancelled or not.
    Because it's not cancelled it changes that block to what it should be.
    Here's what you do: cancel the event, then set the block to Air - just like @adrianpopa1998 said.
     
    • Like Like x 2
  5. Oh! It's resolved. Thank you.
     
  6. thx genius!
     
  7. thx!