Bypassable Cooldowns?

Discussion in 'Plugin Help/Development/Requests' started by MistaKewl, Feb 2, 2015.

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

    MistaKewl

    Hey guys! So, I have a KitPvP plugin with multiple kits and cooldowns which work fine, but there's a huge bug that when players re-log into the server the cooldown resets and they can just get the kit. I've tried fixing it but I have no idea. So I thought that I would come here and ask for some help. I might just be really stupid and be doing something wrong.

    This is one of the kits which use the cooldown. Thanks in advance for any help!

    Code:
    if (cmd.getName().equalsIgnoreCase("Fisherman") && sender instanceof Player) {
            if (cooldown.contains(p)) {
                p.sendMessage(ChatColor.GRAY + "Cooldown " + ChatColor.RED + ">>" + ChatColor.GRAY + " There is a [" + ChatColor.RED + "15m" + ChatColor.GRAY + "] Cooldown on this kit!");
                if(!sender.hasPermission("kitpvp.cooldown.fisherman")) {
                    return true;
           
            }
    
            }
            if(!sender.hasPermission("kitpvp.kit.fisherman")) {
                sender.sendMessage(ChatColor.GRAY + "Permissions " + ChatColor.RED + ">>" + ChatColor.GRAY + " You do not have permission use this kit!");
                return true;
    
            }
    
            p.getInventory().clear();
            ItemStack starterbow = new ItemStack(Material.STONE_SWORD);
            ItemMeta m = starterbow.getItemMeta();
            m.setDisplayName("" + ChatColor.YELLOW + ChatColor.BOLD + "Fisherman Sword");
            starterbow.setItemMeta(m);
            starterbow.addEnchantment(Enchantment.DAMAGE_ALL, 1);
            starterbow.addEnchantment(Enchantment.DURABILITY, 3);
            p.getInventory().addItem(starterbow);
            p.getInventory().addItem(new ItemStack(Material.FISHING_ROD, 1));
            p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
            p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
            p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
            p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
            p.getInventory().addItem(new ItemStack(Material.COOKED_FISH, 16));
            p.setMaxHealth(20);
            p.setHealth(20);
            p.setFoodLevel(20);
            p.setSaturation(40);
            p.sendMessage(ChatColor.GRAY + "Kits " + ChatColor.GOLD + ">>" + ChatColor.GRAY + " You were given the " + ChatColor.YELLOW + "Fisherman" + ChatColor.GRAY + " Kit!");
            cooldown.add(p);
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    cooldown.remove(p);
                }
            }, 18000);
            return true;
           
        }
     
  2. Offline

    crolemol

    @MistaKewl
    the probem is that cooldown is a list of player objects. but when a player relogs it gets a different object so it will not be in the list. to fix it use p.getUniqueID().toString instead and check if the lists contains this string
     
    Last edited: Feb 2, 2015
  3. Offline

    MistaKewl

    Sorry, I'm not the best at Coding. I'm new. Could I get a example using my current code? Thanks. @crolemole
     
  4. Offline

    crolemol

    @MistaKewl
    you spelt my name wrong so I wasn't tagged. you know object orientated programming,so when they preform the command the player is logged in and has a very specific object and it is added to the cooldown list. when the player logs out the garbage collector is coming around and takes the players object with it so it is gone but it is still in your list. When the player relogs it gets a completely new object which is different from the one in the list so the list will not contain it yet. to fix it you pas something to the list which will be always the same like a string or something. here is an example:
    Code:
    if (cmd.getName().equalsIgnoreCase("Fisherman") && sender instanceof Player) {
                // here i changed something
            if (cooldown.contains(p.getUniqueId().toString())) {
                p.sendMessage(ChatColor.GRAY + "Cooldown " + ChatColor.RED + ">>" + ChatColor.GRAY + " There is a [" + ChatColor.RED + "15m" + ChatColor.GRAY + "] Cooldown on this kit!");
                if(!sender.hasPermission("kitpvp.cooldown.fisherman")) {
                    return true;
         
            }
            }
            if(!sender.hasPermission("kitpvp.kit.fisherman")) {
                sender.sendMessage(ChatColor.GRAY + "Permissions " + ChatColor.RED + ">>" + ChatColor.GRAY + " You do not have permission use this kit!");
                return true;
            }
            p.getInventory().clear();
            ItemStack starterbow = new ItemStack(Material.STONE_SWORD);
            ItemMeta m = starterbow.getItemMeta();
            m.setDisplayName("" + ChatColor.YELLOW + ChatColor.BOLD + "Fisherman Sword");
            starterbow.setItemMeta(m);
            starterbow.addEnchantment(Enchantment.DAMAGE_ALL, 1);
            starterbow.addEnchantment(Enchantment.DURABILITY, 3);
            p.getInventory().addItem(starterbow);
            p.getInventory().addItem(new ItemStack(Material.FISHING_ROD, 1));
            p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
            p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
            p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
            p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
            p.getInventory().addItem(new ItemStack(Material.COOKED_FISH, 16));
            p.setMaxHealth(20);
            p.setHealth(20);
            p.setFoodLevel(20);
            p.setSaturation(40);
            p.sendMessage(ChatColor.GRAY + "Kits " + ChatColor.GOLD + ">>" + ChatColor.GRAY + " You were given the " + ChatColor.YELLOW + "Fisherman" + ChatColor.GRAY + " Kit!");
               // here i changed something
            cooldown.add(p.getUniqueId().toString());
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    cooldown.remove(p);
                }
            }, 18000);
            return true;
         
        }
    this will work because the uniqueid will always be the same and the list will contain your string
     
  5. Offline

    MistaKewl

    Sorry for spelling your name wrong. Thanks I'll try it tomorrow. I don't know what I'd do with out you ! Thanks!
     
  6. Offline

    crolemol

  7. Offline

    1Rogue

    It'd be a lot easier to just store the UUID directly, rather than converting it to and from a string.
     
  8. Offline

    MistaKewl

    Do I also do cooldown.remove(p.getUniqueID.toString))

    A little error under
    cooldown.add(p.getUniqueId().toString()) it says to change to addAll(...) but comes with the same error. To change back to .add(...)
    Code:
    p.sendMessage(ChatColor.GRAY + "Kits " + ChatColor.GOLD + ">>" + ChatColor.GRAY + " You were given the " + ChatColor.DARK_GRAY + "Ghost" + ChatColor.GRAY + " Kit!");
            cooldown.add(p.getUniqueId().toString());
     
    Last edited by a moderator: Feb 3, 2015
  9. Offline

    crolemol

    @MistaKewl
    that's because your list is of the type player object. look for your list and change <Player> to <String> and indeed
    in your runnable instead of cooldown.remove(p)

    @1Rogue
    I did this before but i mentioned it doesn't work because it returns random uuid object with the string value so the list does not contain it
     
    Last edited by a moderator: Feb 3, 2015
  10. Offline

    MistaKewl

    I changed ArrayList<Player> cooldown = new ArrayList<Player>();
    to
    ArrayList<String> cooldown = new ArrayList<String>();

    there are no errors in the code but when I go and choose a kit in-game it doesn't work. I get a internal error.
    This is some of the code:
    Code:
    ArrayList<String> cooldown = new ArrayList<String>();
    
    
    and
    
    
    if (cmd.getName().equalsIgnoreCase("Fisherman") && sender instanceof Player) {
            // here i changed something
        if (cooldown.contains(p.getUniqueId().toString())) {
            p.sendMessage(ChatColor.GRAY + "Cooldown " + ChatColor.RED + ">>" + ChatColor.GRAY + " There is a [" + ChatColor.RED + "15m" + ChatColor.GRAY + "] Cooldown on this kit!");
            if(!sender.hasPermission("kitpvp.cooldown.fisherman")) {
                return true;
        }
        }
        if(!sender.hasPermission("kitpvp.kit.fisherman")) {
            sender.sendMessage(ChatColor.GRAY + "Permissions " + ChatColor.RED + ">>" + ChatColor.GRAY + " You do not have permission use this kit!");
            return true;
        }
        Player z = p; p.getUniqueId().toString();
        p.getInventory().clear();
        ItemStack starterbow = new ItemStack(Material.STONE_SWORD);
        ItemMeta m = starterbow.getItemMeta();
        m.setDisplayName("" + ChatColor.YELLOW + ChatColor.BOLD + "Fisherman Sword");
        starterbow.setItemMeta(m);
        starterbow.addEnchantment(Enchantment.DAMAGE_ALL, 1);
        starterbow.addEnchantment(Enchantment.DURABILITY, 3);
        p.getInventory().addItem(starterbow);
        p.getInventory().addItem(new ItemStack(Material.FISHING_ROD, 1));
        p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
        p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
        p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
        p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
        p.getInventory().addItem(new ItemStack(Material.COOKED_FISH, 16));
        p.setMaxHealth(20);
        p.setHealth(20);
        p.setFoodLevel(20);
        p.setSaturation(40);
        p.sendMessage(ChatColor.GRAY + "Kits " + ChatColor.GOLD + ">>" + ChatColor.GRAY + " You were given the " + ChatColor.YELLOW + "Fisherman" + ChatColor.GRAY + " Kit!");
           // here i changed something
        cooldown.add(p.getUniqueId().toString());
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            public void run() {
                cooldown.remove(p);
            }
        }, 18000);
        return true;
     
  11. Offline

    1Rogue

    wat?

    UUIDs are correctly implemented for hashcode and will work fine in any sort of collection.
     
  12. Offline

    MistaKewl

    Is it because I forgot to cooldown.remove(p.getUniqueId().toString)) ? I can't edit the code rn, I'm off my PC. Or is there something else wrong?
     
  13. Offline

    crolemol

    @1Rogue
    I should be wrong ;P

    @MistaKewl
    please poste the error and yes you should change line 43 to cooldown.remove(p.getUniqueid().toString())
     
  14. Offline

    MistaKewl

    13 more
    03.02 20:53:11 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_72]
    03.02 20:53:11 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_72]
    03.02 20:53:11 [Server] INFO at sun.reflect.GeneratedMethodAccessor212.invoke(Unknown Source) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.handler.menu.InvMenu.onInventoryClick(InvMenu.java:110) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.handler.InventoryGUI$1.onOptionClick(InventoryGUI.java:91) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.util.Util.run(Util.java:39) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.util.Util.performAction(Util.java:99) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer.performCommand(CraftPlayer.java:282) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at me.mistakewl.main.Main.onCommand(Main.java:358) ~[?:?]
    03.02 20:53:11 [Server] INFO The method add(String) in the type ArrayList&lt;String&gt; is not applicable for the arguments (Player)
    03.02 20:53:11 [Server] INFO The method add(String) in the type ArrayList&lt;String&gt; is not applicable for the arguments (Player)
    03.02 20:53:11 [Server] INFO The method add(String) in the type ArrayList&lt;String&gt; is not applicable for the arguments (Player)
    03.02 20:53:11 [Server] INFO The method add(String) in the type ArrayList&lt;String&gt; is not applicable for the arguments (Player)
    03.02 20:53:11 [Server] INFO Caused by: java.lang.Error: Unresolved compilation problems:
    03.02 20:53:11 [Server] INFO ... 13 more
    03.02 20:53:11 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_72]
    03.02 20:53:11 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_72]
    03.02 20:53:11 [Server] INFO at sun.reflect.GeneratedMethodAccessor212.invoke(Unknown Source) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.handler.menu.InvMenu.onInventoryClick(InvMenu.java:110) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.handler.InventoryGUI$1.onOptionClick(InventoryGUI.java:91) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.util.Util.run(Util.java:39) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.goblom.gui.plugin.util.Util.performAction(Util.java:99) ~[?:?]
    03.02 20:53:11 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer.performCommand(CraftPlayer.java:282) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    03.02 20:53:11 [Server] INFO Caused by: org.bukkit.command.CommandException: Unhandled exception executing command 'starter' in plugin KitPvP v2.1
     
  15. Moved to Bukkit Alternates.
     
  16. Offline

    crolemol

    @MistaKewl
    please tag me by clicking the tahg me button. Your error is very weird and has something to do with bad compiling so stop your server remove your jar file, recompile your plugin and put it back on the server should work. also change cooldowns.remove(p)
     
    Last edited: Feb 4, 2015
  17. Offline

    MistaKewl

Thread Status:
Not open for further replies.

Share This Page