Worlds? Help?

Discussion in 'Spigot Plugin Development' started by jay275475, Mar 19, 2015.

  1. Hey I was wondering when I do this,
    PHP:
    if(args[0].equalsIgnoreCase("join")){
                    if (Bukkit.getWorld(world) != null) {
                    Location loc = new Location(getServer().getWorld(world), getServer().getWorld(world).getSpawnLocation().getX(),getServer().getWorld(world).getSpawnLocation().getY(), getServer().getWorld(world).getSpawnLocation().getZ());
                    player.teleport(loc);
                    } else {
                        player.sendMessage("debug");
                    }
                    }
    But if the player is incorrect, it still has errors.
     
  2. What?

    By the way, it might be more efficient to designated a variable for getServer().getWorld(world); since you seem to use it a lot.
     
  3. Or just teleport the player to the World#getSpawnLocation() instead of breaking it apart just to put it back together again :p
     
    • Agree Agree x 2
  4. That too!
     
  5. What type of error is it that you are getting?
    Also as stated above this would be much more efficient and cleaner:
    Code (Text):
    if(args[0].equalsIgnoreCase("join")){
        World w = Bukkit.getWorld(world);
        if (w != null) {
            player.teleport(w.getSpawnLocation());
        } else {
            player.sendMessage("debug");
        }
    }
    If you are using the location afterwards then you could also use:
    Code (Text):
    Location loc = world.getSpawnLocation();
    p.teleport(loc);
    And if you absolutely have to have another instance of the location (a copy) this use this:
    Code (Text):
    Location loc2 = loc.clone();
     
  6. I mean like if there is no world it doesn't catch it.
    Does anyone know what kind of catch I would use?
     
  7. You are going to have to clarify. I don't understand what you are asking.
    Is something throwing an error? And if so what is the error?
     
  8. Its throwing a null error.
     
  9. PHP:
    World world = Bukkit.getWorld("world")
    if(Bukkit.getWorld(world) != null) {
        // not null
    } else {
        // its null.
    PHP:
    World world = Bukkit.getWorld("world")
    if(Bukkit.getWorld(world) == null) {
        // its null
    } else {
        // its not null.
    Though defining the World variable world as Bukkit.getWorld() before checking if that world is null or not may cause problems.

    is this what you're looking for?
    PHP:
        World world;
       
        try {
            world = Bukkit.getWorld("world");
        } catch(NullPointerException ee) {
            // crap
        }
     
    #9 Coltll, Mar 19, 2015
    Last edited: Mar 19, 2015
  10. Doesn't work. I've tried it.
     
  11. PHP:
                    String world = getServer().getPlayer(args[1]).getUniqueId()+"";
                    World w = Bukkit.getWorld(world);
                    if(w == null) {
                        player.sendMessage("SMP Doesn't Exist!");
                        } else {
                            Location loc = getServer().getWorld(world).getSpawnLocation();
                            player.teleport(loc);
                        }
                    }
    Returns error if the world isn't available
     
  12. Try the try/catch I posted above.

    It's probably throwing a nullpointer because you are setting a variable to the (null?) world before checking if it is null or not. gotta make sure it's not null then define it as a variable.
     
    • Like Like x 1
  13. Thx iXColtXi!
    It works now!
     
  14. Catching NullPointerExceptions instead of treating them like the logic errors they are is a sign of weakness!
     
  15. Not sure what you mean?
     
  16. If you're getting a NullPointerException, unless it's being thrown manually, it means you just didn't account for something. So rather than catching it, it's much better to just find out what you didn't check for and fix it.

    ...

    ...

    ...

    Oh, and it's good practice. :D
     
  17. Fair enough.