adding players to data.yml

Discussion in 'Plugin Development' started by rhunter, Oct 17, 2016.

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

    rhunter

    Hey im making a minigame plugin and i'm trying to make it so when people do /dungeon join, they are put into a data.yml (i used data.yml because i couldn't figure out how to do it with an arraylist) and then once there are 2 people in it, they are supposed to teleport to the set point.
    Here is my code for the join part:
    Code:
    if(args[0].equalsIgnoreCase("join")){
                        Player player = (Player) sender;
                        List<String> lobby = settings.getConfig().getStringList("inLobby");
                        if(!(lobby.contains(player.getName()))){
                        lobby.add(player.getName());
                        getConfig().set("players-in-lobby", lobby);
                        saveConfig();
                        player.sendMessage(ChatColor.GREEN+"Joined the RPG Dungeon lobby. The game will start when another player joins, so be ready to be teleported away!");
                        }else{
                            player.sendMessage(ChatColor.RED+"You are already in the lobby!");
                        }
                    }
                }
    I get an internal error when I use the command, but it still sends the player a message:
    Code:
    [22:42:37 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dungeon' in plugin RPG_Dungeon v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at org.bukkit.command.SimpleCommandM
    ap.dispatch(SimpleCommandMap.java:141) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:647) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1358) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1193) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45-internal]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45-internal]
    at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45-internal]
    Caused by: java.lang.NullPointerException
    at me.reece.dungeon.Main.onCommand(Main.java:89) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    ... 15 more
     
  2. Offline

    Lordloss

    You should really learn how to use a list, it is much more simple than what you are doing there. Please read the link in my signature for solving this stacktrace.
     
  3. Offline

    rhunter

    I used an arraylist first but it wouldnt let me add playernames to it. Can you rxplain how i would do so?
     
  4. Offline

    Zombie_Striker

    @rhunter
    Please click the link in @Lordloss 's signature to fix your error.

    Something is null at line 89. Since you did not post your full class, we have no idea which line is line 89. Please post line 89, and try to figure out what can be null.
     
  5. Offline

    rhunter

    I changed it to an arraylist and here is the code:
    Code:
    if(args[0].equalsIgnoreCase("join")){
                        Player player = (Player) sender;
                        List<String> lobby = settings.getConfig().getStringList("inLobby");
                        if(!(lobby.contains(player.getName()))){
                        lobby.add(player.getName());
                        player.sendMessage(ChatColor.GREEN+"Joined the RPG Dungeon lobby. The game will start when another player joins, so be ready to be teleported away!");
                        }else{
                            player.sendMessage(ChatColor.RED+"You are already in the lobby!");
                        }
                    }
                }
             
                    if(lobby.size()==2){
                        Player players = lobby.toString();
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn." + ".world"));
                        double x = settings.getData().getDouble("spawn." + ".x");
                        double y = settings.getData().getDouble("spawn." + ".y");
                        double z = settings.getData().getDouble("spawn." + ".z");
                        Location spawn = new Location(w,x,y,z);
                        players.teleport(spawn);
                        }
    Now my error is lobby.getString(); cannot convert String to player, but if I change it to String players = lobby.toString(); then it says teleport is undefined for the type string.

    EDIT: Heres all the code:
    Code:
    public class Main extends JavaPlugin implements Listener{
            SettingsManager settings = SettingsManager.getInstance();  
            protected ArrayList<Player> lobby;
            protected ArrayList<String> togglewither;
            public void onEnable(){
                this.togglewither = new ArrayList<String>();
                settings.setup(this);
                getServer().getPluginManager().registerEvents(this, this);}
          
            public void onDisable(){}
          
            //COMMANDS
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if(cmd.getName().equalsIgnoreCase("dungeon")){
                    if(args.length==0){
                        Player player = (Player) sender;
                        player.sendMessage(ChatColor.RED+"Usage: /dungeon set spawn, /dungeon set (mob) (amount) (spawnpoint name)");
                    }
                    if(args[0].equalsIgnoreCase("set")){
                        if(args[1].equalsIgnoreCase("spawn")){
                        Player player = (Player) sender;
                        settings.getData().set("spawn.w", player.getLocation().getWorld().getName());
                        settings.getData().set("spawn.x", player.getLocation().getX());
                        settings.getData().set("spawn.y", player.getLocation().getY());
                        settings.getData().set("spawn.z", player.getLocation().getZ());
                        settings.saveData();
                        player.sendMessage(ChatColor.GREEN + "Dungeon spawn set!");
                        return true;
                        }
                        if(args.length==4){ //dungeon set (mob) (amount) (spawnpoint name)
                                String mob = args[1];
                                int amount = Integer.parseInt(args[3]);
                                String spawnpointName = args[2];
                                Player player = (Player) sender;
                                settings.getData().set(spawnpointName+".w", player.getLocation().getWorld().getName());
                                settings.getData().set(spawnpointName+".x", player.getLocation().getX());
                                settings.getData().set(spawnpointName+".y", player.getLocation().getY());
                                settings.getData().set(spawnpointName+".z", player.getLocation().getZ());
                                settings.getData().set(spawnpointName+".amount", mob);
                                settings.saveData();
                                player.sendMessage(ChatColor.GREEN+mob+" spawnpoint "+spawnpointName+" set and will spawn "+amount+" "+mob+"s");
                        }
                    }
                    if(args[0].equalsIgnoreCase("join")){
                        Player player = (Player) sender;
                        if(!(lobby.contains(player.getName()))){
                        lobby.add(player.getName());
                        player.sendMessage(ChatColor.GREEN+"Joined the RPG Dungeon lobby. The game will start when another player joins, so be ready to be teleported away!");
                        }else{
                            player.sendMessage(ChatColor.RED+"You are already in the lobby!");
                        }
                    }
                }
              
                for(Player players : Bukkit.getServer().getOnlinePlayers()){
                    //if(lobby.size()==2){
                    if(lobby.contains(players.getName())){
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn." + ".world"));
                        double x = settings.getData().getDouble("spawn." + ".x");
                        double y = settings.getData().getDouble("spawn." + ".y");
                        double z = settings.getData().getDouble("spawn." + ".z");
                        Location spawn = new Location(w,x,y,z);
                        players.teleport(spawn);
                        }
                }
     
    Last edited: Oct 18, 2016
  6. Offline

    Zombie_Striker

    @rhunter
    That is not how arrays work. Do the following instead:
    1. Create a for-each loop to loop through all the strings in the "lobby" array.
    2. For each string, use Bukkit.getPlayer(STRING) to get the player with that name. Remember to null check the player, as this will return a null value if the player is not online.
    3. Using the player from #2, teleport the player to that new location.
    Remember, only create the WXYZ values once, so don't put them inside the for-each loop.

    [EDIT]
    This should not work. Unless you have a black value in your path (which means your YML is bad), you either added an extra period or are forgetting to add something between these two strings. Fix this to match your YML.
     
  7. Offline

    rhunter

    I think I understood all that. Heres the new piece of code:
    Code:
    for(String players : lobby){
                    if(Bukkit.getPlayer(players) != null){
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn." + ".world"));
                        double x = settings.getData().getDouble("spawn." + ".x");
                        double y = settings.getData().getDouble("spawn." + ".y");
                        double z = settings.getData().getDouble("spawn." + ".z");
                        Location spawn = new Location(w,x,y,z);
                        players.teleport(spawn);
                        }
                }
    but now there is an error on players.teleport(spawn);. It says teleport is undefined for the type String.

    EDIT: Sorry for being noob
     
    Last edited: Oct 18, 2016
  8. Offline

    Zombie_Striker

    @rhunter
    Yes. That is what I meant.

    If it works, mark this thread as solved. If not, post your updated code below.
     
  9. Offline

    rhunter

    teleport is undefined for the type String
     
  10. Offline

    Zombie_Striker

    @rhunter
    You have to turn the String into a player using the getPlayer method.
     
  11. Offline

    rhunter

    Console just tells me there is an error in line 91 and I don't know why.
    Code:
    public class Main extends JavaPlugin implements Listener{
            SettingsManager settings = SettingsManager.getInstance();   
            ArrayList<String> lobby = new ArrayList<String>();
            protected ArrayList<String> togglewither;
            public void onEnable(){
                this.togglewither = new ArrayList<String>();
                settings.setup(this);
                getServer().getPluginManager().registerEvents(this, this);}
           
            public void onDisable(){}
           
            //COMMANDS
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if(cmd.getName().equalsIgnoreCase("dungeon")){
                    if(args.length==0){
                        Player player = (Player) sender;
                        player.sendMessage(ChatColor.RED+"Usage: /dungeon set spawn, /dungeon set (mob) (amount) (spawnpoint name)");
                    }
                    if(args[0].equalsIgnoreCase("set")){
                        if(args[1].equalsIgnoreCase("spawn")){
                        Player player = (Player) sender;
                        settings.getData().set("spawn.world", player.getLocation().getWorld().getName());
                        settings.getData().set("spawn.x", player.getLocation().getX());
                        settings.getData().set("spawn.y", player.getLocation().getY());
                        settings.getData().set("spawn.z", player.getLocation().getZ());
                        settings.saveData();
                        player.sendMessage(ChatColor.GREEN + "Dungeon spawn set!");
                        return true;
                        }
                        if(args.length==4){ //dungeon set (mob) (amount) (spawnpoint name)
                                String mob = args[1];
                                int amount = Integer.parseInt(args[3]);
                                String spawnpointName = args[2];
                                Player player = (Player) sender;
                                settings.getData().set(spawnpointName+".w", player.getLocation().getWorld().getName());
                                settings.getData().set(spawnpointName+".x", player.getLocation().getX());
                                settings.getData().set(spawnpointName+".y", player.getLocation().getY());
                                settings.getData().set(spawnpointName+".z", player.getLocation().getZ());
                                settings.getData().set(spawnpointName+".amount", mob);
                                settings.saveData();
                                player.sendMessage(ChatColor.GREEN+mob+" spawnpoint "+spawnpointName+" set and will spawn "+amount+" "+mob+"s");
                        }
                    }
                    if(args[0].equalsIgnoreCase("join")){
                        Player player = (Player) sender;
                        if(!(lobby.contains(player.getName()))){
                        lobby.add(player.getName());
                        player.sendMessage(ChatColor.GREEN+"Joined the RPG Dungeon lobby. The game will start when another player joins, so be ready to be teleported away!");
                        }else{
                            player.sendMessage(ChatColor.RED+"You are already in the lobby!");
                        }
                    }
                }
               
                for(String players : lobby){
                    if(Bukkit.getPlayer(players) != null){
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
                        double x = settings.getData().getDouble("spawn.x");
                        double y = settings.getData().getDouble("spawn.y");
                        double z = settings.getData().getDouble("spawn.z");
                        Location spawn = new Location(w,x,y,z);
                        Player p = Bukkit.getPlayer(players);
                        p.teleport(spawn);
                        p.sendMessage(ChatColor.GREEN+"The game has started!");
                        }
                }
    line 91:
    Code:
    World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
     
  12. Offline

    Zombie_Striker

    @rhunter
    There is most likely more to that error. Is it an NPE? If NPE, either the Data is null or the String from the config is null. Debug to figure out what is null.
     
  13. Offline

    rhunter

    Console is telling me that "name cannot be null". I don't know what to change it to since I was following your instructions for this part.
    Code:
    [22:39:03 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dungeon' in plugin RPG_Dungeon v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:647) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1358) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1193) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45-internal]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45-internal]
    at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45-internal]
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
    at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getWorld(CraftServer.java:1020) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
    at me.reece.dungeon.Main.onCommand(Main.java:93) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[gbinvl_minecraft_spigot_1_10_2.jar:git-Spigot-570526c-c1b529e]
     
  14. Offline

    Lordloss

    I really dont want to be rude, but i have to tell you something here. You have to learn a lot more basic java. You need to learn the principles of object orientation to fully understand what is going on. Here are some points you really must know:
    - Differenciate between objects and primitive values like integer, double, boolean.
    - using methods correctly. Which methods are offered by a specific object? Which arguments do they need to work? Which kind of objects you get back from it as a return value? You tried to call the teleport method of a String, which it obviously dont has.
    - Exceptions. Learn what causes specific kinds of exceptions like the nullPointer you got. What means null? What exactly is null? Most important, why is it null?
     
    Zombie_Striker likes this.
  15. Offline

    Zombie_Striker

    @rhunter
    Although, as @Lordloss is saying, you should better familiarize yourself with Java before working on Bukkit, the issue currently only has to deal with the inability to read logs. To fix that, please read this thread.

    The error is saying the name you provided on that line is null. That means that the string returned from the config is null. Please make sure that the path does exist before you get the world name.
     
Thread Status:
Not open for further replies.

Share This Page