mrkirby153

Untitled

Apr 30th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package polaris.game.world;
  2.  
  3. import org.apache.commons.io.FileUtils;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.World;
  6. import org.bukkit.WorldCreator;
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.entity.Player;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12.  
  13. public class MinigameWorld {
  14.  
  15.     private World world;
  16.     private WorldHandler handler;
  17.     private String templateWorld;
  18.  
  19.     private int playersNeeded;
  20.     private int maxPlayers;
  21.  
  22.     public MinigameWorld(WorldHandler handler, String templateWorld) {
  23.         this.handler = handler;
  24.         this.templateWorld = templateWorld;
  25.     }
  26.  
  27.     public int getPlayersNeeded() {
  28.         return playersNeeded;
  29.     }
  30.  
  31.     public int getMaxPlayers() {
  32.         return maxPlayers;
  33.     }
  34.  
  35.     // TODO: 4/30/2016 Async this?
  36.     public void initialize() {
  37.         if (!Bukkit.unloadWorld(Bukkit.getWorld(templateWorld), false)) {
  38.             System.out.println("Failed to unload world " + templateWorld);
  39.         }
  40.         copyWorld();
  41.         File uid = new File(new File(Bukkit.getWorldContainer(), "minigame"), "uid.dat");
  42.         uid.delete();
  43.         FileConfiguration cfg = handler.getConfiguration();
  44.         playersNeeded = cfg.getInt(templateWorld + ".playersNeeded");
  45.         maxPlayers = cfg.getInt(templateWorld + ".maxPlayers");
  46.     }
  47.  
  48.     public void destroy() {
  49.         destroyWorld();
  50.         handler.worldUnloaded();
  51.     }
  52.  
  53.     private void destroyWorld() {
  54.         if (world.getPlayers().size() > 0) {
  55.             for (Player p : world.getPlayers())
  56.                 handler.getManager().returnToLobby(p);
  57.         }
  58.         if (Bukkit.unloadWorld(world, false)) {
  59.             File dir = new File(Bukkit.getWorldContainer(), "minigame");
  60.             try {
  61.                 FileUtils.deleteDirectory(dir);
  62.             } catch (IOException e) {
  63.                 this.handler.getPlugin().getLogger().warning("There was an error deleting the world! (" + e.getCause().toString() + ": " + e.getMessage() + ")");
  64.             }
  65.         } else {
  66.             this.handler.getPlugin().getLogger().warning("Not deleting world as the world was not unloaded!");
  67.         }
  68.     }
  69.  
  70.     private void copyWorld() {
  71.         File dest = new File(Bukkit.getWorldContainer(), "minigame");
  72.         if (!dest.exists()) {
  73.             dest.mkdirs();
  74.         }
  75.         WorldCreator newCreator = new WorldCreator("minigame");
  76.         newCreator.seed(handler.getConfiguration().getLong(this.templateWorld + ".seed"));
  77.         try {
  78.             FileUtils.copyDirectory(new File(Bukkit.getWorldContainer(), this.templateWorld), dest);
  79.         } catch (IOException e) {
  80.             this.handler.getPlugin().getLogger().severe("There was an error copying the template world! (" + e.getCause().toString() + ": " + e.getMessage() + ")");
  81.             return;
  82.         }
  83.         world = Bukkit.createWorld(newCreator);
  84.     }
  85.  
  86.     public String getName() {
  87.         return this.templateWorld;
  88.     }
  89.  
  90.     public World getBukkitWorld() {
  91.         return world;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment