Solved SWITCH method issue causing mass NPE in console

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

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

    OTF Catastrophe

    So I'm trying to make a plugin for cosmetics and under these cosmetics I have a wardrobe where players can pick and choose types of "clothing"(Armor) they want to wear. I have a clothing type called Rainbow Armor and what this does is if you have the right permissions then you will have armor set on you that switches colors through a switch method. The code is as below:


    Code:
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rainbow"))
                {
                   
                    p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                   
                    getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
                    {
                   
                        public void run()
                        {
                           
                            switch (Main$1.this.time)
                            {
                            case 59:
                              Main$1.this.r = 255;
                              Main$1.this.g = 0;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 58:
                              Main$1.this.r = 255;
                              Main$1.this.g = 68;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 57:
                              Main$1.this.r = 255;
                              Main$1.this.g = 111;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 56:
                              Main$1.this.r = 255;
                              Main$1.this.g = 171;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 55:
                              Main$1.this.r = 255;
                              Main$1.this.g = 255;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 54:
                              Main$1.this.r = 188;
                              Main$1.this.g = 255;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 53:
                              Main$1.this.r = 128;
                              Main$1.this.g = 255;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 52:
                              Main$1.this.r = 43;
                              Main$1.this.g = 255;
                              Main$1.this.b = 0;
                              Main$1.this.time -= 1;
                              break;
                            case 51:
                              Main$1.this.r = 0;
                              Main$1.this.g = 255;
                              Main$1.this.b = 9;
                              Main$1.this.time -= 1;
                              break;
                            case 50:
                              Main$1.this.r = 0;
                              Main$1.this.g = 255;
                              Main$1.this.b = 51;
                              Main$1.this.time -= 1;
                              break;
                            case 49:
                              Main$1.this.r = 0;
                              Main$1.this.g = 255;
                              Main$1.this.b = 111;
                              Main$1.this.time -= 1;
                              break;
                            case 48:
                              Main$1.this.r = 0;
                              Main$1.this.g = 255;
                              Main$1.this.b = 162;
                              Main$1.this.time -= 1;
                              break;
                            case 47:
                              Main$1.this.r = 0;
                              Main$1.this.g = 255;
                              Main$1.this.b = 230;
                              Main$1.this.time -= 1;
                              break;
                            case 46:
                              Main$1.this.r = 0;
                              Main$1.this.g = 239;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 45:
                              Main$1.this.r = 0;
                              Main$1.this.g = 196;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 44:
                              Main$1.this.r = 0;
                              Main$1.this.g = 173;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 43:
                              Main$1.this.r = 0;
                              Main$1.this.g = 162;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 42:
                              Main$1.this.r = 0;
                              Main$1.this.g = 137;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 41:
                              Main$1.this.r = 0;
                              Main$1.this.g = 100;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 40:
                              Main$1.this.r = 0;
                              Main$1.this.g = 77;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 39:
                              Main$1.this.r = 0;
                              Main$1.this.g = 34;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 38:
                              Main$1.this.r = 17;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 37:
                              Main$1.this.r = 37;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 36:
                              Main$1.this.r = 68;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 35:
                              Main$1.this.r = 89;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 34:
                              Main$1.this.r = 102;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 33:
                              Main$1.this.r = 124;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 32:
                              Main$1.this.r = 154;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 31:
                              Main$1.this.r = 222;
                              Main$1.this.g = 0;
                              Main$1.this.b = 255;
                              Main$1.this.time -= 1;
                              break;
                            case 30:
                              Main$1.this.r = 255;
                              Main$1.this.g = 0;
                              Main$1.this.b = 247;
                              Main$1.this.time -= 1;
                              break;
                            case 29:
                              Main$1.this.r = 255;
                              Main$1.this.g = 0;
                              Main$1.this.b = 179;
                              Main$1.this.time -= 1;
                              break;
                            case 28:
                              Main$1.this.r = 255;
                              Main$1.this.g = 0;
                              Main$1.this.b = 128;
                              Main$1.this.time = 59;
                             
                            }
                           
                            Color c = Color.fromRGB(Main$1.this.r, Main$1.this.g, Main$1.this.b);
                                   
                            if (!p.getInventory().getHelmet().getItemMeta().hasDisplayName() && p.getInventory().getHelmet().getType().equals(Material.LEATHER_HELMET))
                            {
                                       
                                p.getInventory().setHelmet(getColorArmor(Material.LEATHER_HELMET, c));
                                p.getInventory().setChestplate(getColorArmor(Material.LEATHER_CHESTPLATE, c));
                                p.getInventory().setLeggings(getColorArmor(Material.LEATHER_LEGGINGS, c));
                                p.getInventory().setBoots(getColorArmor(Material.LEATHER_BOOTS, c));
                               
                            }
    
                        }
                       
                    },0,2L);
                   
                }
    Basically the player clicks the inventory and if the player clicks the suit with "Rainbow" in it's name it'll set the players full armor to Rainbow armor. The issue I'm having is when removing this armor with setArmorContents() causes a huge NPE spam ever 2 ticks because that's how fast the rainbow armor changes due to the scheduler. Is there any fix to this issue? Possibly a way to cancel the switch even after the armor is gone?
     
  2. Online

    timtower Moderator Moderator

    @OTF Catastrophe Couple things:
    1. Why Main$1.this
    2. Does the current item exist? Does it have meta? Does the meta has a display name?
    3. Does the runnable get stopped or do you start new ones every time and while doing so stacking them?
     
  3. Offline

    AlvinB

    @OTF Catastrophe
    What's with the $'s? Is this code decompiled or something?

    Also, to solve the issue, we probably need your stacktrace aswell as the code for the "getColorArmor" method.
     
  4. Offline

    OTF Catastrophe

    I knew someone was going to ask about it, no it's not decompiled, I mean if you look at my code is sloppy enough that even I wouldn't take the code from it. It's only like that because I always use the same root for my projects (me.sirapathy.main). It's a bad habit but I started using the plugin name inside the root to make it all different without using things like $1 and what not.

    The whole class: http://pastebin.com/5CKVLsdj
    The error: http://pastebin.com/qqTKZvdA
     
  5. Online

    timtower Moderator Moderator

    @OTF Catastrophe And this is why we use packages with names.
    Like I expected: you never check the meta etc, point 2 on the list.
    And if you have 2 of those runnables then they override each other.
    And for your onCommand: check if the sender is a player before you cast.
     
  6. Offline

    AlvinB

    @OTF Catastrophe
    NPE on this line (196):
    Code:java
    1. if (!p.getInventory().getHelmet().getItemMeta().hasDisplayName() && p.getInventory().getHelmet().getType().equals(Material.LEATHER_HELMET))
    Probably because you haven't checked .hasItemMeta().
     
  7. Offline

    OTF Catastrophe

    @timtower @AlvinB
    Slightly confused. I realized that the display name doesn't really matter if I'm going to just check if the item has ItemMeta. But replacing that with checking if the item has meta and checking if the item on their head is a leather helmet makes it so it doesn't run at all. Since the original statement checked if it didn't have a displayName, the leather helmet being put on the players head before the repeating task occurs would be what caused the armor to change to rainbow armor in the first place. If I check for the armor having item meta with a regular leather helmet it won't run.

    But along with that, checking if the armor doesn't have item meta it only runs once because the first case changes the armor color, therefore making it have item meta with its color.

    Am I misinterpreting what you're telling me to check? I'm sorry if I don't understand fully aha

    EDIT: On inventory click of an item named Rainbow I set the players helmet to a Leather Helmet with a random name to give it item meta then in the run statement I checked if the helmet wasn't null along with checking if the helmet has item meta and it works perfect. Thank you so much guys! :)
     
    Last edited: Dec 29, 2016
Thread Status:
Not open for further replies.

Share This Page