Message doesn't stop sending

Discussion in 'Plugin Development' started by sum_17427, Dec 11, 2014.

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

    sum_17427

    My code:
    [java]Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    public void run(){
    cooldown.remove(player);
    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
    }
    }, 3600);[/java]
    Why that message doesn't stop sending?
     
  2. Offline

    teej107

    @sum_17427 Your shown code tells us that it only runs once.
     
  3. Offline

    Monkey_Swag

  4. Offline

    sum_17427

    @teej107 @Monkey_Swag
    Code:
    public void onInventoryClick(PlayerInteractEvent e){
            final Player player = e.getPlayer();
            if (player.hasPermission("mycraft.use.normal")){
                if (player.getItemInHand().getType().equals(Material.STONE_AXE)
                        || player.getItemInHand().getType().equals(Material.STONE_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 5){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 300, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "1 phút hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 5);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 1200);
                        }
                    }
                }
    }
     
  5. Offline

    Monkey_Swag

    @sum_17427 not exactly sure why it's running multiple times, but what I do know is that instead of usin .equals, you need to use '=='. This is a more correct way of comparing Enums. That might be the problem.
     
  6. Offline

    sum_17427

    @Monkey_Swag
    This Message
    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 1200);
    It's don't stop sending message while im not do action
     
  7. Offline

    teej107

    @Monkey_Swag I doubt it. @sum_17427 on another note, it is advised that you don't un-cancel an event unless you have an intention of changing the outcome of other plugins (it might even break them). Right now, this
    Code:
    e.setCancelled(false);
    is doing nothing in relation to your plugin. Also your method name is a bit misleading. How many times does it send the message? Is it consistent every time?
     
  8. Offline

    sum_17427

    @teej107
    Its sendmessage after im do the action rightclick and its don't stop
    Could i add "return" to
    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 1200);
    return;
     
    Last edited: Dec 11, 2014
  9. Offline

    teej107

    @sum_17427 Show me the code where you register your event.
     
  10. Offline

    sum_17427

    @teej107
    Here full code:
    Code:
    package me.summc.mycraft;
    
    import java.util.HashSet;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    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 Logger log = Logger.getLogger("Minecraft");
        HashSet<Player> cooldown = new HashSet<Player>();
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
          }
       
        @EventHandler(priority=EventPriority.NORMAL)
        public void onInventoryClick(PlayerInteractEvent e){
            final Player player = e.getPlayer();
            if (player.hasPermission("mycraft.use.normal")){
                if (player.getItemInHand().getType().equals(Material.STONE_AXE)
                        || player.getItemInHand().getType().equals(Material.STONE_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 5){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 300, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "1 phút hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 5);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 1200);
                        }
                    }
                }
                if (player.getItemInHand().getType().equals(Material.IRON_AXE)
                        || player.getItemInHand().getType().equals(Material.IRON_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 10){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 300, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "2 phút hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 10);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 2400);
                        }
                    }
                }
                if (player.getItemInHand().getType().equals(Material.DIAMOND_AXE)
                        || player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 15){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 300, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ " Bạn đã sử dụng skill!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Thời gian hồi skill: 3 phút.");
                                player.setFoodLevel(player.getFoodLevel() - 15);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 3600);
                        }
                    }
                }
            }
            if (player.hasPermission("mycraft.use.vip")){
                if (player.getItemInHand().getType().equals(Material.STONE_AXE)
                        || player.getItemInHand().getType().equals(Material.STONE_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 5){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 200, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "30 giây hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 5);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 600);
                        }
                    }
                }
                if (player.getItemInHand().getType().equals(Material.DIAMOND_AXE)
                        || player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 10){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 300, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "1 phút 30 giây hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 10);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                }
                            }, 1800);
                        }
                    }
                }
                if (player.getItemInHand().getType().equals(Material.DIAMOND_AXE)
                        || player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 15){
                            if (cooldown.contains(player)){
                                e.setCancelled(false);
                            } else {
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 600, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "2 phút 30 giây hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 15);
                                cooldown.add(player);
                            }
                            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                public void run(){
                                    cooldown.remove(player);
                                    player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");   
                                }
                            }, 3000);
                        }
                    }
                }
            }
        }   
    }
    
     
  11. Offline

    Avygeil

    @sum_17427 The part where you schedule is not in your else { }.

    EDIT :
    Also :
    1. Don't use the Minecraft logger
    2. getItemInHand() can return null
    3. The first 'if' part where you cancel the event is not necessary, remove it and make the check !contains
    4. You just broke whole OOP with a single class
    5. Use a Set of UUID
     
  12. Offline

    teej107

    Your code if very repetitive. That's why they invented methods!
     
    Avygeil likes this.
  13. Offline

    sum_17427

    @Avygeil
    1. How would i change Minecraft logger to ?
    3 + 5. Could i have an example?
     
  14. Offline

    Avygeil

    @sum_17427
    1. Use this.getServer().getLogger()
    5. Set<UUID> = new HashSet<UUID>(), and then you add/check against it with player.getUniqueId()
    3. What I meant is right now you have :

    if (contains) { // Unnecessary code) }
    else { // interesting code }
    Schedule

    Change to :
    if (!contains) { // interesting code + Schedule }
     
    sum_17427 likes this.
  15. @sum_17427 Sans the getServer() - that will just end up returning Minecraft's Logger anyway. JavaPlugin#getLogger() returns the Logger specifically for that plugin ;)
     
    Avygeil likes this.
  16. Offline

    sum_17427

    @Avygeil
    Im did right?
    Code:
    if (player.getItemInHand().getType().equals(Material.IRON_AXE)
                        || player.getItemInHand().getType().equals(Material.IRON_PICKAXE)){
                    if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
                        if (player.getFoodLevel() >= 10){
                            if (!(cooldown.contains(player))){
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 5);
                                player.getLocation().getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 5);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 300, 1));
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN+ "Sử dụng!");
                                player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "2 phút hồi.");
                                player.setFoodLevel(player.getFoodLevel() - 10);
                                cooldown.add(player);
                                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                                    public void run(){
                                        cooldown.remove(player);
                                        player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Skill" + ChatColor.DARK_RED + "] " + ChatColor.DARK_GREEN + "Hồi!");
                                    }
                                }, 2400);
                            }
                        }
                    }
                }
    Code:
    public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            this.getServer().getLogger();
        }
     
  17. Offline

    Avygeil

    @AdamQpzm Yeah I messed up, typed too fast :)

    @sum_17427 If you do the null check, then yes. But only you can tell me, does it work now?

    EDIT : Actually, if you did change to Set<UUID>, you have to check for contains(player.getUniqueId()), not contains(player). Same for remove and add.
     
    Last edited: Dec 11, 2014
    sum_17427 and AdamQpzm like this.
  18. Offline

    sum_17427

    @Avygeil
    Thanks. It's work smoothly!
    Change to this? "HashSet<UUID> cooldown = new HashSet<UUID>();"
     
    Last edited: Dec 11, 2014
    Avygeil likes this.
  19. Offline

    Avygeil

    @sum_17427 Yes, this will prevent memory leaks since you don't remove the player on leave. But if you do it, also change :
    cooldown.contains(player) => cooldown.contains(player.getUniqueId())
    cooldown.add(player) => cooldown.add(player.getUniqueId())
    cooldown.remove(player) => cooldown.remove(player.getUniqueId())
    But you get the logic. :)
     
    sum_17427 likes this.
  20. @sum_17427 Actually it should be "Set<UUID> cooldown = new HashSet<UUID>();". I'll insert a snippet of one of garbagemule's long posts to try and explain why :p

     
    Avygeil likes this.
  21. Offline

    mythbusterma

    @sum_17427

    Don't use Runnables to create cooldowns, associate a time with the player and then check it when you try to perform the action, using Runnables to do this is just lazy.
     
  22. @mythbusterma Although admittedly it's the only way to do it and send them a message that they can use it again.

    @Avygeil Your liking of my post helped me to ninja you here ;)
     
    Avygeil likes this.
  23. Offline

    Avygeil

    @mythbusterma I think he wants to notify the player when the cooldown finishes, judging by the code. I can't think of how to do it without Runnables, but maybe you know?

    EDIT : Dat ninja
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page