Can someone please provide an callback example

  • Hi, so on my old server I wrote the entire plugin UI using things such as onPlayerGuiElementClickEvent


    I see now that we have some really useful pre-canned message boxes such as;


    Java
    player.showMessageBox(MessageBoxButtons.Yes_No_Cancel, "Title", "Question", 0, null);


    (I've dropped a null in the callback :crazy: ... )


    Can someone please provide a simple example of a callback function (I understand the concept, just not the syntax).


    Ideally showing how we get this callback function to call another method passing the MessageBox arguments?


    Thanks in advance! :saint:

  • Hi, this can be a simple lambda:

    Java
    event.getPlayer().showMessageBox(MessageBoxButtons.Yes_No_Cancel, "Title", "Question", 0, button -> {
    switch (button) {
    case 0 -> event.getPlayer().sendTextMessage("YES!");
    case 1 -> event.getPlayer().sendTextMessage("NO!");
    case 2 -> event.getPlayer().sendTextMessage("CANCEL! (Or just timeout)");
    }
    });
  • Or if your player is stored in field you can use a method:

    Java
    player.showMessageBox(MessageBoxButtons.Yes_No_Cancel, "Title", "Question", 0, this::processMenuButton);
    Java
    private void processMenuButton(int button) {
    switch (button) {
    case 0 -> player.sendTextMessage("YES!");
    case 1 -> player.sendTextMessage("NO!");
    case 2 -> player.sendTextMessage("CANCEL! (Or just timeout)");
    }
    }

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!