Solved World getting is always returning null.

Discussion in 'Plugin Help/Development/Requests' started by IconByte, Jan 31, 2015.

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

    IconByte

    Hello.
    I am having problem with teleporting player to another world when the game starts, but it is always giving me NPE error..

    This is giving me always NPE error:
    Code:
                    double X = config.getDouble("Arena.Spawnpoints.Red.X");
                    double Y = config.getDouble("Arena.Spawnpoints.Red.Y");
                    double Z = config.getDouble("Arena.Spawnpoints.Red.Z");
                    World w = Bukkit.getServer().getWorld("Arena");
                    Location loc = new Location(w, X, Y, Z);
        >>>>        player.teleport(loc);
    
    Console NPE error:
    Code:
    [11:46:03 WARN]: [GoldRush] Task #46 for GoldRush v0.1.0 generated an exception
    java.lang.NullPointerException
            at org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer.teleport(CraftPlaye
    r.java:462) ~[Spigot.jar:git-Spigot-29dbaa7-262c777]
            at org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity.teleport(CraftEntit
    y.java:204) ~[Spigot.jar:git-Spigot-29dbaa7-262c777]
            at net.playfriik.goldrush.GoldRush.gamingTimer(GoldRush.java:163) ~[?:?]
    
            at net.playfriik.goldrush.GoldRush$2.run(GoldRush.java:109) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.run(CraftTask.java
    :71) ~[Spigot.jar:git-Spigot-29dbaa7-262c777]
            at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [Spigot.jar:git-Spigot-29dbaa7-262c777]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    94) [Spigot.jar:git-Spigot-29dbaa7-262c777]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [Spigot.jar:git-Spigot-29dbaa7-262c777]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    23) [Spigot.jar:git-Spigot-29dbaa7-262c777]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :526) [Spigot.jar:git-Spigot-29dbaa7-262c777]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_20]
     
  2. Offline

    tommykins20

    Are you sure the world exists and is loaded? Not sure if this is important but are you also sure the coordinates are valid?
     
  3. Offline

    IconByte

    Yeah, coordinates are 100% valid, also I am loading the map 5 seconds before teleporting with:
    Code:
    mrb.loadMap("Arena");
    MRB class file is:
    Code:
    package net.playfriik.goldrush.maps;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    
    public class MapRollback {
        private final Logger log;
        private final File dataFolder;
    
        public MapRollback(Logger log, File dataFolder) {
            this.dataFolder = dataFolder;
            this.log = log;
        }
    
        public boolean loadMap(String name) {
            File mapsFolder = new File(dataFolder, "maps");
            if (!mapsFolder.exists())
                return false;
            File source = new File(mapsFolder, name);
            if (!source.exists())
                return false;
           
            Bukkit.unloadWorld(name, false);
    
            File destination = new File(dataFolder.getParentFile().getParentFile(), name + File.separator + "region");
            try {
                copyFolder(source, destination);
                return true;
            } catch (IOException e) {
                log.log(Level.SEVERE, "Could not load map {0}", name);
                e.printStackTrace();
                return false;
            }
        }
       
        public boolean saveMap(String name) {
            File mapsFolder = new File(dataFolder, "maps");
            if (!mapsFolder.exists())
                mapsFolder.mkdir();
            File source = new File(dataFolder.getParentFile().getParentFile(), name + File.separator + "region");
            if (!source.exists())
                return false;
           
            File destination = new File(mapsFolder, name);
            try {
                copyFolder(source, destination);
                return true;
            } catch (IOException e) {
                log.log(Level.SEVERE, "Could not save map {0}", name);
                e.printStackTrace();
                return false;
            }
        }
    
        private void copyFolder(File src, File dest) throws IOException {
            if (!src.exists()) {
                log.log(Level.SEVERE, "File {0} does not exist, cannot copy", src.toString());
                return;
            }
    
            if (src.isDirectory()) {
                boolean existed = dest.exists();
                if (!existed)
                    dest.mkdir();
    
                String[] srcFiles = src.list();
    
                if (existed)
                    log.log(Level.INFO, "Copying folder {0} and overwriting {1}", new Object[]{src.getAbsolutePath(), dest.getAbsolutePath()});
                else
                    log.log(Level.INFO, "Copying folder {0} to {1}", new Object[]{src.getAbsolutePath(), dest.getAbsolutePath()});
    
                for (String file : srcFiles) {
                    File srcFile = new File(src, file);
                    File destFile = new File(dest, file);
                    copyFolder(srcFile, destFile);
                }
    
                if (existed)
                    log.log(Level.INFO, "Overwrote folder {0}", dest.getAbsolutePath());
                else
                    log.log(Level.INFO, "Copied folder {0}", dest.getAbsolutePath());
    
            } else {
                OutputStream out;
                boolean existed;
                try (InputStream in = new FileInputStream(src)) {
                    out = new FileOutputStream(dest);
                    existed = dest.exists();
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = in.read(buffer)) > 0) {
                        out.write(buffer, 0, length);
                    }
                }
                out.close();
    
                if (existed)
                    log.log(Level.INFO, "Overwrote file {0}", dest.getName());
                else
                    log.log(Level.INFO, "Copied file {0}", dest.getName());
            }
        }
    }
     
  4. Offline

    tommykins20

    I skimmed through that code and the only thing I saw was that you're unloading a world.
     
  5. Offline

    IconByte

    Yeah, I am only unloading, because I am overwriting the unloaded world. ;)
    Pretty much I have maps folder in my plugin's folder and then the plugin copies map from "maps" folder and puts it to default server folder or sth.
    @tommykins20
     
  6. Offline

    tommykins20

    But then from what I see you are not loading the world back in again.
     
  7. Offline

    IconByte

    Oh, I see... Gonna work on it.
    @tommykins20

    //EDIT: Fixed at the moment!

    Code:
                        WorldCreator wc = new WorldCreator("Arena");
                        wc.generator(new VoidGenerator());
                        Bukkit.createWorld(wc);
    I just added this after one second.


    EDIT by Timtower: Moved to Bukkit alternatives
     
    Last edited by a moderator: Jan 31, 2015
    tommykins20 likes this.
Thread Status:
Not open for further replies.

Share This Page