My plugin no longer functions correctly for a player if they rejoin

Discussion in 'Plugin Development' started by jorrik98, Mar 8, 2015.

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

    jorrik98

    So today I started to work on a plugin to simulate the sugar in MineZ
    Basically how it works is:
    • When you right click sugar it gives you speed effect for 40 seconds
    • It starts a cooldown of 40 seconds, so when you try to right click while already sugared, it gives you the message 'You must wait (number) seconds before using sugar again!'
    • After 30 seconds the speed gets removed and the player gets 10 seconds of Slowness II
    • If you die or rejoin the server , the cooldown gets removed so you don't need to wait any more before using sugar again
    So I exported it in eclipse, reloaded my server and everything seemed to work perfectly with multiple players ...
    until i rejoined the server.

    So after reloading the plugin gives speed and after 30 seconds it gives the player slowness, but if the player rejoins after the server is reloaded it will not give the slowness effect anymore after the 30 seconds of speed.

    Basically:
    Plugin gets loaded ->
    • Before player rejoins; 40 seconds of speed and after 30 seconds he gets slowness
    • After playe rejoins; 40 seconds of speed and no slowness
    I have tried everything that I know of but i have not found how to fix this bug.
    Would any of you be so kind to look over my code to see what could be wrong?
    Also if you know of any other improvements that would be awesome!

    PS: I have not been coding for very long so don't judge me :)

    The code:
    Code:
    package me.jorrik98.Sugar;
    
    import java.util.HashMap;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin implements Listener {
       
        public final HashMap<String, Integer> SecondsMap = new HashMap<String, Integer>();
       
        @SuppressWarnings("deprecation")
        public void onEnable() {
           
            getServer().getPluginManager().registerEvents(this, this);
           
            for(final Player p :  getServer().getOnlinePlayers()){
                getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    public void run() {
                        if(!(SecondsMap.containsKey(p.getName()))){
                            return;
                        }
                        SecondsMap.put(p.getName(), SecondsMap.get(p.getName()) - 1);
                        if(SecondsMap.get(p.getName()).equals(10)) {
                            p.removePotionEffect(PotionEffectType.SPEED);
                            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 1), true);
                        }
                        if(SecondsMap.get(p.getName()).equals(-1)) {
                            SecondsMap.remove(p.getName());
                        }
                    }
                }, 20L, 20L);
            }
    
        }
       
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
    
            if(!(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_AIR))) return;
           
            if (!(p.getItemInHand().getType().equals(Material.SUGAR))) return;
           
            if((SecondsMap.containsKey(p.getName()))) {
                p.sendMessage(ChatColor.RED + "You must wait " + SecondsMap.get(p.getName()) + " seconds before using sugar again!");
                return;
            }
           
            SecondsMap.put(p.getName(), 40);
            ItemStack newItemInHand = p.getItemInHand();
            newItemInHand.setAmount(p.getItemInHand().getAmount() - 1);
            p.setItemInHand(newItemInHand);
            p.getWorld().playSound(p.getLocation(), Sound.BURP, 2.0F, 1.0F);
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 800, 1));
               
        }
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e){
            Player p = e.getEntity().getPlayer();
            if(SecondsMap.containsKey(p.getName())){
                SecondsMap.remove(p.getName());
            }
        }
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e){
            Player p = e.getPlayer();
            if(SecondsMap.containsKey(p.getName())){
                SecondsMap.remove(p.getName());
            }
        }
       
    }
    
     
  2. Offline

    mine-care

    Hmm, Mabe it's because the map used is reset on reloads or restarts.
     
  3. Offline

    jorrik98

    @mine-care Well the plugin works after it's loaded, it just doesn't function properly anymore if the players joins after the plugin has been loaded
     
  4. Offline

    CXdur

    @jorrik98
    Because at onEnable you loop through all the online players and see who needs to be added to the hashmap, it's obvious why it doesn't work.

    You need to use onPlayerJoin and see whether or not it already contains the player > if it doesn't, add the player to the hashmap.

    EDIT:

    Wow, I'm such an idiot. I was just reading an old topic about someone who wanted to use something PlayerData like and had a similar issue with not checking reloads to see if players are already online, and with only a couple of hours of sleep I thought you were doing something similar until I realized that in cooldowns it might not be a good idea to set cooldowns before players even deserve them lol.

    But yes, the issue is that cooldowns reset on reloads / restarts because the maps are removed. You would have to store the data some way if you want to fix this.
     
Thread Status:
Not open for further replies.

Share This Page