Change Seed of an existing world (1.8.8)

Discussion in 'Resources' started by Cookky, Dec 4, 2021.

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

    Cookky

    Hello guys !
    i wrote a code to change the seed of an already created world myself because i wanted to use it for a project but since i found another way to do what i wanted this code is useless
    and after searching on internet i found out that no one before wrote an equivalent program (maybe because no one needs it :D)
    so I'm posting it here in case someone maybe one day will want it...:p

    Code:
    public void changeSeed() {
            World world = Bukkit.getWorld("world"); // set to your world name
            world.setAutoSave(false);
            world.getPlayers().forEach(player->{        //kick all players on the map
                player.kickPlayer("Sorry");
            });
            Bukkit.unloadWorld(world, false);             //unload the world
            Chunk[] chunks = world.getLoadedChunks();
            for (Chunk chunk : chunks) {
                chunk.unload(false,false);                       // idk why but without unloading each chunks after unloading the world it doesn't work
            }
            File initialFile = new File(world.getWorldFolder().getPath()+"\\level.dat");// get the file containing the seed
            NBTTagCompound nbt = ReadNBT(initialFile);   // get the file content as nbt
            nbt.getCompound("Data").setLong("RandomSeed", generateRandomSeed()); // change the seed by a new one
            WriteNBT(initialFile, nbt); //set new nbt with the new seed
            Bukkit.shutdown();              // you must immediatly shutdown the server or it will then set the modified level.dat in a level.dat_old and reset level.dat
        }
    and you will also need these function :

    Code:
    public static void WriteNBT(File file, NBTTagCompound nbt)
          {
             try
             {
               if(!file.exists())
                 file.createNewFile();
               OutputStream out = new FileOutputStream(file);
               NBTCompressedStreamTools.a(nbt,out);
             }
             catch (IOException e) { e.printStackTrace(); }
          }
    public static NBTTagCompound ReadNBT(File file)
          {
             try
             {
                 InputStream in;
                 if(file.exists())
                 in = new FileInputStream(file);
                 else
                     return new NBTTagCompound();
       
                  NBTTagCompound nbtdata = NBTCompressedStreamTools.a(in);
                  in.close();
                  return nbtdata;
              }
              catch (Exception e)
              {
                  e.printStackTrace();
                  return new NBTTagCompound();
              }
          }
    private long generateRandomSeed() {
            long seed = new WorldCreator("seed").seed();
            return seed;
        }
    after executing this code you just have to generate non generated chunk with the new seed on in your plugin use :

    Code:
    Bukkit.getWorld("world").regenerateChunk(x, z) //replace x and z with your chunk coordinate
     
    Last edited: Dec 5, 2021
Thread Status:
Not open for further replies.

Share This Page