Skip to content

Example GUI Code

Hamza Coşkun edited this page Apr 6, 2024 · 11 revisions

Example GUI

public class SelectServerGUI extends Gui {

	public SelectServerGUI(Player player) {
		super(player, "select-server-gui", "Select Server to Connect", 6);
	}

	@Override
	public void onOpen(InventoryOpenEvent event) {

		//add stone to the first available slot of gui
		addItem(Material.STONE); 

		//add stone to the first available slot of gui
		addItem(new ItemStack(Material.STONE));

		//add emerald to 13. slot of gui
		addItem(13, new ItemStack(Material.EMERALD));

		//add emerald to 14. slot of gui
		addItem(14, new Icon(Material.EMERALD));

		//add emerald named 'Green Diamond' to 15. slot of gui
		addItem(15, new Icon(Material.EMERALD).setName("Green Diamond"));

		//create custom icon. set their name and lore.
		Icon anIcon = new Icon(Material.GRASS).setName("Skyblock Server").setLore("You wanna play skyblock?", "");

		if (!player.isOp()) {
			//if player doesn't have permission, append new lore
			anIcon.appendLore("We are in maintenance!", "come back later!"); 
		} else {
			//if player is op, append another lore
			anIcon.appendLore("Click to connect!"); 
		}

		anIcon.setAmount(10); // set amount of icon

		anIcon.hideFlags(); // hide flags of icon (ex: damage of sword)

		anIcon.enchant(Enchantment.ARROW_DAMAGE, 100); //enchant the icon (supports enchanted book enchantments)

		anIcon.setDurability(10); // set durability(damage) of icon

		anIcon.onClick(e -> { //define click event as variable named 'e'
			player.sendMessage("You clicked the icon!"); //send message when player clicked
			e.getCursor(); //you can use click action as 'e' variable
		});

		anIcon.onDrag(e -> { //define drag event as variable named 'e'
			player.sendMessage("You dragged the icon!"); //send message when player dragged
			e.getCursor(); //you can use drag action as 'e' variable
		});

		updateTask(0, 20, update -> {
			player.sendMessage("hi"); //say hi every second until gui is closed
		});


		sendTitleUpdate("New Title"); // sets title of gui

		sendSizeUpdate(54); //sets size(not row) of gui

		/*
		Other GUI class methods

		getInventory(); bukkit inventory of gui
		getAdvancedSlotManager(); advanced slot manager of gui
		getId(); id of gui
		getItems(); item map of gui
		getPagination(); pagination manager of gui
		getPlugin(); your plugin's instance
		getSize(); 9 * row amount of the gui
		getTitle(); title text of the gui

		 */
	}

	@Override
	public boolean onClick(InventoryClickEvent event) {
                super.onClick(event);
		player.sendMessage("You clicked to the gui!");
		return false; // false -> cancel the event, true -> do not cancel the event.
	}

	@Override
	public void onClose(InventoryCloseEvent event) {
                super.onClose(event);
		Bukkit.getScheduler().runTask(plugin, this::open); //see 1.
		player.sendMessage("You can not close this menu!");
	}


}

1. <- Explained why we don't use just open() at close.

Clone this wiki locally