1.14.4 [HELP] NPE

Discussion in 'Spigot Plugin Development' started by DragonLord__, Feb 17, 2020.

  1. Okay so, I am creating a plugin which involves cooldowns.
    Now, I am beginner at cooldowns and java overall.
    I need to get cooldown earlier because if not, it goes to negative and player needs to execute command once more so it works.
    Code (Text):

    long currentMillis = System.currentTimeMillis();
                    //long playerCooldown = plugin.getPlayers().getLong("Players." + p.getUniqueId().toString() + ".EnchanterCooldown");
                    long playerCooldown = plugin.getSledeciUse(p) - currentMillis; //this line shows error
                    long armorCooldown = plugin.getC().getInt("Armors.EnchantArmor.Cooldown");
                    long sledeciUse = 0;
                    long[] vrijeme = new long[3];
                    if(playerCooldown >= 1)
                    {
                        sledeciUse = plugin.getSledeciUse(p);
                        playerCooldown = (sledeciUse - currentMillis) / 1000;  //Same thing is done here as up there, but if only this exists and not the previous line, everything works...
                        vrijeme[0] = 0;
                        vrijeme[1] = 0;
                        vrijeme[2] = 0;
                        vrijeme[0] = playerCooldown;
                        while(vrijeme[0] >= 60)
                        {
                            vrijeme[1]++;
                            vrijeme[0] -= 60;
                            if(vrijeme[1] >= 60)
                            {
                                vrijeme[2]++;
                                vrijeme[1] = 0;
                            }
                        }
                        plugin.getPlayers().set("Players." + p.getUniqueId().toString() + ".PlayerName", p.getName());
                        plugin.getPlayers().set("Players." + p.getUniqueId().toString() + ".EnchanterCooldown", playerCooldown);
                        plugin.saveYaml();
                        String message = plugin.getC().getString("Messages.timerMessage");
                        message = message.replace("%hours%", String.valueOf(vrijeme[2]));
                        message = message.replace("%minutes%", String.valueOf(vrijeme[1]));
                        message = message.replace("%seconds%", String.valueOf(vrijeme[0]));
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
                        return true;
                    }
                    else
                    {
                        ItemStack hand = p.getInventory().getItemInMainHand();
                       
                        if(hand == null || hand.getEnchantments().size() <= 0)
                        {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getC().getString("Messages.enchanterNoItem")));
                            return true;
                        }
                        sledeciUse = currentMillis + (armorCooldown*1000);
                        loadAllEnchs();
                        int y = 0;
                        status = 0;
                        for(int i = 0; i < 28; i++)
                        {
                            if(hand.getEnchantments().get(ench[i]) != null)
                            {
                                int level = hand.getEnchantmentLevel(ench[i]);
                                int max_level = ench[i].getMaxLevel();
                                if(max_level > level)
                                {
                                    hand.removeEnchantment(ench[i]);
                                    hand.addEnchantment(ench[i], level + 1);
                                    names[y] = ench[i].getKey().toString().substring(10, ench[i].getKey().toString().length());
                                    names[y] = StringUtils.capitalize(names[y]);
                                    stari[y] = level;
                                    novi[y] = hand.getEnchantmentLevel(ench[i]);
                                    y++;
                                    status = 1;
                                }
                            }
                        }
                        if(status == 0)
                        {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getC().getString("Messages.nothingChanged")));
                            return true;
                        }
                       
                        plugin.removeSledeciUse(p);
                        plugin.putSledeciUse(p, sledeciUse);
                        plugin.getPlayers().set("Players." + p.getUniqueId().toString() + ".EnchanterCooldown", armorCooldown);
                        plugin.saveYaml();
                        aa.replaceStatus(p, 0);
                        aa.replaceLoreStatus(p, 0);
                       
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getC().getString("Messages.enchantmentsChanged")));
                        for(int i = 0; i < 28; i++)
                        {
                            if(names[i] != null)
                            {
                                String message = plugin.getC().getString("Messages.enchantmentsUpgraded").replaceAll("%ench%", names[i]).replaceAll("%level%", String.valueOf(stari[i])).replaceAll("%newlevel%", String.valueOf(novi[i]));
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
                            }
                            names[i] = null;
                        }
                        return true;
                    }
    [/B]
     
    ERROR
    Code (Text):

    org.bukkit.command.CommandException: Unhandled exception executing command 'vench' in plugin Special_Armor v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:909) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:902) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:819) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at me.venom.specarmor.Commands.EnchantCMD.onCommand(EnchantCMD.java:53) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            ... 17 more
    [/B]
     
     
  2. Strahan

    Benefactor

    Add plugin.getLogger().info("Test: " + ((plugin.getSledeciUse(p) == null)?"it's null":"not null")); to see if whatever that function is returning is null or not.

    Also that's an odd way to handle cooldowns. If a player has an item that has like a 30 second cooldown, when the player uses the item I'd store current time millis + 30000 somewhere. Then the next time they go to use it, check if the current time millis > the stored value. If so, wipe stored value and let them use item otherwise throw the "too soon". Much less complicated than all that you are doing up there.
     
    • Winner Winner x 1
  3. I think I understood what you wanted to say. I was stupid yea. I don't need to store cooldown in seconds (I just need it in code)...

    I think I fixed it (HOPEFULLY). But I cannot use this because using if(plugin.getSledeciUse(p) > System.currentTimeMillis()) drops an error. I still don't know why did it work previous time and not now...

    Code (Text):

    if(plugin.getSledeciUse(p) > System.currentTimeMillis())
                    {
                        long[] vrijeme = new long[3];
                        for(int i = 0; i < 3; i++)
                        {
                            vrijeme[i] = 0;
                        }
                        vrijeme[0] = (plugin.getSledeciUse(p) - System.currentTimeMillis()) / 1000;
                        while(vrijeme[0] >= 60)
                        {
                            vrijeme[1]++;
                            vrijeme[0] -= 60;
                            if(vrijeme[1] >= 60)
                            {
                                vrijeme[2]++;
                                vrijeme[1] = 0;
                            }
                        }
                        String message;
                        message = plugin.getC().getString("Messages.timerMessage").replace("%hours%", String.valueOf(vrijeme[2]).replace("%minutes%", String.valueOf(vrijeme[1]).replace("%seconds%", String.valueOf(vrijeme[0]))));
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
                        return true;
                    }
                    else
                    {
                        ItemStack hand = p.getInventory().getItemInMainHand();
                     
                        if(hand == null || hand.getEnchantments().size() <= 0)
                        {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getC().getString("Messages.enchanterNoItem")));
                            return true;
                        }
                        loadAllEnchs();
                        int y = 0;
                        status = 0;
                        for(int i = 0; i < 28; i++)
                        {
                            if(hand.getEnchantments().get(ench[i]) != null)
                            {
                                int level = hand.getEnchantmentLevel(ench[i]);
                                int max_level = ench[i].getMaxLevel();
                                if(max_level > level)
                                {
                                    hand.removeEnchantment(ench[i]);
                                    hand.addEnchantment(ench[i], level + 1);
                                    names[y] = ench[i].getKey().toString().substring(10, ench[i].getKey().toString().length());
                                    names[y] = StringUtils.capitalize(names[y]);
                                    stari[y] = level;
                                    novi[y] = hand.getEnchantmentLevel(ench[i]);
                                    y++;
                                    status = 1;
                                }
                            }
                        }
                        if(status == 0)
                        {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getC().getString("Messages.nothingChanged")));
                            return true;
                        }
                        long sledeciUse = System.currentTimeMillis() + (plugin.getC().getLong("Armors.EnchantArmor.Cooldown") * 1000);
                        plugin.removeSledeciUse(p);
                        plugin.putSledeciUse(p, sledeciUse);
                        //plugin.getPlayers().set("Players." + p.getUniqueId().toString() + ".EnchanterCooldown", armorCooldown);
                        //plugin.saveYaml();
                        aa.replaceStatus(p, 0);
                        aa.replaceLoreStatus(p, 0);
                     
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getC().getString("Messages.enchantmentsChanged")));
                        for(int i = 0; i < 28; i++)
                        {
                            if(names[i] != null)
                            {
                                String message = plugin.getC().getString("Messages.enchantmentsUpgraded").replaceAll("%ench%", names[i]).replaceAll("%level%", String.valueOf(stari[i])).replaceAll("%newlevel%", String.valueOf(novi[i]));
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
                            }
                            names[i] = null;
                        }
                        return true;
     
    @Strahan I fixed issue. (I was stupid Vol 2). Is this a good way to use cooldowns?
     
    #3 DragonLord__, Feb 17, 2020
    Last edited: Feb 17, 2020