Solved Need help removing Rainbow Armor from scheduleSyncRepeatingTask

Discussion in 'Plugin Development' started by OTF Catastrophe, Dec 25, 2016.

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

    OTF Catastrophe

    So the issue I'm having is getting rid of "Rainbow Armor" placed on players. I have this gui that I made that when you for instance click on a helmet named "Rainbow Helmet" it'll set your helmet to a leather helmet and then switch between different combinations of colors from RGB. This all works fine but I've been trying to figure out a method to try and get rid of the armor by clicking a "Remove Armor" item in the gui. It's a scheduleSyncRepeatingTask so it's not as simple as just cancelling it but honestly Ive tried so many ways using a BukkitRunnable and just none of it works because I'm 99% sure I'm doing it wrong. Heres my code for the rainbow armor: (This is all in a InventoryClickEvent so that's why I check for e.getWhoClicked)


    Code:
    final HumanEntity p = e.getWhoClicked();
               
                getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
                {
               
                    public void run()
                    {
                       
                        Random r = new Random();
    
                        final Color c = Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255));
    
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rainbow Chestplate") && p.getInventory().getHelmet().getType().equals(Material.LEATHER_HELMET))
                        {
                               
                            p.getInventory().setHelmet(getColorArmor(Material.LEATHER_HELMET, c));
                               
                        }
                           
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rainbow Chestplate") && p.getInventory().getChestplate().getType().equals(Material.LEATHER_CHESTPLATE))
                        {
                               
                            p.getInventory().setChestplate(getColorArmor(Material.LEATHER_CHESTPLATE, c));
                               
                        }
                           
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rainbow Leggings") && p.getInventory().getLeggings().getType().equals(Material.LEATHER_LEGGINGS))
                        {   
                               
                            p.getInventory().setLeggings(getColorArmor(Material.LEATHER_LEGGINGS, c));
                               
                        }
                           
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rainbow Boots") && p.getInventory().getBoots().getType().equals(Material.LEATHER_BOOTS))
                        {
                               
                            p.getInventory().setBoots(getColorArmor(Material.LEATHER_BOOTS, c));
                               
                        }
                           
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Remove Armor"))
                        {
                               
                            p.getInventory().setArmorContents(null);
                               
                        }
                   
                    }
               
                }, 0, 10L);
    Any help would be greatly appreciated. I've seen timtower ask people to explain what color armor is in other peoples posts so heres what rainbow armor is: https://dev.bukkit.org/projects/rainbow-armour
    (That is someone elses plugin just for reference)
     
  2. @OTF Catastrophe
    What if you try to set each individual armour slot to null seperatley,using .setBoots() .setLeggings() etc

    Alternatively you could always try passing in Material.AIR ItemStacks to either the individual slots, or as an array to .setArmorContents()
     
  3. Offline

    OTF Catastrophe

    I've tried that but since it's a repeating task it keeps doing it no matter what so setting the armor slots to null just cause a buggy repeating task to occur(Timings get thrown off). The remove item in the gui sets all the armor to null and it does for all the armor except rainbow(Does the buggy repeating task issue also).

    I've tried checking if the player has leather armor on first to do it as well and that does fix the issue. BUT the way I have it set up is it sets the armor to a color instead of setting armor then setting a color. This is because trying to remove the armor with the repeating task would just cause it to place armor again. Sorry if this is confusing, honestly I figured it would just kill the task if I set the armor to null but it just doesn't seem to want to work that way aha.

    EDIT: I was thinking of keeping a loop running checking if a player has a helmet named "RainbowHelmet" or something like that and only then applying the rainbow color but when i check for a display name it doesn't do anything. Can you tell me if theres comething wrong with this code?


    Code:
    final Color c = Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255));
    
                        for (Player p : Bukkit.getServer().getOnlinePlayers())
                        {
                           
                            if (p.getInventory().getHelmet().getItemMeta().getDisplayName().equals("Test"))
                            {
                               
                                p.getInventory().setHelmet(getColorArmor(Material.LEATHER_HELMET, c));
    
                            }
                           
                        }
     
    Last edited: Dec 26, 2016
  4. @OTF Catastrophe
    Other than the fact that the if statement can throw a whole bunch of NPE's, no.
     
  5. Offline

    OTF Catastrophe

    Found my issue, it was a really simple fix really. The issue was armor kept going back on my body so there was nothing I could change. I changed it to:
    Code:
    final HumanEntity p = e.getWhoClicked();
               
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rainbow Helmet"))
                {
                   
                    p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                   
                    getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
                    {
                   
                        public void run()
                        {
                           
                            Random r = new Random();
                           
                            final Color c = Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255));
                                   
                            if (!p.getInventory().getHelmet().getItemMeta().hasDisplayName() && p.getInventory().getHelmet().getType().equals(Material.LEATHER_HELMET))
                            {
                                       
                                p.getInventory().setHelmet(getColorArmor(Material.LEATHER_HELMET, c));
                               
                            }
                           
                            else
                            {
                               
                                return;
                               
                            }
    
                        }
                       
                    },0,10L);
                   
                }
    Had to throw in the else statement because like you said, there was tons of NPE's in console.
     
Thread Status:
Not open for further replies.

Share This Page