Replace a world with a copied one

Discussion in 'Plugin Development' started by thorvaldemar, Jan 3, 2016.

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

    thorvaldemar

    Hey guys.

    I'm trying to make so that then you write a command it will create a world and then copy another and paste it in the created one. I know how to create, copy and paste, but when i'm doing it the world is the same. you know the copied world isn't bieng pasted. So my question is:
    how do i replace a exiting world with a copy world?

    Here is my code:
    Code:
    WorldCreator worldCreator = new WorldCreator(args[1]);
                        worldCreator.generateStructures(false);
                        worldCreator.type();
                       
                        Bukkit.getServer().createWorld(worldCreator);
                      
                        World source = Bukkit.getWorld("world");
                        File sourceFolder = source.getWorldFolder();
                      
                        World target = Bukkit.getWorld(args[1]);
                        File targetFolder = target.getWorldFolder();
                      
                        copyWorld(sourceFolder, targetFolder);
    And some more coding:
    Code:
    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) {
        
            }
        }
    Hope you can help...
     
  2. Offline

    thorvaldemar

    The thing that you are doing is coping the world to another map to that you can save the world... What i want to do is when i'm writing /sb create [World name] it will create a world and then copy another existing world and then override the world i just created... it's a hard to explain so hope you understand XD
     
  3. Offline

    teej107

    @thorvaldemar Maybe it'll help to not silence exceptions being thrown. You should also unload the world you want to replace.
     
  4. Offline

    thorvaldemar

    @teej107 Okay. So i've tryed to do this instead
    Code:
    WorldCreator worldCreator = new WorldCreator(args[1]);
                                worldCreator.generateStructures(false);
                                worldCreator.type();
    
                                Bukkit.getServer().createWorld(worldCreator);
                             
                                World unload = Bukkit.getWorld(args[1]);
                                Bukkit.getServer().unloadWorld(unload, true);
    
                                World source = Bukkit.getWorld("world");
                                File sourceFolder = source.getWorldFolder();
    
                                World target = Bukkit.getWorld(args[1]);
                                File targetFolder = target.getWorldFolder();
    
                                copyWorld(sourceFolder, targetFolder);
                             
                                Bukkit.getServer().getWorlds().add(unload);
    and i gave the exceptions a printStackTrace() thing. but when i'm trying to run the command, it'll give me a NullPointerException?

    BTW it's
    Code:
    File targetFolder = target.getWorldFolder();
    this line thats the problem
     
    Last edited: Jan 4, 2016
  5. Offline

    mcdorli

    target is probably null. Where do you initialize it?
     
  6. Offline

    thorvaldemar

    @mcdorli I don't know exacly what you mean, but here is the hole code:

    Code:
    package me.thorvaldemar.Commands;
    
    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.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.OfflinePlayer;
    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 me.thorvaldemar.Main;
    
    public class PlotsC implements CommandExecutor {
       
        public HashMap<Player, Player> invited = new HashMap<Player, Player>();
    
        private Main plugin;
    
        public PlotsC(Main pl) {
            plugin = pl;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("sb") || label.equalsIgnoreCase("skyblock")) {
                Player p = (Player) sender;
    
                if (args.length == 2) {
                    if (args[0].equalsIgnoreCase("accept")) {
                        @SuppressWarnings("deprecation")
                        Player senter = p.getServer().getPlayer(args[1]);
                        if (invited.get(senter).equals(p)) {
                            if (plugin.getConfig().getBoolean(p.getName()) == false) {
                               
                                p.sendMessage(ChatColor.GREEN + "You joined " + ChatColor.DARK_GREEN + args[1] + ChatColor.GREEN + " island");
                                senter.sendMessage(ChatColor.DARK_GREEN + p.getName() + ChatColor.GREEN + " accepted your invitaion");
                                invited.remove(p.getName());
                               
                                plugin.getConfig().set(p.getName(), true);
                                plugin.getConfig().set(p.getName() + "-skyworld", plugin.getConfig().getString(senter + "-skyworld"));
                                plugin.getConfig().set(p.getName() + "-rank", "member");
                            } else {
                                p.sendMessage(ChatColor.RED + "You already have an island");
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + "You din't get eny invitaions from " + ChatColor.DARK_RED + args[1]);
                        }
                    } else if (args[0].equalsIgnoreCase("invite")) {
                        if (plugin.getConfig().getBoolean(p.getName()) == true) {
                            if (plugin.getConfig().getString(p.getName() + "-rank") != "member") {
                                if (plugin.getConfig().contains(args[1])) {
                                    if (plugin.getConfig().getBoolean(args[1]) == false) {
                                        @SuppressWarnings("deprecation")
                                        Player sendto = p.getServer().getPlayer(args[1]);
                                        invited.remove(p);
                                        invited.put(p, sendto);
    
                                        sendto.sendMessage(ChatColor.DARK_GREEN + "" + p.getName() + ChatColor.GREEN
                                                + " Has invited you to his island " + ChatColor.GOLD + "\n\"/sb accept "
                                                + p.getName() + "\" or \"/sb deny " + p.getName() + "\"");
                                       
                                    } else {
                                        p.sendMessage(ChatColor.DARK_RED + args[1] + ChatColor.RED + " allready have a island");
                                    }
                                } else {
                                    p.sendMessage(ChatColor.DARK_RED + args[1] + ChatColor.RED + " isn't a player");
                                }
                            } else {
                                p.sendMessage(ChatColor.RED + "You are not high enogh rank on your island");
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + "Your are not member of a island");
                        }
                    } else if (args[0].equalsIgnoreCase("create")) {
                        if (Bukkit.getWorld(args[1]) == null) {
                            if (plugin.getConfig().getBoolean(p.getName()) == false) {
                                p.sendMessage(ChatColor.RED + "Your island is being created, please wait!");
    
                                WorldCreator worldCreator = new WorldCreator(args[1]);
                                worldCreator.generateStructures(false);
                                worldCreator.type();
                               
    
                                Bukkit.getServer().createWorld(worldCreator);
                               
                                World unload = Bukkit.getWorld(args[1]);
                                Bukkit.getServer().unloadWorld(unload, true);
    
                                World source = Bukkit.getWorld("world");
                                File sourceFolder = source.getWorldFolder();
    
                                World target = Bukkit.getWorld(args[1]);
                                File targetFolder = target.getWorldFolder();
    
                                copyWorld(sourceFolder, targetFolder);
                               
                                Bukkit.getServer().getWorlds().add(unload);
    
                                p.sendMessage(ChatColor.GREEN + "World has been created");
                                plugin.getConfig().set(p.getName(), true);
                                plugin.getConfig().set(p.getName() + "-skyworld", args[1]);
                                plugin.getConfig().set(p.getName() + "-skyrank", "owner");
    
                                plugin.saveConfig();
                            } else {
                                p.sendMessage(ChatColor.RED + "You allready have a island");
                            }
                        } else {
                            p.sendMessage(ChatColor.DARK_RED + args[1] + ChatColor.RED + " is allready taken");
                        }
                    }
                } else if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("invite")) {
                        if (plugin.getConfig().getBoolean(p.getName()) == true) {
                            if (plugin.getConfig().getString(p.getName() + "-rank") != "member") {
                               
                                p.sendMessage(ChatColor.RED + "Usage: /sb invite [Player]");
                               
                            } else {
                                p.sendMessage(ChatColor.RED + "You are not high enogh rank on your island");
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + "Your are not member of a island");
                        }
                    } else if (args[0].equalsIgnoreCase("create")) {
                        p.sendMessage(ChatColor.RED + "Usage: /sb create [name]");
    
                    } else if (args[0].equalsIgnoreCase("remove")) {
                        if (plugin.getConfig().getBoolean(p.getName()) == true) {
                            if (plugin.getConfig().getString(p.getName() + "-skyrank") != "member") {
                               
                                for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
                                    Bukkit.broadcastMessage("2 gange");
                                    if (plugin.getConfig().getString(player + "-rank") == "member") {
                                        Bukkit.broadcastMessage(player + " is member");
                                    }
                                }
                               
                                p.sendMessage(ChatColor.GREEN + "Your island has been removed");
                                p.sendMessage(ChatColor.RED
                                        + "WARNING: The name that your island had, can't be used again until tommorow");
    
                                Bukkit.unloadWorld(p.getName() + "-skyworld", false);
    
                                plugin.getConfig().set(plugin.getConfig().getString(p.getName() + "-skyworld"), "slet mig");
                                plugin.getConfig().set(p.getName(), false);
                                plugin.getConfig().set(p.getName() + "-skyworld", "ingen");
                                plugin.getConfig().set(p.getName() + "-skyrank", "ingen");
    
                                plugin.saveConfig();
                            } else {
                                p.sendMessage(ChatColor.RED + "You are not in high enogth rank to do that");
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + "You do not have a island");
                        }
                    } else if (args[0].equalsIgnoreCase("home")) {
                        if (plugin.getConfig().getBoolean(p.getName()) == true) {
                            World world = Bukkit.getWorld(plugin.getConfig().getString(p.getName() + "-skyworld"));
                            p.teleport(new Location(world, 0, 57, 0));
                        } else {
                            p.sendMessage(ChatColor.RED + "You do not have a island");
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "Invald args \"/sb\" for more help");
                    }
                } else {
                    p.sendMessage(ChatColor.RED + "Usage: /sb [create:home:remove:leave:invite:accept:deny:reset:tp]");
                }
                return false;
            }
            return false;
        }
    
        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();
            }
        }
    
    }
     
  7. Offline

    thorvaldemar

  8. My code can do the exact same thing? What's the point of creating a world if you're just going to overwrite it with an existing world? Instead, you could just skip the creation and copy the existing world (with my code for example)?

    The instance 'target' is null. This is because the world inputted in args[1] hasn't been loaded as you unloaded it a few lines of code ago. I don't see a point in unloading it then loading it again straight after...?
    If you really have it not loaded, you could just use:
    Code:
    File targetFolder = new File(Bukkit.getWorldContainer(), args[0]);
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
Thread Status:
Not open for further replies.

Share This Page