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

5 Response handling

XenialDan edited this page Sep 13, 2017 · 1 revision

Notice: This will not have the API handler in it yet. Its still in a beta phase!

You came to create and send an UI already? Great! Now you want to know what was clicked/set? Okay, here we go. Be ready for a complicated session, Harry.

Basics

The API will handle the packets itself. You only have to listen for the events called UIDataReceiveEvent and UICloseEvent in your plugin's EventListener.

Response types

General

All UI's send NULL if closed via back button, pressing x or ESC key. You don't need to check that, instead you check the UICloseEvent and receive an id within it.

SimpleForm

A simple form, existing out of mostly just buttons, replies with the index of the pressed button. In our example, if the second button gets pressed, we get this data:

int(1)

, which is the second button, because indexes start at 0.

OTHER FORM TYPES LATER, I AM BORED OF THIS RIGHT NOW

Getting the response

In your EventListener, add this function, and remember to import the imports..

	/**
	 * @param UIDataReceiveEvent $event
	 */
	public function onUIDataReceiveEvent(UIDataReceiveEvent $event){
		/* This makes sure that only events for this plugin are handled */
		if($event->getPlugin() !== $this->owner) return;
		switch ($id = $event->getID()){
			case Loader::$uis['simpletest']: {
				$event->getPlayer()->sendMessage(UIAPI::handle($this->owner, $id, $event->getData(), $event->getPlayer()));
				/* OR */
				var_dump($event->getData());
				break;
			}
		}
	}

I WANT IT SIMPLE, DUMBLEDORE!

Okay Harry, here you have literature to execute commands (these kids nowadays.. -.-)

<?php

namespace xenialdan\HarryPotter;

use pocketmine\event\Listener;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\Player;
use pocketmine\plugin\Plugin;
use pocketmine\Server;
use xenialdan\customui\API as UIAPI;
use xenialdan\customui\event\UICloseEvent;
use xenialdan\customui\event\UIDataReceiveEvent;
use xenialdan\customui\network\ModalFormResponsePacket;

class EventListener implements Listener{
	/** @var Loader */
	public $owner;

	public function __construct(Plugin $plugin){
		$this->owner = $plugin;
	}

	/**
	 * @group UI Response Handling
	 * @param DataPacketReceiveEvent $ev
	 */
	public function onPacket(DataPacketReceiveEvent $ev){
		$packet = $ev->getPacket();
		$player = $ev->getPlayer();
		switch ($packet::NETWORK_ID){
			case ModalFormResponsePacket::NETWORK_ID: {
				/** @var ModalFormResponsePacket $packet */
				$this->handleModalFormResponse($packet, $player);
				$packet->reset();
				$ev->setCancelled(true);
				break;
			}
		}
	}

	/**
	 * @group UI Response Handling
	 * @param ModalFormResponsePacket $packet
	 * @param Player $player
	 * @return bool
	 */
	public function handleModalFormResponse(ModalFormResponsePacket $packet, Player $player): bool{
		$ev = new UIDataReceiveEvent($this->owner, $packet, $player);
		if (is_null($ev->getData())) $ev = new UICloseEvent($this->owner, $packet, $player);
		Server::getInstance()->getPluginManager()->callEvent($ev);
		return true;
	}

	/**
	 * @param UIDataReceiveEvent $event
	 */
	public function onUIDataReceiveEvent(UIDataReceiveEvent $event){
		/* This makes sure that only events for this plugin are handled */
		if($event->getPlugin() !== $this->owner) return;
		switch ($id = $event->getID()){
			case Loader::$uis['wandbuy']: {
				$event->getPlayer()->sendMessage(UIAPI::handle($this->owner, $id, $event->getData(), $event->getPlayer()));
				break;
			}
			case Loader::$uis['simpletest']: {
				switch($event->getData()){
					case 0:{
						/* Button 1 was pressed */
						// whatever you want here
						break;
					}
					case 1:{
						/* Button 2 was pressed and makes the player execure a say command */
						$command = "say HI i pressed button 2";
						$this->owner->getServer()->getCommandMap()->dispatch($event->getPlayer(), $command);
						break;
					}
					case 2:{
						/* Button 3 was pressed and makes the player execute /gamemode 1 */
						$command = "gamemode 1";
						$this->owner->getServer()->getCommandMap()->dispatch($event->getPlayer(), $command);
						break;
					}
				}
				break;
			}
		}
	}
}