Need help with... OfflinePlayer I think

Discussion in 'Plugin Development' started by Markcrafter1, Sep 21, 2016.

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

    Markcrafter1

    Yo,

    Problem Class:
    Code:
    package commands;
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import p1.Main;
    public class Friend implements CommandExecutor{
      
        public static File file2 = new File("plugins//SoulForge","config");
        public static YamlConfiguration cfg2 = YamlConfiguration.loadConfiguration(file2);
        public static File file3 = new File("plugins//SoulForge//Friend","");
        public static YamlConfiguration cfg3 = YamlConfiguration.loadConfiguration(file3);
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("Friend")){
                boolean ak = cfg2.getBoolean("Einstellungen.Friend.Aktivieren");
                Player p = (Player) sender;
                if(ak == false){
                    p.sendMessage(Main.getInstance().prefix + "'Friend' wurde nicht aktiviert. Bitte aktiviere es in der Config.");
                    return true;
                }
              
            if(args.length == 0){
                p.sendMessage(Main.getInstance().prefixdebug + "=============== FRIEND ===============");
                p.sendMessage(Main.getInstance().prefixdebug + "§4§lFRIEND  IST IN ENTWICKLUNG");
                p.sendMessage(Main.getInstance().prefixdebug + "§4§lBITTE ALLE FEHLER AN M4RK4YY MELDEN");
                p.sendMessage(Main.getInstance().prefixdebug + "Für alle Befehle benutze /friend cmd");
                p.sendMessage(Main.getInstance().prefixdebug + "=============== FRIEND ===============");
                return true;  
            }
            if(args.length == 1){
                if(args[0].equalsIgnoreCase("add")){
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    UUID pID= UUID.fromString(target.getUniqueId().toString());
                    UUID PlayerID= UUID.fromString(p.getUniqueId().toString());
              
                if(cfg3.getString("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName()) == target.getName()){
                    p.sendMessage(Main.getInstance().prefixdebug + "Du hast diesen Spieler bereits als Freund.");
                    return true;
                  
                }
                    if(!target.hasPlayedBefore()){
                        p.sendMessage(Main.getInstance().prefixdebug + "§cDer Spieler muss mind. 1x gejoint sein und online sein.");
                        return true;
                    }
                    if(target.hasPlayedBefore()){
                        if(target.isOnline()){
                            cfg3.addDefault("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName());
                            cfg3.options().copyDefaults(true);
                            try {
                                cfg3.save(file3);
                            } catch (IOException e) {
                            }
                            return true;
                        }
                        if(!target.isOnline()){
                            p.sendMessage(Main.getInstance().prefixdebug + "Der Spieler muss online sein, damit du ihn hinzufügen kannst.");
                            return true;
                        }
                          
                          
                    }
                        p.sendMessage(Main.getInstance().prefixdebug + "Syntax: /friend add <NAME>");
                        return true;
                    }
              
                /*if(args[0].equalsIgnoreCase("delete")){
                    if(cfg2.c){
                      
                }
                }*/
              
                }
              
              
            }
          
          
            return true;
        }
    }
    Error in Console
    Code:
    [21:51:36 INFO]: M4rk4yy issued server command: /friend add
    [21:51:36 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'friend' in plugin SoulForge v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at commands.Friend.onCommand(Friend.java:43) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            ... 15 more
     
  2. Offline

    geomap

    Something in your Friends class on line 43 is returning Null.
     
  3. Offline

    Ste4lthPr0xy

    If you could post the main.java aswell, it would be way easyier to help you, because if ur refferences are on the main file, we cant really tell right away, what the exact problem is :)
     
  4. Offline

    Zombie_Striker

    This is most likely your issue. target can be null. You have to null check the target variable before using it.
     
    JanTuck likes this.
  5. Offline

    JanTuck

    You are trying to get player "add"
    As Zombie says remember to check if the player isvalid.
    Dont try to get something from the config before checking its there.

    Sent from Tapatalk
     
  6. Offline

    Mindlessmink

    Like @JanTuck said, You're doing:

    Code:java
    1. Player target = Bukkit.getPlayer(args[0]);

    Obviously that argument is already taken by another thing...
    Code:java
    1. if(args[0].equalsIgnoreCase("add")){

    You're using args[0] for 2 things. So, The Player you'd be returning is the Player whos name's "add". Change your target to args[1], As for the returning null error, What is your line 43?
     
  7. Offline

    JanTuck

    Right under he is getting string from config. Without checking if the section exists

    Code:java
    1.  
    2. Player target = Bukkit.getServer().getPlayer(args[0]);
    3. UUID pID= UUID.fromString(target.getUniqueId().toString());
    4. UUID PlayerID= UUID.fromString(p.getUniqueId().toString());
    5.  
    6. if(cfg3.getString("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName()) == target.getName()){
    7. p.sendMessage(Main.getInstance().prefixdebug + "Du hast diesen Spieler bereits als Freund.");
    8. return true;
    9.  
    10. }
    11.  


    This is what i ment if the config section doesn't exists it will return null.

    @Markcrafter1
    Sent from Tapatalk
     
    Last edited: Sep 23, 2016
  8. Offline

    Markcrafter1

    Sorry for answering so late, I thought I get Email notification.

    Main:

    Code:
    package p1;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import commands.Friend;
    import commands.PlayerInfo;
    import commands.Vote;
    import listener.DeathListener;
    import listener.InDerTat;
    import listener.Reload;
    
    public class Main extends JavaPlugin{
    
        public static ArrayList<String> Login = new ArrayList<String>();
     
        public static File ordner = new File("plugins//SoulForge");
        public static File ordner2 = new File("plugins//SoulForge//Friend");
        public static File ordner3 = new File("plugins//SoulForge//CMDLog");
        public static File file = new File("plugins//SoulForge//CMDLog","Log");
        public static YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
        public static File file2 = new File("plugins//SoulForge","config");
        public static YamlConfiguration cfg2 = YamlConfiguration.loadConfiguration(file2);
        public static File file3 = new File("plugins//SoulForge//Friend","Friends");
        public static YamlConfiguration cfg3 = YamlConfiguration.loadConfiguration(file3);
     
        public static String prefix = "§6SoulForge §7>> §3";
        public static String prefixdebug = "§6SF-Debug §7>> §3";
     
        public static Inventory inv = Bukkit.createInventory(null, 9, "§3Freunde");
         
        private static Main instance;
     
        public void onLoad(){
            Main.instance = this;
        }
     
        public static Plugin plugin;
     
        public void onDisable(){
            getLogger().info("Soulforge wurde deaktiviert");
        }
     
        public void onEnable(){
            setcfg();
            this.plugin = this;
         
            getLogger().info("Soulforge wurde aktiviert");
         
            if(!ordner.exists()){
                ordner.mkdirs();
            }
         
            if(!ordner2.exists()){
                ordner2.mkdirs();
            }
            if(!ordner3.exists()){
                ordner3.mkdirs();
            }
         
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                }
            }
         
            if(!file2.exists()){
                try {
                    file2.createNewFile();
                } catch (IOException e) {
                }
            }
         
            if(!file3.exists()){
                try {
                    file3.createNewFile();
                } catch (IOException e) {
                }
            }
         
         
         
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(new Reload(), this);
            pm.registerEvents(new InDerTat(), this);
            pm.registerEvents(new DeathListener(), this);
         
    //================ CMDRegister Ende   ================
            this.getCommand("pinfo").setExecutor(new PlayerInfo());
            this.getCommand("vote").setExecutor(new Vote());
            this.getCommand("friend").setExecutor(new Friend());
         
         
        }
    
        public static Main getInstance(){
            return Main.instance;
        }
     
        public void setcfg(){
            cfg2.addDefault("Einstellungen.Vote.Votelink1", "");
            cfg2.options().copyDefaults(true);
            try {
                cfg2.save(file2);
            } catch (IOException e) {
            }
         
            cfg2.addDefault("Einstellungen.Vote.Votelink2", "");
            cfg2.options().copyDefaults(true);
            try {
                cfg2.save(file2);
            } catch (IOException e) {
            }
         
            cfg2.addDefault("Einstellungen.Vote.Votelink3", "");
            cfg2.options().copyDefaults(true);
            try {
                cfg2.save(file2);
            } catch (IOException e) {
            }
         
            cfg2.addDefault("Einstellungen.Friend.Aktivieren", false);
            cfg2.options().copyDefaults(true);
            try {
                cfg2.save(file2);
            } catch (IOException e) {
            }
        }
    
    }
    
    @Mindlessmink I rewrote my Friend.java to this:

    Code:
    if(args.length == 1){
                if(args[0].equalsIgnoreCase("add")){
                    if(Bukkit.getServer().getPlayer(args[1]).getName() == ""){
                        p.sendMessage(Main.prefixdebug + "Spieler " + args[1] + " ist entweder nicht");
                        p.sendMessage(Main.prefixdebug + "online oder war noch nie auf dem Server.");
                        return true;
                    }
                  
            //    Player target =
                UUID pID= UUID.fromString(Bukkit.getServer().getPlayer(args[1]).getUniqueId().toString());
                UUID PlayerID= UUID.fromString(p.getUniqueId().toString());
              
              
              
              
                if(cfg3.getString("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName()) == Bukkit.getServer().getPlayer(args[1]).getName()){
                    p.sendMessage(Main.getInstance().prefixdebug + "Du hast diesen Spieler bereits als Freund.");
                    return true;
                }
              
                if(!Bukkit.getServer().getPlayer(args[1]).hasPlayedBefore()){
                    p.sendMessage(Main.getInstance().prefixdebug + "§cDer Spieler muss mind. 1x gejoint sein und online sein.");
                    return true;
                }
              
                if(!Bukkit.getServer().getPlayer(args[1]).isOnline()){
                    p.sendMessage(p1.Main.prefixdebug + "Der Spieler muss online sein.");      
                    return true;
                }
              
                if(Bukkit.getServer().getPlayer(args[1]).hasPlayedBefore()){
                    if(Bukkit.getServer().getPlayer(args[1]).isOnline()){
    
                        cfg3.addDefault("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName());
                        cfg3.options().copyDefaults(true);
                        try {
                            cfg3.save(file3);
                        } catch (IOException e) {
                        }
                        return true;
                    }
                      
                      
                }
                    p.sendMessage(Main.getInstance().prefixdebug + "Syntax: /friend add <NAME>");
                    return true;
                }
            }
            }
                      
            return true;
    
    Still doesn't work.
     
    Last edited: Sep 24, 2016
  9. Offline

    JanTuck

    @Markcrafter1
    As i said you arent checking if the config section is there.
    Code:java
    1.  
    2. if(cfg3.getString("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName()) == Bukkit.getServer().getPlayer(args[1]).getName()){
    3. p.sendMessage(Main.getInstance().prefixdebug + "Du hast diesen Spieler bereits als Freund.");
    4. return true;
    5. }
    6.  

    You could check by doing something like
    Code:java
    1.  
    2. if(cfg3.contains("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName())){
    3. if(cfg3.getString("Friends." + p.getName() + " : " + PlayerID, pID + " : " + p.getName()) == Bukkit.getServer().getPlayer(args[1]).getName()){
    4. p.sendMessage(Main.getInstance().prefixdebug + "Du hast diesen Spieler bereits als Freund.");
    5. return true;
    6. }
    7.  



    Also please post your stacktrace(Error)
     
    Last edited: Sep 24, 2016
  10. Offline

    Markcrafter1

    Code:
    [20:44:56 INFO]: M4rk4yy issued server command: /friend add
    [20:44:56 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'friend' in plugin SoulForge v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at commands.Friend.onCommand(Friend.java:46) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Spigot-5391d73-0ebb9c7]
            ... 15 more
    
    
     
  11. Offline

    Zombie_Striker

    @Markcrafter1
    The error states that you are trying to get an object at an index that is greater than the size of the array.
    The thing is, you never provide an "args[1]". You never provide anything past args[0].

    To fix this, make sure there are atleast two args before trying to get the second arg.
     
Thread Status:
Not open for further replies.

Share This Page