GUI

Discussion in 'Spigot Plugin Development' started by ula492, Jun 27, 2015.

  1. This is my first time working with GUI I did everything correct im pretty sure, the only problem is that when I open the chest and click on the grass block or DiamondChestplate it actually lets me have it and nothing is runned when the item was clicked

    Code (Text):
       
        /**
         * GUI code start
         *
         */
        private void openGUI(Player p){
            Inventory inv = Bukkit.createInventory(null, 9, $SERVERSELECTOR);
           
            ItemStack test1 = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta test1Meta = test1.getItemMeta();
           
            ItemStack test2 = new ItemStack(Material.GRASS);
            ItemMeta test2Meta = test2.getItemMeta();
           
            test1Meta.setDisplayName(ChatColor.RED + "test1");
            test1.setItemMeta(test1Meta);
           
            test2Meta.setDisplayName(ChatColor.BLUE + "test2");
            test2.setItemMeta(test2Meta);
           
            inv.setItem(3, test1);
            inv.setItem(5, test2);
           
            p.openInventory(inv);
           
            // 0123456789
        }
        public void onInventoryClick(InventoryClickEvent event) {
            if(ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("ServerTools"))
                    return;
            Player p =(Player) event.getWhoClicked();
            event.setCancelled(true);
           
            if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
                p.closeInventory();
                return;
            }
           
            switch(event.getCurrentItem().getType()) {
            case DIAMOND_CHESTPLATE:
               
                p.getPlayer().playEffect(p.getLocation(), Effect.FLAME, 5);
                p.getPlayer().playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 5);
                  p.getPlayer().playSound(p.getLocation(), Sound.LEVEL_UP, 1F, 1F);
                  p.sendMessage(ChatColor.RED + "Test2 has been Correct!");
                  p.closeInventory();
                  break;
                 
            case GRASS:
                p.getPlayer().playEffect(p.getLocation(), Effect.FLAME, 5);
                p.getPlayer().playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 5);
                  p.getPlayer().playSound(p.getLocation(), Sound.LEVEL_UP, 1F, 1F);
                  p.sendMessage(ChatColor.RED + "Test2 has been Correct!");
                  p.closeInventory();
                  break;  
                default:
                    p.closeInventory();
                    break;
            }
        }
       
       
        @EventHandler
        public void onPlayerJoin1(PlayerJoinEvent event) {
            event.getPlayer().getInventory().clear();
            event.getPlayer().getInventory().addItem(new ItemStack(Material.CHEST));
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Action a = event.getAction();
            ItemStack is = event.getItem();
           
            if(a == Action.PHYSICAL || is == null || is.getType()==Material.AIR)
                return;
           
            if (is.getType() == Material.CHEST)
                    openGUI(event.getPlayer());
        }
       
       
       
        /**
         * GUI Code end
         *
         */
     
  2. It looks like this should have an exclamation point:
    Code (Text):
            if(ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("ServerTools"))
                    return;
    So, I think it should look like this:
    Code (Text):
            if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("ServerTools"))
                    return;

    EDIT: Also, I'm not really sure how the whole dollar sign thing works, but wouldn't it be better to use like:
    Code (Text):
    if(!event.getInventory().getName().equals($SERVERSELECTOR))
                    return;
     
  3. The DollarSign works like this

    Code (Text):
    public static final String $SERVERSELECTOR = "" + ChatColor.BLUE + "Server Tools";
    Also the thing you told me to do, does not work :/
    Code (Text):
    if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("ServerTools"))
                    return;
     
  4. That's because the title of your inventory has a space, what you are checking for does not.

    Also, I'm just wondering, why did you put a dollar sign in front?
     
  5. a DollarSign is like a variable so lets say ur gonna do
    Code (Text):
    p.sendMessage(ChatColor.RED + "Test");
    To make it easier and not see all that long text u can use
    Code (Text):
    p.sendMessage($TEST)
    But you need to make the Nick Name have something so
    Code (Text):
    public static final String $TEST = "" + ChatColor.BLUE + "TEST";
    it just makes it look good, and it is still not working
     
  6. You must cancel the event in the switch case.
     
  7. I understand what a variable is, I was just wondering about the dollar sign, I thought that wasn't really used in java, so I thought you were getting like a system variable or something.

    Also, what did you try differently? Did you remove/add the space?

    This isn't really the problem. He does cancel the event before the switch case, the plugin's just not reaching that code for some reason.
     
  8. EventHandler?
     
    • Agree Agree x 1
  9. Because he checks if the inventory name is ServerTools and then he returns. The OP must add a - ! - to make sure it reaches the switch case. In the switch you must do getType().toString(); and put your DIAMOND_CHESTPLATE into a string.

    EDIT: Have you registered your events? Put annotion eventhandler above the method, let the class implement listener. register in your onEnable.
     
  10. I'm pretty sure what he did with the switch was fine. I don't think switches need to be strings.

    I was just saying the where he put event.setCancelled(true) was fine, he didn't need to move it to the switch case.
     
  11. when he search for getType() he can't DIAMOND_CHESTPLATE. But searching for a string, he can.

    PS: Didn't saw the e.setCancelled();
     
  12. It looks like you haven't put the @EventHandler on the InventoryClickEvent. Make sure you register yours events in the onEnable() method too :p
     
  13. Events are registered and @EventHandler did nothing, it still dosent work :/
     
  14. Could you show your current code?
     
  15. Same thing as before I haven't changed anything on it
     
  16. Hey Ula lol, Skype me I'll help you fix it
     
  17. I got a dif Skype, it's priv doe soz
     
  18. :(
     
  19. Do u have kik?