Inventory click events and hashmaps for /duel <Player>

Discussion in 'Plugin Development' started by HCMatt, Feb 18, 2015.

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

    HCMatt

    Its a duel plugin that you do /duel <Player> and it sends them an inventory with items to accept or decline the duel, but i want a kit selector inventory but need help with it.
    The HashMaps:

    Code:
    Map<UUID, UUID> duels = new HashMap<UUID, UUID>();
    Map<UUID, UUID> selecting = new HashMap<UUID, UUID>();
    The command:

    Code:
    if (cmd.getName().equalsIgnoreCase("duel")) {
    if (!(args.length == 1)) {
    sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
    return true;
    
    } else if (args.length == 1) {
    Player p = Bukkit.getServer().getPlayer(args[0]);
    if (p != null) {
    if (p.equals(sender)) {
    sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
    return true;
    } else {
    if (duels.containsKey(p) || duels.containsKey(sender)) {
    sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
    return true;
    } else
    
    openKitSelector((Player) sender);
    selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId());
    
    The KitSelector inventory:

    Code:
    public void openKitSelector(Player p) {
    Inventory selector = Bukkit.createInventory(p, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "KitSelector" + ChatColor.DARK_RED + "]");
    
    ItemStack diamond = new ItemStack(Material.DIAMOND_SWORD);
    ItemMeta diamondMeta = diamond.getItemMeta();
    diamondMeta.setDisplayName(ChatColor.DARK_AQUA + "Diamond Kit");
    diamond.setItemMeta(diamondMeta);
    
    ItemStack iron = new ItemStack(Material.IRON_SWORD);
    ItemMeta ironMeta = iron.getItemMeta();
    ironMeta.setDisplayName(ChatColor.DARK_GREEN + "Iron Kit");
    iron.setItemMeta(ironMeta);
    
    selector.setItem(0, diamond);
    selector.setItem(1, iron);
    
    p.openInventory(selector);
    
    }
    
    The Accept/Deny inventory:

    Code:
    private void openGUI(Player player) {
    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]");
    
    ItemStack accept = new ItemStack(Material.EMERALD_BLOCK);
    ItemMeta acceptMeta = accept.getItemMeta();
    
    ItemStack decline = new ItemStack(Material.REDSTONE_BLOCK);
    ItemMeta declineMeta = decline.getItemMeta();
    
    acceptMeta.setDisplayName(ChatColor.GREEN + "Accept!");
    accept.setItemMeta(acceptMeta);
    
    declineMeta.setDisplayName(ChatColor.RED + "Decline!");
    decline.setItemMeta(declineMeta);
    
    inv.setItem(3, accept);
    inv.setItem(5, decline);
    
    player.openInventory(inv);
    }
    
    So what i need to happen is, when someone types /duel <player> , they will get an inventory up with a selection of items that will give them kits which i have done correctly and tested it. Then when they select a kit, the target of the /duel command will get an inventory with an accept item and decline item. I got everything working up to the part were the target gets the inventory for accept or deny as i am not really experienced with HashMaps and i am a little confused. Sorry if its a bit much to ask but i really need this doing for my server.

    Thanks for your time and any help or constructive criticism is appreciated. ~Matty
     
    Last edited: Feb 18, 2015
  2. Offline

    Zombie_Striker

    At this point, you should just have a InventoryClickEvent that checks the Inventory name and then checks the item that was clicked. Right now, you look like you're on the right track.

    One comment I do have is about your openKitSelector method; You should save the kits in a map that way you could always add more kits at any time.
     
  3. Offline

    HCMatt

    Yes but, when i do /duel, and it brings the KitSelector up, and click the kit i want, it gives me an error saying that it cant find the player 'target' theres something messed up with the HashMaps and getting/putting them in and out of them.
     
  4. Offline

    1Rogue

Thread Status:
Not open for further replies.

Share This Page