Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| library:inventorygui [2020/09/14 19:51] – created phoenix616 | library:inventorygui [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | # InventoryGui | ||
| - | A library that simplifies the creation of chest GUIs for Bukkit/ | ||
| - | allows assigning of the GUI to a specific InventoryHolder. If you are in need of a GUI for text inputs then take a look at [WesJD](https:// | ||
| - | Please note that this is **not a plugin!** | ||
| - | |||
| - | Requires Java 8. | ||
| - | |||
| - | ## Using InventoryGui | ||
| - | Take a look at the examples below to learn how to create a GUI with this library or use the [InventoryGui Javadocs](https:// | ||
| - | |||
| - | ### Defining the GUI setup | ||
| - | Every line in the array define the line in the inventory chest interface. | ||
| - | |||
| - | The characters are getting assigned to certain elements laters, similar to how recipes are defined. | ||
| - | Any empty slot will be filled with a filler character that you can also define yourself. | ||
| - | |||
| - | ```java | ||
| - | String[] guiSetup = { | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | }; | ||
| - | ``` | ||
| - | |||
| - | The GUI supports 3\*3, 5\*1 and 9\*x inventory sizes. | ||
| - | Sizes that do not match these are getting expanded to the next bigger one. | ||
| - | |||
| - | ### Creating the GUI | ||
| - | You create GUIs assigned to an InventoryHolder like a Container or a LivingEntity. | ||
| - | |||
| - | If the holder is `null` then you are not able to retrieve the GUI by the holder via `InventoryGui.get(holder)`. | ||
| - | |||
| - | ```java | ||
| - | InventoryGui gui = new InventoryGui(yourPlugin, | ||
| - | gui.setFiller(new ItemStack(Material.GRAY_STAINED_GLASS, | ||
| - | ``` | ||
| - | |||
| - | ### Adding to the GUI | ||
| - | You can add certain GUI elements to the GUI which will be assigned to a certain character from the setup. | ||
| - | With these elements you define the actions that should happen when the player interacts with the element. | ||
| - | E.g. you can run some code when the player clicks it. Some elements (like the state one) have predefined | ||
| - | actions that happen when they are clicked. (e.g. toggling between the possible states) | ||
| - | |||
| - | #### Static Element | ||
| - | A simple, static element that runs some action when it is clicked. | ||
| - | ```java | ||
| - | gui.addElement(new StaticGuiElement(' | ||
| - | new ItemStack(Material.REDSTONE), | ||
| - | 42, // Display a number as the item count | ||
| - | click -> { | ||
| - | if (click.getEvent().getWhoClicked().getName().equals(" | ||
| - | click.getEvent().getWhoClicked().sendMessage(ChatColor.RED + "I am Redstone!" | ||
| - | return true; // returning true will cancel the click event and stop taking the item | ||
| - | } | ||
| - | return false; // returning false will not cancel the initial click event to the gui | ||
| - | }, | ||
| - | "You can add lines describing this element here!", | ||
| - | "The first line is displayed as the displayname,", | ||
| - | "any additional ones as the lore!", | ||
| - | "Any text the ItemStack had will be overwritten." | ||
| - | )); | ||
| - | ``` | ||
| - | All of these arguments (besides the ItemStack) are optional. | ||
| - | See the docs for more details on the available convenience constructors. | ||
| - | |||
| - | #### Storage Element | ||
| - | An Element that directly accesses the holder inventory. | ||
| - | This can be used to handle placing and retrieving items from parts of a GUI as if that part | ||
| - | was a normal inventory. You can even back the element by a virtual Bukkit inventory instead | ||
| - | of a real one! | ||
| - | |||
| - | If the element is displayed only in one slot it will show the first item in the inventory. | ||
| - | In two slots the first two and so on. | ||
| - | ```java | ||
| - | // With an existing holder (e.g. block or entity) | ||
| - | gui.addElement(new GuiStorageElement(' | ||
| - | |||
| - | // With a virtual inventory to access items later on | ||
| - | Inventory inv = Bukkit.createInventory(null, | ||
| - | gui.addElement(new GuiStorageElement(' | ||
| - | gui.setCloseAction(close -> { | ||
| - | saveInv(inv); | ||
| - | return false; // Don't go back to the previous GUI (true would automatically go back to the previously opened one) | ||
| - | }) | ||
| - | ``` | ||
| - | #### State Element | ||
| - | An element that can have certain states that trigger some code when changed to. | ||
| - | and automatically changes the ItemStack icon. | ||
| - | ```java | ||
| - | gui.addElement(new GuiStateElement(' | ||
| - | new GuiStateElement.State( | ||
| - | change -> { | ||
| - | change.getEvent().getWhoClicked().setFlying(true); | ||
| - | change.getEvent().getWhoClicked().sendMessage(" | ||
| - | }, | ||
| - | new ItemStack(Material.WOOL, | ||
| - | " | ||
| - | ChatColor.GREEN + " | ||
| - | "By clicking here you will start flying" | ||
| - | ), | ||
| - | new GuiStateElement.State( | ||
| - | change -> { | ||
| - | change.getEvent().getWhoClicked().setFlying(false); | ||
| - | change.getEvent().getWhoClicked().sendMessage(" | ||
| - | }, | ||
| - | new ItemStack(Material.WOOL, | ||
| - | " | ||
| - | ChatColor.RED + " | ||
| - | "By clicking here you will stop flying" | ||
| - | ) | ||
| - | )); | ||
| - | ``` | ||
| - | ... you can define as many states as you want, they will cycle through on each click | ||
| - | you can also set the state directly via `GuiStateElement# | ||
| - | |||
| - | #### Dynamic Element | ||
| - | You can also dynamically load elements each time the GUI is re-drawn. E.g. when you want to cache GUIs but not the | ||
| - | text of some buttons or dynamically change them while they are open without closing and reopening them. | ||
| - | |||
| - | Dynamic elements just return one of the other elements that should be displayed each time `InventoryGui# | ||
| - | The slot character for the returned element doesn' | ||
| - | the DynamicGuiElement' | ||
| - | ```java | ||
| - | gui.addElement(new DynamicGuiElement(' | ||
| - | return new StaticGuiElement(' | ||
| - | click -> { | ||
| - | click.getGui().draw(); | ||
| - | return true; | ||
| - | }, | ||
| - | " | ||
| - | })); | ||
| - | ``` | ||
| - | As you can see you can change the content of a DynamicGuiElement after a player click on it by calling `InventoryGui# | ||
| - | |||
| - | #### Element Group | ||
| - | A group can contain multiple different elements and if there are more elements in the group than display slot you can use the GuiPageElement to switch between pages. | ||
| - | |||
| - | ```java | ||
| - | GuiElementGroup group = new GuiElementGroup(' | ||
| - | for (String text : texts) { | ||
| - | // Add an element to the group | ||
| - | // Elements are in the order they got added to the group and don't need to have the same type. | ||
| - | group.addElement((new StaticGuiElement(' | ||
| - | } | ||
| - | gui.addElement(group); | ||
| - | ``` | ||
| - | ##### Pagination | ||
| - | It will automatically detect GuiElementGroup elements with more elements in them than available slots with that character in the GUI and go to the according page on click. (depending on type) | ||
| - | There are also some pagination specific placeholders available for the element descriptions. | ||
| - | |||
| - | ```java | ||
| - | // First page | ||
| - | gui.addElement(new GuiPageElement(' | ||
| - | |||
| - | // Previous page | ||
| - | gui.addElement(new GuiPageElement(' | ||
| - | |||
| - | // Next page | ||
| - | gui.addElement(new GuiPageElement(' | ||
| - | |||
| - | // Last page | ||
| - | gui.addElement(new GuiPageElement(' | ||
| - | ``` | ||
| - | |||
| - | ### Retrieving and showing the GUI | ||
| - | After you have created the GUI you can retrieve it with the original holder and show it to a player. | ||
| - | ```java | ||
| - | InventoryGui gui = InventoryGui.get(InventoryHolder holder); | ||
| - | gui.show(player); | ||
| - | ``` | ||
| - | Obviously you can also show the GUI directly after creating it. | ||
| - | |||
| - | ## Depending on InventoryGui with maven | ||
| - | You can easily depend on the library with maven. | ||
| - | ```xml | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | ``` | ||
| - | ```xml | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | <!--The following version may not be the latest. Check it before using.--> | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | ``` | ||
| - | As this is not a stadalone plugin you have to shade it into your plugin! | ||
| - | E.g. with the maven-shade-plugin [like this](https:// | ||
| - | |||
| - | You can also get development builds directly from the [Minebench Jenkins ci server](https:// | ||
| - | if you want to manually add it to your project but I strongly advise using a dependency management tool like maven or gradle! | ||
| - | |||
| - | ## License | ||
| - | InventoryGui is licensed under the following, MIT license: | ||
| - | |||
| - | ``` | ||
| - | Copyright 2017 Max Lee (https:// | ||
| - | |||
| - | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| - | of this software and associated documentation files (the " | ||
| - | in the Software without restriction, | ||
| - | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| - | copies of the Software, and to permit persons to whom the Software is | ||
| - | furnished to do so, subject to the following conditions: | ||
| - | |||
| - | The above copyright notice and this permission notice shall be included in all | ||
| - | copies or substantial portions of the Software. | ||
| - | |||
| - | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| - | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| - | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| - | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| - | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| - | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| - | SOFTWARE. | ||
| - | ``` | ||