Skip to content

Creating Ordinary Menus

Mazen edited this page Sep 10, 2024 · 13 revisions

Perquisites

Lotus depends on java 17 to operate. make sure you have those before we begin

Initializing Lotus API

You must create a Lotus instance in your onEnable() Quick example:

public class Test extends JavaPlugin {

   private Lotus api;
	
   @Override
   public void onEnable() {
     api = Lotus.load(this); //or you can use the other Lotus#load method to specify EventPriority for onClick event
   }

}

Creating and opening menus

In Lotus, menus object MenuView is not meant to be dealt with when using the API. You should stick to using a Menu; it's an interface that defines the information created when opening the menus.

Here's an example:

public final class ExampleMenu implements Menu {

	private final ItemStack[] fruits =  {
			  ItemBuilder.legacy(Material.APPLE).build(),
			  ItemBuilder.legacy(Material.CARROT_ITEM).build(),
			  ItemBuilder.legacy(Material.GOLDEN_APPLE).build(),
			  ItemBuilder.legacy(Material.GOLDEN_CARROT).build()
	};
	private int index = 0;
	/**
	 * @return The unique name for this menu
	 */
	@Override
	public String getName() {
		return "example menu";
	}

	/**
	 * @param extraData the data container for this menu for extra data
	 * @param opener    the player who is opening this menu
	 * @return the title for this menu
	 */
	@Override
	public @NotNull MenuTitle getTitle(DataRegistry extraData, Player opener) {
		return MenuTitles.createLegacy("&cExample Menu");
	}

	/**
	 * @param extraData the data container for this menu for extra data
	 * @param opener    the player who is opening this menu
	 * @return the capacity/size for this menu
	 */
	@Override
	public @NotNull Capacity getCapacity(DataRegistry extraData, Player opener) {
		return Capacity.ofRows(3);//Alternative approach: Capacity.ofRows(3)
	}

	/**
	 * Creates the content for the menu
	 *
	 * @param extraData the data container for this menu for extra data
	 * @param opener    the player opening this menu
	 * @param capacity  the capacity set by the user above
	 * @return the content of the menu to add (this includes items)
	 */
	@Override
	public @NotNull Content getContent(DataRegistry extraData,
	                                   Player opener, Capacity capacity) {
		Button borderPane = Button.clickable(
				  ItemBuilder.legacy(Material.STAINED_GLASS_PANE, 1, (short)5).build(),
				  (menuView, event)-> {
					  event.setCancelled(true);
					  //we want nothing to happen here
				  }
		);
		Button transformingFruit = Button.transformerItem(fruits[0], (menuView, event)-> {
			event.setCancelled(true);
			index++;
			if(index >= fruits.length) {
				index = 0;
			}
			return fruits[index];
		});

		return Content.builder(capacity)
				  .apply(content -> {
					  content.fill(transformingFruit);
					  content.fillBorder(borderPane);
				  })
				  .build();
	}
}

Opening the menus

Call the method Lotus#openMenu example >>

api.openMenu(player, new ExampleMenu());

Result

example-menus

More

If you want more detailed wiki check out other pages on the right-side of this page and thanks in advance <3

Clone this wiki locally