Development Assistance Cooldown Help

Discussion in 'Plugin Help/Development/Requests' started by minecrft, Apr 16, 2016.

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

    minecrft

    Hello!

    I have been working on a lobby gadget plugin for my minigame server, and I've run into a bit of an issue. I'm trying to create a "thor" gadget just as a test of my skills. And as you can tell of me being here, it's not going so well. I'm trying to create a cooldown on the "thor" gadget that strikes down lightning and when you try and use it while the cooldown is still active it tells you how much time is left! I have done an attempt at it but it seems to not work. haha. If you need anymore details about it just ask!

    Main -
    Code:
    package me.bukkit.minecrft;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ThorGadget extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getCommand("hammer").setExecutor(new CommandThor());
            getServer().getPluginManager().registerEvents(new Lightning(this), this);
        }
    
        @Override
        public void onDisable() {
    
        }
    }
    Commands -
    Code:
    package me.bukkit.minecrft;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class CommandThor implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player){
                if(label.equalsIgnoreCase("hammer")){
                    Player p = (Player) sender;
                    ItemStack i = new ItemStack(Material.GOLD_AXE);
                    ItemMeta meta  = i.getItemMeta();
                    List<String> lore = new ArrayList<String>();
                    lore.add(" ");
                    lore.add(ChatColor.BLUE + "Become thor with");
                    lore.add(ChatColor.BLUE + "this awesome gadget!");
                    meta.setLore(lore);
                    meta.setDisplayName(ChatColor.RED + ChatColor.BOLD.toString() + "Thor's Hammer!");
                    i.setItemMeta(meta);
                    p.getInventory().addItem(i);
                    return true;
                }
            }
            return true;
        }
    
    }
    
    Listener -
    Code:
    package me.bukkit.minecrft;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Lightning extends JavaPlugin implements Listener {
        public Lightning(ThorGadget thorGadget) {
        }
    
        private List<Player> cd = new ArrayList<Player>();
    
        @EventHandler
        public void onUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getItem().getItemMeta().getDisplayName().toLowerCase().contains("thor's hammer")) {
                if (!cd.contains(p)) {
                    p.getWorld().strikeLightningEffect(p.getTargetBlock((Set<Material>) null, 25).getLocation());
                    cd.add(p);
    
                }
    
            }
    
        }
    
        public void cooldownheal(final Player player) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    cd.remove(player.getName());
                    player.sendMessage("" + ChatColor.DARK_AQUA + ChatColor.BOLD + "Your healing ability has recharged!");
                }
            }, 240); // 12 sec cooldown. 20 ticks = 1 sec.
        }
    }
    
    Thanks,
    Jake B.
     
  2. Offline

    crzassassin

    create a hasmap of
    public static HashMap<String, Integer> cooldown = new HashMap<String, Integer>();

    make a repeating task set to 20 ticks and have the repeating task get the integer from the hasmap and set it to + 1
    then when the play tries to use it get the integer from the cooldown

    make the repeating task check if the player is on the hashmap and if it isnt return;
    and have the delayed task you already have remove the player from the hashmap

    but u have to add the player.getName() to the hashmap when they use the ability thing !
    example of adding :
    Code:
                cooldown.put(player.getName(), 0);
    
    example of removing :
    Code:
               cooldown.remove(player.getName());
    
     
  3. Offline

    minecrft

    Hello @crzassassin !

    I sort of get what you mean, but I think I did what you said, and it still doesn't work? I'm not 100% sure I followed correctly

    Listener -
    Code:
    package me.bukkit.minecrft;
    
    import java.util.HashMap;
    import java.util.Set;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Lightning extends JavaPlugin implements Listener {
        public Lightning(ThorGadget thorGadget) {
        }
    
        public static HashMap<String, Integer> cooldown = new HashMap<String, Integer>();
    
        @EventHandler
        public void onUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getItem().getItemMeta().getDisplayName().toLowerCase().contains("thor's hammer")) {
                if (!cooldown.containsValue(p.getName())) {
                    p.getWorld().strikeLightningEffect(p.getTargetBlock((Set<Material>) null, 25).getLocation());
                    cooldown.put(p.getName(), 0);
    
                }
    
            }
    
        }
    
        public void cooldownheal(final Player player) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    cooldown.remove(player.getName());
                    player.sendMessage("" + ChatColor.DARK_AQUA + ChatColor.BOLD + "Your healing ability has recharged!");
                }
            }, 240); // 12 sec cooldown. 20 ticks = 1 sec.
        }
    }
    
    Thanks,
    Jake B.
     
  4. Online

    timtower Moderator Moderator

    @minecrft You can't have multiple classes extend JavaPlugin
     
  5. Offline

    minecrft

    Hello @timtower !

    So I did what you said, and it sort of worked! It strikes lightning now, but it strikes it every time? Anything else I could do to fix that?

    Thanks,
    Jake B.
     
  6. Online

    timtower Moderator Moderator

    @minecrft change containsValue to containsKey
     
Thread Status:
Not open for further replies.

Share This Page