GetItemComponent - ParzivalExe/guiapi GitHub Wiki

From Overview

As most components GetItemComponent also gives away it's function in it's name. With this Component you can give the player who clicked one or more items into his Inventory. You can specify where exactly this item should be located in his Inventory and if these items should also be added and override if there is already another item present in this slot already.

Implementation

Like the TeleportComponent, the GetItemComponent is pretty simple to implement as a component itself as well, but you do need to understand how to use it's DataObject, in this case the InvItemStack. But first, the general code for this Component:

ComponentMeta getItemMeta = new ComponentMeta("GetEquipment", new ItemStack(Material.STORAGE_MINECART));
//manipulate getItemMeta here

ArrayList<InvItemStack> items = new ArrayList<InvItemStack>();
//add InvItemStacks to ArrayList
items.add(new InvItemStack(Material.DIAMOND_BOOTS, 1, (short) 400, (byte) 0, InvItemStack.POSITION_BOOTS));
items.add(new InvItemStack(Material.DIAMOND_LEGGINGS, 1, (short) 450, (byte) 0, InvItemStack.POSITION_LEGGINGS));
items.add(new InvItemStack(Material.DIAMOND_CHESTPLATE, 1, (short) 500, (byte) 0, InvItemStack.POSITION_CHESTPLATE));
items.add(new InvItemStack(Material.DIAMOND_HELMET, 1, (short) 300, (byte) 0, InvItemStack.POSITION_HELMET));
items.add(new InvItemStack(Material.DIAMOND_SWORD, 1, (short) 1500, (byte) 0, 0));
items.add(new InvItemStack(Material.GOLDEN_APPLE, InvItemStack.POSITION_UP_OFFSET + 13));

GetItemComponent getItemComponent = new GetItemComponent(getItemMeta, items);
//manipulate getItemComponent

gui.addComponent(getItemComponent);

Now lets focus our attention on how to use the InvItemStack. As you can see there are different Constructors available. The easiest one would probably be a simple public InvItemStack(ItemStack itemStack, int invPosition) {...} but as all the more simple once are pretty self-explanatory once you understand the most complex one, I will only focus on it.

So, let's take a closer look at InvItemStack. You can see, that the first argument let's you give the material (or type) of the Item. After that we have the amount of this Item, then the durability (the higher this number, the more damage this item has already gotten although every item has it's own maximum durability value) and then the data (for Items like colored Wool important). So far so similar to a normal ItemStack from Bukkit, but now we come to the special part: The Position inside our Inventory:

Here, generally, you have to give an int for the Position inside a players inventory. 0 for example would land in the low-inventory (the one you see all the time) in the first position. Once we go though all 9 of these position we start at the top of the upper inventory (the one you have to press E to see). To make it a bit easier to read you can also simply use the value InvItemStack.POSITION_UP_OFFSET plus the position starting in the left top where you want the item to be. In your example the InvItemStack.POSITION_UP_OFFSET + 13 would translate to a final int-value of 22.

You could however also simply use InvItemStack.NO_POSITION which then would give the Item into the lowest Inventory-Slot available, even taken into account that it could Stack the Items on already existing Stacks.

And finally there are the slots for Armor. Here we can simply use InvItemStack.POSITION_BOOTS | POSITION_LEGGINGS ... as you can see demonstrated in your example.


In addition we also have two fields inside the GetItemComponent itself we need to explain quickly.

First, there is the boolean overrideInInv, which determines weather an item can still be placed into it's intended position, even if there is already an item at that same position (in which case it would destroy this item and take it's place). If this is false, meaning that nothing can be overwritten, the Moment this doesn't work, it will try to find any other position in the Inventory for this Item and if that is unsuccessful, it will simply drop the Item at the same location the player is standing. The default for this field is overrideInInv = false, meaning, that items can't be overwritten.

The second field is (like for many Components) boolean closeGui, which determines, weather the Gui should be closed once the player has interacted once with the Item. The default for this field is closeGui = true.


That was a longer explanation, hope it still was at least somewhat understandable. If you are still a bit confused, maybe it is a good idea to simply play around a bit with this component and look at the results.

Events

ComponentClickedEvent = fired, when the GetItemComponent (or any other Component) is clicked

GetItemComponentClickedEvent = fired, when the GetItemComponent is clicked

XML-Attributes

title="{Component-Title}" = sets the Title of this Component (example: title="Hello World")

look="{AMOUNT}xID:{DATA}{[DURABILITY]}" = sets the Look of this Component (example: look="3x322:1" or look="3xGOLDEN_APPLE")

description="[{line1}, {line2}...]" = sets the description of this Component (example: description="[This is a Description, with a \,]")

place="{PLACE}" = sets the place this Component should be placed at in the Gui (example: place="4")

items="[{pos0=amount0xtype0:data0[durability0]}, {pos1=amount1xtype1:data1[durability1]}, ...]" = again, a bit more complicated. First of all, because this is an ArrayList you ALWAYS have to use brackets ("[...]") at the beginning and end of the attribute-value. The different items are divided through a comma (, ). Now we come to the most difficult part: the InvItemStack itself. First you give the Position where this Item should be (for example 0 for first position in the lower inventory). The next value defines the amount. After that you give the type of the Item (for example 276 for a DIAMOND_SWORD). After that you can give the data which is important for item-variants, like a colored wool-block. The last value is in brackets and defines the durability which defines how much the item is damaged (the higher the more damage BUT every item has it's own max durability value). For the position you can also write HELMET, CHESTPLATE, LEGGINGS and BOOTS instead of the normal int or you can write LOWx, with x replaced by the position in the lower inventory (a value between 0 and 8) or UPx with x replaced by the position in the upper inventory (a value between 9 and 35). You can also entirely remove the = and all the information about the InvPosition in which case it would act the exact same as InvItemStack.NO_POSITION would in code (see in Implementation). Lastly, like with a normal ItemStack in .mgui XML you can also leave out amount, data and/or durability together with their identifiers (amount = x | data = : | durability = [...]) if you want to use their default-values (amount = 1, data = 0, durability = 0)

Here is the xml-code for the example in implementation: items="[BOOTS=315[400], LEGGINGS=312[450], CHESTPLATE=311[500], HELMET=310[300], LOW0=276[1500], UP13=322]"


overrideInInv="true|false" = determines weather Items at the same position should be overwritten (example: overrideInInv="false")

closeGui="true|false" = determines weather the Gui should be closed when the player has interacted with the component once (example: closeGui="false")

Resources

Component-Code: io.github.parzivalExe.guiApi.components.GetItemComponent
InvItemStack-Code: io.github.parzivalExe.guiApi.objects.InvItemStack


Great, now you know how to implement a GetItemComponent with a few (more) lines. Next Component on the List is the Folder