Need help with KitGUI I am making

Discussion in 'Plugin Development' started by DAKNIGHT101, Dec 4, 2016.

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

    DAKNIGHT101

    I am making a kit gui and I got everything setup the way I want and everything works fine except one thing.
    I made it so when you do /kit it opens a menu and there are 2 kit categories, Regular and Donor.
    But when you click on either one it just opens the donor menu and I don't know why. It's in a custom plugin I am making so I am not going to show all the code I have but just the code for the commands the open the 2 guis and the click events for them.

    Click Event for when you click on regular category:
    Code:
    package com.DAKNIGHT101.CustomPL.event.inventory;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    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 com.DAKNIGHT101.CustomPL.KrypticCraftPlugin;
    
    public class KitInventoryClick implements Listener {
    
        private KrypticCraftPlugin plugin;
       
        public KitInventoryClick(KrypticCraftPlugin pl) {
            plugin = pl;
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if(!(event.getWhoClicked() instanceof Player))
                return;
           
            String prefix = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Kit Inventory Name"));
           
            Inventory inv = event.getInventory();
            if(!inv.getTitle().equals(prefix))
                return;
           
            Player player = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
            if(item.getType()==Material.STONE_SWORD) {
                event.setCancelled(true);
                player.closeInventory();
            }
            if(item.getType()==Material.STONE_SWORD) {
               
                player.playSound(player.getLocation(), Sound.BLOCK_ENDERCHEST_OPEN, 10, 1);
           
            }
           
                event.setCancelled(true);
                player.closeInventory();
                Bukkit.getServer().dispatchCommand(player, "openregularkits");
        }
    }
    Click event for when you click on donor category:
    Code:
    package com.DAKNIGHT101.CustomPL.event.inventory;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    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 com.DAKNIGHT101.CustomPL.KrypticCraftPlugin;
    
    public class KitInventoryDonorClick implements Listener {
    
        private KrypticCraftPlugin plugin;
       
        public KitInventoryDonorClick(KrypticCraftPlugin pl) {
            plugin = pl;
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if(!(event.getWhoClicked() instanceof Player))
                return;
           
            String prefix = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Kit Inventory Name"));
           
            Inventory inv = event.getInventory();
            if(!inv.getTitle().equals(prefix))
                return;
           
            Player player = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
            if(item.getType()==Material.NETHER_STAR) {
                event.setCancelled(true);
                player.closeInventory();
            }
            if(item.getType()==Material.NETHER_STAR) {
               
                player.playSound(player.getLocation(), Sound.BLOCK_ENDERCHEST_OPEN, 10, 1);
           
            }
           
                event.setCancelled(true);
                player.closeInventory();
                Bukkit.getServer().dispatchCommand(player, "opendonorkitsmenu");
    
        }
    }
    
    Kit Command:
    Code:
    
        private KrypticCraftPlugin plugin;
       
        public Kit(KrypticCraftPlugin pl) {
            plugin = pl;
        }
       
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("kit")){
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command");
                return false;
            }
           
            String invName = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Kit Inventory Name"));
            String reg = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Regular Kits"));
            String donor = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Donor Kits"));
           
            Player player = (Player) sender;
           
            Inventory inv = Bukkit.createInventory(null, 27, invName);
            ItemStack regularKits = nameItem(Material.STONE_SWORD, reg);
            ItemStack donorKits = nameItem(Material.NETHER_STAR, donor);
           
            inv.setItem(12, regularKits);
            inv.setItem(14, donorKits);
           
            player.openInventory(inv);
            player.playSound(player.getLocation(), Sound.BLOCK_ENDERCHEST_OPEN, 10, 1);
           
            }
            return true;
    }
       
    private ItemStack nameItem(ItemStack item, String name) {
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);
        item.setItemMeta(meta);
        return item;
    }
    
    private ItemStack nameItem(Material item, String name) {
        return nameItem(new ItemStack(item), name);
    }
    
    }
    
    Regular Kits Command:
    Code:
    package com.DAKNIGHT101.CustomPL.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.DAKNIGHT101.CustomPL.KrypticCraftPlugin;
    
    public class RegularKitsMenu implements CommandExecutor {
    
    
        private KrypticCraftPlugin plugin;
       
        public RegularKitsMenu(KrypticCraftPlugin pl) {
            plugin = pl;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("openregularkits")){
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command");
                return false;
            }
           
            String invenName = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("RKits Name"));
           
            Player player = (Player) sender;
            Inventory inv = Bukkit.createInventory(null, 27, invenName);
            ItemStack starter = new ItemStack(Material.COAL);
            ItemMeta meta = starter.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_GRAY + "Starter");
            starter.setItemMeta(meta);
           
            ItemStack food = new ItemStack(Material.COOKED_BEEF);
            ItemMeta meta2 = food.getItemMeta();
            meta2.setDisplayName(ChatColor.GOLD + "Food");
            food.setItemMeta(meta2);
           
            inv.setItem(12, starter);
            inv.setItem(14, food);
           
            player.openInventory(inv);
            player.playSound(player.getLocation(), Sound.BLOCK_ENDERCHEST_OPEN, 10, 1);
           
            }
            return true;
        }
    
    }
    
    Donor Kits Command:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.DAKNIGHT101.CustomPL.KrypticCraftPlugin;
    
    public class DonorKitsMenu implements CommandExecutor {
    
    
        private KrypticCraftPlugin plugin;
       
        public DonorKitsMenu(KrypticCraftPlugin pl) {
            plugin = pl;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("opendonorkitsmenu")){
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command");
                return false;
            }
           
            String invName = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("DKits Name"));
           
            Player player = (Player) sender;
            Inventory inv = Bukkit.createInventory(null, 27, invName);
            ItemStack starter = new ItemStack(Material.BARRIER);
            ItemMeta meta = starter.getItemMeta();
            meta.setDisplayName(" ");
            starter.setItemMeta(meta);
           
            ItemStack food = new ItemStack(Material.BARRIER);
            ItemMeta meta2 = food.getItemMeta();
            meta2.setDisplayName(" ");
            food.setItemMeta(meta2);
           
            inv.setItem(12, starter);
            inv.setItem(14, food);
           
            player.openInventory(inv);
            player.playSound(player.getLocation(), Sound.BLOCK_ENDERCHEST_OPEN, 10, 1);
           
            }
            return true;
        }
    }
     
  2. Offline

    Lordloss

    In both your listeners you are checking for the same string in your config. You dont differentiate between them, so im pretty sure that both your listeners are going to run to the end. Both your commands get executed, and which is executed last will dominate.

    I dont really get why do you use two listeners for this, it simply makes no much sense. You are repeating yourself here. Also i dont see a reason why you let the player dispatch a command, where you could simply call a method.

    Why do you have two times the same if statement?
    I can calm you here, after all i saw im sure no one is going to steal your rather bad code ;)
     
  3. Offline

    r0m3x

    Instead of dispatching a command, why not make use the player.openInventory(); method?
     
  4. Offline

    DAKNIGHT101

    I didn't know there was a method. What is it? Also so just put them both in the same class from checking the material and below? And I didn't even realize I had two of the same if statements until you said that

    Replied to the wrong person. How would I use this to in my class? I am not very familiar with it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 22, 2016
Thread Status:
Not open for further replies.

Share This Page