Copy a exist world (from main folder) via command

Discussion in 'Plugin Development' started by tonihenkel, Dec 26, 2023.

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

    tonihenkel

    Hello, I have a Problem. I create a World in SinglePlayer with Water Only. I copy this world in the Main World of my Server an will create a own world for every player by using command /copyworld. The name of copied world in Minecraft is "OriginalWelt". But Minecraft cant find this world (Also I think the world is not register on Worldserver). How I can fix this problem? Thanks <3

    Code:
    package de.timelesslife.projektminecraft;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import java.io.*;
    import java.util.*;
    
    public class WorldCopyCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("copyworld") && sender instanceof Player) {
    
                World originalWorld = Bukkit.getWorld("OriginalWelt"); // Ändern Sie den Namen der Welt bei Bedarf
                if (originalWorld != null) {
                    // Kopieren Sie die Welt und benennen Sie sie um
                    World copiedWorld = Bukkit.createWorld(new WorldCreator("___CUSTOMWORLD_" + player.getUniqueId()));
                } else {
                    // Die Welt 'world' wurde nicht gefunden.
                    player.sendMessage("Welt nicht gefunden. ");
                }
    
    
    
            }
            return false;
        }
    
    
    
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @tonihenkel Is it not better to just write a custom generator instead?
    Then you don't need an original world either.
     
  3. Offline

    tonihenkel

    no sorry, everyone gets the same map. and pls no multiverse plugin.
     
  4. Offline

    timtower Administrator Administrator Moderator

    If there is stuff in the map already then I can believe it, else it is just using the same seed over and over again.

    And else you need something to copy the data over before you create the world.
     
  5. Offline

    tonihenkel

    Copy is easy, but I cant found the map in 2nd step(join this map)

    http://prntscr.com/OSuG_7yANpud



    Code:
    package de.timelesslife.projektminecraft;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class WorldCopyCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("copyworld") && sender instanceof Player) {
    
                File srcDir = new File("OriginalWelt");
                File destDir = new File("___CUSTOMWORLD_" + player.getUniqueId());
    
                try {
                    copyDirectory(srcDir, destDir);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                File file = new File("___CUSTOMWORLD_" + player.getUniqueId() + "uid.dat");
                file.delete();
            }
            return false;
        }
    
        // Rekursiv alle Unterordner und Dateien kopieren
        private void copyDirectory(File source, File destination) throws IOException {
            if (source.isDirectory()) {
                if (!destination.exists()) {
                    destination.mkdirs();
                }
    
                String[] files = source.list();
                for (String file : files) {
                    File srcFile = new File(source, file);
                    File destFile = new File(destination, file);
    
                    copyDirectory(srcFile, destFile);
                }
            } else {
                // Wenn es sich um eine Datei handelt, diese kopieren
                FileInputStream in = new FileInputStream(source);
                FileOutputStream out = new FileOutputStream(destination);
    
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
    
                in.close();
                out.close();
            }
        }
    }
    
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @tonihenkel I don't see the worldcreator either anymore, so don't find that weird
     
  7. Offline

    tonihenkel

    ok, than I will try to intigrate multiverse :/
     
  8. Offline

    timtower Administrator Administrator Moderator

    Why? You just removed the code that actually loads the world into the server.
     
  9. Offline

    tonihenkel

    Slowly again:
    Everyone who enters the server comes to the “world” dimension.

    I created a map in single player. Anyone who enters the command "/copyworld" creates a copy of this created map. You can enter this map with “/joinworld”. So everyone has the same map, only everyone has their own dimension.


    =====

    I have solved the problem now. He overwrote the single player map at WorldCreator because it had the wrong structure due to the single player. I used this as the main map once and now it works.


    Thanks amd Sorry for my confused Text ;)
     
Thread Status:
Not open for further replies.

Share This Page