FriedrichLP

Untitled

Mar 26th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package friedrichlp.tsc.asm.mixins.world;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.concurrent.Callable;
  7. import java.util.zip.Deflater;
  8. import java.util.zip.GZIPOutputStream;
  9.  
  10. import org.spongepowered.asm.mixin.Mixin;
  11. import org.spongepowered.asm.mixin.Overwrite;
  12.  
  13. import friedrichlp.tsc.TSC;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.util.math.ChunkPos;
  16. import net.minecraft.world.World;
  17. import net.minecraft.world.chunk.Chunk;
  18. import net.minecraft.world.chunk.storage.AnvilChunkLoader;
  19. import net.minecraft.world.gen.ChunkProviderServer;
  20. import net.minecraftforge.common.MinecraftForge;
  21. import net.minecraftforge.common.chunkio.ChunkIOExecutor;
  22. import net.minecraftforge.event.world.ChunkDataEvent;
  23.  
  24. @Mixin(ChunkIOExecutor.class)
  25. public class MixinChunkIOExecutor {
  26. private static List<Entry> chunksToLoad = new ArrayList<Entry>();
  27. private static long ticks;
  28.  
  29. @Overwrite
  30. public static void queueChunkLoad(World world, AnvilChunkLoader loader, ChunkProviderServer provider, int x, int z, Runnable runnable) {
  31. Entry e = new Entry(world, loader, provider, x, z);
  32. e.callbacks.add(runnable);
  33.  
  34. chunksToLoad.add(e);
  35. }
  36.  
  37. @Overwrite
  38. public static void dropQueuedChunkLoad(World world, int x, int z, Runnable runnable) {
  39. Entry entry = null;
  40.  
  41. for (Entry e : chunksToLoad) {
  42. if (e.x == x && e.z == z) {
  43. e.callbacks.remove(runnable);
  44.  
  45. if (e.callbacks.isEmpty()) {
  46. entry = e;
  47. }
  48.  
  49. break;
  50. }
  51. }
  52.  
  53. if (entry != null) {
  54. chunksToLoad.remove(entry);
  55. }
  56. }
  57.  
  58. @Overwrite
  59. public static void tick () {
  60. if (ticks % 5 == 0) {
  61. try {
  62. TSC.WORKER_THREADS_EXECUTOR.invokeAll(chunksToLoad);
  63. } catch (InterruptedException ex) {
  64. ex.printStackTrace();
  65. }
  66.  
  67. List<Entry> toRemove = new ArrayList<Entry>();
  68. chunksToLoad.forEach(e -> {
  69. if (e.run) {
  70. e.finish();
  71. toRemove.add(e);
  72. }
  73. });
  74.  
  75. chunksToLoad.removeAll(toRemove);
  76. }
  77.  
  78. ticks++;
  79. }
  80.  
  81. private static class Entry implements Callable<Void> {
  82. private World w;
  83. private AnvilChunkLoader loader;
  84. private ChunkProviderServer provider;
  85. private int x;
  86. private int z;
  87. private List<Runnable> callbacks = new ArrayList<Runnable>();
  88. private boolean run;
  89.  
  90. private Chunk c;
  91. private NBTTagCompound nbt;
  92.  
  93. private Entry (World w, AnvilChunkLoader loader, ChunkProviderServer provider, int x, int z) {
  94. this.w = w;
  95. this.loader = loader;
  96. this.provider = provider;
  97. this.x = x;
  98. this.z = z;
  99. }
  100.  
  101. private void runCallbacks () {
  102. this.callbacks.forEach(Runnable::run);
  103. }
  104.  
  105. private void finish () {
  106. if (this.c == null) {
  107. this.runCallbacks();
  108. return;
  109. }
  110.  
  111. // Load Entities
  112. this.loader.loadEntities(this.w, this.nbt.getCompoundTag("Level"), this.c);
  113.  
  114. MinecraftForge.EVENT_BUS.post(new ChunkDataEvent.Load(this.c, this.nbt)); // Don't call ChunkDataEvent.Load async
  115.  
  116. this.c.setLastSaveTime(provider.world.getTotalWorldTime());
  117. this.provider.chunkGenerator.recreateStructures(this.c, this.x, this.z);
  118.  
  119. provider.id2ChunkMap.put(ChunkPos.asLong(this.x, this.z), this.c);
  120. this.c.onLoad();
  121. this.c.populate(provider, provider.chunkGenerator);
  122.  
  123. this.runCallbacks();
  124. }
  125.  
  126. @Override
  127. public Void call() throws Exception {
  128. try {
  129. Object[] data = null;
  130. try {
  131. data = this.loader.loadChunk__Async(this.w, this.x, this.z);
  132. } catch (IOException e) {
  133. throw new RuntimeException(e); // Allow exception to bubble up to afterExecute
  134. }
  135.  
  136. if (data != null) {
  137. this.nbt = (NBTTagCompound)data[1];
  138. this.c = (Chunk)data[0];
  139. }
  140. } finally {
  141. this.run = true;
  142. }
  143.  
  144. return null;
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment