2

I think this is correct but if I click with the Axe nothing happens, no error

I don't know what's wrong because in the internet it works

package mrleaw.tools.main;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener{

String prefix = "§0[§4RollmopsMC | Tools§0] §5";

public void onEnable() {
    Bukkit.getConsoleSender().sendMessage(prefix + "§aDas Plugin wurde aktiviert!");
    Bukkit.getConsoleSender().sendMessage(prefix + "§6Coded by MrLeaw");
}
public void onDisable() {
    Bukkit.getConsoleSender().sendMessage(prefix + "§cDas Plugin wurde deaktiviert!");
    Bukkit.getConsoleSender().sendMessage(prefix + "§6Coded by MrLeaw");
}

@EventHandler
void onRightClickTool(PlayerInteractEvent e){
    try {
        if(e.getAction() == Action.LEFT_CLICK_AIR 
                || e.getAction() == Action.RIGHT_CLICK_AIR
                || e.getAction() == Action.LEFT_CLICK_BLOCK
                || e.getAction() == Action.RIGHT_CLICK_BLOCK){
            Player player = e.getPlayer();
            if(player.getItemInHand() != null && player.getItemInHand().getType() == Material.WOOD_AXE){
                player.sendMessage(prefix + "test");
            }
        }
    }catch(Exception exception){
    }catch(Error error){
    }
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
MrLeaw
  • 33
  • 4
  • 2
    1. Don't catch `Error`s. [Bad things happen if you do](https://stackoverflow.com/q/11017304). 2. Don't have empty `catch` clauses. If something's wrong, why wouldn't you want to hear about it? 3. You don't register your event handler anywhere. See [`PluginManager.registerEvents()`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/plugin/PluginManager.html#registerEvents(org.bukkit.event.Listener,%20org.bukkit.plugin.Plugin)). – Siguza May 06 '17 at 15:20

1 Answers1

3

Add getServer().getPluginManager().registerEvents(this, this); to onEnable()

You have to do this to register your listener, as per the Bukkit API reference

otoomey
  • 939
  • 1
  • 14
  • 31