Solved Needing help with creating a GUI

Discussion in 'Plugin Development' started by xxmobkiller, Nov 6, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    xxmobkiller

    Hi, I am needing help with my plugin, This what I want to do,

    I am trying to create a GUI that is like HYPIXEL Vanity Shop,
    It has a all pets in 1 GUI then at the bottom it says Back and then right next to that it
    says Page 2 then when you click on Page 2 it opens the 2ND GUI but at the bottom it says Page 1 and then right next to that it says Page 3 I am wanting to create something like that but make it like so I can allow it in to my API so others can use to create there own GUI, How would I go about doing this,

    And How would I create a GUI that allowes me to create something like on HYPIXEL GameMenu it has Animation I got Anamtions but I don't know how to make it so it updates the Lore with the next Animation! I know this is a lot to ask but I am still learning about JAVA I am good at coding small plugins but This one is my very first huge plugin that is made for Lobbys or Hubs!

    If you like to help me with this project just PM
     
  2. Offline

    Tecno_Wizard

    Does anyone else feel like they are listening to a child on high amounts of caffiene/sugar when they read that? Lol.
    xxmobkiller, this is a fairly complicated system that is not for the unexperienced with java or the minecraft api. I am actually doing this in a later version of a plugin I am developing right now. In my circumstance, I have to deal with multiple windows based on the amount of entries of a user in the config. It gets complicated fast. Also, there are several libs and resources that do this already (which I do not plan on using, I like the challenge)
     
    leon3001 likes this.
  3. Offline

    Skionz

    Tecno_Wizard Couldn't have said it better myself.
    xxmobkiller There are plenty of tutorials on how to make GUIs and as for pets you will have to look into NMS and packets
     
    Tecno_Wizard likes this.
  4. Offline

    xxmobkiller

    I got some help from a friend for animation Lores But This what I got for more then 1 page GUI

    It works but if more then 1 player opens it and if the other player clicks on something it want give it to that player but he must have to click it 2 times for it to give him the item,


    Code:java
    1.  
    2.  
    3. package com.xxmobkiller.menu;
    4.  
    5. import java.util.ArrayList;
    6. import java.util.Arrays;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10.  
    11. import org.apache.commons.lang.StringUtils;
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.Material;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.inventory.InventoryClickEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20.  
    21. import com.xxmobkiller.main.LobbyEssentials;
    22. import com.xxmobkiller.utilities.ItemMakerUtils;
    23. import com.xxmobkiller.utilities.UtilsMath;
    24.  
    25. public class GadgetGUI implements Listener {
    26.  
    27. public final String title;
    28. public final List<MenuPage> pages;
    29. public final ItemStack[] buttons;
    30. public final MenuAction action;
    31.  
    32. public GadgetGUI(String title, ItemStack[] buttons, MenuAction action) {
    33. this.title = title;
    34. this.buttons = buttons;
    35. this.pages = new ArrayList<MenuPage>();
    36. this.action = action;
    37. int current = 0;
    38. int i = 0;
    39. for (int maxPages = UtilsMath.ceil(buttons.length / 21) + 1; i < maxPages; i++) {
    40. int id = i + 1;
    41. Map<Integer, ItemStack> indexes = getButtonsInRange(current, current + 21);
    42. if ((id > 1) || (id < maxPages)) {
    43. if (id > 1) {
    44. int idd1 = id - 1;
    45. indexes.put(Integer.valueOf(39), new ItemMakerUtils(Material.ARROW).setTitle(StringUtils.join(Arrays.asList(replace("&aPage %page%")), ", ").replace("%page%", "" + idd1)).build());
    46. }
    47. indexes.put(Integer.valueOf(40), new ItemMakerUtils(Material.GLASS).setTitle(StringUtils.join(Arrays.asList(replace("&cReset Gadget")), ", ")).build());
    48. if (id < maxPages) {
    49. int idd2 = id + 1;
    50. indexes.put(Integer.valueOf(41), new ItemMakerUtils(Material.ARROW).setTitle(StringUtils.join(Arrays.asList(replace("&aPage %page%")), ", ").replace("%page%", "" + idd2)).build());
    51. }
    52. }
    53. indexes.put(Integer.valueOf(40), new ItemMakerUtils(Material.GLASS).setTitle(StringUtils.join(Arrays.asList(replace("&cReset Gadget")), ", ")).build());
    54. this.pages.add(new MenuPage(id, title, indexes));
    55. current += 21;
    56. }
    57. Bukkit.getPluginManager().registerEvents(this, LobbyEssentials.getInstants());
    58. }
    59.  
    60. public Map<Integer, ItemStack> getButtonsInRange(int min, int max) {
    61. Map<Integer, ItemStack> indexes = new HashMap<Integer, ItemStack>();
    62. ItemStack[] buttons = (ItemStack[])Arrays.copyOfRange(this.buttons, min, max);
    63. int itemSlot = 10;
    64. int currentLine = 2;
    65. for (ItemStack button : buttons){
    66. if (itemSlot == currentLine * 9 - 1){
    67. itemSlot += 2;
    68. currentLine++;
    69. }
    70. indexes.put(Integer.valueOf(itemSlot), button);
    71. itemSlot++;
    72. }
    73. return indexes;
    74. }
    75.  
    76. @EventHandler
    77. public void onInventoryClick(InventoryClickEvent event) {
    78. Inventory inventory = event.getInventory();
    79. if (inventory.getTitle().equals(title)){
    80. event.setCancelled(true);
    81. if ((event.getSlot() == event.getRawSlot()) && ((event.getWhoClicked() instanceof Player))) {
    82. Player player = (Player)event.getWhoClicked();
    83. for (MenuPage page : this.pages) {
    84. if (page.isSame(inventory)){
    85. this.action.onInventoryClick(player, event.getCurrentItem(), event.getSlot(), this, page);
    86. return;
    87. }
    88. }
    89. }
    90. }
    91. }
    92.  
    93. public void open(Player player, int pageId){
    94. for (MenuPage page : this.pages) {
    95. if (page.id == pageId){
    96. page.open(player);
    97. return;
    98. }
    99. }
    100. }
    101.  
    102. public static abstract interface MenuAction {
    103. public abstract void onInventoryClick(Player paramPlayer, ItemStack paramItemStack, int paramInt, GadgetGUI paramMenu, MenuPage paramMenuPage);
    104. }
    105.  
    106. public static class MenuPage {
    107.  
    108. private final int id;
    109. private final String title;
    110. private final Map<Integer, ItemStack> buttons;
    111. private ItemStack[] contents;
    112.  
    113. public MenuPage(int id, String title, Map<Integer, ItemStack> buttons) {
    114. this.id = id;
    115. this.title = title;
    116. this.buttons = buttons;
    117. }
    118.  
    119. public ItemStack[] getContents() {
    120. return this.contents;
    121. }
    122.  
    123. protected boolean canEqual(Object other) {
    124. return other instanceof MenuPage;
    125. }
    126.  
    127. public int getId() {
    128. return this.id;
    129. }
    130.  
    131. public String getTitle() {
    132. return this.title;
    133. }
    134.  
    135. public Map<Integer, ItemStack> getButtons() {
    136. return this.buttons;
    137. }
    138.  
    139. public String toString() {
    140. return "Menu.MenuPage(id=" + getId() + ", title=" + getTitle() + ", buttons=" + getButtons() + ", contents=" + Arrays.deepToString(getContents()) + ")";
    141. }
    142.  
    143. public void setContents(ItemStack[] contents) {
    144. this.contents = contents;
    145. }
    146.  
    147. public boolean equals(Object o) {
    148. if (o == this) {
    149. return true;
    150. }
    151. if (!(o instanceof MenuPage)) {
    152. return false;
    153. }
    154. MenuPage other = (MenuPage)o;
    155. if (!other.canEqual(this)) {
    156. return false;
    157. }
    158. if (getId() != other.getId()) {
    159. return false;
    160. }
    161. Object thistitle = getTitle();
    162. Object othertitle = other.getTitle();
    163. if (thistitle == null ? othertitle != null : !thistitle.equals(othertitle)) {
    164. return false;
    165. }
    166. Object thisbuttons = getButtons();
    167. Object otherbuttons = other.getButtons();
    168. if (thisbuttons == null ? otherbuttons != null : !thisbuttons.equals(otherbuttons)) {
    169. return false;
    170. }
    171. return Arrays.deepEquals(getContents(), other.getContents());
    172. }
    173.  
    174. public void open(Player player) {
    175. Inventory inventory = Bukkit.createInventory(player, 54, this.title);
    176. for (Map.Entry<Integer, ItemStack> entry : this.buttons.entrySet()) {
    177. inventory.setItem(((Integer)entry.getKey()).intValue(), (ItemStack)entry.getValue());
    178. }
    179. player.openInventory(inventory);
    180. }
    181.  
    182. public boolean isSame(Inventory inventory) {
    183. for (int i = 0; i < 54; i++) {
    184. ItemStack item = inventory.getContents()[i];
    185. if ((item != null) && (!item.isSimilar((ItemStack)this.buttons.get(Integer.valueOf(i))))) {
    186. return false;
    187. }
    188. }
    189. return true;
    190. }
    191. }
    192.  
    193. public static String replace(String msg) {
    194. return msg.replaceAll("(&([a-fk-or0-9]))", "\u00A7$2")
    195. .replace("{0}", "ยป")
    196. .replace("<3", "\u2764")
    197. .replace("[*]", "\u2605")
    198. .replace("[**]", "\u2739")
    199. .replace("[p]", "\u25CF")
    200. .replace("[v]", "\u2714")
    201. .replace("[+]", "\u25C6")
    202. .replace("[++]", "\u2726")
    203. .replace("[/]", "\u2588")
    204. .replace("[cross]", "\u2720")
    205. .replace("[arrow_right]", "\u27A1")
    206. .replace("[arrow_down]", "\u2B07")
    207. .replace("[arrow_left]", "\u2B05")
    208. .replace("[small_line_up]", "\u258F")
    209. .replace("%enabled%", "ENABLED")
    210. .replace("%disabled%", "DISABLED")
    211. ;
    212. }
    213.  
    214. }
    215.  
    216. [I][/I][/i]



    The GadgetsMenuActions is what calls GadgetsGUI and then LobbyEssentials is what calls GadgetsMenuActions

    Code:java
    1.  
    2.  
    3. GadgetGUI gadgets = new GadgetGUI(StringUtils.join(Arrays.asList("Gadgets"), ""), getMenuItems(), new GadgetsMenuAction());
    4.  
    Code:java
    1.  
    2.  
    3.  
    4.  
    5.  
    6.  
    7. [I][/I]


    I use that to call when I want to open a menu.



    Code:java
    1.  
    Code:java
    1.  
    2.  
    3.  
    4. package com.xxmobkiller.menu;
    5.  
    6. import java.util.Arrays;
    7.  
    8. import org.bukkit.Material;
    9. import org.bukkit.Sound;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.meta.ItemMeta;
    13.  
    14. import com.excitedgamerdud.gui.Gadgets;
    15. import com.xxmobkiller.main.LobbyEssentials;
    16. import com.xxmobkiller.utilities.Utils;
    17.  
    18. public class GadgetsMenuAction implements GadgetGUI.MenuAction{
    19.  
    20. public void onInventoryClick(Player player, ItemStack item, int slot, GadgetGUI menu, GadgetGUI.MenuPage page) {
    21. if ((item != null) && (item.getType() != Material.AIR)) {
    22. if (item.getType() == Material.ARROW) {
    23. menu.open(player, slot == 41 ? page.getId() + 1 : page.getId() - 1);
    24. } else {
    25. if (item.getType() == Material.GLASS) {
    26. player.getInventory().setItem(3, null);
    27. player.playSound(player.getLocation(), Sound.NOTE_PLING, 2.0F, 4.0F);
    28. } else {
    29. for (Gadgets g : LobbyEssentials.getInstants().gadgetslots.values()) {
    30. if (g.getItem().equals(item)) {
    31. ItemStack items = g.getItem();
    32. ItemMeta it = items.getItemMeta();
    33. it.setLore(Arrays.asList(Utils.color("&7Right Click to use this gadget!")));
    34. items.setItemMeta(it);
    35. player.getInventory().setItem(3, items);
    36. break;
    37. }
    38. }
    39. }
    40. player.closeInventory();
    41. }
    42. }
    43. }
    44.  
    45. }
    46.  
    47. [I][/I]
     
  5. Offline

    Tecno_Wizard

    xxmobkiller, I have to ask, is English your first language, because I am finding your writing to be very hard to understand.
    Besides that, I am assuming (without looking at the code too much, it's just a lot to look over), that both players are probably viewing the same inventory. Make sure that each player is getting a custom instance of the inventory and the object is not being shared.

    EDIT: okay, they are separate inventories, but otherwise I'm not sure what your problem is. Commenting out things as you write the code is usually the best debugging tool you will ever use, because it allows others to understand what is happening. Try commenting what each line of your code does and you might find the problem.
     
  6. Offline

    xxmobkiller

    I only know how to speak English
    And thank you but this was not coded by me it was coded from a friend of my that quit helping me on this project because it was taking to much time to code!
     
  7. Offline

    Cyber_Pigeon

    You are doing WAYYYY more than you need to, to code a simple pet GUI.

    Start with this :
    Code:
    Inventory //your inventory name// = Bukkit.createInventory(null, 54, "Pet Shop");
    Then when a player right clicks a certain block or something, put this :
    Code:
    p.openInventory(//your inventory name//);
    When you want to add items just do :
    Code:
    ItemStack //name// = new ItemStack(Material.WHATEVER); inv.addItem(//Item name//);
    Always make sure to clear your inventory as soon as the play opens it so it doesn't double all the items :
    Code:
    inv.clear();
    Hope this helped!
     
Thread Status:
Not open for further replies.

Share This Page