Search code examples
javaminecraftbukkit

How to return block type in a message for Spigot 1.13.2


I'm attempting to get the block type that is right clicked by the player and return it back as a message sent to the player in-game. Currently i'm getting absolutely nothing.

public class BlockIdentifier extends JavaPlugin {
    public void onEnable(){
        getLogger().info("BlockIdentifier started!");
    }

    @EventHandler
    public void onInteract(PlayerInteractEvent event){
        Action action = event.getAction();
        Player player = event.getPlayer();
        Block block = event.getClickedBlock();

        if(action.equals(Action.LEFT_CLICK_BLOCK)){
            player.sendMessage(block.getType().toString());
        }

    }

    public void onDisable(){
        getLogger().info("BlockIdentifier stopped!");
    }
}

Solution

  • As well as doing what Darkilen suggested (implementing Listener) you need to register your events/listener in your onEnable using:

    getServer().getPluginManager().registerEvents​(Listener listener, Plugin plugin)
    

    For your case, this would look like:

    public void onEnable(){
        getLogger().info("BlockIdentifier started!");
        getServer().getPluginManager().registerEvents(this, this);
    }