Solved Timer For Separate Players

Discussion in 'Plugin Development' started by MrGriefer_, Aug 23, 2016.

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

    MrGriefer_

    Hello,

    I'm trying to do something like when a players types /testtimer a 30 second timer will start but the problem is when another player runs the same command it will continue were the 30 seconds was left! I know its something with HashMaps<String, Integer> it didn't work for me.

    Here is my code so far:

    Code:
    public Map<String, Integer> taskID = new HashMap<String, Integer>();
        public int tnttimer = 30;
       
        @Override
        public void onEnable() {
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("testtimer")) {
                if (!taskID.containsKey(player.getName())) {
                    scheduleRepeatingTask(player, 20);
                } else {
                    return true;
                }
            }
            return true;
        }
       
        public void scheduleRepeatingTask(final Player p, long ticks) {
            final int tid = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    ItemStack tnt = new ItemStack(Material.TNT, tnttimer);
                    ItemMeta meta = tnt.getItemMeta();
                    meta.setDisplayName(ChatColor.RED + "Time " + ChatColor.BLACK + "Bomb");
                    tnt.setItemMeta(meta);
                    p.getInventory().setItem(8, tnt);
                   
                    if (tnttimer == 0) {
                        endTask(p);
                        p.setHealth(0.0D);
                        Location loc = p.getLocation();
                        p.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 1.0F, false, false);
                        p.sendMessage("Timer ended!");
                        p.getInventory().clear();
                    }
                    tnttimer--;
                }
            }, 0, ticks);
           
            taskID.put(p.getName(), tid);
        }
       
        public void endTask(Player p) {
            if (taskID.containsKey(p.getName())) {
                int tid = taskID.get(p.getName());
                this.getServer().getScheduler().cancelTask(tid);
                tnttimer = 30;
                taskID.remove(p.getName());
            }
        }
    I know I'm doing something wrong but not sure where! A little guidance will be appreciated.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MrGriefer_ Instead of having the time as field, have it in a map of some sort, just like you have the taskID's
     
  3. Offline

    MrGriefer_

    Like an ArrayList<Integer> sorry i'm so confused
     
  4. Offline

    timtower Administrator Administrator Moderator

    @MrGriefer_ Map, not list, map.
    Look at your taskID's map.
     
  5. Offline

    HeartandSoul

    It works for me when I store it In a different class, then make a new instance it it.
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    HeartandSoul

    What do you mean, "overkill" xD
     
  8. Offline

    timtower Administrator Administrator Moderator

    No need for a new class when a hashmap would do
     
  9. Offline

    MrGriefer_

    Could you provide me with a code?
     
  10. Offline

    timtower Administrator Administrator Moderator

    Copy your current hashmap, rename it.
    Profit.
     
  11. Offline

    MrGriefer_

    OK but how would I check when the time is 0 ?
     
  12. Offline

    HeartandSoul

    Its fine for me though, I don't mind. Before we de-rail the threads main topic, Lemme help:

    HashMap<String, Integer> inty = new HashMap<String, Integer>();

    I use this as a field. Create this. Then in a method, put a String as 1st arg, integer as a 2nd arg. Get the value. If you have the integer in the config, you can do something like 'Bukkit.getPluginManager().getPlugin("name").getConfig().getInt("name of integer in the config");'
     
  13. Offline

    InstanceofDeath

    <<code>>
    new HashMap<String, BukkitRunnable> PersonallyTimeSavingHashMap = new HashMap<String,
    //here you write your schedular repeating task thing>();

    Do you know what a HashMap actually does and is? When not, than ask me to tell you a story :j

    The first String is the playername ... You never should use the player object in it, because then the HashMaps save all data with it, like the Inventory, Effects, Location etc. So dont do that

    The <> are generics. That means that you can only use a String Variable as the key and the BukkitRunnable as a value. So its just a help for you, that you dont use wrong datatypes later. Your IDE will show a mistake then. This feature was added in java 5

    P.S Its not a spoonfeed .. I explained him the code .. Bye
     
  14. Offline

    HeartandSoul

    Please learn some java, as this is simple for those who do. Sorry for being that guy.

    Code:
    if (integer == 0){
        //do stuff
    }
     
  15. Offline

    MrGriefer_

    Nvm! Thanks all for helping, I got it working.

    Here is my code:
    PHP:
    private HashMap<StringIntegercooldownTime;
        private 
    HashMap<StringBukkitRunnablecooldownTask;
       
        @
    Override
        
    public void onEnable() {
            
    cooldownTime = new HashMap<StringInteger>();
            
    cooldownTask = new HashMap<StringBukkitRunnable>();
        }
       
        @
    Override
        
    public boolean onCommand(CommandSender senderCommand cmdString labelString[] args) {
            
    Player p = (Playersender;
            if (
    cmd.getName().equalsIgnoreCase("testtimer")) {
                
    startTimer(p);
            }
            return 
    true;
        }
       
        public 
    void startTimer(Player p) {
            if (
    cooldownTime.containsKey(p.getName())) {
                
    p.sendMessage("§aRemaining time is: §c" cooldownTime.get(p.getName()));
                return;
            }
           
            
    cooldownTime.put(p.getName(), 30);
            
    cooldownTask.put(p.getName(), new BukkitRunnable() {
                public 
    void run() {
                    
    cooldownTime.put(p.getName(), cooldownTime.get(p.getName()) - 1);
                   
                    
    ItemStack is = new ItemStack(Material.TNTcooldownTime.get(p.getName()));
                    
    ItemMeta im is.getItemMeta();
                    
    im.setDisplayName("§cTime§fBomb");
                    
    is.setItemMeta(im);
                    
    p.getInventory().setItem(8is);
                    if (
    cooldownTime.get(p.getName()) == 0) {
                        
    cooldownTime.remove(p.getName());
                        
    cooldownTask.remove(p.getName());
                        
    cancel();
                        
    p.setHealth(0.0D);
                        
    Location loc p.getLocation();
                        
    p.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 1.0Ffalsefalse);
                        
    p.sendMessage("§a§lTimer ended!");
                        
    p.getInventory().clear();
                    }
                }
            });
           
            
    cooldownTask.get(p.getName()).runTaskTimer(this020);
           
            return;
        }
     
Thread Status:
Not open for further replies.

Share This Page