Response

A slot Response consists of two fields: a slot number and a "responder" class triggered by clicking said slot's Item. Without a Response, your Item is purely cosmetic and will do nothing once clicked.

You may attach a Response to your Item like this:

// Show a cute fruit in the first slot
setItem(0, new Item(Material.APPLE));
        
// Add a command which admins can change later
getGui().addTask(0, "say Fruit for life!");
        
// Set the responder class for executing a command
addResponse(0, new CommandResponder(commandSender, "say Fruit for life!", true));

Alternatively, you can set everything at once through an ItemSection at instantiation:

setItem(new ItemSection(0, new Item(Material.ITEM),
"say Fruit for life!", // Task
new CommandResponder(commandSender, "say Fruit for life!", true)); // Response

Note how we set the String of "say Fruit for life!" twice. This because the Task will be saved to file, while the Response will not. If you should wish to load your GUI from file, where the command has potentially been changed, you could now do this instead:

public class MyCustomGUI extends BasicGUI {
    public MyCustomGUI(Player player) {
        super(new GUI("Fruit Fanatic"), Rows.SIX);
    
        final BasicGUI loaded /*= ...loadGuiData(fileName)*/;
        if (loaded == null) {
           // First time opening GUI
           setItem(0, new Item(Material.APPLE));
           getGui().addTask(0, "say Fruit for life!");
           addResponse(0, new CommandResponder(cmdSender, "say Fruit for life!", true));
        } else {
           // Loaded saved GUI from file
           setGUI(loaded.getGui());
           
           for (final Entry<Integer, String> entry : getGui().getTasks().entrySet()) {
               addResponse(entry.getKey(), new CommandResponder(cmdSender, entry.getValue(), true));
           }
        }
    }
}

Obviously, this is only an example. You may instead wish to copy all your GUI files to disk beforehand or only load files into memory at server startup. That's up to you!

Last updated