Advertisement
Guest User

Untitled

a guest
Dec 28th, 2015
1,801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.84 KB | None | 0 0
  1. package com.ngxdev.napi.async.v1_8_R3;
  2.  
  3. import com.ngxdev.napi.main.nApi;
  4. import net.minecraft.server.v1_8_R3.*;
  5. import org.apache.commons.lang.Validate;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Server;
  8. import org.bukkit.World;
  9. import org.bukkit.WorldCreator;
  10. import org.bukkit.configuration.ConfigurationSection;
  11. import org.bukkit.configuration.file.YamlConfiguration;
  12. import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
  13. import org.bukkit.craftbukkit.v1_8_R3.chunkio.ChunkIOExecutor;
  14. import org.bukkit.craftbukkit.v1_8_R3.util.LongHash;
  15. import org.bukkit.event.world.ChunkLoadEvent;
  16. import org.bukkit.event.world.WorldInitEvent;
  17. import org.bukkit.event.world.WorldLoadEvent;
  18. import org.bukkit.generator.ChunkGenerator;
  19. import org.bukkit.plugin.Plugin;
  20. import org.bukkit.scheduler.BukkitRunnable;
  21.  
  22. import java.io.File;
  23. import java.lang.reflect.Field;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26.  
  27. public class AsyncWorldLoader {
  28.     private static World ret = null;
  29.     private static boolean aborted = false;
  30.     private static boolean alreadyLoading = false;
  31.     private static ChunkGenerator generator;
  32.     private static Chunk wait = null;
  33.  
  34.     public static World createAsyncWorld(WorldCreator creator) {
  35.         while (alreadyLoading) {
  36.             try {
  37.                 Thread.sleep(50);
  38.             } catch (InterruptedException e) {
  39.                 e.printStackTrace();
  40.             }
  41.         }
  42.         alreadyLoading = true;
  43.         aborted = false;
  44.         wait = null;
  45.         generator = null;
  46.         ret = null;
  47.         Validate.notNull(creator, "Creator may not be null");
  48.  
  49.         String name = creator.name();
  50.         generator = creator.generator();
  51.         File folder = new File(getWorldContainer(), name);
  52.         World world = getCraftServer().getWorld(name);
  53.         net.minecraft.server.v1_8_R3.WorldType type = net.minecraft.server.v1_8_R3.WorldType.getType(creator.type().getName());
  54.         boolean generateStructures = creator.generateStructures();
  55.  
  56.         if (world != null) {
  57.             return world;
  58.         }
  59.  
  60.         if ((folder.exists()) && (!folder.isDirectory())) {
  61.             throw new IllegalArgumentException("File exists with the name '" + name + "' and isn't a folder");
  62.         }
  63.  
  64.         if (generator == null) {
  65.             generator = getGenerator(name);
  66.         }
  67.  
  68.         Convertable converter = new WorldLoaderServer(getWorldContainer());
  69.         if (converter.isConvertable(name)) {
  70.             Bukkit.getLogger().info("Converting world '" + name + "'");
  71.             converter.convert(name, new IProgressUpdate() {
  72.                 private long b = System.currentTimeMillis();
  73.  
  74.                 public void a(String s) {
  75.                 }
  76.  
  77.                 public void a(int i) {
  78.                     if (System.currentTimeMillis() - this.b >= 1000L) {
  79.                         this.b = System.currentTimeMillis();
  80.                         MinecraftServer.LOGGER.info("Converting... " + i + "%");
  81.                     }
  82.                 }
  83.  
  84.                 public void c(String s) {
  85.                 }
  86.             });
  87.         }
  88.         new BukkitRunnable() {
  89.             public void run() {
  90.                 int dimension2 = 10 + getServer().worlds.size();
  91.                 boolean used = false;
  92.                 do
  93.                     for (WorldServer server : getServer().worlds) {
  94.                         used = server.dimension == dimension2;
  95.                         if (used) {
  96.                             dimension2++;
  97.                             break;
  98.                         }
  99.                     }
  100.                 while (used);
  101.                 boolean hardcore = false;
  102.                 final int dimension = dimension2;
  103.                 new Thread() {
  104.                     public void run() {
  105.  
  106.                         Object sdm = new ServerNBTManager(getWorldContainer(),
  107.                                 name, true);
  108.                         WorldData worlddata = ((IDataManager) sdm)
  109.                                 .getWorldData();
  110.                         if (worlddata == null) {
  111.                             WorldSettings worldSettings = new WorldSettings(
  112.                                     creator.seed(),
  113.                                     WorldSettings.EnumGamemode
  114.                                             .getById(getCraftServer()
  115.                                                     .getDefaultGameMode()
  116.                                                     .getValue()),
  117.                                     generateStructures, hardcore, type);
  118.                             worldSettings.setGeneratorSettings(creator
  119.                                     .generatorSettings());
  120.                             worlddata = new WorldData(worldSettings, name);
  121.                         }
  122.                         worlddata.checkName(name);
  123.                         WorldServer internal = (WorldServer) new WorldServer(
  124.                                 getServer(), (IDataManager) sdm, worlddata,
  125.                                 dimension, getServer().methodProfiler,
  126.                                 creator.environment(), generator).b();
  127.                         new BukkitRunnable() {
  128.                             public void run() {
  129.                                 try {
  130.                                     Field w = CraftServer.class.getDeclaredField("worlds");
  131.                                     w.setAccessible(true);
  132.                                     if (!((Map<String, World>) w.get(getCraftServer())).containsKey(name.toLowerCase())) {
  133.                                         aborted = true;
  134.                                         return;
  135.                                     }
  136.                                 } catch (Exception e) {
  137.                                     e.printStackTrace();
  138.                                     aborted = true;
  139.                                     return;
  140.                                 }
  141.                                 new Thread() {
  142.                                     public void run() {
  143.                                         internal.scoreboard = getCraftServer()
  144.                                                 .getScoreboardManager()
  145.                                                 .getMainScoreboard()
  146.                                                 .getHandle();
  147.                                         internal.tracker = new EntityTracker(
  148.                                                 internal);
  149.                                         internal.addIWorldAccess(new WorldManager(
  150.                                                 getServer(), internal));
  151.                                         internal.worldData
  152.                                                 .setDifficulty(EnumDifficulty.EASY);
  153.                                         internal.setSpawnFlags(true, true);
  154.                                         getServer().worlds.add(internal);
  155.  
  156.                                         if (generator != null) {
  157.                                             internal.getWorld()
  158.                                                     .getPopulators()
  159.                                                     .addAll(generator
  160.                                                             .getDefaultPopulators(internal
  161.                                                                     .getWorld()));
  162.                                         }
  163.  
  164.                                         new BukkitRunnable() {
  165.                                             public void run() {
  166.                                                 Bukkit.getPluginManager()
  167.                                                         .callEvent(
  168.                                                                 new WorldInitEvent(
  169.                                                                         internal.getWorld()));
  170.                                             }
  171.                                         }.runTask(nApi.inst);
  172.                                         System.out
  173.                                                 .print("Preparing start region for level " + (getServer().worlds.size() - 1) + " (Seed: " + internal.getSeed() + ")");
  174.                                         if (internal.getWorld().getKeepSpawnInMemory()) {
  175.                                             short short1 = 196;
  176.                                             long i = System.currentTimeMillis();
  177.                                             for (int j = -short1; j <= short1; j += 16) {
  178.                                                 for (int k = -short1; k <= short1; k += 16) {
  179.                                                     long l = System.currentTimeMillis();
  180.  
  181.                                                     if (l < i) i = l;
  182.  
  183.                                                     if (l > i + 1000L) {
  184.                                                         int i1 = (short1 * 2 + 1) * (short1 * 2 + 1);
  185.                                                         int j1 = (j + short1) * (short1 * 2 + 1) + k + 1;
  186.  
  187.                                                         System.out.println("Preparing spawn area for " + name + ", " + j1 * 100 / i1 + "%");
  188.                                                         i = l;
  189.                                                     }
  190.  
  191.                                                     BlockPosition chunkcoordinates = internal.getSpawn();
  192.                                                     getChunkAt(internal.chunkProviderServer, chunkcoordinates.getX() + j >> 4, chunkcoordinates.getZ() + k >> 4);
  193.                                                 }
  194.                                             }
  195.                                         }
  196.                                         new BukkitRunnable() {
  197.                                             public void run() {
  198.                                                 Bukkit.getPluginManager().callEvent(new WorldLoadEvent(internal.getWorld()));
  199.                                             }
  200.                                         }.runTask(nApi.inst);
  201.                                         ret = internal.getWorld();
  202.                                     }
  203.  
  204.                                     private Chunk getChunkAt(ChunkProviderServer cps, int i, int j) {
  205.                                         Runnable runnable = null;
  206.                                         cps.unloadQueue.remove(i, j);
  207.                                         Chunk chunk = (Chunk)cps.chunks.get(LongHash.toLong(i, j));
  208.                                         ChunkRegionLoader loader = null;
  209.                                         try{
  210.                                             Field f = ChunkProviderServer.class.getDeclaredField("chunkLoader");
  211.                                             f.setAccessible(true);
  212.                                             if ((f.get(cps) instanceof ChunkRegionLoader)) {
  213.                                                 loader = (ChunkRegionLoader)f.get(cps);
  214.                                             }
  215.                                         }catch(Exception e){
  216.                                             e.printStackTrace();
  217.                                         }
  218.  
  219.                                         if ((chunk == null) && (loader != null) && (loader.chunkExists(cps.world, i, j))) {
  220.                                             final ChunkRegionLoader loader1 = loader;
  221.                                             wait = null;
  222.                                             new BukkitRunnable(){public void run(){
  223.                                                 wait = ChunkIOExecutor.syncChunkLoad(cps.world, loader1, cps, i, j);
  224.                                             }}.runTask(nApi.inst);
  225.                                             while(wait==null){
  226.                                                 try {
  227.                                                     Thread.sleep(10);
  228.                                                 } catch (InterruptedException e) {
  229.                                                     e.printStackTrace();
  230.                                                 }
  231.                                             }
  232.                                             chunk = wait;
  233.                                         }
  234.                                         else if (chunk == null) {
  235.                                             chunk = originalGetChunkAt(cps, i, j);
  236.                                         }
  237.  
  238.                                         if (runnable != null) {
  239.                                             runnable.run();
  240.                                         }
  241.  
  242.                                         return chunk;
  243.                                     }
  244.                                     public Chunk originalGetChunkAt(ChunkProviderServer cps, int i, int j) {
  245.                                         cps.unloadQueue.remove(i, j);
  246.                                         Chunk chunk = (Chunk)cps.chunks.get(LongHash.toLong(i, j));
  247.                                         boolean newChunk = false;
  248.  
  249.                                         if (chunk == null) {
  250.                                             cps.world.timings.syncChunkLoadTimer.startTiming();
  251.                                             chunk = cps.loadChunk(i, j);
  252.                                             if (chunk == null) {
  253.                                                 if (cps.chunkProvider == null)
  254.                                                     chunk = cps.emptyChunk;
  255.                                                 else {
  256.                                                     try {
  257.                                                         chunk = cps.chunkProvider.getOrCreateChunk(i, j);
  258.                                                     } catch (Throwable throwable) {
  259.                                                         CrashReport crashreport = CrashReport.a(throwable, "Exception generating new chunk");
  260.                                                         CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Chunk to be generated");
  261.  
  262.                                                         crashreportsystemdetails.a("Location", String.format("%d,%d", new Object[] { Integer.valueOf(i), Integer.valueOf(j) }));
  263.                                                         crashreportsystemdetails.a("Position hash", Long.valueOf(LongHash.toLong(i, j)));
  264.                                                         crashreportsystemdetails.a("Generator", cps.chunkProvider.getName());
  265.                                                         throw new ReportedException(crashreport);
  266.                                                     }
  267.                                                 }
  268.                                                 newChunk = true;
  269.                                             }
  270.  
  271.                                             cps.chunks.put(LongHash.toLong(i, j), chunk);
  272.                                             final Chunk chunki = chunk;
  273.                                             final boolean newChunki = newChunk;
  274.                                             new BukkitRunnable(){public void run(){
  275.                                                 chunki.addEntities();
  276.  
  277.                                                 Server server = cps.world.getServer();
  278.                                                 if (server != null)
  279.                                                 {
  280.                                                     server.getPluginManager().callEvent(new ChunkLoadEvent(chunki.bukkitChunk, newChunki));
  281.                                                 }
  282.                                             }}.runTask(nApi.inst);
  283.  
  284.                                             for (int x = -2; x < 3; x++) {
  285.                                                 for (int z = -2; z < 3; z++) {
  286.                                                     if ((x == 0) && (z == 0)) continue;
  287.  
  288.                                                     Chunk neighbor = cps.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
  289.                                                     if (neighbor != null) {
  290.                                                         neighbor.setNeighborLoaded(-x, -z);
  291.                                                         chunk.setNeighborLoaded(x, z);
  292.                                                     }
  293.                                                 }
  294.                                             }
  295.  
  296.                                             loadNearby(chunk, cps, cps, i, j);
  297.                                             cps.world.timings.syncChunkLoadTimer.stopTiming();
  298.                                         }
  299.  
  300.                                         return chunk;
  301.                                     }
  302.                                     public void loadNearby(Chunk c, IChunkProvider ichunkprovider, IChunkProvider ichunkprovider1, int i, int j) {
  303.                                         c.world.timings.syncChunkLoadPostTimer.startTiming();
  304.                                         boolean flag = ichunkprovider.isChunkLoaded(i, j - 1);
  305.                                         boolean flag1 = ichunkprovider.isChunkLoaded(i + 1, j);
  306.                                         boolean flag2 = ichunkprovider.isChunkLoaded(i, j + 1);
  307.                                         boolean flag3 = ichunkprovider.isChunkLoaded(i - 1, j);
  308.                                         boolean flag4 = ichunkprovider.isChunkLoaded(i - 1, j - 1);
  309.                                         boolean flag5 = ichunkprovider.isChunkLoaded(i + 1, j + 1);
  310.                                         boolean flag6 = ichunkprovider.isChunkLoaded(i - 1, j + 1);
  311.                                         boolean flag7 = ichunkprovider.isChunkLoaded(i + 1, j - 1);
  312.  
  313.                                         if ((flag1) && (flag2) && (flag5)) {
  314.                                             if (!c.isDone())
  315.                                                 getChunkAt((ChunkProviderServer) ichunkprovider1, i, j);
  316.                                             else {
  317.                                                 ichunkprovider.a(ichunkprovider1, c, i, j);
  318.                                             }
  319.  
  320.                                         }
  321.  
  322.                                         if ((flag3) && (flag2) && (flag6)) {
  323.                                             Chunk chunk = getOrCreateChunk((ChunkProviderServer) ichunkprovider, i - 1, j);
  324.                                             if (!chunk.isDone())
  325.                                                 getChunkAt((ChunkProviderServer) ichunkprovider1, i - 1, j);
  326.                                             else {
  327.                                                 ichunkprovider.a(ichunkprovider1, chunk, i - 1, j);
  328.                                             }
  329.                                         }
  330.  
  331.                                         if ((flag) && (flag1) && (flag7)) {
  332.                                             Chunk chunk = getOrCreateChunk((ChunkProviderServer) ichunkprovider, i, j - 1);
  333.                                             if (!chunk.isDone())
  334.                                                 getChunkAt((ChunkProviderServer) ichunkprovider1, i, j - 1);
  335.                                             else {
  336.                                                 ichunkprovider.a(ichunkprovider1, chunk, i, j - 1);
  337.                                             }
  338.                                         }
  339.  
  340.                                         if ((flag4) && (flag) && (flag3)) {
  341.                                             Chunk chunk = getOrCreateChunk((ChunkProviderServer) ichunkprovider, i - 1, j - 1);
  342.                                             if (!chunk.isDone())
  343.                                                 getChunkAt((ChunkProviderServer) ichunkprovider1, i - 1, j - 1);
  344.                                             else {
  345.                                                 ichunkprovider.a(ichunkprovider1, chunk, i - 1, j - 1);
  346.                                             }
  347.                                         }
  348.  
  349.                                         c.world.timings.syncChunkLoadPostTimer.stopTiming();
  350.                                     }
  351.                                     public boolean a(IChunkProvider ichunkprovider, Chunk chunk, int i, int j)
  352.                                     {
  353.                                         if ((ichunkprovider != null) && (ichunkprovider.a(ichunkprovider, chunk, i, j))) {
  354.                                             Chunk chunk1 = getOrCreateChunk((ChunkProviderServer) ichunkprovider, i, j);
  355.  
  356.                                             chunk1.e();
  357.                                             return true;
  358.                                         }
  359.                                         return false;
  360.                                     }
  361.  
  362.                                     private Chunk getOrCreateChunk(
  363.                                             ChunkProviderServer ip,
  364.                                             int i, int j) {
  365.  
  366.  
  367.                                         Chunk chunk = (Chunk)ip.chunks.get(LongHash.toLong(i, j));
  368.  
  369.                                         chunk = chunk == null ? getChunkAt(ip, i, j) : (!ip.world.ad()) && (!ip.forceChunkLoad) ? ip.emptyChunk : chunk;
  370.  
  371.                                         if (chunk == ip.emptyChunk) return chunk;
  372.                                         if ((i != chunk.locX) || (j != chunk.locZ)) {
  373.                                             System.err.println("Chunk (" + chunk.locX + ", " + chunk.locZ + ") stored at  (" + i + ", " + j + ") in world '" + ip.world.getWorld().getName() + "'");
  374.                                             System.err.println(chunk.getClass().getName());
  375.                                             Throwable ex = new Throwable();
  376.                                             ex.fillInStackTrace();
  377.                                             ex.printStackTrace();
  378.                                         }
  379.  
  380.                                         return chunk;
  381.  
  382.                                     }
  383.  
  384.                                 }.start();
  385.                             }
  386.                         }.runTask(nApi.inst);
  387.                     }
  388.                 }.start();
  389.             }
  390.         }.runTask(nApi.inst);
  391.         while (ret == null && !aborted) {
  392.             try {
  393.                 Thread.sleep(50);
  394.             } catch (InterruptedException e) {
  395.                 e.printStackTrace();
  396.             }
  397.         }
  398.         try {
  399.             Thread.sleep(1000);
  400.         } catch (InterruptedException e) {
  401.             e.printStackTrace();
  402.         }
  403.         alreadyLoading = false;
  404.         return ret;
  405.     }
  406.  
  407.     private static File getWorldContainer() {
  408.         if (getServer().universe != null) {
  409.             return getServer().universe;
  410.         }
  411.         try {
  412.             Field container = CraftServer.class.getDeclaredField("container");
  413.             container.setAccessible(true);
  414.             Field settings = CraftServer.class.getDeclaredField("configuration");
  415.             settings.setAccessible(true);
  416.             File co = (File) container.get(getCraftServer());
  417.             if (co == null) container.set(getCraftServer(), new File(((YamlConfiguration) settings.get(getCraftServer())).getString("settings.world-container", ".")));
  418.  
  419.             return (File) container.get(getCraftServer());
  420.         } catch (Exception e) {
  421.             e.printStackTrace();
  422.             return null;
  423.         }
  424.     }
  425.  
  426.     private static MinecraftServer getServer() {
  427.         return getCraftServer().getServer();
  428.     }
  429.  
  430.     private static CraftServer getCraftServer() {
  431.         return ((CraftServer) Bukkit.getServer());
  432.     }
  433.  
  434.     private static ChunkGenerator getGenerator(String world) {
  435.         try {
  436.             Field settings = CraftServer.class.getDeclaredField("configuration");
  437.             settings.setAccessible(true);
  438.             ConfigurationSection section = ((YamlConfiguration) settings.get(getCraftServer())).getConfigurationSection("worlds");
  439.             ChunkGenerator result = null;
  440.  
  441.             if (section != null) {
  442.                 section = section.getConfigurationSection(world);
  443.  
  444.                 if (section != null) {
  445.                     String name = section.getString("generator");
  446.  
  447.                     if ((name != null) && (!name.equals(""))) {
  448.                         String[] split = name.split(":", 2);
  449.                         String id = split.length > 1 ? split[1] : null;
  450.                         Plugin plugin = Bukkit.getPluginManager().getPlugin(split[0]);
  451.  
  452.                         if (plugin == null)
  453.                             Bukkit.getLogger().severe("Could not set generator for default world '" + world + "': Plugin '" + split[0] + "' does not exist");
  454.                         else if (!plugin.isEnabled())
  455.                             Bukkit.getLogger().severe("Could not set generator for default world '" + world + "': Plugin '" + plugin.getDescription().getFullName() + "' is not enabled yet (is it load:STARTUP?)");
  456.                         else {
  457.                             try {
  458.                                 result = plugin.getDefaultWorldGenerator(world, id);
  459.                                 if (result == null)
  460.                                     Bukkit.getLogger().severe("Could not set generator for default world '" + world + "': Plugin '" + plugin.getDescription().getFullName() + "' lacks a default world generator");
  461.                             } catch (Throwable t) {
  462.                                 plugin.getLogger().log(Level.SEVERE, "Could not set generator for default world '" + world + "': Plugin '" + plugin.getDescription().getFullName(), t);
  463.                             }
  464.                         }
  465.                     }
  466.                 }
  467.             }
  468.  
  469.             return result;
  470.         } catch (Exception e) {
  471.             e.printStackTrace();
  472.             return null;
  473.         }
  474.     }
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement