Solved [HELP] NPE WRONG SECTION (SORRY)

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

Thread Status:
Not open for further replies.
  1. Okay so, I am making a plugin and I am using HashMap to get cooldowns for players. I have 2 questions. First is: does HashMap containing playerUUID gets removed (I mean does player with that UUID gets removed) when he leaves the server?
    Second question:
    Code (Text):

    long currentMillis = System.currentTimeMillis();
                    long playerCooldown = plugin.getPlayers().getLong("Players." + p.getUniqueId() + ".EnchanterCooldown");
                    long armorCooldown = plugin.getC().getInt("Armors.EnchantArmor.Cooldown");
                    long sledeciUse = 0;
                    long[] vrijeme = new long[3];
     
    This is part of code. When I try to playerCooldown = plugin.getSledeciUse(p) - currentMillis; it throws an error. When I do THE SAME THING 5 lines below (inside if statement) it just does it... I need playerCooldown to be changed here because if not, cooldown will go to - and player would need to write command one more time so he can use it
    Code (Text):

    null
    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]
     
     
    #1 DragonLord__, Feb 17, 2020
    Last edited: Feb 17, 2020
  2. Strahan

    Benefactor

    Only if you do it. It doesn't happen automatically. You need to listen to the PlayerQuitEvent and handle it.

    As to the rest, post your EnchantCMD and mark line 53. The whole class. Also you are using p.getUniqueId() when you should be using p.getUniqueId().toString().

    Also if you are worrying about a cooldown going negative you aren't implement cooldowns right. All you need do is when the player does something that involves a cooldown, mark current time + whatever amount of cooldown seconds is required. Then when the action is performed that is subject to cooldown, you check if current time >= the saved cooldown expiration. If it is, you remove the saved expiration and perform action. If not, you bitch at the player. Negatives never come into it, as you don't need to be subtracting anything.
     
  3. I will fix getUniqueID, thank u

    Code (Text):

    long currentMillis = System.currentTimeMillis();
                    //long playerCooldown = plugin.getPlayers().getLong("Players." + p.getUniqueId() + ".EnchanterCooldown");
                    long armorCooldown = plugin.getC().getInt("Armors.EnchantArmor.Cooldown");
                    long sledeciUse = 0;
                    long playerCooldown = plugin.getSledeciUse(p) - currentMillis; //Line with error
                    long[] vrijeme = new long[3];
                    if(playerCooldown >= 1)
                    {
                        sledeciUse = plugin.getSledeciUse(p);
                        playerCooldown = (sledeciUse - currentMillis) / 1000; //Same thing is done here as up there... I could use a bit of understanding how to create good cooldowns
                        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() + ".PlayerName", p.getName());
                        plugin.getPlayers().set("Players." + p.getUniqueId() + ".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() + ".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;
                    }
     
    The whole thing that does things. This is where I "handle" cooldowns. I am pretty new to cooldown things.
     
    #3 DragonLord__, Feb 17, 2020
    Last edited: Feb 17, 2020
Thread Status:
Not open for further replies.