Clear a Players Info from a Config

Discussion in 'Plugin Development' started by Icelaunche, Jan 28, 2014.

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

    Icelaunche

    I am trying to reset the number of warnings a player got from the config.

    Code:java
    1. }
    2. if(label.equalsIgnoreCase("scc")){
    3. if(args.length == 1){
    4. Player tgtPlayer = player.getServer().getPlayer(args[0]);
    5. int warnings = plugin.getConfig().getInt("warnings" + "." + tgtPlayer);
    6. plugin.getConfig().set("warnings" + "." + tgtPlayer, warnings *0);
    7. player.sendMessage(ChatColor.GREEN + "[SafeChat]You Have cleared" + tgtPlayer + "'s warnings");
    8.  
    9. }


    That is what I have. I get an error for line 34 which is
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings" + "." + tgtPlayer);


    The config looks like:
    Code:
    # SafeChat Config
    # Plugin made by Icelaunche
    # What words are banned?
    bannedwords:
    - words
    - words
     
    alert: Please do not use that language
    warningstokick: 3
    warnings:
      CraftPlayer{name=Icelaunche}: 2
    
    Thanks in advance.
     
  2. Offline

    WizKhalifa

    Check your parameters. Pretty sure you have the wrong syntax.
     
  3. Offline

    Icelaunche

    The plugin works fine when it adds players and warnings to the config. I only get the error when i use the command /scc to clear a players errors.
     
  4. Offline

    WizKhalifa

    What's the error message?
     
  5. Offline

    Icelaunche

    The Important Part:

    Code:
    Caused by: java.lang.NullPointerException
        at me.Icelaunche.SafeChat.CommandClass.onCommand(CommandClass.java:34) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Full Error:

    Code:
    [20:24:21] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'scc' in plugin SafeChat v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at me.Icelaunche.SafeChat.CommandClass.onCommand(CommandClass.java:34) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 13 more
    [20:24:27] [Server thread/INFO]: Icelaunche issued server command: /scc Icelaunche
    [20:24:27] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'scc' in plugin SafeChat v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at me.Icelaunche.SafeChat.CommandClass.onCommand(CommandClass.java:34) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 13 more
    
     
  6. Offline

    rangersmash

    Replace player.getServer() to Bukkit.getServer()?
    Also check that tgtPlayer isn't null
     
  7. Offline

    Icelaunche

    That moved the error down to line 39.
    Line 39:
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings" + "." + tgtPlayer);



    New Code:
    Code:java
    1. if(label.equalsIgnoreCase("scc")){
    2. if(args.length == 1){
    3. Player tgtPlayer = Bukkit.getServer().getPlayer(args[0]);
    4. if(tgtPlayer == null){
    5. player.sendMessage(ChatColor.DARK_RED + "[SafeChat]Player not Online");
    6.  
    7. }else{
    8. int warnings = plugin.getConfig().getInt("warnings" + "." + tgtPlayer);
    9. plugin.getConfig().set("warnings" + "." + tgtPlayer, warnings *0);
    10. player.sendMessage(ChatColor.GREEN + "[SafeChat]You Have cleared" + tgtPlayer + "'s warnings");
    11. }
    12.  
    13. }
    14.  
    15. return true;
    16. }
     
  8. Offline

    Icelaunche

    Bump. Anyone know how to do this?
     
  9. Icelaunche
    This is wrong:
    Code:java
    1. plugin.getConfig().set("warnings" + "." + tgtPlayer, warnings *0);


    Instead try
    Code:java
    1. plugin.getConfig().set("warnings." + tgtPlayer, 0);
    2. plugin.saveConfig();


    No need to multiply the amount by 0. You also forgot to save the file ;)
     
  10. Offline

    Icelaunche

    The Gaming Grunts
    I still get the NullPointerException on line 40:
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings." + tgtPlayer);

    Whole Thing:
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings." + tgtPlayer);
    2. plugin.getConfig().set("warnings." + tgtPlayer, 0);
    3. plugin.saveConfig();
    4. player.sendMessage(ChatColor.GREEN + "You Have cleared" + tgtPlayer + "'s warnings");
     
  11. Icelaunche
    Add a null check & make sure the path is correct. Can I see what your config looks like?
     
  12. Offline

    Icelaunche

    The Gaming Grunts
    Here is most of the config (Copied and pasted form the first post). Nothing has changed with the warning methods:
    The config looks like:
    Code:
    # SafeChat Config
    # Plugin made by Icelaunche
    # What words are banned?
    bannedwords:
    - words
    - words
     
    alert: Please do not use that language
    warningstokick: 3
    warnings:
      CraftPlayer{name=Icelaunche}: 2
    
    And I already have a null check.
    Code:java
    1. if(args.length == 1){
    2.  
    3. Player tgtPlayer = Bukkit.getServer().getPlayer(args[0]);
    4.  
    5. if(tgtPlayer == null){
    6.  
    7. player.sendMessage(ChatColor.DARK_RED + "Player not Online");
    8.  
     
  13. Icelaunche
    You are not storing the player's name correctly:
    Code:
      CraftPlayer{name=Icelaunche}: 2
    It should simply be:
    Code:
    Icelaunche: 2
    In your code, you get the player, but you never get their name:
    Code:java
    1. Player tgtPlayer = Bukkit.getServer().getPlayer(args[0]);


    That is the reason why you are getting an NPE. It is looking for something (the player's name) and can't find it. So, when you are setting and calling info in the config, use tgtPlayer.getName()
     
  14. Offline

    Icelaunche

    The Gaming Grunts
    Ok, I fixed the CraftPlayer thing no now the config looks like this:
    Code:
    # SafeChat Config
    # Plugin made by Icelaunche
    # What words are banned?
    bannedwords:
    - yoloswaggar
    alert: Please do not use that language
    warningstokick: 3
    warnings:
      Icelaunche: 2
    But I still get the NullPointer on line 40:
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings." + tgtPlayer);
     
  15. Icelaunche
    I don't see why that would be throwing an error. Does the config file actually exist?
     
  16. Offline

    xTigerRebornx

    Icelaunche You need to be using tgtPlayer.getName() instead of tgtPlayer, otherwise it will get a null value from the path
     
  17. Offline

    Icelaunche

    The Gaming Grunts
    Yes and the rest of plugin works.

    xTigerRebornx
    I made them all tgtPlayer.getName() and this is what it looks like:
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings." + tgtPlayer.getName());
    2.  
    3. plugin.getConfig().set("warnings." + tgtPlayer.getName(), 0);
    4.  
    5. plugin.saveConfig();
    6.  
    7. player.sendMessage(ChatColor.GREEN + "[SafeChat] You Have cleared" + tgtPlayer.getName() + "'s warnings");

    And I still get the Null Pointer on line 40. The IDE is not showing any errors so I don't think there is a typo.

    Line 40:
    Code:java
    1. int warnings = plugin.getConfig().getInt("warnings." + tgtPlayer.getName());
     
  18. Offline

    xTigerRebornx

    Icelaunche I believe you are getting the NPE because the tgtPlayer may not be online, just use the String you get from the args and check if it has any warnings
     
  19. Offline

    Icelaunche

    xTigerRebornx
    When I am testing the command I use it on myself. I have a null check which works and does not throw any errors.
     
  20. Offline

    diage

    Have you checked to make sure 'plugin' is set correctly and the the .getConfig is returning the correct config?
     
  21. Offline

    Icelaunche

    diage
    I only have one config and the warning system (auto logs warnings into the config) is working.
     
Thread Status:
Not open for further replies.

Share This Page