Chapter 6: Creating A GUI

GUIs are used a lot in plugins to give them a cleaner look, and make the plugin simpler and easier to use since multiple commands won't have to be memorized. Did you know we can do the same in Skript?

In This Chapter

We're going to be creating GUIs making use of the Skript type (refer to Chapter 5) inventory to open a custom menu to the player when they execute a certain command.

Part 1: Creating The GUI

In this tutorial, we'll be using Vanilla GUIs to create our GUI. You should always attempt to create your GUIs using Vanilla and not using an addon. Since Vanilla GUIs give you a lot of customizability and they are reliable. Let's begin.

First, we're going to create a new local variable named inv, set the value of it to an inventory, and tell Skript how many rows the inventory should contain. You should not give your inventory more than six rows, or else it will start to glitch out, and you may experience errors. We also have to tell Skript the name of the inventory. If you did not understand that, just look at the syntax.

set {_inv} to chest inventory with %number% rows named %text%

For this tutorial, I've just given my inventory 3 rows, and named it "Ted"

set {_inv} to chest inventory with 3 rows named "Ted"

Part 2: Adding Buttons To The GUI

Now, we don't just want an empty inventory to show up. It should have some buttons. So let's add an item to the inventory. We will code it so that the item gives the player a diamond in the third part of this tutorial. So, let's add the item. First, let's decide what the item should be. In my case, I want the item to be a grass block, so that's exactly what I'm going to set it as. Next, you need to decide where in the inventory (on which slot) will the item be displayed? Remember! In Skript, an inventory's slots start from 0, and go up from there. So the first slot of an inventory is actually slot 0 in Skript! In my case, I want the item to display in the first slot of the second row, or the 10th slot of my inventory. Remembering what I just told you, we know that the 10th slot is actually the 9th slot for Skript, so I'll set the slot of my item to 9. And that's it! The syntax for this is

set slot 9 of {_inv} to grass block

Remember! In skript, you can create items with custom names, and lores. To do this, the syntax is %material% [named %text%] [with lore %texts%]. You can add enchantments by setting %material% to something like diamond sword of sharpness or stone sword of sharpness 3, unbreaking 3 and fire aspect.

Finally, open the inventory to the player by using the open inventory effect. Now you are done with the second part of this chapter. Your code will look something like this.

command /diamond:
    trigger:
        set {_inv} to chest inventory with 3 rows named "Ted"
        set slot 9 of {_inv} to grass block
        open {_inv} to player

Part 3: Listening For Click

We've added our button. But players can steal the item if they open the inventory. Plus, the button doesn't actually do anything when clicked. Let's fix this. Now, let's think, what could we do to listen for a click? Let's go through the skript docs and find what we need.

Hmm! That's interesting. The Click Event. Perhaps this is what we need? But if you read the description, and the example, you'll know that this event is not called inside a GUI. Oh there's another one! The Inventory Click Event. And this one IS the one we need. So we're going to use this one.

What I just did above, is called searching the docs. Usually, you should search the docs before asking for help from other people either on the skUnity website, or the discord. Anyways, now that we've found our event, let's use it! For the Inventory Click Event, we'll need to confirm that it's our inventory. To do this, I'll just compare the name of the inventory with "Ted" using an if statement. Now, in the if statement, we're going to cancel the event. What's a cancel the event? It basically means we tell minecraft to not let the event happen. So this means that if a player clicks anywhere in our inventory, our code tells minecraft to not accept their click. So even if they try stealing the grass block, they will be unsuccessful. Pro Tip! The inventory click event is also called when a player uses number keys to steal the item, so that will also be cancelled. It will also be cancelled when the player pressed Q on the item! Back to our code now, after cancelling the event, we'll need to check which slot was clicked. In our case, we want to confirm that the 9th slot of the inventory was clicked. So we'll compare clicked slot with 9 using an if statement. Now finally, in the if statement, we can give the player a diamond. If you didn't understand, here's the code.

on inventory click:
    if name of inventory is "Ted":
        cancel event
        if clicked slot is 9:
            give player a diamond

Conclusion

If you've made it this far, congratulations! You just finished the first section of this tutorial. You can move onto the second section to learn more about the way Skript!

Last updated