1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Skript Vanilla GUIs

Discussion in 'Tutorials' started by Runakai, Mar 20, 2019.

  1. Runakai

    Runakai Well-Known Member

    Joined:
    Apr 27, 2018
    Messages:
    458
    Likes Received:
    26
    Vanilla GUIs ~ Create your own little world in a prison called inventory

    Vanilla GUIs. You were using TuSKe or SkQuery and you basically got bullied for using those Addons for GUIs? Or are you just looking for a good way of making GUIs in Skript? Then you're more than welcome to read this tutorial. If you have used vanilla GUIs before I recommend to read this page again since it will include some useful information and will only require vanilla Skript now since we're approaching them differently now. No need for Skript-mirror, SkQuery or Sk-Bee to get the inventory name. We don't need them. (Unless you're below 1.9 minecraft, then you need them!)

    Metadata comparison does not work on 1.12. You need to use a snippet that's provided at the bottom of the tutorial!


    Preparation
    To get started you need following Skript versions:
    Using Vanilla Skript is possible by having the Skript version 2.2dev36. They can be used earlier on but since 2.2dev36 is recommended for 1.8, use that. Since we will focus on Metadata however, dev36 is needed. If you need it to work on earlier versions, use the name of event-inventory effect.

    Link:
    https://github.com/SkriptLang/Skript/releases/download/dev36/Skript.jar

    Just use the latest version of skript and you're fine.
    Link: https://github.com/SkriptLang/Skript/releases
    Okay, let me explain how vanilla GUIs work by describing it. You open an inventory and set the slots. Every time you click on the slot you check if certain conditions are met. Based on that you perform effects. You didn't understand that? Look at that:


    Code (Text):
    1. command /vanillaGUI:
    2.     description: The best way of doing something.
    3.     trigger:
    4.         set metadata tag "vanillaGUI" of player to chest inventory with 3 rows named "&7Hello There"
    5.         set slot 0 of metadata tag "vanillaGUI" of player to stone named "&6General Kenobi"
    6.         open (metadata tag "vanillaGUI" of player) to player

    What is metadata?

    Metadata means data within data. So basically it is like using variables. The only difference is that metadata have a holder that's usually an entity or a block and it gets deleted when the server stops since it's saved in RAM. Skript's metadata implementation can only store single values per metadata key per holder. So no lists are possible. It is great for saving information shor-term based of a single entity or block.
    In our case we use it to distinguish inventories.

    Inventory Click Event

    To perform different actions you use the Inventory click event, add some conditions and based on that build your own little actions for individual slots OR the entire inventory!
    Let's say you want to cancel every click in an inventory without checking each individual slot every time. Simply use this:

    Code (Text):
    1. on inventory click:
    2.     if event-inventory = (metadata tag "vanillaGUI" of player):
    3.         #check if the clicked inventory is the one we just created
    4.         cancel event
    5.         #cancel the clicking. This makes the Item unstealable

    Pretty self-explanatory, right? Cancel the event, cancel stealing.
    The event-values for this event are:

    Event-item:
    Get the TYPE of an Item. If you click on a stone named "General Kenobi" this will only return stone.
    Event-slot:

    Get the clicked item including name etc.
    Index of event-slot:

    The index effect is universal but we use it to get the clicked slot. Numbers rangin from 0-54
    Event-click type:

    This is by far the most interesting one. Get the way a player clicked on an Item! All of them can be found here:
    https://skripthub.net/docs/?id=2137
    Event-inventory action:

    Basically the same as event-click type, just with more options and possibilities such as detecting if a player picked up all of the items or half of the stack etc. List can be found here:
    https://skripthub.net/docs/?id=2149
    Event-Inventory:

    Get the type of the event-inventory. Ranging from Hoppers to Chests up to Brewing stands etc. A list of them can be found here:
    https://skripthub.net/docs/?id=2151

    Let's make a bit of code!


    Code (Text):
    1. command /vanillaGUI:
    2.     description: The best way of doing something.
    3.     trigger:
    4.         set metadata tag "vanillaGUI" of player to chest inventory with 3 rows# named "&7Hello There"
    5.         set slot 0 of metadata tag "vanillaGUI" of player to stone named "&6General Kenobi"
    6.         set slot 1 of metadata tag "vanillaGUI" of player to player's skull named "&6%player%" with lore "&7Right click me to receive my head!"
    7.         open (metadata tag "vanillaGUI" of player) to player
    8.  
    9. on inventory click:
    10.     if event-inventory = (metadata tag "vanillaGUI" of player):
    11.         #check if the clicked inventory is the one we just created
    12.         cancel event
    13.         #cancel the clicking. This makes the Item unstealable
    14.         if index of event-slot is 0:
    15.             #check if the clicked slot is the stone, so 0
    16.             give event-slot to player
    17.             #give the clicked item to the player.
    18.         else if index of event-slot is 1:
    19.             #check if the clicked slot is the skull, so 1
    20.             if event-click type is right mouse button:
    21.                 #check if the player right clicks the item
    22.                 set {_playerHead} to uncolored name of event-slot parsed as player
    23.                 give event-slot to player
    24.                 send "&6Player %player% has received a skull of you!" to {_playerHead}
    25.                 #since we can't use arguments, we just parse the name of the event-slot and get the skull by doing that.
    26.                 #you can use the variable as you like. I simply sent a plain text to the player.
    27.                 #ban {_playerHead} would also work
    28.                 #using NBT will work as well!

    If you have any questions let me know in the comment section.

    Some Snippets. Some of them require skript mirror 2.0.0. Since it's the most superior add-on you should have it installed anyway. The metadata fix doesn't require skript mirror .
    Code (Text):
    1. import:
    2.     org.bukkit.event.inventory.InventoryClickEvent
    3. expression [event( |-)]([number ]key|hot[[ ]bar][ key])[ button]:
    4.     return type: integer
    5.     get:
    6.         return event.getHotbarButton() + 1 if event.getHotbarButton() != -1
    7.         return -1
    8.  
    9. expression [event( |-)]([number ]key|hot[[ ]bar][ key])[ button]( |-)slot:
    10.     return type: integer
    11.     get:
    12.         return event.getHotbarButton() if event.getHotbarButton() != -1
    13.         return -1
    Code (Text):
    1. function compare(1: inventory, 2: inventory) :: boolean:
    2.     return false if (name of {_1}) != (name of {_2})
    3.     return false if (rows of {_1}) != (rows of {_2})
    4.     return false if type of {_1} != type of {_2}
    5.     return true
    Code (Text):
    1.  
    2. command /gui:
    3.     trigger:
    4.         set metadata tag "vanillaGUI" of player to chest inventory with 3 rows named "Best GUIs"
    5.         set slot 1 of (metadata tag "vanillaGUI" of player) to diamond hoe named "&d:("
    6.         open (metadata tag "vanillaGUI" of player) to player
    7. on inventory click:
    8.     if compare(metadata tag "vanillaGUI" of player, event-inventory) = true:
    9.         cancel event
    10.  
    11. [/COLOR]
    12.  
    Thanks for mr.gigi for providing a workaround!
     
    #1 Runakai, Mar 20, 2019
    Last edited: Jun 24, 2020
    • Winner Winner x 3
    • Like Like x 2
    • Useful Useful x 2
    • Creative Creative x 1
  2. mtrD359360

    mtrD359360 Member

    Joined:
    May 2, 2017
    Messages:
    17
    Likes Received:
    1
    Sorry but I have a concern using the skript vanila GUI.
    I tried to prevent stealing from all type of clicking, but it seems that the expression
    Code (Text):
    1. cancel event
    only works with left click, while the other click type can still steal the item. How can I solve that?
     
  3. Runakai

    Runakai Well-Known Member

    Joined:
    Apr 27, 2018
    Messages:
    458
    Likes Received:
    26
    That's something that appeared to me as well, should've mentioned it in the tutorial, you're right! Normally every click should get cancelled however, sometimes it doesn't. I fixed that by adding a condition to see what kind of click type was still able to steal the item and cancel that. That's annoying, I know but it doesn't appear for everyone! On my localhost server it seemed to work perfectly fine on my hosted server however it didn't. Maybe installing newer versions if skript/paper fixes the issue
     
  4. mtrD359360

    mtrD359360 Member

    Joined:
    May 2, 2017
    Messages:
    17
    Likes Received:
    1
    But I am already using the latest version of skript, which is v2.5-alpha2. So is there another solution?
     
  5. Runakai

    Runakai Well-Known Member

    Joined:
    Apr 27, 2018
    Messages:
    458
    Likes Received:
    26
    May show me the code?
     
  6. mtrD359360

    mtrD359360 Member

    Joined:
    May 2, 2017
    Messages:
    17
    Likes Received:
    1
    It is so long, but I already put "cancel event" under ALL tab of my custom GUIs, without considering the click type.
    Even if I do something like this:

    Code (Text):
    1. on inventory click:
    2.     if event-inventory is not player's inventory:
    3.         if event-click type = left mouse button:
    4.             cancel event
    5.             # Generate some GUI shits
    6.         else:
    7.             cancel event
    8.  
    or this:
    Code (Text):
    1. on inventory click:
    2.     if event-inventory is not player's inventory:
    3.         cancel event
    4.         # Generate some GUI shits
    5.  
    also doesn't work.
     
  7. Runakai

    Runakai Well-Known Member

    Joined:
    Apr 27, 2018
    Messages:
    458
    Likes Received:
    26
    I don't know then, the "ugly" way of cancelling would be to set a variable to the cursor slot, set the cursor slot to air and then setting the original slot to the item in the variable
     
  8. mtrD359360

    mtrD359360 Member

    Joined:
    May 2, 2017
    Messages:
    17
    Likes Received:
    1
    What is the expression to get or modify cursor slot?
     
  9. Govindas

    Govindas Well-Known Member

    Joined:
    Feb 5, 2017
    Messages:
    82
    Likes Received:
    4
    I never have this kind of problem with skript 2.4, maybe you have addon that breaks it?
     
    • Agree Agree x 1
  10. Nayzal

    Nayzal Member

    Joined:
    Jul 5, 2020
    Messages:
    2
    Likes Received:
    0
    Hi, sorry for replying to such an old thread, but I can't seem to get it to work for an item in the GUI triggering another guy. I can open the GUI, but I can't seem to make the items in the second GUI unstealable. here is code:
    Code (Text):
    1. on right click on villager:
    2.     if player is sneaking:
    3.         teleport target to location 200 meters below target
    4.     else:
    5.         cancel event
    6.         set {_name} to name of target entity
    7.         set {_uuid} to target entity's uuid
    8.         set metadata tag "npcgui.%{_uuid}%" of player to chest inventory with 1 row named "%{_name}%"
    9.         set slot 1 of metadata tag "npcgui.%{_uuid}%" of player to writable book named "&6Talk"
    10.         set slot 4 of metadata tag "npcgui.%{_uuid}%" of player to emerald named "&aTrade"
    11.         set slot 7 of metadata tag "npcgui.%{_uuid}%" of player to ender pearl named "&1Quest"
    12.         open (metadata tag "npcgui.%{_uuid}%" of player) to player
    13. on inventory click:
    14.     if event-inventory = (metadata tag "npcgui.63a60701-09cb-4235-a347-42b8ff9344cb" of player):
    15.         cancel event
    16.         if index of event-slot is 1:
    17.             close inventory of player  
    18.             play sound "entity.experience_orb.pickup" with volume 0.5 to the player
    19.             send subtitle "&6Hi there! The name's Bernard." to player
    20.             wait 2 seconds
    21.             play sound "entity.experience_orb.pickup" with volume 0.5 to the player
    22.             send subtitle "&6I found you lying unconscious outside my house." to player
    23.             wait 2 seconds
    24.             play sound "entity.experience_orb.pickup" with volume 0.5 to the player
    25.             send subtitle "&6You had a nasty burn on your shoulder, I fixed you up." to player
    26.             wait 2 seconds
    27.             play sound "entity.experience_orb.pickup" with volume 0.5 to the player
    28.             send subtitle "&6Care to repay me? Water's scarce these days..."
    29.         else if index of event-slot is 4:
    30.             set {_name} to name of targeted entity
    31.             set metadata tag "npctrade.%{_uuid}%" of player to chest inventory with  3 rows named "%{_name}%&e's Trades"
    32.             set slot 0 of metadata tag "npctrade.%{_uuid}%" of player to trident named "&3Pitchfork" with lore "&6ATK: 2"
    33.             open (metadata tag "npctrade.%{_uuid}%" of player) to player
    34.         else if index of event-slot is 7:
    35.             send "&6Quests are still a work in progress!" to player
    36.             close player's inventory
    37. on inventory click:
    38.     if event-inventory = (metadata tag "npctrade.63a60701-09cb-4235-a347-42b8ff9344cb" of player):
    39.         cancel event
    Thanks for any help!
     
  11. Govindas

    Govindas Well-Known Member

    Joined:
    Feb 5, 2017
    Messages:
    82
    Likes Received:
    4
    I se only 1 gui code here, not two?
     
  12. Nayzal

    Nayzal Member

    Joined:
    Jul 5, 2020
    Messages:
    2
    Likes Received:
    0
    No, this code includes two GUIs, the first one has an item which should trigger the second.
     
  13. Runakai

    Runakai Well-Known Member

    Joined:
    Apr 27, 2018
    Messages:
    458
    Likes Received:
    26
    You do not have a target entity in the inventory click event. Your uuid local variable is not set in the inventory click
     
    • Informative Informative x 1
Loading...