Resource Chat Click Event Callback (ChatComponent)

Discussion in 'Spigot Plugin Development' started by rodel77, May 6, 2017.

  1. Hi, you probably know Chat Component API with this you can send json tellraw messages using a simple API, and this support ClickEvent in Chat, but the problem is you only can invoke only Command, URL etc... but maybe you need sometimes a callback

    Get only one class source here

    Example:

    My chat message: Click here to teleport

    Callback without this "lib":

    Code (Java):
    getCommand("teleport").setExecutor(new CommandExecutor() {
                @Override
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    //Teleport my player
                    //This is a simple callback but more complex things need to send/parse args, and make some checks...
                    ((Player) sender).teleport(location);
                    return true;
                }
            });
           
            getCommand("hub").setExecutor(new CommandExecutor() {
                @Override
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    TextComponent component = new TextComponent("Click here to teleport");
                    // You cant do /tp x y z because you need add the permission
                    component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/teleport"));
                    ((Player) sender).spigot().sendMessage(component);
                    return true;
                }
            });
    With this "lib":

    Code (Java):
    getCommand("hub").setExecutor(new CommandExecutor() {
                @Override
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    TextComponent component = new TextComponent("Click here to teleport");
                   
                    SpigotCallback.createCommand(component, player -> {
                        //Teleport player
                        //You dont need arguments parse
                        player.teleport(location);
                    });
                   
                    ((Player) sender).spigot().sendMessage(component);
                    return true;
                }
            });
    As you can see you dont need to scroll in the second example, is easy, and secure cause works with Unique IDs and one time user click on it the callback disapear

    This lib is based in Chat Callbacks Sponge API

     
    • Like Like x 2
    • Winner Winner x 1