keeping track of time

Discussion in 'Plugin Development' started by Jeff.Halbert, Jun 20, 2012.

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

    Jeff.Halbert

    i'm in the middle of developing a simple kit plugin for a few community members that have requested it, but i've hit a snag. I've got the plugin sending the correct kits (including item, metadata, and amount) but I don't know how to keep track of when the last time a player sent the command.

    What I'm trying to do:
    stop a player from using a kit if the time set has not passed yet.

    pls help
     
  2. Offline

    r0306

    Jeff.Halbert
    Get the current system time when the player uses the /kit command and add it to a hashmap with the player as the key and the time in millis as the value. When the player uses the command again, get the current time in millis and subtract the value in the hashmap from this time to get the difference. Then compare it to a preset time to determine if enough time has elapsed.
     
  3. Offline

    Jeff.Halbert

    ok ok. so i'm new to hashmaps, does it save it in ram, on disk in temp, or in a file? would it be reset if the server where to restart, or if plugins where reloaded?

    edit:
    I did some reading on hashmaps, and understand now that there is a key and a value. The only problem I have is... if the player is the key, then the time would go for everytime he uses the /kit command. This would cause an error for me. Example: a player uses /kit tools. The plugin saves the time, and will allow the command again in 600 seconds. now if the same player uses /kit diamondArmor 20 seconds later, it would cancel the event because it would compare the time(value) saved to the player(key). How can I save a new <key, value> combination for each kit?
     
  4. Offline

    BH72

    In the case of multiple /kit commands and in staying consistent with the above method using hashmap you would use a seperate hashmap for each different /kit command. For instance one hashmap would contain player/time key-values for playera that used the /kit tools command, another map for the ones that use the /kit diamond armor command. When a player executes a command you check the appropriate list for the player key and see if enough time has elapsed since the time value stored to that player key. Using this method you need to insure that player/time pairs are removed from the map when no longer needed, possibly a repeating timer that checks the map for times that are greater than 600 seconds old, otherwise the map will become a serious memory leak.

    Another possibility to consider is to use player metadata to store time. You could set a metadata key of say "kittools", and store the time value in the key. When a player execute the /kit tools command you check to see if the player had metadata "kittools", and if so retrieve the value stored in "kittools" for comparison. Each command would have a unique metadata key.
     
  5. Offline

    Jeff.Halbert

    would it be possible to make the key like this?
    Code:
    hashMap<String, long> timer = new hashMap<String, long>();
    string key = player.getName() + " " + kitName;
    
    kinda like a first and last name
     
  6. short answer: no
    long answer, you cant use primitifs at generics, use Long
     
  7. Offline

    Jeff.Halbert

    ferrybig
    nice catch man, thanks :-D

    Everyone
    So this is what I have. The problem I'm having is: it's not saving the information in the hashmap..... maybe I'm doing something wrong (of course I am ... lol) but I can't see where. Maybe you guys can? Here's my code...

    Code:
    package me.GBCsubspec.SimpleKits;
     
    import java.util.HashMap;
    import java.util.List;
    import java.util.ListIterator;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SimpleKits extends JavaPlugin {
       
        Logger log = Logger.getLogger("Minecraft");
        private String pre = "[SimpleKits] ";
        HashMap<String, Long> timer = new HashMap<String, Long>();
       
        public void onDisable() {
            log.info(pre + "Disabled!");
        }
       
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            log.info(pre + "Enabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("kit")) {
                if ((sender instanceof Player)) {
                    Player player = (Player)sender;
                    if (args.length == 0) {
                        player.sendMessage("show available kits here...");
                        return true;
                    } else if (args.length == 1) {
                        String kitName = args[0].toLowerCase();
                        if (getConfig().contains("kits." + kitName)) {
                            if (player.hasPermission("SimpleKits.kit." + kitName)) {
                                if (onCooldown(player, kitName)) {
                                    return difference(player, kitName);
                                } else {
                                    return sendKit(player, kitName);
                                }
                            } else if (player.hasPermission("SimpleKits.kit.*")) {
                                if (onCooldown(player, kitName)) {
                                    return difference(player, kitName);
                                } else {
                                    return sendKit(player, kitName);
                                }
                            } else {
                                player.sendMessage("You do not have permission to use this kit.");
                                return true;
                            }
                        } else {
                            player.sendMessage("This kit does not exist!");
                            return true;
                        }
                    } else {
                        player.sendMessage("Too many arguments.");
                        return true;
                    }
                }
            }
            return false;
        }
       
        public boolean sendKit(Player player, String kitName) {
            List<String> itemList = getConfig().getStringList("kits." + kitName.toLowerCase() + ".items");
            ListIterator<String> it = itemList.listIterator();
            if (it.hasNext()) {
                log.info("Giving Kit: " + kitName + ", To: " + player.getName());
                while (it.hasNext()) {
                    String[] line = it.next().split(" ");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "give " + player.getName() + " " + line[0] + " " + line[2] + " " + line[1]);
                }
                long start = System.currentTimeMillis();
                String key = player.getName() + " " + kitName.toLowerCase();
                timer.put(key, start);
                log.info("Done giving kit: " + kitName + ", To: " + player.getName());
            }
            return true;
        }
       
        public boolean onCooldown(Player player, String kitName) {
            String key = player.getName() + " " + kitName.toLowerCase();
            if (timer.containsKey(key)) {
                long last = timer.get(key);
                long difference = System.currentTimeMillis() - last;
                if (difference < getConfig().getLong("kits." + kitName + ".cooldown")) {
                    return true;
                }
            }
            return false;
        }
       
        public boolean difference(Player player, String kitName) {
            String key = player.getName() + " " + kitName.toLowerCase();
            long last = timer.get(key);
            long difference = System.currentTimeMillis() - last;
            player.sendMessage(ChatColor.RED + "You still have " + difference + " seconds before you can use this kit again!");
            return true;
        }   
    }
    
     
  8. may cause of the problem:
    "kits." + kitName + ".cooldown" may not be in config
    you may have written inside config "kits." + kitName + ".cooldown" as seconds, while our code wants mili secs
    may cause problems whit other plugins:
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "give " + player.getName() + " " + line[0] + " " + line[2] + " " + line[1]);
     
  9. Offline

    Jeff.Halbert

    yeah it was the seconds thing... I had to divide the difference by 1000
     
Thread Status:
Not open for further replies.

Share This Page