Solved Why is this not loading the correct world? WorldCreator

Discussion in 'Spigot Plugin Development' started by Realm, May 30, 2017.

  1. So essentially, I created a directory that world folders can be uploaded to. These world folders then get copied over into the main directory to load them as a world. My issue is that it doesn't seem to be creating the world properly.

    Code (Text):
        public static void loadWorlds() {
            ArrayList<String> filesInMainFolder = new ArrayList<>(Arrays.asList(Bukkit.getWorldContainer().list()));
            List<String> mapsToLoad = getMaps().stream()
                    .filter(name -> !filesInMainFolder.contains(name))
                    .collect(Collectors.toList());

            if (mapsToLoad == null || mapsToLoad.isEmpty()) return;

            String mapName = mapsToLoad.stream().findFirst().get();
            System.out.println("loadWorlds " + mapName);

            newEnd = new WorldCreator(mapName).createWorld();
        }
    newEnd is a world object.

    My objective with this code is to copy the world folder into the main directory if it isn't there. I think there is a slight issue with the streams because I dont think it does that, regardless thats not my main concern.

    I have a custom world, another 'End' world. I copy this folder into the main directory and attempt to load it as a world. However, when I try to teleport to the default spawn location of the new world, 'newEnd', it instead teleports me to an overworld world. Any ideas what Im doing wrong? I presume it's with WorldCreator, not sure what though.
     
  2. Check which name you're actually loading, and check whether the copy is completed properly (do a copy, and then stop - Don't load the world as it'll generate a world). You can also try both in a different server, and compare folder structure.

    Also worth checking if the spawn is actually set, and if the chunks you copy are the spawn chunks. If that's not the case, it might teleport you somewhere else and generate new chunks with the wrong world environment.
     
  3. Where are you copying the world??
     
  4. Thanks for the response. Literally just fixed it seconds ago. I think the issue was the fact that, for some reason, it didn't like the WorldCreator#createWorld method. It liked it before when I had tried it in different plugins doing similar things, I'm not sure why it happened to not work this time. I just ended up using Bukkit#getWorld, and it was already loaded as a world somehow and it worked fine.
     
  5. I was copying it to the Bukkit#getWorldContainer. I still am doing that, but if you see my aforementioned response, you can see that I instead used Bukkit#getWorld, and that seemed to solve my issue. Still not sure why WorldCreator didnt want to work as I thought it would though.
     
  6. Weird, that should only return loaded worlds, whereas WorldCreator either returns a loaded world, loads an existing world, or creates a new one (in that order, or at least it used to)
     
  7. Yeah turns out that it worked for a split second, but then I ended up changing stuff all together because it stopped working.
    I have no idea why I was having issues before with WorldCreator, but it seems to be working fine just now.
    Code (Text):
        private static void copyFileStructure(File source, File target){
            try {
                List<String> ignore = Arrays.asList("uid.dat", "session.lock");
                if(!ignore.contains(source.getName())) {
                    if(source.isDirectory()) {
                        if(!target.exists())
                            if (!target.mkdirs())
                                throw new IOException("Couldn't create world directory!");
                        String files[] = source.list();
                        for (String file : files) {
                            File srcFile = new File(source, file);
                            File destFile = new File(target, file);
                            copyFileStructure(srcFile, destFile);
                        }
                    } else {
                        InputStream in = new FileInputStream(source);
                        OutputStream out = new FileOutputStream(target);
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = in.read(buffer)) > 0)
                            out.write(buffer, 0, length);
                        in.close();
                        out.close();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void copyWorldToMain(String mapName) {
            if (Bukkit.getWorlds().stream().anyMatch(map -> map.getName().equalsIgnoreCase(mapName))) {
                newEnd = Bukkit.getWorld(mapName);
                return;
            }
            copyFileStructure(new File(path, mapName), new File(Bukkit.getWorldContainer(), mapName));
            newEnd = new WorldCreator(mapName).createWorld();
        }
     
  8. Why not just use the Files class? or even FileUtils??
     
  9. I knew someone was going to mention that lol. Simply because I was lazy, copy pasted something from one of my previous plugins that worked:p


    Sent from my iPhone using Tapatalk