-
Notifications
You must be signed in to change notification settings - Fork 0
ChestGUI
The ChestGUI is an easy to use inventory-creating chest api GUI. We have made this api as we found it quite annoying to spam our code with useless Inventory creations in the main classes and such.
Make sure you import GUICallback before importing ChestGUI as bugs might occur if you don't do so. After you have done the following you can proceed into creating a GUI:
new ChestGUI(player, 27, "Player Options | Management", false, new GUICallback() {In this method:
- player: Resembles the Bukkit player whom we'll open the GUI to.
- 27: Resembles an int, the amount of chest slots available. Make sure that the int is valid; or else the GUI will not open. The integers available are: [9,18,27,36,45,54].
- "Player Options | Management" is the name of the GUI. It can be colored using ColoredUtils.colorizeMessage("&5Player Options | Management");
- The boolean (false/true) can be used to detect if it's an open-gui or closed one. If it's true people can get the items set in the GUI and put them in the hotbar (used for trash chests etc..), if it's false it won't allow them to pick them up from the gui.
- The new GUICallback() method will initiate a new GUICallback instance.
new ChestGUI(player, 27, "Player Options | Management", false, new GUICallback() {
@Override
public void callback(ChestGUI gui, CallbackType callback, ItemStack item) {}
@Override
public void onSecond(ChestGUI gui) {}
});After you have initiated the instances, go ahead and have these methods ready.
- gui is an instance of the ChestGUI, used to add items inside the chest.
- callback is an instance of CallbackType; check below for documentation.
- item is the item clicked or used in different CallbackTypes.
This method will launch after a second of opening a GUI, you can have animated gui options in there and such:
- gui is an instance of the ChestGUI.
new ChestGUI(player, 27, "Player Options | Management", false, new GUICallback() {
@Override
public void callback(ChestGUI gui, CallbackType callback, ItemStack item) {
if(callback == CallbackType.INIT) {
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.LAPIS_BLOCK, 1, 0, "&3&lServer TPS &7| &bLoading..."));
}
if(callback == CallbackType.CLICK) {
String d = ChatColor.stripColor(item.getItemMeta().getDisplayName());
if(d.equalsIgnoreCase("Server TPS | Loading...") {
p.sendMessage("Clicked server TPS!");
p.closeInventory();
}
}
@Override
public void onSecond(ChestGUI gui) {}
});Let's start off with the callback types. First, we're gonna check if the callback is in INIT state, meaning the player is just Idling in the gui, or the gui has just opened. If the callback does equal that, we're gonna add items using:
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.LAPIS_BLOCK, 1, 0, "&3&lServer TPS &7| &bLoading..."));What we're doing here is setting the 12th slot of the GUI an ItemStack created using the OctoCore.getItemCreator() to create an item which has:
- Material as LAPIS_BLOCK, get a list of materials here.
- 1 as in the amount of Material to give. If set to 2, there will be a little (2) sign next to the Material in the GUI.
- 0 as in the data ID, can be used to give red wool using:
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.WOOL, 1, 14, "&4&lRed Wool"));- "&3&lServer TPS &7| &bLoading..." is the name of the item. It is AUTOMATICALLY colored.
- OPTIONAL: you can have a lore for the item, you can use Arrays.asList("lore1", "lore2"); as follows:
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.WOOL, 1, 14, "&4&lRed Wool",
Arrays.asList("&7Line 1", "&4Line 2"));To know where to set the item slot, we've made a detailed image with the number of every slot (first int, here: 12)
new ChestGUI(player, 27, "Player Options | Management", false, new GUICallback() {
@Override
public void callback(ChestGUI gui, CallbackType callback, ItemStack item) {
if(callback == CallbackType.INIT) {
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.LAPIS_BLOCK, 1, 0, "&3&lServer TPS &7| &bLoading..."));
}
if(callback == CallbackType.CLICK) {
String d = ChatColor.stripColor(item.getItemMeta().getDisplayName());
if(d.equalsIgnoreCase("Server TPS | Loading...") {
p.sendMessage("Clicked server TPS!");
p.closeInventory();
}
}
@Override
public void onSecond(ChestGUI gui) {}
});Next in this code we're gonna mess with the CallbackType.CLICK enum, this is invoked after a player clicks an item. What we're gonna do is strip the colors off of the clicked item, so if the item equals a string that is set as an item's name in the INIT method WITHOUT color codes, we're gonna send him a message and close the inventory.
###onSecond() method documentation: If you're wondering what we can do in the onSecond() method, here's the example:
new ChestGUI(player, 27, "Player Options | Management", false, new GUICallback() {
@Override
public void callback(ChestGUI gui, CallbackType callback, ItemStack item) {
if(callback == CallbackType.INIT) {
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.LAPIS_BLOCK, 1, 0, "&3&lServer TPS &7| &bLoading..."));
}
if(callback == CallbackType.CLICK) {
String d = ChatColor.stripColor(item.getItemMeta().getDisplayName());
if(d.equalsIgnoreCase("Server TPS | Loading...") {
p.sendMessage("Clicked server TPS!");
p.closeInventory();
}
}
@Override
public void onSecond(ChestGUI gui) {
gui.i.setItem(12, OctoCore.getItemCreator().createItem(Material.LAPIS_BLOCK, 1, 0, "&3&lServer TPS &7| 20"));
}
});What we did here is after 1 second of opening the GUI, we replaced the normal item located in the callback method with another item after one second. This method can be used if you're loading player files and need time to load, or just don't want to consume a lot of RAM by requesting it all at the same time.
If you have any questions regarding the AnvilGUI api, please PM ElieTGM/JosephGP over at Skype or ask in the #development section of the staff chat.
