Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package polaris.game.world;
- import org.apache.commons.io.FileUtils;
- import org.bukkit.Bukkit;
- import org.bukkit.World;
- import org.bukkit.WorldCreator;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import java.io.File;
- import java.io.IOException;
- public class MinigameWorld {
- private World world;
- private WorldHandler handler;
- private String templateWorld;
- private int playersNeeded;
- private int maxPlayers;
- public MinigameWorld(WorldHandler handler, String templateWorld) {
- this.handler = handler;
- this.templateWorld = templateWorld;
- }
- public int getPlayersNeeded() {
- return playersNeeded;
- }
- public int getMaxPlayers() {
- return maxPlayers;
- }
- // TODO: 4/30/2016 Async this?
- public void initialize() {
- if (!Bukkit.unloadWorld(Bukkit.getWorld(templateWorld), false)) {
- System.out.println("Failed to unload world " + templateWorld);
- }
- copyWorld();
- File uid = new File(new File(Bukkit.getWorldContainer(), "minigame"), "uid.dat");
- uid.delete();
- FileConfiguration cfg = handler.getConfiguration();
- playersNeeded = cfg.getInt(templateWorld + ".playersNeeded");
- maxPlayers = cfg.getInt(templateWorld + ".maxPlayers");
- }
- public void destroy() {
- destroyWorld();
- handler.worldUnloaded();
- }
- private void destroyWorld() {
- if (world.getPlayers().size() > 0) {
- for (Player p : world.getPlayers())
- handler.getManager().returnToLobby(p);
- }
- if (Bukkit.unloadWorld(world, false)) {
- File dir = new File(Bukkit.getWorldContainer(), "minigame");
- try {
- FileUtils.deleteDirectory(dir);
- } catch (IOException e) {
- this.handler.getPlugin().getLogger().warning("There was an error deleting the world! (" + e.getCause().toString() + ": " + e.getMessage() + ")");
- }
- } else {
- this.handler.getPlugin().getLogger().warning("Not deleting world as the world was not unloaded!");
- }
- }
- private void copyWorld() {
- File dest = new File(Bukkit.getWorldContainer(), "minigame");
- if (!dest.exists()) {
- dest.mkdirs();
- }
- WorldCreator newCreator = new WorldCreator("minigame");
- newCreator.seed(handler.getConfiguration().getLong(this.templateWorld + ".seed"));
- try {
- FileUtils.copyDirectory(new File(Bukkit.getWorldContainer(), this.templateWorld), dest);
- } catch (IOException e) {
- this.handler.getPlugin().getLogger().severe("There was an error copying the template world! (" + e.getCause().toString() + ": " + e.getMessage() + ")");
- return;
- }
- world = Bukkit.createWorld(newCreator);
- }
- public String getName() {
- return this.templateWorld;
- }
- public World getBukkitWorld() {
- return world;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment