Problem with cooldowns

Discussion in 'Plugin Development' started by Juancomaster1998, Jan 10, 2015.

Thread Status:
Not open for further replies.
  1. Hello!
    I`m trying to make it so when a player uses a kit, it`s added to a cooldown so the player must wait to use it again.
    I can do that for a limited amount of kits, but I don`t know how many kits there`re going to be, so I need to make it for unlimited kits. (and for unlimited players too).
    I`ve done it this way but the problem is that Player and s (kit`s name) must be final for schedule, so that will work only for 1 player and 1 kit.
    Code (open)

    Code:
                    final Player p = (Player) e.getWhoClicked();
                    if (e.getInventory().getName().equals(k.getDName())) {
                        for (final String s : k.d) {
                            ck.load(s);
                            cp.load(p.getName());
                            try {
                                if (cp.get().getConfigurationSection("Temp.Kits").getKeys(false).contains(s)) {
                                    p.sendMessage(ChatColor.RED + "You must wait to use that kit again!");
                                    e.setCancelled(true);
                                    p.closeInventory();
                                    return;
                                }
                            } catch(NullPointerException ee) { }
                            ItemStack i = new ItemStack(Material.getMaterial(ck.get().getString("ID")), ck.get().getInt("Amount"), (short) ck.get().getInt("Meta"));
                            if (ck.get().getString("Enchanted").equalsIgnoreCase("yes")) {
                                i = addGlow(i);
                            }
                            ItemMeta im = i.getItemMeta();
                            String iname = ck.get().getString("Name");
                            iname = iname.replace("&", "ยง");
                            im.setDisplayName(iname);
                            i.setItemMeta(im);
                            if (e.getCurrentItem().equals(i)) {
                                try {
                                    for (String ss : ck.get().getConfigurationSection("Inv.Items").getKeys(false)) {
                                        ItemStack ii = ck.get().getItemStack("Inv.Items." + ss);
                                        p.getInventory().addItem(ii);
                                    }
                                    if (ck.get().getString("Inv.Helmet") != null) p.getInventory().setHelmet(ck.get().getItemStack("Inv.Helmet"));
                                    if (ck.get().getString("Inv.Chestplate") != null) p.getInventory().setChestplate(ck.get().getItemStack("Inv.Chestplate"));
                                    if (ck.get().getString("Inv.Leggings") != null) p.getInventory().setLeggings(ck.get().getItemStack("Inv.Leggings"));
                                    if (ck.get().getString("Inv.Boots") != null) p.getInventory().setBoots(ck.get().getItemStack("Inv.Boots"));
                                    cp.get().createSection("Temp.Kits." + s);
                                    cp.save();
                                    Bukkit.getScheduler().scheduleSyncDelayedTask(CrooksKitPvp.plugin, new Runnable() {
                                        public void run() {
                                            cp.load(p.getName());
                                            cp.get().set("Temp.Kits." + s, null);
                                            cp.save();
                                        }
                                    }, ck.get().getInt("Cooldown")*20);
                                } catch(NullPointerException ee) {
                                    p.sendMessage(ChatColor.RED + "That kit doesn`t have any inventory!");
                                }
                                e.setCancelled(true);
                                p.closeInventory();
                            }
                        }
                    }


    Thanks! ^^
     
  2. Offline

    teej107

    @Juancomaster1998 Why
    1. Are you catching a NullPointerException? Don't do that.
    2. Are you using a scheduler for cooldowns? Using Maps are much better. Put the Player (as key) and the System time (as value) in the Map and compare the current system time with the player's long in the Map.
     
  3. @teej107
    It`s possible to put 3 values on a map or something like that? It will be so much easier for me to make it on a runnable, but I need 3 values for make that.

    The NullPointerException is there purpousely. It`s on my whole code and it always worked for me.
     
  4. Offline

    teej107

    Wat.... Just replace the try catch with a null check. Your code will be more efficient. Take this for example:
    Code:
    try
    {
        //Simulating work being done.
        Thread.sleep(2000);
        throw new NullPointerException();
    }
    catch(Exception e)
    {
        //Do code
    }
    You just wasted 2 seconds.
    What are the 3 values that are needed? It might be best to create a class to hold the information.
     
  5. I have no knowledges on throws... I still have to learn them.


    Well, I have to save the player, the used kit and the time.
    The time will be decreasing by 1 every second so it will be laggy make it thru configs...
    I tried using maps, but it isn`t compatible for multiple kits, for example, imagine I use kit "1", so the hashmap is saved with the key player and value kit. When I use kit "2", kit "1" is replaced by kit "2" on hashmap, so that doesnt work for this.
     
Thread Status:
Not open for further replies.

Share This Page