Skip to content

Actions

timsixth edited this page Aug 7, 2023 · 16 revisions

What are actions ?

Action executes when player click on item in inventory.
Library has two types of action.

The first simple action executes when player click on item in inventory.
This type has only two variables in YAML. (action_name and arguments).

The second action is a section action which must have extra section in YAML to work.
This type must be enabled in MenuManager before use

First type

 click_action:
   type: 'SEND_MESSAGE' #action name
   args:
    - '&aKOKOS' #arguments is always list 

Second type

 click_action:
   type: 'GIVE_ITEMS' #action name
   args:
    - '' #arguments
     items: #extra section
       DIAMOND:
         amount: 1

How to create actions in code ?

Every (simple) action must extend AbstractAction and implement ClickAction.
In constructor must be name of action, the second param is optional.

public class SendMessageAction extends AbstractAction implements ClickAction {

    public SendMessageAction() {
        super("SEND_MESSAGE", ActionType.CLICK //(optional));
    }

    // executes when player click on item
    @Override
    public void handleClickEvent(InventoryClickEvent event, MenuItem menuItem) {
        Player player = (Player) event.getWhoClicked();

        player.sendMessage(getMessage());

        event.setCancelled(true);
    }

    private String getMessage() {
        if (!hasArgs()) {
            throw new IllegalStateException("List of action arguments is empty (Action = " + getName() + " )");
        }

        return ChatUtil.chatColor(getArgs().get(0));
    }
}
Clone this wiki locally