Solved Particle effect plays a second time after removing cooldown

Discussion in 'Plugin Development' started by Pr0Pancakeslol, Nov 17, 2015.

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

    Pr0Pancakeslol

    Hey guys it seems when I call my command such as /hearteffect it plays the effect just like it should but if I call my command /reset it removes the effect and my UUID from the cooldown. But it seems after that if I type the command again /hearteffect, it then plays the effect TWICE instead of normally just playing it once.

    HeartEffect Command (open)

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (!(sender instanceof Player))
            {
                getLogger().log(null, "Console cannot use this command");
            }
            if(cmd.getName().equalsIgnoreCase("hearteffect"))
            {
                if(heartcd.containsKey(player.getUniqueId()))
                {
                    player.sendMessage(ChatColor.RED + "That effect is still on cooldown. - Cooldowns are 1 hour");
                    return false;
                }
                if(player.isOp() || player.hasPermission("effects.heart"))
                {
                    if(!heartcd.containsKey(player.getUniqueId()))
                    {
                        heartcd.put(player.getUniqueId(), 1);
                        HeartEffect(player);
                        player.sendMessage("HeartEffect Enabled!");
                    }
                }
            }




    Reset Command (open)

    Code:
    if(cmd.getName().equalsIgnoreCase("reset"))
            {
                if(player.isOp())
                {
                    heartcd.remove(player.getUniqueId(), 1);
                    notecd.remove(player.getUniqueId(), 1);
                    player.sendMessage("All your particle effects have been disabled.");
                }
            }
            return true;
        }


    Thanks for any ideas, or suggestions.
     
  2. Offline

    Scimiguy

    What's heartEffect()

    Where's that reset command code coming from
     
  3. Offline

    Pr0Pancakeslol

    Sorry, i'd been better off to just post all the code.

    Here's everything, thanks for the help.

    Edit: I also switched from a hashmap to an arraylist just to remove the , 1

    Whole main class. (open)

    Code:
    public class Main
    extends JavaPlugin
    implements Listener {
        ArrayList<UUID> heartcd = new ArrayList<UUID>();
        ArrayList<UUID> notecd = new ArrayList<UUID>();
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            // implement GUI later.
        
        }
        public void onDisable() {
            // clear effects here later.
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (!(sender instanceof Player))
            {
                getLogger().log(null, "Console cannot use this command");
            }
            if(cmd.getName().equalsIgnoreCase("hearteffect"))
            {
                if(heartcd.contains(player.getUniqueId()))
                {
                    player.sendMessage(ChatColor.RED + "That effect is still on cooldown. - Cooldowns are 1 hour");
                    return false;
                }
                if(player.isOp() || player.hasPermission("effects.heart"))
                {
                    if(!heartcd.contains(player.getUniqueId()))
                    {
                        heartcd.add(player.getUniqueId());
                        HeartEffect(player);
                        player.sendMessage("HeartEffect Enabled!");
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("NoteEffect"))
            {
                if(notecd.contains(player.getUniqueId()))
                {
                    player.sendMessage("That effect is still on cooldown. -Cooldowns are 1 hour");
                    return false;
                }
                if(player.isOp() || player.hasPermission("effects.note"))
                {
                    if(!notecd.contains(player.getUniqueId()))
                    {
                        notecd.add(player.getUniqueId());
                        NoteEffect(player);
                        player.sendMessage("NoteEffect Enabled!");
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("reset"))
            {
                if(player.isOp())
                {
                    heartcd.remove(player.getUniqueId());
                    notecd.remove(player.getUniqueId());
                    player.sendMessage("All your particle effects have been disabled.");
                }
            }
            return true;
        }
    public void HeartEffect(Player player)
    {
        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            public void run() {
                if(heartcd.contains(player.getUniqueId()))
                {
                    ParticleEffect.HEART.display(0, 0, 0, 3, 1, player.getLocation().add(0, 2, 0), 32);
                }
            }
        }, 20, 80L);
    }
    public void NoteEffect(Player player)
    {
        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            public void run() {
                if(notecd.contains(player.getUniqueId()))
                {
                    ParticleEffect.NOTE.display(0, 0, 0, 3, 1, player.getLocation().add(0, 2, 0), 32);
                }
            }
        }, 20, 80L);
      }
    }
     
  4. Offline

    Scimiguy

    If you don't want it to play more than once, then why are you using a repeating task?

    Also you should return after each command, for clarity
     
  5. Offline

    caderape

    @Pr0Pancakeslol
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()

    I will skip the part where you cast the player before the check.

    You are calling a repeating task. Remove the player from the array don't stop the task. When you do your command a second time, you launch a new task, but the old one is still active. It's why you get your twice effect.

    You should make a global task for all the players.
     
  6. Offline

    TheNewTao

    Question, are you using an API for particle effects or what are you using? I have never used particles and I wouldn't mind learning.
     
  7. Offline

    Pr0Pancakeslol

    I am using DarkBlade12's library or (API) it comes with 2 classes you just create and copy into your plugin.
     
  8. Offline

    TheNewTao

  9. Offline

    Pr0Pancakeslol

    ugh that sounds messy, the task for all players.
     
  10. Offline

    Scimiguy

    @TheNewTao
    Particles really aren't that hard to get into without APIs to be honest, Just have to learn about packets a bit

    @Pr0Pancakeslol
    Well you may not need that, depending on what you're trying to do
     
  11. Offline

    TheNewTao

    @Scimiguy

    I'll start looking into packets. What is the difference between packets and NSM?
     
  12. Offline

    Scimiguy

    NMS is initialism for Network Management System.
    Basically it's the ability to modify non-api elements of the code from your own plugin. Mostly this is based around modifying existing fields, duplicating and modifying existing functionality, and drawing from internal structures.

    Packets are what are sent to relay trafficable information.
    When a particle is emitted on the server for example, a packet is sent to all the nearby clients telling that client to make a particle for the player.
    This is because Particles are generated client-side, not server side, and so the server doesn't create them itself, per-se.. it just tells all the clients to make it look like it is
     
  13. Offline

    TheNewTao

    @Scimiguy

    So packets are generated by client-side, rather than the server, and NMS doesn't involve the Bukkit API? So is NMS not exclusively for Minecraft?
     
  14. Offline

    Scimiguy

    Packets are generated by both the client and the server for varying reasons, such as a player moving, particles generating, something spawning, etc..
    Basically packets are sent to notify either end to do something, or that something has happened

    When it comes to particles, the server would send particle packets to the client to tell it to show them

    Nms is a general term for computer systems, it refers to the ability to manage various parts of itself from other parts. When we talk about nms here, we refer obviously to the nms system of minecraft
    I.e. To modify bits of minecraft stuff while not technically coding within minecraft

    Any chance a moderator could split these last few posts into a new thread? This is a bit off topic
     
Thread Status:
Not open for further replies.

Share This Page