First Component - ParzivalExe/guiapi GitHub Wiki

Create a component

What is a component?

Before we create our first component have to answer the question what actually is a Component.

A Component is basically one element of the Gui with one specific function. In the case of Minecraft-Guis, a Component is the Item you interact with inside the Inventory/Gui. What happens once you interact with a Component is unique to every Component-Type.

For example, there could be one Component called TeleportComponent, that teleports you to a specific location once you interact with it or one, that gives you a specific Item into your Inventory.

Component Meta

To create a component you need a ComponentMeta. A ComponentMeta is for a component like an ItemMeta for an item. The ComponentMeta has most of the cosmetic information about a Component saved while the component is concerned with the functionality and other variables like the place in which it should be placed inside a Gui.

First create a ComponentMeta with this code: ComponentMeta componentMeta = new ComponentMeta("HELLO WORLD!", new ItemStack(Material.BARRIER));. The first argument of the constructor of the ComponentMeta needs a String. This String is the title of the Component. The second argument of the constructor of ComponentMeta needs an ItemStack. The Component will look like this ItemStack.


The first Component

This API has many components. All components are based on the class Component which is abstract. In this chapter we will create one of the easier Components, the MessageComponent. You can create a MessageComponent with this line: MessageComponent messageComponent = new MessageComponent(componentMeta, "Hello World!");. The first argument in this constructor is the ComponentMeta holding all your cosmetic information for this Component, the second argument the Message this MessageComponent should send.

If you run this command now it would create the same empty Gui as before, because we haven't added the Component to the Gui. You can do this with the method gui.addComponent(messageComponent);.

So, here is the final Code for creating the Gui with it's first Component:

Gui gui = new Gui("first gui");

//New Code
ComponentMeta componentMeta = new ComponentMeta("HELLO WORLD!", new ItemStack(Material.BARRIER));
MessageComponent messageComponent = new MessageComponent(componentMeta, "Hello World!");
gui.addComponent(messageComponent);
//End New Code

gui.openGui(player);

If you run this command now you will see a new Item in the Gui. This is the first component you have programmed with the Gui API. At this moment it looks like a normal Item, but if you try to take this Item in your inventory it will not work, instead you will receive the message you have stored in this specific MessageComponent.

In the next chapters I will explain you what all the other components can do. Here you come to the next chapter.