1.12.2 I need help with world copying

Discussion in 'Spigot Plugin Development' started by fHpro0, Jun 14, 2021.

  1. I need help!

    I am trying to create a new world, unload, copying and load this world, but somehow my code wont go further. There are no errors and i get event the generate message for this new world.

    Code (Java):

    String finalLobbyID = "newWorld";
    CompletableFuture.supplyAsync(() -> {
        //Copy lobby world

        //Prepare world
        worldManager lobbyWorld = new worldManager();
        lobbyWorld.generateWorld(finalLobbyID, null);
        System.out.println("generate world");
        lobbyWorld.unloadMap();
        System.out.println("unload world");
        lobbyWorld.copyWorld(new File("lobby_template"), new File(finalLobbyID));
        System.out.println("copied");
        lobbyWorld = new worldManager();
        lobbyWorld.generateWorld(finalLobbyID, new Location(null, 480.5, 117, 510.5));
        System.out.println("load world");
        return lobbyWorld;
    });
    The last line before the return statement wont show in the console.

    More Info:
    • The world lobby_template is just a folder with a level.dat file
    • Console Output:
      Code (Text):
      > [15:22:02 INFO]: -------- World Settings For [Xkfhkd0QjqTmQKm] --------
      > [15:22:02 INFO]: Cactus Growth Modifier: 100%
      > [15:22:02 INFO]: Cane Growth Modifier: 100%
      > [15:22:02 INFO]: Melon Growth Modifier: 100%
      > [15:22:02 INFO]: Mushroom Growth Modifier: 100%
      > [15:22:02 INFO]: Pumpkin Growth Modifier: 100%
      > [15:22:02 INFO]: Sapling Growth Modifier: 100%
      > [15:22:02 INFO]: Wheat Growth Modifier: 100%
      > [15:22:02 INFO]: NetherWart Growth Modifier: 100%
      > [15:22:02 INFO]: Vine Growth Modifier: 100%
      > [15:22:02 INFO]: Cocoa Growth Modifier: 100%
      > [15:22:02 INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16 / Tiv true
      > [15:22:02 INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
      > [15:22:02 INFO]: Mob Spawn Range: 4
      > [15:22:02 INFO]: Hopper Transfer: 8 Hopper Check: 1 Hopper Amount: 1
      > [15:22:02 INFO]: Random Lighting Updates: false
      > [15:22:02 INFO]: Structure Info Saving: true
      > [15:22:02 INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617 Monument: 10387313 Slime: 987234911
      > [15:22:02 INFO]: Max TNT Explosions: 100
      > [15:22:02 INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
      > [15:22:02 INFO]: Item Merge Radius: 2.5
      > [15:22:02 INFO]: Item Despawn Rate: 6000
      > [15:22:02 INFO]: Arrow Despawn Rate: 1200
      > [15:22:02 INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
      > [15:22:02 INFO]: View Distance: 10
      > [15:22:02 INFO]: Experience Merge Radius: 3.0
      > [15:22:02 INFO]: Zombie Aggressive Towards Villager: true
      > [15:22:02 INFO]: Nerfing mobs spawned from spawners: false
      > [15:22:03 INFO]: Preparing start region for level 1 (Seed: 446091788263643559)
      > [15:22:03 INFO]: generate world
      > [15:22:03 INFO]: unload world
      > [15:22:03 INFO]: copied
      > [15:22:03 INFO]: -------- World Settings For [Xkfhkd0QjqTmQKm] --------
      > [15:22:03 INFO]: Cactus Growth Modifier: 100%
      > [15:22:03 INFO]: Cane Growth Modifier: 100%
      > [15:22:03 INFO]: Melon Growth Modifier: 100%
      > [15:22:03 INFO]: Mushroom Growth Modifier: 100%
      > [15:22:03 INFO]: Pumpkin Growth Modifier: 100%
      > [15:22:03 INFO]: Sapling Growth Modifier: 100%
      > [15:22:03 INFO]: Wheat Growth Modifier: 100%
      > [15:22:03 INFO]: NetherWart Growth Modifier: 100%
      > [15:22:03 INFO]: Vine Growth Modifier: 100%
      > [15:22:03 INFO]: Cocoa Growth Modifier: 100%
      > [15:22:03 INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16 / Tiv true
      > [15:22:03 INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
      > [15:22:03 INFO]: Mob Spawn Range: 4
      > [15:22:03 INFO]: Hopper Transfer: 8 Hopper Check: 1 Hopper Amount: 1
      > [15:22:03 INFO]: Random Lighting Updates: false
      > [15:22:03 INFO]: Structure Info Saving: true
      > [15:22:03 INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617 Monument: 10387313 Slime: 987234911
      > [15:22:03 INFO]: Max TNT Explosions: 100
      > [15:22:03 INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
      > [15:22:03 INFO]: Item Merge Radius: 2.5
      > [15:22:03 INFO]: Item Despawn Rate: 6000
      > [15:22:03 INFO]: Arrow Despawn Rate: 1200
    • worldManager Class:
      Code (Java):

      package com.mc.smash.world;
      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.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      import java.util.concurrent.CompletableFuture;
      import org.bukkit.*;
      import org.bukkit.block.Block;
      import org.bukkit.entity.Player;
      public class worldManager {
         
          private World world;
          private Location spawnLocation;
          public void setSpawnLocation(Location spawn){
              this.spawnLocation = spawn;
          }
          public void setGamerules(){
              this.world.setTime(6000);
              this.world.setGameRuleValue("doDaylightCycle", "false");
              this.world.setGameRuleValue("doFireTick", "false");
              this.world.setGameRuleValue("doMobLoot", "false");
              this.world.setGameRuleValue("doMobSpawning", "false");
              this.world.setGameRuleValue("mobGriefing", "false");
          }
          public void generateWorld(String name, Location spawn) {
              WorldCreator newWorld = new WorldCreator(name);
             
              newWorld.type(WorldType.FLAT);
              newWorld.generatorSettings("2;0;1;");
              this.world = newWorld.createWorld();
              setGamerules();
              setSpawnLocation(spawn);
          }
         
          public void copyWorld(File source, File target) {
              try {
                  ArrayList<String> ignore = new ArrayList<String>(Arrays.asList("uid.dat", "session.dat"));
                  if(!ignore.contains(source.getName())) {
                      if(source.isDirectory()) {
                          if(!target.exists())
                          target.mkdirs();
                          String files[] = source.list();
                          for (String file : files) {
                              File srcFile = new File(source, file);
                              File destFile = new File(target, file);
                              copyWorld(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 World getWorld() {
              return this.world;
          }
          public void unloadMap() {
              Bukkit.getServer().unloadWorld(this.world, true);
          }
         
          public void addBlock(String placeWorld, Material block, int x, int y, int z, int data) {
              CompletableFuture.supplyAsync(() -> {
                  Block newBlock = Bukkit.getWorld(placeWorld).getBlockAt(x, y, z);
                  newBlock.setType(block);
                  newBlock.setData((byte) data);
                  return true;
              });
          }
         
          public void teleportPlayers(List<Player> players, GameMode gamemode) {
              for (Player player : players) {
                  Player selectedPlayer = Bukkit.getPlayer(player.getUniqueId());
                  if(selectedPlayer != null) {
                      selectedPlayer.teleport(new Location(this.world, this.spawnLocation.getX(), this.spawnLocation.getY(), this.spawnLocation.getZ()));
                      selectedPlayer.setGameMode(gamemode);
                      selectedPlayer.playSound(selectedPlayer.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1f, 1.4f);
                  }
              }
          }
         
          public void parseWorld(World world) {
              this.world = world;
          }
         
          public void deleteWorld() {
              for(Player playerInWorld: Bukkit.getOnlinePlayers()){
                  if(playerInWorld.getWorld() == this.world){
                      playerInWorld.chat("/lobby");
                  }
              }
              File worldFolder = this.world.getWorldFolder();
              Bukkit.getServer().unloadWorld(this.world, true);
              deleteWorldFunc(worldFolder);
          }
         
          public boolean deleteWorldFunc(File path) {
                if(path.exists()) {
                    File files[] = path.listFiles();
                    for(int i=0; i<files.length; i++) {
                        if(files[i].isDirectory()) {
                            deleteWorldFunc(files[i]);
                        } else {
                            files[i].delete();
                        }
                    }
                }
                return(path.delete());
          }
         
      }


       

    Can somebody help?