Solved Preblems with adding to an int in an fileConfiguration

Discussion in 'Plugin Help/Development/Requests' started by Ungemonstert, Apr 13, 2015.

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

    Ungemonstert

    Hello,
    I try to code an clan plugin. The basic things I have completed. Now I wanna add the clan kills and clan death. I have written it like in every other plugin, but it won't work this time.

    My code:

    Code:
    public class LISTENER_respawn implements Listener
    {
        @EventHandler
        public void onRespawn(PlayerRespawnEvent e) throws IOException
        {
            Player death = e.getPlayer();
            World ww = toter.getWorld();
            World p = Bukkit.getWorld("PvP");
            if(ww == p)
            {
                Player killer = (Player) death.getKiller();
              
                //User file - killer
                File kuf = new File("plugins//MDC//userdata//"+p.getName()+".yml");
                FileConfiguration kucfg = YamlConfiguration.loadConfiguration(kuf);
                //Clan - killer
                String kclanName = kucfg.getString("Clan.Clan");
                //User file - death
                File tuf = new File("plugins//MDC//userdata//"+p.getName()+".yml");
                FileConfiguration tucfg = YamlConfiguration.loadConfiguration(tuf);
                //Clan - death
                String tclanName = tucfg.getString("Clan.Clan");
              
              
                addClanKill(killer, kclanName);
                kucfg.save(kuf);
              
                addClanDeath(toter, tclanName);
                tucfg.save(tuf);
            }
            else
            {
                return;
            }
        }
    public void addClanKill(Player p, String clanName)
        {
           
            File kcf = new File("plugins//Clans//clans//", clanName+".yml");
            FileConfiguration kccfg = YamlConfiguration.loadConfiguration(kcf);
          
           
            int kck = kccfg.getInt(clanName+".Kills");
          
           
            kccfg.set(clanName+".Kills", kck +1);
        }
      
        public void addClanDeath(Player p, String clanName)
        {
           
            File tcf = new File("plugins//Clans//clans//", clanName+".yml");
            FileConfiguration tccfg = YamlConfiguration.loadConfiguration(tcf);
          
           
            int tct = tccfg.getInt(clanName+".Death");
          
           
            tccfg.set(clanName+".Death", tct +1);
        }
    }
    Please help mee :(
     
    Last edited: Apr 13, 2015
  2. Offline

    I Al Istannen

  3. Dont compare objects with ==
    Use .equals

    Here is why:
    == compares objects by their memory location.
    .equals compares object by their content.
    Example:
    Code:java
    1. World w = Bukkit.getWorld("world");
    2. World w2 = Bukkit.getWorld("world");
    3. World w3 = w2;
    4.  
    5. System.out.println(w == w2);
    6. System.out.println(w.equals(w2));
    7. System.out.println(w2 == w3);
    8. System.out.println(w2.equals(w3));

    Output:
    Code:
    false
    true
    true
    true
     
    Last edited: Apr 13, 2015
  4. Offline

    I Al Istannen

    @FisheyLP Oh, i haven't even read that line. You should be right :p
     
  5. Offline

    Ungemonstert

    Thanks. I would test it in a few minutes. But I have written it excactly like this in my Scoreboard functions. There it works. Why not here?

    And sorry for my english, I'm from Germany :)

    @I Al Istannen
    No there isn't an error code.

    EDIT by Timtower: merged posts

    EDIT
    sorry Timtower ;)

    Okay. I created a plugin with only that one function. If I die, there is an error code:

    Code:
    [15:41:07] [Server thread/ERROR]: Could not pass event PlayerRespawnEvent to test v0.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.PlayerList.moveToWorld(PlayerList.java:497) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.PlayerList.moveToWorld(PlayerList.java:437) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1318) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.PacketPlayInClientCommand.a(SourceFile:31) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.PacketPlayInClientCommand.a(SourceFile:8) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:686) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:627) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:530) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
        at classes.LISTENER_respawn.onRespawn(LISTENER_respawn.java:57) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
        ... 16 more
    




    EDIT again

    My codes. All complete and with main etc:

    main:
    Code:
    package classes;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public class Main extends JavaPlugin {
     
        public void onEnable()
        {
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(new LISTENER_respawn(), this);
         
            System.out.print("======================================");
            System.out.print("           [Test] Enabled!            ");
            System.out.print("======================================");
        }
     
        public void onDisable()
        {
            System.out.print("=======================================");
            System.out.print("           [Test] Disabled!            ");
            System.out.print("=======================================");
        }
    }
    
    LISTENER_respawn
    Code:
    package classes;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerRespawnEvent;
    
    public class LISTENER_respawn implements Listener
    {
        @EventHandler
        public void onRespawn(PlayerRespawnEvent e)
        {
            Player toter = e.getPlayer();
            World ww = toter.getWorld();
            World p = Bukkit.getWorld("PvP");
            if(ww.equals(p))
            {
                Player killer = (Player) toter.getKiller();
             
                //User file - killer
                File kuf = new File("plugins//MDC//userdata//"+p.getName()+".yml");
                FileConfiguration kucfg = YamlConfiguration.loadConfiguration(kuf);
                //Clan - killer
                String kclanName = kucfg.getString("Clan.Clan");
                //User file - killer
                File tuf = new File("plugins//MDC//userdata//"+p.getName()+".yml");
                FileConfiguration tucfg = YamlConfiguration.loadConfiguration(tuf);
                //Clan - killer
                String tclanName = tucfg.getString("Clan.Clan");
             
             
                addClanKill(killer, kclanName);
                try {
                    kucfg.save(kuf);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
             
                addClanDeath(toter, tclanName);
                try {
                    tucfg.save(tuf);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
             
                toter.sendMessage("Du wurdest von §3"+killer.getName()+" §4getötet.");
                killer.sendMessage("Du hast §3"+toter.getName()+" §4getötet");
            }
            else
            {
                return;
            }
        }
     
        public static void addClanKill(Player p, String clanName)
        {
            //Clan file - killer
            File kcf = new File("plugins//Clans//clans//", clanName+".yml");
            FileConfiguration kccfg = YamlConfiguration.loadConfiguration(kcf);
         
            //get Clan kills - killer
            int kck = kccfg.getInt(clanName+".Kills");
         
            //set clan kills +1 - killer
            kccfg.set(clanName+".Kills", kck +1);
        }
     
        public static void addClanDeath(Player p, String clanName)
        {
            //Clan file - toter
            File tcf = new File("plugins//Clans//clans//", clanName+".yml");
            FileConfiguration tccfg = YamlConfiguration.loadConfiguration(tcf);
         
            //get Clan tode - toter
            int tct = tccfg.getInt(clanName+".Tode");
         
            //set clan tode +1 - toter
            tccfg.set(clanName+".Tode", tct +1);
        }
    }
    plugin.yml
    Code:
    name: test
    version: 0.1
    author: Ungemonstert
    
    main: classes.Main
    hope you can help me better now.
     
    Last edited: Apr 14, 2015
  6. Offline

    I Al Istannen

    @Ungemonstert Hallo, what is your line 57 in the Listener class? It says there is an NullPointerException.
     
  7. Offline

    Ungemonstert

    I Al Istannen Line 57 is
    Code:
    toter.sendMessage("Du wurdest von §3"+killer.getName()+" §4getötet.");
    Is it possible that it's an error, because the server can't send the message in the next line to an zombie?
     
  8. Offline

    I Al Istannen

    @Ungemonstert Oh. You cast the killer to a player. If that isnt a player thats bad. So check if the killer is a player before casting it to a player or sending a message.

    EDIT: You try to get the clan of the killer... Good luck getting the clan of zombie :p
     
  9. Offline

    Ungemonstert

    Oh okay thanks. That answers why it worked a few days ago :D
    But I think I get the clan of the zombie :D
    thanks for help :) I would test it now

    Edit:
    No error. Thank youu :)

    Edit:
    But one thing is incompleted. Why Doesn't add the server 1 to clanDeath?
     
    Last edited: Apr 14, 2015
  10. Offline

    I Al Istannen

    @Ungemonstert Does the config file exist? Try putting an System#out#println(clanname and other stuff you need to check) in the method to check if it gets called correct. And for your configs. The YAMLConfiguration.load() can throw several exceptions: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/configuration/file/FileConfiguration.html#load(java.io.File)

    EDIT: Right method link: https://hub.spigotmc.org/javadocs/b...ration.html#loadConfiguration(java.io.Reader)

    EDIT: And you don't want to use the player.getName() to save their playerdata. Use player.getUniqueId() for that.
     
    Last edited: Apr 14, 2015
  11. Offline

    Ungemonstert

    Okay. The fails were at the wrong variable using. I checked the clan in the user file of the world :D
    Now everything works succesfully. And with the UniqueID. I started to change it but I'm not ready yet.
    Thanks for all your help I Al Istannen :)
     
  12. Offline

    I Al Istannen

Thread Status:
Not open for further replies.

Share This Page