Using Metadata Tags In GUI's

Hello everyone, I recently learned how to use metadata tags inside of GUI's, and I felt like sharing it in this simple little guide. This guide is intended to be easy to use, but may have some complicated functions in it.

SkQuery may be required

1)

For the first step of this tutorial, you need to open up your writing tool of choice. This can be using Microsoft Word, Notepad, or any other interface that will allow you to type your code.

2)

For the second step of this tutorial, we will be setting up a basic gui that will open when the player runs a command. Normally we would just use "set (variable) to chest inventory," but since we are using metadata tags, we will use a code like this:
command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"


What we can now do is fill in the slots of the GUI with items of our choice by using lines such as this:
command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"
set slot 0 of metadata tag "YourGuiName" of player to (your item name) named "Your Name Here"

#note that the first slot of the GUI is "slot 0" as opposed to "slot 1"


Now, if we want to set all of the items in the GUI to the same item, we can do a line of code like this, which will set a selection of our GUI to an item of our choice. For this example I will be using white glass panes, as they make a nice background for a GUI.
command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"
loop numbers between 0 and 44:
set slot loop-number of metadata tag "YourGuiName" of player to white stained glass pane named " "

#note that if you name the glass "" it will appear as "White Stained Glass Pane." To avoid this, put a space in between the quotes like this: " "


3)
For the third step, we will be putting in the line of code to open the GUI to the player that runs the command. To do this we will put the metadata tag in parentheses like this:
command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"
loop numbers between 0 and 44:
set slot loop-number of metadata tag "YourGuiName" of player to white stained glass pane named " "
open (metadata tag "YourGuiName" of player) to player


By doing this, when the command is run by the player, it will properly fill in the items and open the GUI to the player. Our only problem now is that the players will be able to take the items out of the GUI, which you will probably not want.

4)
For the fourth step, we will be making it so that the players are unable to take any items from the chests. This is where the metadata tags become most helpful. If we were to be using code without metadata tags, the skript will often times interfere with the player inventory and cause issues. By using our metadata tags, we avoid this issue.
command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"
loop numbers between 0 and 44:
set slot loop-number of metadata tag "YourGuiName" of player to white stained glass pane named " "
open (metadata tag "YourGuiName" of player) to player


on inventory click:
event-inventory = (metadata tag "YourGuiName" of player):
cancel event


This piece of the Skript will make it so that when a player tries to pick up any item in the GUI, it will be immediately put back down. If we want to have other items do things when clicked, we will use slot events like this:

command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"
loop numbers between 0 and 44:
set slot loop-number of metadata tag "YourGuiName" of player to white stained glass pane named " "
open (metadata tag "YourGuiName" of player) to player


on inventory click:
event-inventory = (metadata tag "YourGuiName" of player):
cancel event
index of event-slot = 22:
(your event here)
close inventory of player

#slot 22 is the direct center of this specific GUI


To make slot 22 stand out more, we can set it to an item such as a diamond block with the name "Info." For this example I will make the block give you some basic information.


command /opengui:
trigger:
set metadata tag "YourGuiName" of player to chest inventory with 5 rows named "YourGuiName"
loop numbers between 0 and 44:
set slot loop-number of metadata tag "YourGuiName" of player to white stained glass pane named " "
set slot 22 of metadata tag "YourGuiName" of player to diamond block named "&3&lInfo" and lore "&bClick for info in chat!"
open (metadata tag "YourGuiName" of player) to player


on inventory click:
event-inventory = (metadata tag "YourGuiName" of player):
cancel event
index of event-slot = 22:
send "&3Tutorial made by &7- &bZsCortzones"
send "&3Tutorial On &7- &bMetadata Tags in GUIs"
close inventory of player


5)
Customize your Skript!