Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.kyle1320.ChunkLoader;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Logger;
- import org.bukkit.Chunk;
- import org.bukkit.World;
- import org.bukkit.WorldCreator;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.world.ChunkLoadEvent;
- import org.bukkit.event.world.ChunkPopulateEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- public class ChunkLoader extends JavaPlugin implements Listener {
- List<String> allChunks = new ArrayList<String>();
- Integer size = 1;
- Logger logger = Logger.getLogger("Minecraft");
- @Override
- public void onEnable() {
- logger.info("ChunkLoader enabled.");
- getServer().getPluginManager().registerEvents(this, this);
- }
- @Override
- public void onDisable() {
- logger.info("ChunkLoader disabled.");
- }
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
- if (cmd.getName().equalsIgnoreCase("loadchunks")) {
- //if the world exists
- if (getServer().getWorld("world_chunkloader") != null) {
- //delete it
- World world = getServer().getWorld("world_chunkloader");
- getServer().unloadWorld(world, false);
- delDirectory(world.getWorldFolder());
- logger.info("World deleted.");
- //create a new world
- getServer().createWorld(new WorldCreator("world_chunkloader"));
- logger.info("World created.");
- }else { //if the world doesn't exist
- //create a new world
- getServer().createWorld(new WorldCreator("world_chunkloader"));
- logger.info("World created.");
- }
- //sets the width of the square of chunks to be loaded
- if (args.length >= 1) {
- try {
- size = Integer.parseInt(args[0]);
- }catch (NumberFormatException e) {
- }
- }
- World world = getServer().getWorld("world_chunkloader");
- logger.info("Preparing world..");
- //unloads any chunks loaded in the world, hopefully cuts down on lag
- for (Chunk chunk : world.getLoadedChunks()) {
- chunk.unload(true);
- }
- logger.info("Loading chunks..");
- for (int x = 0; x<size; x++) {
- for (int z = 0; z<size; z++) {
- // 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.
- allChunks.add(world.getName() + "," + x + "," + z);
- }
- }
- for (int x = 0; x<size; x++) {
- for (int z = 0; z<size; z++) {
- //if the chunk is loaded
- if (world.isChunkLoaded(x, z)) {
- //unload it, makes sure that every chunk in the square gets handled by the listeners
- world.unloadChunk(x, z);
- }
- //load the chunk
- world.loadChunk(x, z);
- }
- }
- while (!allChunks.isEmpty()) {
- logger.info("Not empty!");
- //not a good way of doing it, but attempts to load any chunks the listeners missed
- //because it does happen occasionally if enough chunks are passed into it.
- for (int i = 0; i<allChunks.size(); i++) {
- String s = allChunks.get(i);
- int x = 0;
- int z = 0;
- try {x = Integer.parseInt(s.split(",")[1]);}catch(NumberFormatException e) {}
- try {z = Integer.parseInt(s.split(",")[2]);}catch(NumberFormatException e) {}
- if (world.isChunkLoaded(x, z)) {
- world.unloadChunk(x, z);
- }
- world.loadChunk(x, z);
- }
- }
- return true;
- }
- return false;
- }
- @EventHandler
- public void onLoad(ChunkLoadEvent event) { //called when the chunk is loaded in the world
- final Chunk chunk = event.getChunk();
- final String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
- final int index = allChunks.indexOf(key);
- final boolean newChunk = event.isNewChunk();
- //if the chunk is in the list and has already been generated
- if (index != -1 && !newChunk) {
- //remove it, unload it
- allChunks.remove(index);
- chunk.unload(true);
- if (allChunks.size() % size == 0) {
- logger.info("(" + (size - (allChunks.size() / size)) + "/" + size + ")");
- }
- }
- }
- @EventHandler
- public void onPopulate(ChunkPopulateEvent event) { //called after the chunks finish being generated
- final Chunk chunk = event.getChunk();
- final String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
- final int index = allChunks.indexOf(key);
- //if the chunk if in the list
- if (index != -1) {
- //remove it, unload it
- allChunks.remove(index);
- chunk.unload(true);
- if (allChunks.size() % size == 0) {
- logger.info("(" + (size - (allChunks.size() / size)) + "/" + size + ")");
- }
- }
- }
- public void delDirectory(File file) { //deletes the world directory
- if (file.isDirectory()) {
- File[] files = file.listFiles();
- if (files.length != 0) {
- for (File f : files) {
- delDirectory(f);
- }
- }else {
- file.delete();
- }
- }else {
- file.delete();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement