fbpx
#Gamedev TutorialsProgrammingWhat's up!

Minecraft Plugins Development – How to Create & use GUIs

Tutorial - First steps on Spigot

Share
FavoriteLoadingAdd to favorites

Minecraft Plugins development will give you new powers to create and interact with your favorite game.

In this tutorial, I will be teaching you how to create and use GUIs in Minecraft plugin development or Spigot. Before we start, I want to go over some things.

Let's start with asking ourselves why would we need to know this? Well with the uprising of Minecraft plugins and developers, GUIs have become an essential part of plugins. Almost all premium plugins I have seen contain some sort of GUI. This is because GUIs have the power to make things as simple as possible. With just a few clicks, you can make a player opped or teleport them somewhere. GUIs are for sure the next generation of development for Minecraft lovers.

Now let’s get into what you will need for this, by starting with the IDE. In this tutorial I will be using Intellij, which I highly suggest but other IDEs such as eclipse will work too. The next thing is a bit of Java/Spigot knowledge. There's going to be a lot of functions and you are going to need to know what they do. I will try to explain as much as possible but I can’t cover everything. Anyways… let’s get started!

So we are going to start out by creating our project. I am going to be developing this in 1.8 but it should be similar for all versions.

If you are on Intellij, you want to change the version in the pom.xml and you can simply do that by changing whatever the version says (...in my case 1.12-R0.1-SNAPSHOT) to the version you want.

You only need to change the numbers, so I will change 1.12 to 1.8. On eclipse, you just need to set your buildpath to whatever jar you want to use, which can be downloaded from https://www.getbukkit.org. Alright, now that we got that setup, let’s start creating our main class.

This doesn’t need anything in it for now. Just leave it blank (..besides onEnable and onDisable).

 

For our next step we are going to create a class that will help us out a lot. We are going to name this GuiBuilder and this is just going to make a useful util so we can put everything into methods. I also put this in a new package to make things more organized but you don’t have to do that, if  you want.

 

 

Now we can create our first method! This is what’s going to be creating the GUI or the inventory.

 

 

Let’s go over what this method is doing. We have it set to return an Inventory because we need to get a new Inventory out of this. The parameters are String title and int size. We are going to be using the title as what you see in the top of the GUI for example. You could put ‘Select a Gamemode’ or something like that. Feel free to use ChatColors as that works in this (Ex. ChatColor.RED + “My Title”). For the size,  that is how big the inventory is. This is only in multiples of 9, but at the same time, not really. Java always starts at 0 so the inventory will also start at 0. Refer to this image here.

 

Now it’s weird because when creating the inventory you don’t need it to be like this. It should just be a multiple of nines. For example, as you can see if we wanted 2 rows we would do 18 or if we wanted 5 we would do 45. Also, you can see here this is just a chest, but at the top it says “Large Chest”. That is what your title will look like.

Now, the next thing we are going to learn is how to put items in the inventory. This picture will still come in handy so don’t forget about it.

So, for this, we are just going to look at this method here…

gui.setItem(int index, ItemStack item) [returns void]

This method will put items into our inventory. The int or index is the slot, so if we wanted it in the first slot we would do 0 and so on. For help, you can just look at the chart above. So let’s test this out. The first thing we need to do is create an ItemStack.

Let’s quickly go over the ItemStack. Basically here we have created a diamond item by adding the Material.DIAMOND inside of the ItemStacks parameters. This will return a diamond. So we can now add this to our GUI by selecting a slot and putting the item. For the example I will set it in the 2nd slot or, in this case, 1.

So now, inside the second slot, a diamond will appear. Now, let’s start using this now that we have gone over the 2 things you will see the most.

I have created a method called openInventory and put the parameters as Player p so we can open the inventory for a specific player.

Now let’s create our inventory or GUI using the method we already created. Let's just make it 3 rows.

Now we have created our GUI and we can add to it. Let’s start by adding our first item and call it close. This item will close the GUI. We will put it in the very last slot.

So there we go. We have now created a basic GUI, but you will realize if you click on the item it will not do anything. So let's work on this. Here is a screenshot of the GUI.

Awesome, right? You have now created your first ever GUI. Let’s add some function to this so it will work. We will need to create a listener and I will just call it GUIListener.

Make sure you implement Listener. Let’s get started with the event. So, the event we are going to use is InventoryClickEvent. This event will check what was clicked and things like that. So let’s start out with some basic code. 

So let me explain what each of these things do. The whatWasClicked is the item that was clicked. If we clicked the barrier then the whatWasClicked would return a barrier block. The player is just who clicked so if we want to send them a message when they click something, we can. The clickType is the kind of click that is happening, so are they shift clicking or right clicking. Let’s add a function to our closeItem that will close when you click it and send a message.

This will simply close the inventory and send the message BUT we need to check if this is our GUI or not. To do this we can do a simple check.

This will look if the title equals ours then run the code. We also put e.setCancelled(true); inside the if loop because if we don’t, they can still take the items. So cancelling the event will make it so they can’t take the item anymore.

So there we go! We have made a super simple inventory. Now let’s create an advanced one that can change your gamemode. Let’s start by creating all the items in the GUI. In this part, I won’t explain as much as this is knowledge you should have by now.

For this part, I made a simple util that can create an Item and name it so we don’t have to do this every time.

So here it is! I used the code above with the numbers to lay out my items. This is what it looks like without any function to it.

Minecraft Plugins Development - How to Create & use GUIs - Test GUI
Minecraft Plugins Development - How to Create & use GUIs - Test GUI

There it is! An easy, fresh looking, GUI! Let’s add some function to it and then we should be good. So let’s head back to our listener class.

So here is the first part. I added a check for each item we have. Now say you want 2 of the same item. What you can do is something like this…

If (whatWasClicked.getItemMeta().getDisplayName().equals(“ItemName”) {
// do code

}

So that is how you can have duplicate items. Since ours is all different, we don’t need to do that. We can just check what was clicked. So let’s add the function to change the gamemode each time it’s clicked.

So that code is very copy and paste, but the point is you can add code to each item and make it do anything you want! I added a simple function that checks if you are already in that gamemode. If you are, then it doesn’t change your gamemode and it will send you a message telling you that you’re already in that gamemode.

For a few last things, to open an inventory you will need to do p.openInventory(gui). This will open up the GUI or the inventory. Make sure to replace gui with whatever you named your inventory.

Thanks so much for reading this and I really hope you learned something. If you have any questions, feel free to ask me. I will be pretty active.

 

 

Join us!


How about writing your own piece for IndieWatch?


Micah

Job: Java Developer Hobbies: Baseball, video games, friends and more. United States - CST Time -Message Me for development projects :D

One Comment

  1. Hey, I have a question. I want to make a GUI where there should be 5 items. So it should be a giveaway GUI. One time a moneydrop where you choose an amount and a random player is drawn and gets this amount, One item where a rank chosen by the admin is given to a random player. Then Crates then the same principle you have to choose a crate and it will be given to a random player. And if you do /giveaway and press the rank item you should get a menu where all ranks are listed and that for every item

Leave a Reply

Your email address will not be published.

Back to top button