Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

3 Create UI

XenialDan edited this page Sep 13, 2017 · 1 revision

Basics

Before creating an UI, you need to have customui installed. Check out DEVirion for developing from source code and the virion support for information how to use virions.

1: Importing the API

To use the API in a class you must import it. Do this by adding use xenialdan\customui\API as UIAPI; at your imports. The as UIAPI ensures that it doesn't collide or intersects with other API's

2: Pre-work

Make sure you completed #1 in your main class of the plugin you write. (example: Main.php, Loader.php)

To create UI's, the packets must be registered first. Import these files in your main class:

use xenialdan\customui\network\ModalFormRequestPacket;
use xenialdan\customui\network\ModalFormResponsePacket;
use xenialdan\customui\network\ServerSettingsRequestPacket;
use xenialdan\customui\network\ServerSettingsResponsePacket;

Remember: you need to import the type of UI you use and elements too!

Then add this in the end of onEnable():

		PacketPool::registerPacket(new ModalFormRequestPacket());
		PacketPool::registerPacket(new ModalFormResponsePacket());
		PacketPool::registerPacket(new ServerSettingsRequestPacket());
		PacketPool::registerPacket(new ServerSettingsResponsePacket());
		/** call this AFTER registering packets! */
		$this->registerUIs();

This will make sure that the packets exist before attempting to create an UI.

The registerUIs() call is a call to a function, so onEnable() keeps relatively clean.

The registerUIs() function:

	private function registerUIs(){}

3: Creating an simple ui

This example will only explain a button only UI, also known as SimpleForm.

First, you need a way to save the IDs of the UIs. To make it simple, we will just use a public static variable called $uis. Remember: you need to import the type of UI you use and elements too! In this example you would need to import SimpleForm and Button! This should be in the beginning of the file before every all functions, after class <name>:

/** @var int[] **/
public static $uis = [];
	private function registerUIs(){
		$ui = new SimpleForm('Title', 'Description text in the form, can also be empty');
		$button = new Button('Button without image');
		$ui->addButton($button);
		$button = new Button('Button with Minecraft texture (stick)'); $button->addImage(Button::IMAGE_TYPE_PATH, "textures/items/stick");
		$ui->addButton($button);
		$button = new Button('Button with image from the internet'); $button->addImage(Button::IMAGE_TYPE_URL, "https link to a squared image");
		$ui->addButton($button);
		/* This will save the ID of the UI into an array for later sending */
		/* $ui will be NULL after this! You can't add any more elements to the UI after this! */
		/* self::$uis should be created before. See above for the code */
		self::$uis['simpletest'] = UIAPI::addUI($this, $ui);
	}

Congratulations, you created your first UI.

Continue with the page 'Sending custom UIs to players'

Clone this wiki locally