HashChunkManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.gmail.nossr50.util.blockmeta;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.World;
  4. import org.bukkit.block.Block;
  5. import org.bukkit.block.BlockState;
  6. import org.jetbrains.annotations.NotNull;
  7. import org.jetbrains.annotations.Nullable;
  8. import java.io.DataInputStream;
  9. import java.io.DataOutputStream;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.*;
  13. public class HashChunkManager implements ChunkManager {
  14. private final HashMap<CoordinateKey, McMMOSimpleRegionFile> regionMap = new HashMap<>(); // Tracks active regions
  15. private final HashMap<CoordinateKey, HashSet<CoordinateKey>> chunkUsageMap = new HashMap<>(); // Tracks active chunks by region
  16. private final HashMap<CoordinateKey, ChunkStore> chunkMap = new HashMap<>(); // Tracks active chunks
  17. @Override
  18. public synchronized void closeAll() {
  19. // Save all dirty chunkstores
  20. for (ChunkStore chunkStore : chunkMap.values())
  21. {
  22. if (!chunkStore.isDirty())
  23. continue;
  24. World world = Bukkit.getWorld(chunkStore.getWorldId());
  25. if (world == null)
  26. continue; // Oh well
  27. writeChunkStore(world, chunkStore);
  28. }
  29. // Clear in memory chunks
  30. chunkMap.clear();
  31. chunkUsageMap.clear();
  32. // Close all region files
  33. for (McMMOSimpleRegionFile rf : regionMap.values())
  34. rf.close();
  35. regionMap.clear();
  36. }
  37. private synchronized @Nullable ChunkStore readChunkStore(@NotNull World world, int cx, int cz) throws IOException {
  38. McMMOSimpleRegionFile rf = getReadableSimpleRegionFile(world, cx, cz);
  39. if (rf == null)
  40. return null; // If there is no region file, there can't be a chunk
  41. try (DataInputStream in = rf.getInputStream(cx, cz)) { // Get input stream for chunk
  42. if (in == null)
  43. return null; // No chunk
  44. return BitSetChunkStore.Serialization.readChunkStore(in); // Read in the chunkstore
  45. }
  46. }
  47. private synchronized void writeChunkStore(@NotNull World world, @NotNull ChunkStore data) {
  48. if (!data.isDirty())
  49. return; // Don't save unchanged data
  50. try {
  51. McMMOSimpleRegionFile rf = getWriteableSimpleRegionFile(world, data.getChunkX(), data.getChunkZ());
  52. try (DataOutputStream out = rf.getOutputStream(data.getChunkX(), data.getChunkZ())) {
  53. BitSetChunkStore.Serialization.writeChunkStore(out, data);
  54. }
  55. data.setDirty(false);
  56. }
  57. catch (IOException e) {
  58. throw new RuntimeException("Unable to write chunk meta data for " + data.getChunkX() + ", " + data.getChunkZ(), e);
  59. }
  60. }
  61. private synchronized @NotNull McMMOSimpleRegionFile getWriteableSimpleRegionFile(@NotNull World world, int cx, int cz) {
  62. CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz);
  63. return regionMap.computeIfAbsent(regionKey, k -> {
  64. File regionFile = getRegionFile(world, regionKey);
  65. regionFile.getParentFile().mkdirs();
  66. return new McMMOSimpleRegionFile(regionFile, regionKey.x, regionKey.z);
  67. });
  68. }
  69. private synchronized @Nullable McMMOSimpleRegionFile getReadableSimpleRegionFile(@NotNull World world, int cx, int cz) {
  70. CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz);
  71. return regionMap.computeIfAbsent(regionKey, k -> {
  72. File regionFile = getRegionFile(world, regionKey);
  73. if (!regionFile.exists())
  74. return null; // Don't create the file on read-only operations
  75. return new McMMOSimpleRegionFile(regionFile, regionKey.x, regionKey.z);
  76. });
  77. }
  78. private @NotNull File getRegionFile(@NotNull World world, @NotNull CoordinateKey regionKey) {
  79. if (world.getUID() != regionKey.worldID)
  80. throw new IllegalArgumentException();
  81. return new File(new File(world.getWorldFolder(), "mcmmo_regions"), "mcmmo_" + regionKey.x + "_" + regionKey.z + "_.mcm");
  82. }
  83. private @Nullable ChunkStore loadChunk(int cx, int cz, @NotNull World world) {
  84. try {
  85. return readChunkStore(world, cx, cz);
  86. }
  87. catch (Exception ignored) {}
  88. return null;
  89. }
  90. private void unloadChunk(int cx, int cz, @NotNull World world) {
  91. CoordinateKey chunkKey = toChunkKey(world.getUID(), cx, cz);
  92. ChunkStore chunkStore = chunkMap.remove(chunkKey); // Remove from chunk map
  93. if (chunkStore == null)
  94. return;
  95. if (chunkStore.isDirty())
  96. writeChunkStore(world, chunkStore);
  97. CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz);
  98. HashSet<CoordinateKey> chunkKeys = chunkUsageMap.get(regionKey);
  99. chunkKeys.remove(chunkKey); // remove from region file in-use set
  100. if (chunkKeys.isEmpty()) // If it was last chunk in region, close the region file and remove it from memory
  101. {
  102. chunkUsageMap.remove(regionKey);
  103. regionMap.remove(regionKey).close();
  104. }
  105. }
  106. @Override
  107. public synchronized void chunkUnloaded(int cx, int cz, @NotNull World world) {
  108. unloadChunk(cx, cz, world);
  109. }
  110. @Override
  111. public synchronized void unloadWorld(@NotNull World world) {
  112. UUID wID = world.getUID();
  113. // Save and remove all the chunks
  114. List<CoordinateKey> chunkKeys = new ArrayList<>(chunkMap.keySet());
  115. for (CoordinateKey chunkKey : chunkKeys) {
  116. if (!wID.equals(chunkKey.worldID))
  117. continue;
  118. ChunkStore chunkStore = chunkMap.remove(chunkKey);
  119. if (!chunkStore.isDirty())
  120. continue;
  121. try {
  122. writeChunkStore(world, chunkStore);
  123. }
  124. catch (Exception ignore) { }
  125. }
  126. // Clear all the region files
  127. List<CoordinateKey> regionKeys = new ArrayList<>(regionMap.keySet());
  128. for (CoordinateKey regionKey : regionKeys) {
  129. if (!wID.equals(regionKey.worldID))
  130. continue;
  131. regionMap.remove(regionKey).close();
  132. chunkUsageMap.remove(regionKey);
  133. }
  134. }
  135. private synchronized boolean isTrue(int x, int y, int z, @NotNull World world) {
  136. CoordinateKey chunkKey = blockCoordinateToChunkKey(world.getUID(), x, y, z);
  137. // Get chunk, load from file if necessary
  138. // Get/Load/Create chunkstore
  139. ChunkStore check = chunkMap.computeIfAbsent(chunkKey, k -> {
  140. // Load from file
  141. ChunkStore loaded = loadChunk(chunkKey.x, chunkKey.z, world);
  142. if (loaded == null)
  143. return null;
  144. // Mark chunk in-use for region tracking
  145. chunkUsageMap.computeIfAbsent(toRegionKey(chunkKey.worldID, chunkKey.x, chunkKey.z), j -> new HashSet<>()).add(chunkKey);
  146. return loaded;
  147. });
  148. // No chunk, return false
  149. if (check == null)
  150. return false;
  151. int ix = Math.abs(x) % 16;
  152. int iz = Math.abs(z) % 16;
  153. return check.isTrue(ix, y, iz);
  154. }
  155. @Override
  156. public synchronized boolean isTrue(@NotNull Block block) {
  157. return isTrue(block.getX(), block.getY(), block.getZ(), block.getWorld());
  158. }
  159. @Override
  160. public synchronized boolean isTrue(@NotNull BlockState blockState) {
  161. return isTrue(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld());
  162. }
  163. @Override
  164. public synchronized void setTrue(@NotNull Block block) {
  165. set(block.getX(), block.getY(), block.getZ(), block.getWorld(), true);
  166. }
  167. @Override
  168. public synchronized void setTrue(@NotNull BlockState blockState) {
  169. set(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld(), true);
  170. }
  171. @Override
  172. public synchronized void setFalse(@NotNull Block block) {
  173. set(block.getX(), block.getY(), block.getZ(), block.getWorld(), false);
  174. }
  175. @Override
  176. public synchronized void setFalse(@NotNull BlockState blockState) {
  177. set(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld(), false);
  178. }
  179. private synchronized void set(int x, int y, int z, @NotNull World world, boolean value){
  180. CoordinateKey chunkKey = blockCoordinateToChunkKey(world.getUID(), x, y, z);
  181. // Get/Load/Create chunkstore
  182. ChunkStore cStore = chunkMap.computeIfAbsent(chunkKey, k -> {
  183. // Load from file
  184. ChunkStore loaded = loadChunk(chunkKey.x, chunkKey.z, world);
  185. if (loaded != null)
  186. {
  187. chunkUsageMap.computeIfAbsent(toRegionKey(chunkKey.worldID, chunkKey.x, chunkKey.z), j -> new HashSet<>()).add(chunkKey);
  188. return loaded;
  189. }
  190. // If setting to false, no need to create an empty chunkstore
  191. if (!value)
  192. return null;
  193. // Mark chunk in-use for region tracking
  194. chunkUsageMap.computeIfAbsent(toRegionKey(chunkKey.worldID, chunkKey.x, chunkKey.z), j -> new HashSet<>()).add(chunkKey);
  195. // Create a new chunkstore
  196. return new BitSetChunkStore(world, chunkKey.x, chunkKey.z);
  197. });
  198. // Indicates setting false on empty chunkstore
  199. if (cStore == null)
  200. return;
  201. // Get block offset (offset from chunk corner)
  202. int ix = Math.abs(x) % 16;
  203. int iz = Math.abs(z) % 16;
  204. // Set chunk store value
  205. cStore.set(ix, y, iz, value);
  206. }
  207. private @NotNull CoordinateKey blockCoordinateToChunkKey(@NotNull UUID worldUid, int x, int y, int z) {
  208. return toChunkKey(worldUid, x >> 4, z >> 4);
  209. }
  210. private @NotNull CoordinateKey toChunkKey(@NotNull UUID worldUid, int cx, int cz){
  211. return new CoordinateKey(worldUid, cx, cz);
  212. }
  213. private @NotNull CoordinateKey toRegionKey(@NotNull UUID worldUid, int cx, int cz) {
  214. // Compute region index (32x32 chunk regions)
  215. int rx = cx >> 5;
  216. int rz = cz >> 5;
  217. return new CoordinateKey(worldUid, rx, rz);
  218. }
  219. private static final class CoordinateKey {
  220. public final @NotNull UUID worldID;
  221. public final int x;
  222. public final int z;
  223. private CoordinateKey(@NotNull UUID worldID, int x, int z) {
  224. this.worldID = worldID;
  225. this.x = x;
  226. this.z = z;
  227. }
  228. @Override
  229. public boolean equals(Object o) {
  230. if (this == o) return true;
  231. if (o == null || getClass() != o.getClass()) return false;
  232. CoordinateKey coordinateKey = (CoordinateKey) o;
  233. return x == coordinateKey.x &&
  234. z == coordinateKey.z &&
  235. worldID.equals(coordinateKey.worldID);
  236. }
  237. @Override
  238. public int hashCode() {
  239. return Objects.hash(worldID, x, z);
  240. }
  241. }
  242. }