I can't figure out how to make a cooldown

Discussion in 'Plugin Development' started by kensclark15, Jan 9, 2012.

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

    kensclark15

    I made a Found Diamond plugin for the server I own. Every time someone breaks a diamond ore block, it announces it to the server then gets a list of online players and gives them each 2 iron. Could you tell me how to make it so it will announce it to the server once per diamond vein, give them a random number of iron (1 - 15), then cooldown. I want it so when someone breaks a diamond block/vein, it will only announce it and wait about 10 - 15 seconds before checking if they break another one. Here are my two classes:
    Code:
     * Property of Kenneth Clark.
     * Made for my server SingularityCraft.
     */
    package durzo.founddiamondore.server;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        final Logger log = Logger.getLogger("Minecraft");
        private final BlockBreakGet blockBreak = new BlockBreakGet(this);
    
        public void onEnable() {
    
            this.getServer().getPluginManager().registerEvent(Event.Type.BLOCK_BREAK, blockBreak, Event.Priority.Low, this);
    
            logMsg("Plugin Enabled!");
    
        }
    
        public void onDisable() {
    
            logMsg("Plugin Disabled!");
    
        }
    
        private void logMsg(String msg) {
    
            log.info("[SingularityCraft] v:(" + this.getDescription().getVersion() + "): " + msg);
    
        }
    
    }
    
    Code:
    package durzo.founddiamondore.server;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.inventory.ItemStack;
    
    public class BlockBreakGet extends BlockListener {
    
        private Main plugin;
    
        public BlockBreakGet(Main instance) {
            plugin = instance;
        }
        @Override
        public void onBlockBreak(BlockBreakEvent event) {
    
            Player player = event.getPlayer();
            String name = player.getDisplayName();
            Block block = event.getBlock();
    
            if (block.getType() == Material.DIAMOND_ORE) {
    
                plugin.getServer().broadcastMessage(broadcastMsg(name));
                Player[] players = Bukkit.getOnlinePlayers();
    
                for (Player playerP : players) {
    
                    playerP.getInventory().addItem(new ItemStack(Material.IRON_INGOT, 2));
                    playerP.sendMessage(ChatColor.AQUA + "You are awarded 2 Iron!");
    
                }
    
            }
    
        }
    
        private String broadcastMsg(String name) {
    
            return (ChatColor.GOLD + name + " found Diamond Ore!");
    
        }
    
    }
    
     
  2. Offline

    DirtyStarfish

    I guess you could have a variable such as a boolean or something to indicate whether a cooldown is active or not, and then use the scheduler to change it after 15 seconds. Or you could record the current time when you announce that someone has found diamonds, and then compare it to the new current time when the next person does, and see if 15 seconds have passed.

    So, if you had a boolean that is set to true when you give out the iron and send the message, have a task to change it back to false after 15 seconds. Meanwhile, if someone finds diamonds, or gets the rest of the vein, the message wont send because the cooldown boolean is true.

    Or if you go for the time option. Record the time when you give out the iron, and then when someone breaks diamond, check to see if the current time is 15 seconds ahead or not, if so, give out iron, if not, then don't.
     
  3. Offline

    kensclark15

    Couldn't I use a HashMap so if someone else breaks diamond right after the first person does, it annouces it?
     
  4. Offline

    DirtyStarfish

    Yes, then it would be a cooldown for each player. You could put the playername and the current time of them breaking it. Then, when someone breaks a diamond block, check if they are in the HashMap, if they aren't send the message and iron.
    If they are, get the time stored in the map, and compare it with the current time. If 15 seconds has passed, then that players cooldown is over, if 15 seconds hasn't passed, then you know not to send the message.
     
  5. Offline

    kevhog

    In your variable initializations:
    Code:java
    1. long lastTime = 0;

    And when they find diamond:
    Code:java
    1. if(getServer().getWorld("world").getFullTime() > lastTime)
    2. {
    3. code n stuff
    4. lastTime = getServer().getWorld("world").getFullTime();
    5. }

    And change "world" to your world's name if needed. Yeah, put lastTime into a HashSet if you want the cooldown to be per-player, but this should get rid of any chat spam.

    EDIT: This WYSIWYG editor is pissing me off, I DON'T WANT THE DAMN FORMATTING IN THE CODE
     
  6. Offline

    kensclark15

    I actually did it my way and it works great! Could I improve it? Is it real bukkit plugin material?
    Code:
    /*
     * Property of Kenneth Clark.
     * Made for my server SingularityCraft.
     */
    package durzo.founddiamondore.server;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        final Logger log = Logger.getLogger("Minecraft");
        private final BlockBreakGet blockBreak = new BlockBreakGet(this);
        private final QuitListener quitListener = new QuitListener(this);
    
        public void onEnable() {
    
            this.getServer().getPluginManager().registerEvent(Event.Type.BLOCK_BREAK, blockBreak, Event.Priority.Low, this);
            this.getServer().getPluginManager().registerEvent(Event.Type.PLAYER_QUIT, quitListener, Event.Priority.Low, this);
    
            logMsg("Plugin Enabled!");
    
        }
    
        public void onDisable() {
    
            logMsg("Plugin Disabled!");
    
        }
    
        public void logMsg(String msg) {
    
            log.info("[SingularityCraft] v:(" + this.getDescription().getVersion() + "): " + msg);
    
        }
    
    }
    
    Code:
    package durzo.founddiamondore.server;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.inventory.ItemStack;
    
    public class BlockBreakGet extends BlockListener {
    
        private Main plugin;
        public static HashMap<Player, Boolean> cooldownList = new HashMap<Player, Boolean>();
    
        public BlockBreakGet(Main instance) {
            plugin = instance;
        }
        @Override
        public void onBlockBreak(BlockBreakEvent event) {
    
            final Player player = event.getPlayer();
            String name = player.getDisplayName();
            Block block = event.getBlock();
    
            if (block.getType() == Material.DIAMOND_ORE) {
    
                if (cooldownList.containsKey(player)) {
    
                    if (cooldownList.get(player) == true) {
                        plugin.logMsg(name + " found a diamond!");
                    } else if (cooldownList.get(player) == false) {
                        plugin.logMsg(name + " found a diamond!");
    
                        plugin.getServer().broadcastMessage(broadcastMsg(name));
                        Player[] players = Bukkit.getOnlinePlayers();
    
                        for (Player playerP : players) {
    
                            playerP.getInventory().addItem(new ItemStack(Material.IRON_INGOT, 10));
                            playerP.sendMessage(ChatColor.AQUA + "You are awarded 10 Iron!");
    
                        }
    
                        cooldownList.remove(player);
                        cooldownList.put(player, true);
    
                        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                            public void run() {
    
                                Player [] playersL = Bukkit.getOnlinePlayers();
    
                                for (Player onlineL : playersL) {
    
                                    if (onlineL.getName().equals(player)) {
                                        cooldownList.remove(player);
                                        cooldownList.put(player, false);
                                    }
    
                                }
      
                            }
    
                        }, 300L);
    
                    } //end ifs
    
                } else if (!cooldownList.containsKey(player)) {
    
                    plugin.logMsg(name + " found a diamond!");
    
                    plugin.getServer().broadcastMessage(broadcastMsg(name));
                    Player[] players = Bukkit.getOnlinePlayers();
    
                    for (Player playerP : players) {
    
                        playerP.getInventory().addItem(new ItemStack(Material.IRON_INGOT, 10));
                        playerP.sendMessage(ChatColor.AQUA + "You are awarded 10 Iron!");
    
                    }
    
                    cooldownList.put(player, true);
    
                    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                        public void run() {
    
                            Player [] playersL = Bukkit.getOnlinePlayers();
    
                            for (Player onlineL : playersL) {
    
                                if (onlineL.getName().equals(player)) {
                                    cooldownList.put(player, false);
                                }
    
                            }
    
                        }
    
                    }, 300L);
    
                }
    
            }
    
        }
    
        private String broadcastMsg(String name) {
    
            return (ChatColor.GOLD + name + " found Diamond Ore!");
    
        }
    
    }
    Code:
    package durzo.founddiamondore.server;
    
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class QuitListener extends PlayerListener {
    
        private Main plugin;
    
        public QuitListener(Main instance) {
    
            plugin = instance;
    
        }
        @Override
        public void onPlayerQuit(PlayerQuitEvent event) {
    
            if (BlockBreakGet.cooldownList.containsKey(event.getPlayer())) {
    
                BlockBreakGet.cooldownList.remove(event.getPlayer());
                plugin.logMsg(event.getPlayer().getName() + "was removed from the FoundDiamond HashMap!");
    
            }
    
        }
      
    }
    
     
Thread Status:
Not open for further replies.

Share This Page