Remove anvil event

Discussion in 'Plugin Development' started by ChillxHunter, Nov 19, 2018.

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

    ChillxHunter

    It will be possible to prevent a player from opening an anvil when renamed. I have a pickaxe called a stealthy pick and I would like to prevent the players from opening the anvil when he holds the object in his hand and when he tries to open it he sends him a custom error message. Would there be a code to make it?
     
  2. @ChillxHunter

    That will not be very hard. You need to listen for the PlayerInteractEvent, check if the item in the (main) hand of the player is your custom pickaxe. If it is, cancel the event and send your error message to the player.
     
  3. Offline

    ChillxHunter

    But i don't no how to do the code

    Can you send me a code ?
    @knokko
     
    Last edited: Nov 21, 2018
  4. @ChillxHunter

    This forum is not made to send codes to others, so I won't send much.
    Create a class that implements Listener. I will refer to this class as YourEventHandler.
    In that class, add the following code:
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event){
        System.out.println("The item you use is " + event.getPlayer().getInventory().getItemInMainHand());
    }
    In your onEnable method of your main class, add something like
    Code:
    Bukkit.getPluginManager().registerEvents(this, new YourEventHandler());
    I don't know whether that code is exactly correct or not, but it should come close.

    Your job is to finish the onPlayerInteract to do what you need to do.
     
  5. Offline

    The_Spaceman

    should be:

    Bukkit.getPluginManager().registerEvents(new YourEventHandler(), this);
     
Thread Status:
Not open for further replies.

Share This Page