How to return block type in a message for Spigot 1.13.2

Thernandez :

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!");
    }
}
Rishaan Gupta :

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);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=129717&siteId=1