Skip to content

Actions

timsixth edited this page Jan 3, 2025 · 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.

The library offers default actions.

These action names are below:

Default actions:

  • NONE - when you don't set any action this action is default (on args)
  • CLOSE_MENU - closes current inventory (on args)
  • SEND_MESSAGE - sends message(s) to player (require args)
  • GIVE_ITEMS - gives items to player, these items must be configure in 'items' section in YAML. This action must be configured in MenuManager. (no args)
  • NEXT_PAGE - sets the next page in the paginated menu (no args)
  • PREVIOUS_ACTION - sets the previous page in the paginated menu (no args)

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 ?

Both action types

In constructor must be name of action, the second param is optional.
Every action must extend AbstractAction.

First type (Action)

Every (simple) action must implement ClickAction.

public class SendMessageAction extends AbstractClickAction {

    public SendMessageAction() {
        super("SEND_MESSAGE");
    }

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

        getMessages().forEach(player::sendMessage);

        event.setCancelled(true);
    }

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

        return ChatUtil.chatColor(getArgs());
    }
}

Second type (Section Action)

Every (section) action must implement SectionAction.

public class GiveItemsActionImpl extends AbstractAction implements SectionAction {

    private List<ItemStack> items; //declare field, in this list every item from YAML will be loaded. 

    public GiveItemsActionImpl() {
        super("GIVE_ITEMS");
    }

    @Override
    public void handleClickEvent(InventoryClickEvent event, MenuItem menuItem) {
        Player player = (Player) event.getWhoClicked()
        
        for (ItemStack item : items) {
            player.getInventory().addItem(item);
        }
        
        event.setCancelled(true);
    }

    //setter to set this list in MenuManager
    public void setItems(List<ItemStack> items) {
        this.items = items;
    }
}