Skip to content

Unity Library

Hempfest edited this page Oct 7, 2021 · 15 revisions

About

Each menu can be configured with different properties making the menu work how you need it to, the #join() method creates a new instance within the builder and the #orGet(Predicate<Menu>) method returns a new instance if the specified target is non existent. Read the unity javadoc for further explanation on what certain properties do.

Menu Types

Pagination

The below example paginates the head database of labyrinth. Building a paginated menu can be accomplished in 2 easy steps after customization you just need to specify:

1. A list element containing objects you wish to paginate

2. Navigation to let the menu know how to respond

				PaginatedMenu m = MenuType.PAGINATED.build()
				.setTitle("&6&lHeads {0}/{1}")
				.setSize(Menu.Rows.ONE)
				.setHost(Plugin).setKey("test")
				.setProperty(Menu.Property.CACHEABLE, Menu.Property.RECURSIVE)
				.setStock(i -> i.addItem(new ListElement<>(CustomHead.Manager.getHeads()).setLimit(2).setPopulate((value, element) -> {
					element.setElement(value.get());
					element.setElement(edit -> edit.setTitle(value.name()).build());
					element.setClick(c -> {
						c.setCancelled(true);
						c.setHotbarAllowed(false);
					});
				})).addItem(b -> b.setElement(it -> it.setItem(SkullType.ARROW_BLUE_RIGHT.get()).setTitle("&5Next").build()).setType(ItemElement.ControlType.BUTTON_NEXT).setSlot(4))
						.addItem(b -> b.setElement(it -> it.setItem(SkullType.ARROW_BLUE_LEFT.get()).setTitle("&5Previous").build()).setType(ItemElement.ControlType.BUTTON_BACK).setSlot(3)))
				.orGet(me -> me instanceof PaginatedMenu && me.getKey().isPresent() && me.getKey().get().equals("test"));

		m.open(e.getPlayer());

Singular

		SingularMenu m = MenuType.SINGULAR.build()
				.setTitle("&6&lMenu")
				.setSize(Menu.Rows.ONE)
				.setHost(Plugin).setKey("test")
				.setProperty(Menu.Property.CACHEABLE, Menu.Property.RECURSIVE)
				.setStock(i -> i.addItem(b -> b.setElement(it ->
						it.setItem(SkullType.ARROW_BLUE_RIGHT.get())
								.setTitle("&eLogs")
								.setLore("========",
									"Test text.",
									"Hello world.",
									"========")
								.build())
						.setSlot(4)
						.setClick(click -> {
							click.setCancelled(true);

							Player p = click.getElement();
							// Menu#open(p); <- open our menu
							
						}))
						.addItem(b -> b.setElement(it ->
								it.setItem(SkullType.ARROW_BLUE_LEFT.get())
										.setTitle("&5Players")
										.addEnchantment(Enchantment.ARROW_DAMAGE, 1000)
										.setFlags(ItemFlag.HIDE_ENCHANTS)
										.build())
								.setSlot(3)
								.setClick(click -> {
									click.setCancelled(true);

									Player p = click.getElement();
									// Menu#open(p); <- open our menu
									
								})))
				.orGet(me -> me instanceof SingularMenu && me.getKey().isPresent() && me.getKey().get().equals("test"));

		m.open(e.getPlayer());

Printable

	PrintableMenu m = MenuType.PRINTABLE.build()
				.setTitle("&6&lPrinter")
				.setSize(Menu.Rows.ONE)
				.setHost(Plugin).setKey("test")
				.setStock(i -> i.addItem(b -> b.setElement(it -> it.setItem(SkullType.ARROW_BLUE_RIGHT.get()).build()).setSlot(0).setClick(c -> {
					c.setCancelled(true);
					c.setHotbarAllowed(false);
				}))).join()
				.addAction(click -> {
					click.setCancelled(true);
					click.setHotbarAllowed(false);
					if (click.getSlot() == 2) {
						String[] arguments = click.getParent().getName().split(" ");
						click.getElement().sendMessage(String.join(" ", arguments));
					}

				});

		m.open(e.getPlayer());

Menu Elements

Custom ItemElement's

You have the option to attach data to item element's in more than one way! Most elements within a labyrinth Menu will be an inheritance of Menu.Element<T, V> excluding Menu itself but itemelements all the same allow you to attach child components to their individual containers.

See this example:

public class TestItem extends ItemElement<String> {

	private final ItemStack realItem;

	public TestItem() {
		super("Hello world!");// This is our data of choice, a simple string, but it can be whatever you need to attach!
		// Our dummy item, we're making our item inheritance here un-modifiable!
		setElement(new ItemStack(Material.DIRT));
		this.realItem = Items.edit(e -> e.setType(Material.CLOCK).setTitle("&6Bing").build());
	}

	@Override
	public @NotNull ItemStack getElement() {
		return realItem;
	}

	@Override
	public Optional<Integer> getSlot() {
		return Optional.of(2);
	}
}

Then to add it to your menu its as simple as:

	TestItem item = new TestItem();

		Menu m = MenuType.SINGULAR.build()
				.setTitle("&6&lHeads {0}/{1}")
				.setSize(Menu.Rows.ONE)
				.setHost(pl).setKey("test")
				.setProperty(Menu.Property.CACHEABLE, Menu.Property.RECURSIVE)
				.setStock(i -> i.addItem(item))
			.orGet(me -> me instanceof SingularMenu && me.getKey().isPresent() && me.getKey().get().equals("test"));

Custom elements.

Adding your OWN Menu.Elements to either a Menu or Menu.Element is just as easy:

public class TestElement extends Menu.Element<String, Integer>{
	@Override
	public String getElement() {
		return "Hello world!";
	}

	@Override
	public Integer getAttachment() {
		return 420;
	}
}

Now from here as mentioned before we can add this element to any of the following: [Menu, Menu.Element] and pretty much most menu functions are elements :) have fun!

		TestItem item = new TestItem();
		
		TestElement element = new TestElement();
		
		Menu m = MenuType.SINGULAR.build()
				.setTitle("&6&lHeads {0}/{1}")
				.setSize(Menu.Rows.ONE)
				.setHost(pl).setKey("test")
				.setProperty(Menu.Property.CACHEABLE, Menu.Property.RECURSIVE)
				.setStock(i -> {

					// We can add our element to our ItemElement
					item.addElement(element);
					
					i.addItem(item).setClick(c -> {
						c.setCancelled(true);
						c.setHotbarAllowed(false);
						c.getElement().sendMessage("You clicked on test!");
					});
					// We can add our element to our InventoryElement
					i.addElement(element);
					
				})
			.orGet(me -> me instanceof SingularMenu && me.getKey().isPresent() && me.getKey().get().equals("test"));
		
		// We can add our element to our Menu itself!
		m.addElement(element);

		m.open(e.getPlayer());
Clone this wiki locally