My cooldown is not working

Discussion in 'Plugin Development' started by lukaszte, Dec 18, 2014.

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

    lukaszte

    Hello everyone i'm new at java...
    I'm trying to do a resoup plugin but my cooldown is not working please help me!

    My code:
    Code:
    package me.lukaszte;
    
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ClearOnChange extends JavaPlugin implements Listener{
      
      public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this, this);
      }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    final Player p = (Player) sender;
      {
            if(cmd.getName().equalsIgnoreCase("sopa"));
    
          {
              if (p.hasPermission("zte.sopa"))
              {
                  final List<String> cooldown = new ArrayList<String>();
                  if (cooldown.contains(p))
                  {
                      ItemStack[] inv = p.getInventory().getContents();
                      boolean gotBowl = false;
                      for (ItemStack slot : inv) {
                            if ((slot != null) && (slot.getType() == Material.BOWL))
                            {
                              gotBowl = true;
                              slot.setType(Material.MUSHROOM_SOUP);
                              ItemMeta slotMeta = slot.getItemMeta();
                              slotMeta.setDisplayName("§6[§e§lSopa§6]");
                              slot.setItemMeta(slotMeta);
                            }
                          }
                          if (!gotBowl)
                          {
                            p.sendMessage("§b[§6Resoup§b] " + ChatColor.GRAY + "Resoup ;D ");
                          }
                          else
                          {
                            p.sendMessage("§b[§6Resoup§b] " + ChatColor.RED + "Only for vips");
                          }
    
                
                  }
                           {
                                      Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable()
                                    
                                      {
    
                                          @Override
                                          public void run()
                                          {
                                              cooldown.remove(p);
                                              p.sendMessage(ChatColor.GOLD + "now you can use /sopa again");
    
                                          }
    
                                      }, 200);
                                                        }
                         
                              }
          }
      }
      {
          return false;
    
      }
    
    
      }
    }
    

    my code should remove the empty bowls and replace for soups but, when i use the command "/sopa" all the bowls are replaced but the cooldown don't work, when i use the command appears in the chat "Resoup ;D" and after some minutes appears "now you can use /sopa again" but the command keep working even on cooldown.
     
    Last edited: Dec 18, 2014
  2. Offline

    Rocoty

    @lukaszte "It's not working" is not enough. How is it not working? What does it do that you didn't want it to do? What does it not do that you wanted it to do? Be specific. Also please fix your indentation. Your code is really hard to read.
     
  3. Offline

    ColonelHedgehog

  4. Offline

    Rocoty

    @ColonelHedgehog
    There is no one right way. They both have their benefits and drawbacks. For instance, storing the system time is more efficient (negligible in most cases) as it does not add an extra internal check every tick, but it cannot allow you to make a countdown timer, which the scheduler can.

    Just a small heads up about your tutorial. Don't use System.currentTimeMillis(). If the system time changes you're done for. Using System.nanoTime() is commonly known as the correct way to do this sort of stuff.
     
  5. Offline

    lukaszte

    @Rocoty my code should remove the empty bowls and replace for soups but, when i use the command "/sopa" all the bowls are replaced but the cooldown don't work, when i use the command appears in the chat "Resoup ;D" and after some minutes appears "now you can use /sopa again" but the command keep working even on cooldown.
     
  6. Offline

    ColonelHedgehog

    Ah, that's interesting. Thanks for the tip. I'll make sure to put that in.
     
  7. Offline

    mine-care

    Does that play a role that the arraylist of the cool down is initialized within the onCommand block? Also as said by
    @Rocoty (correctly once more...) the runnable has some benefits :) also please fix your casting to player
     
    Rocoty likes this.
  8. Offline

    ColonelHedgehog

    That is true. It's just in this instance, I'm concerned that the cooldowns could lag the server.
     
  9. Offline

    lukaszte

    someone can help me to fix my code?
     
  10. Offline

    Rocoty

    @lukaszte
     
  11. Offline

    lukaszte

    @Rocoty my code should remove the empty bowls and replace for soups but, when i use the command "/sopa" all the bowls are replaced but the cooldown don't work, when i use the command appears in the chat "Resoup ;D" and after some minutes appears "now you can use /sopa again" but the command keep working even on cooldown.
     
  12. Offline

    ColonelHedgehog

    In your code:

    if (cooldown.contains(p))

    This will always return false since "cooldown" is a String collection, not a Player collection. That's why it's not working.
     
  13. Offline

    Rocoty

    @lukaszte Uhm, also the /sopa command will run no matter which of that plugin's commands is run, due to the semicolon on line 30. Please also repost your code with proper indentation, thanks.

    EDIT:
    This!
    Plus the fact that you declare a new list every time the command runs.
     
    Last edited: Dec 18, 2014
Thread Status:
Not open for further replies.

Share This Page