import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.block.Block;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
public class WorldController {
public World createEmptyWorld(String name) {
WorldCreator worldCreator = new WorldCreator(name);
worldCreator.environment(World.Environment.NORMAL);
worldCreator.generateStructures(false);
worldCreator.generator(new ChunkGenerator(){
public List<BlockPopulator> getDefaultPopulators(World world) {
return Arrays.asList(new BlockPopulator[0]);
}
public boolean canSpawn(World world, int x, int z) {
return true;
}
public byte[] generate(World world, Random random, int x, int z) {
return new byte[32768];
}
public Location getFixedSpawnLocation(World world, Random random) {
return new Location(world, 0.0, 64.0, 0.0);
}
});
World world = worldCreator.createWorld();
world.setDifficulty(Difficulty.NORMAL);
world.setSpawnFlags(true, true);
world.setPVP(true);
world.setStorm(false);
world.setThundering(false);
world.setWeatherDuration(Integer.MAX_VALUE);
world.setAutoSave(false);
world.setKeepSpawnInMemory(false);
world.setTicksPerAnimalSpawns(1);
world.setTicksPerMonsterSpawns(1);
world.setGameRuleValue("doMobSpawning", "false");
world.setGameRuleValue("mobGriefing", "false");
world.setGameRuleValue("doFireTick", "false");
world.setGameRuleValue("showDeathMessages", "false");
Block b = world.getBlockAt(0, 20, 0);
b.setType(Material.STONE);
return world;
}
public boolean loadWorld(String worldName) {
String isLobby = worldName.substring(0, Math.min(worldName.length(), 5));
WorldCreator worldCreator = new WorldCreator(worldName);
worldCreator.generateStructures(false);
worldCreator.generator(new ChunkGenerator(){
public List<BlockPopulator> getDefaultPopulators(World world) {
return Arrays.asList(new BlockPopulator[0]);
}
public boolean canSpawn(World world, int x, int z) {
return true;
}
public byte[] generate(World world, Random random, int x, int z) {
return new byte[32768];
}
public Location getFixedSpawnLocation(World world, Random random) {
return new Location(world, 0.0, 64.0, 0.0);
}
});
World world = worldCreator.createWorld();
world.setDifficulty(Difficulty.NORMAL);
world.setSpawnFlags(true, true);
if (isLobby.equalsIgnoreCase("lobby")) {
world.setPVP(false);
} else {
world.setPVP(true);
}
world.setStorm(false);
world.setThundering(false);
world.setWeatherDuration(Integer.MAX_VALUE);
world.setAutoSave(false);
world.setKeepSpawnInMemory(false);
world.setTicksPerAnimalSpawns(1);
world.setTicksPerMonsterSpawns(1);
world.setGameRuleValue("doMobSpawning", "false");
world.setGameRuleValue("mobGriefing", "false");
world.setGameRuleValue("doFireTick", "false");
world.setGameRuleValue("showDeathMessages", "false");
boolean loaded = false;
for (World w : Bukkit.getWorlds()) {
if (!w.getName().equals(world.getName())) continue;
loaded = true;
break;
}
return loaded;
}
public void unloadWorld(String w) {
World world = Bukkit.getWorld(w);
if (world != null) {
Bukkit.unloadWorld(world, true);
}
}
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()) {
String[] files;
if (!target.exists()) {
target.mkdirs();
}
String[] arrstring = files = source.list();
int n = arrstring.length;
int n2 = 0;
while (n2 < n) {
String file = arrstring[n2];
File srcFile = new File(source, file);
File destFile = new File(target, file);
this.copyWorld(srcFile, destFile);
++n2;
}
} else {
int length;
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(target);
byte[] buffer = new byte[1024];
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
}
catch (IOException ignore) {
// empty catch block
}
}
public void deleteWorld(String name) {
this.unloadWorld(name);
File target = new File(Bukkit.getWorldContainer().getAbsolutePath(), name);
this.deleteWorld(target);
}
public boolean deleteWorld(File path) {
if (path.exists()) {
File[] files = path.listFiles();
int i = 0;
while (i < files.length) {
if (files.isDirectory()) {
this.deleteWorld(files);
} else {
files.delete();
}
++i;
}
}
return path.delete();
}
}