Solved player.getWorld() returns incorrect world

Discussion in 'Plugin Development' started by CyberSoul, Nov 17, 2015.

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

    CyberSoul

    I am working on a command that just sets a spawnpoint and/or adds the current location to the config.yml. If you are in the wrong world (ex. Overworld/Nether) then it will send you a message otherwise it will do the command. However, even if you are in the correct world it sends you the fail message. Here is the command:

    Code:
    if(cmd.getName().equalsIgnoreCase("setnetherspawn") && sender instanceof Player) {
              
                Player player = (Player) sender;
              
                if (!player.hasPermission("setNether.allowed")) {
                  
                    player.sendMessage(ChatColor.RED + "You don't have permission to run that command!");
                  
                } else {
                  
                    int length = args.length;
                  
                    if (length == 1) {
                      
                        if (args[0].equalsIgnoreCase("spawn")) {
                          
                            if (player.getWorld().equals("world_nether")) {
                              
                                Location spawnloc = player.getLocation();
                                World world = Bukkit.getWorld("world_nether");
                              
                                world.setSpawnLocation(spawnloc.getBlockX(), spawnloc.getBlockY(), spawnloc.getBlockZ());
                                getConfig().addDefault("netherspawn", spawnloc);
                              
                                player.sendMessage(ChatColor.GOLD + "You have set your current location to the nether spawn.");
                                return true;
                              
                            } else {
                              
                                player.sendMessage(ChatColor.RED + "You must be in the nether to set the nether spawn!");
                                return true;
                              
                            }
                          
                        } else if (args[0].equalsIgnoreCase("exit")) {
                          
                            if (player.getWorld().equals("world")) {
                              
                                Location spawnloc = player.getLocation();
                              
                                getConfig().addDefault("netherexit", spawnloc);
                              
                                player.sendMessage(ChatColor.GOLD + "You have set your current location to the nether exit.");
                                return true;
                              
                            } else {
                              
                                player.sendMessage(ChatColor.RED + "You must be in the overworld to set the nether exit.");
                                return true;
                              
                            }
                          
                        } else {
                          
                            player.sendMessage(ChatColor.RED + "Invalid argument(s)!");
                            return true;
                          
                        }
                      
                    } else {
                      
                        player.sendMessage(ChatColor.RED + "/setnetherspawn <spawn/exit>");
                        return true;
                      
                    }
              
                }
                  
            }
    
    By the way, I type "/setnetherspawn exit" while in the overworld and it outputs: "You must be in the overworld to set the nether exit."
     
    Last edited: Nov 17, 2015
  2. Offline

    Koobaczech

    Hey man try adding .getName() to the world finder. This is because
    public World getWorld(); returns a world entity not a string name. Check out the bukkit online documents for more info or the function declaration in an IDE!
    Code:
    //debug only
    buckkit.broadcastMessage("Player world is "+player.getWorld().getName());
    
    //Working code
    if (player.getWorld().getName().equals("Blah blah"))
    //Then do something
    
     
    Last edited: Nov 17, 2015
  3. Offline

    CyberSoul

    even after adding .getName() it still poses the same problem, but thanks for trying
     
  4. Offline

    Koobaczech

    Print out your world names, make sure they match your statement names. You can also try
    Code:
    //0=main world and 1=nether..MAYBE?? TRY IT
    if (player.getWorld() == getServer().getWorlds().get(1));
     
  5. Offline

    Scimiguy

    @CyberSoul
    It should work if you grab the name
    Show your code with the getname method
     
  6. Offline

    CyberSoul

    Code:
    if(cmd.getName().equalsIgnoreCase("setnetherspawn") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
                if (!player.hasPermission("setNether.allowed")) {
                   
                    player.sendMessage(ChatColor.RED + "You don't have permission to run that command!");
                   
                } else {
                   
                    int length = args.length;
                   
                    if (length == 1) {
                       
                        if (args[0].equalsIgnoreCase("spawn")) {
                           
                            if (player.getWorld().getName().equals("world_nether")) {
                               
                                Location spawnloc = player.getLocation();
                                World world = Bukkit.getWorld("world_nether");
                               
                                world.setSpawnLocation(spawnloc.getBlockX(), spawnloc.getBlockY(), spawnloc.getBlockZ());
                                getConfig().addDefault("netherspawn", spawnloc);
                               
                                player.sendMessage(ChatColor.GOLD + "You have set your current location to the nether spawn.");
                                return true;
                               
                            } else {
                               
                                player.sendMessage(ChatColor.RED + "You must be in the nether to set the nether spawn!");
                                return true;
                               
                            }
                           
                        } else if (args[0].equalsIgnoreCase("exit")) {
                           
                            if (player.getWorld().getName().equals("world")) {
                               
                                Location spawnloc = player.getLocation();
                               
                                getConfig().addDefault("netherexit", spawnloc);
                               
                                player.sendMessage(ChatColor.GOLD + "You have set your current location to the nether exit.");
                                return true;
                               
                            } else {
                               
                                player.sendMessage(ChatColor.RED + "You must be in the overworld to set the nether exit.");
                                return true;
                               
                            }
                           
                        } else {
                           
                            player.sendMessage(ChatColor.RED + "Invalid argument(s)!");
                            return true;
                           
                        }
                       
                    } else {
                       
                        player.sendMessage(ChatColor.RED + "/setnetherspawn <spawn/exit>");
                        return true;
                       
                    }
               
                }
                   
            }
     
  7. Offline

    Scimiguy

    Ok so right before the line you just changed, put
    Code:
    sender.sendMessage(player.getWorld().getName());
    Run it and see what comes up in your chat
     
  8. Offline

    CyberSoul

    Thanks for your help... it works... I must have missed a step on exporting. It works now.
     
Thread Status:
Not open for further replies.

Share This Page