Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. package me.kyle1320.ChunkLoader;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.logging.Logger;
  7.  
  8. import org.bukkit.Chunk;
  9. import org.bukkit.World;
  10. import org.bukkit.WorldCreator;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.event.EventHandler;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.world.ChunkLoadEvent;
  16. import org.bukkit.event.world.ChunkPopulateEvent;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. public class ChunkLoader extends JavaPlugin implements Listener {
  20.    
  21.     List<String> allChunks = new ArrayList<String>();
  22.     Integer size = 1;
  23.     Logger logger = Logger.getLogger("Minecraft");
  24.  
  25.     @Override
  26.     public void onEnable() {
  27.         logger.info("ChunkLoader enabled.");
  28.         getServer().getPluginManager().registerEvents(this, this);
  29.     }
  30.  
  31.     @Override
  32.     public void onDisable() {
  33.         logger.info("ChunkLoader disabled.");
  34.     }
  35.  
  36.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
  37.  
  38.         if (cmd.getName().equalsIgnoreCase("loadchunks")) {
  39.             //if the world exists
  40.             if (getServer().getWorld("world_chunkloader") != null) {
  41.                 //delete it
  42.                 World world = getServer().getWorld("world_chunkloader");
  43.                 getServer().unloadWorld(world, false);
  44.                 delDirectory(world.getWorldFolder());
  45.                 logger.info("World deleted.");
  46.                
  47.                 //create a new world
  48.                 getServer().createWorld(new WorldCreator("world_chunkloader"));
  49.                 logger.info("World created.");
  50.             }else { //if the world doesn't exist
  51.                 //create a new world
  52.                 getServer().createWorld(new WorldCreator("world_chunkloader"));
  53.                 logger.info("World created.");
  54.             }
  55.            
  56.             //sets the width of the square of chunks to be loaded
  57.             if (args.length >= 1) {
  58.                 try {
  59.                     size = Integer.parseInt(args[0]);
  60.                 }catch (NumberFormatException e) {
  61.                 }
  62.             }
  63.            
  64.             World world = getServer().getWorld("world_chunkloader");
  65.  
  66.             logger.info("Preparing world..");
  67.             //unloads any chunks loaded in the world, hopefully cuts down on lag
  68.             for (Chunk chunk : world.getLoadedChunks()) {
  69.                 chunk.unload(true);
  70.             }
  71.  
  72.             logger.info("Loading chunks..");
  73.             for (int x = 0; x<size; x++) {
  74.                 for (int z = 0; z<size; z++) {
  75.                     // Using a "key" for chunks because for some reason it wouldn't recognize if a chunk was in the list if List<Chunk> was used.
  76.                     allChunks.add(world.getName() + "," + x + "," + z);
  77.                 }
  78.             }
  79.  
  80.             for (int x = 0; x<size; x++) {
  81.                 for (int z = 0; z<size; z++) {
  82.                     //if the chunk is loaded
  83.                     if (world.isChunkLoaded(x, z)) {
  84.                         //unload it, makes sure that every chunk in the square gets handled by the listeners
  85.                         world.unloadChunk(x, z);
  86.                     }
  87.                     //load the chunk
  88.                     world.loadChunk(x, z);
  89.                 }
  90.             }
  91.  
  92.             while (!allChunks.isEmpty()) {
  93.                 logger.info("Not empty!");
  94.                 //not a good way of doing it, but attempts to load any chunks the listeners missed
  95.                 //because it does happen occasionally if enough chunks are passed into it.
  96.                 for (int i = 0; i<allChunks.size(); i++) {
  97.                     String s = allChunks.get(i);
  98.                     int x = 0;
  99.                     int z = 0;
  100.                     try {x = Integer.parseInt(s.split(",")[1]);}catch(NumberFormatException e) {}
  101.                     try {z = Integer.parseInt(s.split(",")[2]);}catch(NumberFormatException e) {}
  102.  
  103.                     if (world.isChunkLoaded(x, z)) {
  104.                         world.unloadChunk(x, z);
  105.                     }
  106.                     world.loadChunk(x, z);
  107.                 }
  108.             }
  109.             return true;
  110.         }
  111.         return false;
  112.     }
  113.  
  114.     @EventHandler
  115.     public void onLoad(ChunkLoadEvent event) { //called when the chunk is loaded in the world
  116.         final Chunk chunk = event.getChunk();
  117.         final String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
  118.         final int index = allChunks.indexOf(key);
  119.         final boolean newChunk = event.isNewChunk();
  120.         //if the chunk is in the list and has already been generated
  121.         if (index != -1 && !newChunk) {
  122.             //remove it, unload it
  123.             allChunks.remove(index);
  124.             chunk.unload(true);
  125.            
  126.             if (allChunks.size() % size == 0) {
  127.                 logger.info("(" + (size - (allChunks.size() / size)) + "/" + size + ")");
  128.             }
  129.         }
  130.     }
  131.  
  132.     @EventHandler
  133.     public void onPopulate(ChunkPopulateEvent event) { //called after the chunks finish being generated
  134.         final Chunk chunk = event.getChunk();
  135.         final String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
  136.         final int index = allChunks.indexOf(key);
  137.         //if the chunk if in the list
  138.         if (index != -1) {
  139.             //remove it, unload it
  140.             allChunks.remove(index);
  141.             chunk.unload(true);
  142.            
  143.             if (allChunks.size() % size == 0) {
  144.                 logger.info("(" + (size - (allChunks.size() / size)) + "/" + size + ")");
  145.             }
  146.         }
  147.     }
  148.  
  149.     public void delDirectory(File file) { //deletes the world directory
  150.         if (file.isDirectory()) {
  151.             File[] files = file.listFiles();
  152.             if (files.length != 0) {
  153.                 for (File f : files) {
  154.                     delDirectory(f);
  155.                 }
  156.             }else {
  157.                 file.delete();
  158.             }
  159.         }else {
  160.             file.delete();
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement