InventoryClickEvent - Armor messages/effects

Discussion in 'Plugin Development' started by racoonsru1e, Jan 4, 2016.

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

    racoonsru1e

    Hi guys,

    I'm making a plugin where you get a abilities and things when you have a certain armor set equipped. In this event I am trying to send some messages and add effects and sounds when you equip armor, however nothing happens when I do so. I previously had it working fine with the InventoryCloseEvent however such event does not work since the 1.8 patch.

    This is the whole Event:
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player p = (Player) event.getView().getPlayer();
            ItemStack[] playerArmor = p.getInventory().getArmorContents();
            if((playerArmor[0] != null && playerArmor[0].getType() == Material.LEATHER_HELMET)
                    && (playerArmor[1] != null && playerArmor[1].getType() == Material.LEATHER_CHESTPLATE)
                    && (playerArmor[2] != null && playerArmor[2].getType() == Material.LEATHER_LEGGINGS)
                    && (playerArmor[3] != null && playerArmor[3].getType() == Material.LEATHER_BOOTS)) {
               
                p.sendMessage(ChatColor.RED + "<Armour>" + ChatColor.GRAY.toString() + ChatColor.BOLD.toString() + ChatColor.ITALIC.toString()
                        + "--YOU ARE AN ASSASSIN--");
                p.sendMessage(ChatColor.RED + "<Armour>" + ChatColor.DARK_PURPLE + "Passive: " + ChatColor.LIGHT_PURPLE + "Speed II");
                p.sendMessage(ChatColor.RED + "<Armour>" + ChatColor.DARK_PURPLE + "Passive: " + ChatColor.LIGHT_PURPLE + "Poison Dagger");
                p.sendMessage(ChatColor.RED + "<Armour>" + ChatColor.DARK_BLUE + "Active:" + ChatColor.BLUE + " Leap");
               
                p.getWorld().playSound(p.getLocation(), Sound.LEVEL_UP, 1.0F, 17.0F);
               
                tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    public void run() {
                        (((Player) event).getPlayer()).addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 40, 0));
                    }
                }
                , 0, 20);
            }
           
        }
    Thanks.
     
  2. Offline

    mine-care

    I hope you stop that at some time although the way it is, if two players click and get two tasks running, then variable "tid" will hold the last one, so one of the two will have an infinite task running.
    Hmm have you registered the event? Have you debuged?
     
  3. Offline

    racoonsru1e

    I am registering the event in the onEnable and I have checked for bugs and I can seem to find none.

    The task will be cancelled when someone removes their armor and their potion effect will be removed.

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 5, 2016
  4. Offline

    mine-care

    Not what i ment by "debug"... i mean that you should debug your code by printing messages, variables, whatever to see where the code execution stops and observe abnormalities in the code like wrong variable values.

    As i said, that will be the case individualy.
    So lets say we have Player 1 and Player 2. Players 1 and 2 do the same thing, player 2 does it a few ticks after 1. There are two tasks created (one for each player). Your current code assigns a class variable to the last task created, In the case described above, although there are two tasks created, the variable will hold only the last one, so essentially the one done by player 2. The other one exists in memory and is running normally (and infinitely) but you dont have a pointer to it to cancel it.
    Thus you need a map holding the player UUID and the task object by side, once you want to cancel the task get it from the map (providint the player's uuid as key) and cancel it.

    Have you gone through the basics of Object Orientation?
     
    Last edited: Jan 5, 2016
Thread Status:
Not open for further replies.

Share This Page