My own cooldown problems

Discussion in 'Plugin Development' started by Protom1234, Sep 13, 2015.

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

    Protom1234

    Hey there, I have recently made a block listener to a Beacon so when someone clicks any Beacon they'll get 1 god apple. But people have found a bug. When you click the beacon to get the god apple, they get it and then log out and then back in again and they can take it again. Heres my coding, please I really need this fixing.

    Code:
    package feelthepowerpvp.beacon.goldenapple;
    import java.util.ArrayList;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    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.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    public class GoldenAppleClass extends JavaPlugin implements Listener {
        
            private ArrayList<Player> cooldown = new ArrayList<Player>();
            public void onEnable() {
                    getServer().getPluginManager().registerEvents(this, this);
            }
      
            // Note
            //  1 second irl = 20 ticks ingame
              @EventHandler
              public void onBlockClick(PlayerInteractEvent event)
              {
                final Player ply = event.getPlayer();
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (event.getClickedBlock().getType().equals(Material.BEACON))  {
                            if(!cooldown.contains(ply)){
                                    event.setCancelled(true);
                                    ply.playSound(ply.getLocation(), Sound.LEVEL_UP, 1, 0);
                                
                                    ItemStack apple = new ItemStack(Material.GOLDEN_APPLE, 1, (short)1);
                                    ItemMeta gapple = apple.getItemMeta();
                                    gapple.setDisplayName(ChatColor.DARK_PURPLE + "God Apple");
                                    apple.setItemMeta(gapple);
                                    ply.getInventory().addItem(apple);
                                    ply.updateInventory();
                                    ply.sendMessage("§aYou recieved 1 God Apple from the beacon");
                                    event.setCancelled(true);
                                
                                    cooldown.add(ply);
                                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                            public void run(){
                                                    cooldown.remove(ply);
                                            }
                                    }, 72000);
                            }else{
                                    event.setCancelled(true);
                                    ply.sendMessage(ChatColor.RED + "You can take another God Apple in 1 hour.");
                            }
                    }
                }
              }
    }

    Thanks!
     
    Orange Tabby likes this.
  2. Why do you got event.setCancelled(true) 2 times?
     
  3. Offline

    teej107

    @Protom1234 Remove your cooldown solution. You are doing it wrong! Use a Map with the UUID as key and Long as value. When a player tries to get a god apple, compare their last time used (if any) with the system's current time to see if it is X milliseconds past. When they successfully get a god apple, store their UUID and the system's current time in the Map.
     
  4. Offline

    Irantwomiles

    @teej107 I got a question about this too. So if you store them in a map what will happen if they log out? Will it cause any problems?
     
  5. Offline

    teej107

    @Irantwomiles That issues only applies to storing Players in a Map/Collection and forgetting to handle them when they leave. Storing UUIDs won't have that issue and more importantly, it will solve the problem of players logging out and back in and getting another god apple when they shouldn't. They only way to bypass this is to take advantage of the "bug" is when the server restarts or plugin reloads and in that case, I recommend saving the UUIDs to a config if you are worried about that.
     
    Irantwomiles likes this.
  6. Offline

    Irantwomiles

  7. Offline

    Protom1234

    How do I do this? To be honest it took me ages to code the thing I got now.
     
  8. Offline

    mythbusterma

  9. Offline

    SyTeck

    Maybe that he has probably never used this technique before? He should've explained more about the technique and not just how he has to do it.

    For @Protom1234,
    there's a function called System.currentTimeMillis() which gets the current system time in milliseconds, so by storing the system time when he successfully retrieves the apple, you can check the next time he tries to retrieve an apple if the current system time - the system time when he last retrieved an apple is greater than what your cooldown is.
    The storing of values shouldn't be too difficult.
     
    Orange Tabby likes this.
  10. Offline

    mythbusterma

    @SyTeck

    My question wasn't rhetorical. I was asking what wasn't clear, as he didn't say what he didn't get, only that he doesn't follow. Just explaining the same concept in a slightly different way probably isn't what he's looking for.
     
  11. Offline

    teej107

    What questions do you have? Attempt what I just said and I can then show you what you did wrong or right.
     
  12. Offline

    SyTeck

    It looked very rhetorical, and it wasn't very clear at all. It was just a summary of all the things he had to do to achieve what he was trying to do, I tried to explain how it worked.
     
Thread Status:
Not open for further replies.

Share This Page