Error while making GUI

Discussion in 'Spigot Plugin Help' started by Nighterance, Apr 17, 2017.

  1. Hello guys!
    I was upgrading my plugin to support Custom GUI. I have few things that stopped me, so I hope that you may be can help me :).
    I made GUI class, looks like this
    Code (Text):

    package me.Nighterance.main;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class FreezeGUI implements Listener{
       
        public static Inventory frozengui = Bukkit.getServer().createInventory(null, 9, "Choose");

        static {
             createButton(Material.YELLOW_FLOWER, frozengui, 2, "Test1", "lore");
             createButton(Material.TNT, frozengui, 4, "Test1", "lore");
         }
         
         public static void createButton(Material mat, Inventory inv, int Slot, String name, String lore){
             ItemStack item = new ItemStack(mat);
             ItemMeta meta = item.getItemMeta();
             meta.setDisplayName(name);
             meta.setLore(Arrays.asList(lore));
             item.setItemMeta(meta);
             inv.setItem(Slot, item);
         }
         public void openMenu(Player p){
             p.openInventory(frozengui);
         }
         @EventHandler
         public void click(InventoryClickEvent e){
             Player p = (Player) e.getWhoClicked();
             ItemStack clicked = e.getCurrentItem();
             Inventory inv = e.getInventory();
                     
             if(inv.getName().equals(frozengui.getName())){
                 if (clicked.getType() == Material.YELLOW_FLOWER){
                     e.setCancelled(true);
                     p.closeInventory();
                     p.setGameMode(GameMode.CREATIVE);
                     return;
                 }
             }
             if(inv.getName().equals(frozengui.getName())){
                 if (clicked.getType() == Material.TNT){
                     e.setCancelled(true);
                     p.closeInventory();
                     p.setGameMode(GameMode.SURVIVAL);
                     return;
                 }
             }
         }
         
    }
     
    And on main file onEnable() I wrote getServer().getPluginManager().registerEvents(new FreezeGUI(), this);

    So now the GUI should work, but it does not, it brings error on console

    Code (Text):

    Error occurred while enabling Plugin v1.0 (Is it up to date?)
    java.lang.ExceptionInInitializerError
            at me.Nighterance.main.Freeze.onEnable(Freeze.java:43) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.IllegalArgumentException: Title cannot be longer than 32 characters
            at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom$MinecraftInventory.<init>(CraftInventoryCustom.java:59) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom.<init>(CraftInventoryCustom.java:32) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.createInventory(CraftServer.java:1525) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at me.Nighterance.main.GUI.<clinit>(FreezeGUI.java:18) ~[?:?]
            ... 17 more
     
     
  2. What's line 43? (In the Freeze class)
     
  3. this
    getServer().getPluginManager().registerEvents(new FreezeGUI(), this);
     
  4. Try and use this:
    Code (Text):
          PluginManager pm = getServer().getPluginManager();
             
          pm.registerEvents(new FreezeGUI(), this);
     
  5. What's this? :p
    Code (Text):
        static {
             createButton(Material.YELLOW_FLOWER, frozengui, 2, "Test1", "lore");
             createButton(Material.TNT, frozengui, 4, "Test1", "lore");
         }
     
  6. Brings out at me.Nighterance.main.Freeze.onEnable(Freeze.java:46) ~[?:?] if using your way.

    Actually,
    Yea, had no idea how to make custominventory, did it by tutorial, guess it was pritty bad. xD
     
  7. Example to create a inventory:
    Code (Text):
        public static void openMenu(Player player) {
              Inventory inv = player.getServer().createInventory(player, 9, "§lGUI");
           
              ItemStack button = createButton(Material.TNT, "  §2» §aButton §2«", "§7Test Lore");

              inv.setItem(1, button);
                 
              player.openInventory(inv);
    }
     
  8. Check if the clicked item is not null
     
    • Like Like x 1
  9. I don't think the problem is when clicking gui, the problem is when he's opening the gui.
     
  10. It'd be best to create the inventory once rather than creating the inventory each time the method is called
     
  11. Code (Text):
    Caused by: java.lang.IllegalArgumentException: Title cannot be longer than 32 characters
    You use more than 32 characters in your gui
     
    • Like Like x 1
  12. Maybe this?
    Code (Text):
    Caused by: java.lang.IllegalArgumentException: Title cannot be longer than 32 characters
    EDIT
    Oh, I was too slow :p Thanks @Maxime6678
     
    • Agree Agree x 1
  13. Thing is, he didn't you can look at the code, he didn't use more than 32 characters. :eek:
     
  14. I thinks he need to read her error, this is so hard i thinks
     
    • Like Like x 1
  15. Are you running the latest 1.8.8 build? Because the following piece of stacktrace makes no sense when I place it next to the latest version.
    Code (Text):
    Caused by: java.lang.IllegalArgumentException: Title cannot be longer than 32 characters
            at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom$MinecraftInventory.<init>(CraftInventoryCustom.java:59) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    Could have been a bug, could you update and let us know?
     
  16. I started to make it all over again but a bit differently, now it worked.
    Thanks for help!
     
    #16 Nighterance, Apr 17, 2017
    Last edited: Apr 17, 2017
  17. Hey guys, one more thing, I managed to get it fully working except one thing.
    WhenEver I click on AnyItem on users current inventory or blank space on custom Inventory, it brings out error
    Code (Text):

     Could not pass event InventoryClickEvent to N-Freeze v1.2
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
            at me.Nighterance.main.Freeze.onInventoryClick(Freeze.java:194) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    >

     
     
  18. Check if the item is null example:
    Code (Text):
    Item i =  e.getCurrentItem();
    if(i == null) return;
     
  19. Still did not help.

    Code (Text):


      @EventHandler
      public void onInventoryClick(InventoryClickEvent e)
      {
        Player p = (Player) e.getWhoClicked();
        if (this.frozen.contains(p)) {
          e.setCancelled(true);
        }
        if(e.getInventory().getName().equals("Frozen! Choose option")){
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Admit Cheating")){
                  //ban command
               }
               if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Skype")){
                  e.setCancelled(true);
               }
               if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Donot")){
                  e.setCancelled(true);
               
               if(e.getCurrentItem() == null) {    //Tryed both, with and without .getItemMeta().getDisplayName()
                  return;
               }

        }
      } }
     
     
  20. Code (Text):
      @EventHandler
      public void onInventoryClick(InventoryClickEvent e)
      {
        Player p = (Player) e.getWhoClicked();

        if(e.getCurrentItem() == null) return;

        if (this.frozen.contains(p)) {
          e.setCancelled(true);
        }
        if(e.getInventory().getName().equals("Frozen! Choose option")){
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Admit Cheating")){
                  //ban command
               }
               if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Skype")){
                  e.setCancelled(true);
               }
               if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Donot")){
                  e.setCancelled(true);
               }

        }
      } }