HashChunkletManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package com.gmail.nossr50.util.blockmeta;
  2. import java.io.EOFException;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.StreamCorruptedException;
  10. import java.io.UTFDataFormatException;
  11. import java.util.HashMap;
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.World;
  14. import org.bukkit.block.Block;
  15. import com.gmail.nossr50.mcMMO;
  16. import com.gmail.nossr50.runnables.ChunkletUnloader;
  17. public class HashChunkletManager implements ChunkletManager {
  18. public HashMap<String, ChunkletStore> store = new HashMap<String, ChunkletStore>();
  19. @Override
  20. public void loadChunklet(int cx, int cy, int cz, World world) {
  21. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  22. File cxDir = new File(dataDir, "" + cx);
  23. if(!cxDir.exists()) return;
  24. File czDir = new File(cxDir, "" + cz);
  25. if(!czDir.exists()) return;
  26. File yFile = new File(czDir, "" + cy);
  27. if(!yFile.exists()) return;
  28. ChunkletStore in = deserializeChunkletStore(yFile);
  29. if(in != null) {
  30. store.put(world.getName() + "," + cx + "," + cz + "," + cy, in);
  31. }
  32. }
  33. @Override
  34. public void unloadChunklet(int cx, int cy, int cz, World world) {
  35. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  36. if(store.containsKey(world.getName() + "," + cx + "," + cz + "," + cy)) {
  37. File cxDir = new File(dataDir, "" + cx);
  38. if(!cxDir.exists()) cxDir.mkdir();
  39. File czDir = new File(cxDir, "" + cz);
  40. if(!czDir.exists()) czDir.mkdir();
  41. File yFile = new File(czDir, "" + cy);
  42. ChunkletStore out = store.get(world.getName() + "," + cx + "," + cz + "," + cy);
  43. serializeChunkletStore(out, yFile);
  44. store.remove(world.getName() + "," + cx + "," + cz + "," + cy);
  45. }
  46. }
  47. @Override
  48. public void loadChunk(int cx, int cz, World world) {
  49. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  50. File cxDir = new File(dataDir, "" + cx);
  51. if(!cxDir.exists()) return;
  52. File czDir = new File(cxDir, "" + cz);
  53. if(!czDir.exists()) return;
  54. for(int y = 0; y < 4; y++) {
  55. File yFile = new File(czDir, "" + y);
  56. if(!yFile.exists()) {
  57. continue;
  58. }
  59. ChunkletStore in = deserializeChunkletStore(yFile);
  60. if(in != null) {
  61. store.put(world.getName() + "," + cx + "," + cz + "," + y, in);
  62. }
  63. }
  64. }
  65. @Override
  66. public void unloadChunk(int cx, int cz, World world) {
  67. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  68. for(int y = 0; y < 4; y++) {
  69. if(store.containsKey(world.getName() + "," + cx + "," + cz + "," + y)) {
  70. File cxDir = new File(dataDir, "" + cx);
  71. if(!cxDir.exists()) cxDir.mkdir();
  72. File czDir = new File(cxDir, "" + cz);
  73. if(!czDir.exists()) czDir.mkdir();
  74. File yFile = new File(czDir, "" + y);
  75. ChunkletStore out = store.get(world.getName() + "," + cx + "," + cz + "," + y);
  76. serializeChunkletStore(out, yFile);
  77. store.remove(world.getName() + "," + cx + "," + cz + "," + y);
  78. }
  79. }
  80. }
  81. @Override
  82. public void chunkLoaded(int cx, int cz, World world) {
  83. //loadChunk(cx, cz, world);
  84. }
  85. @Override
  86. public void chunkUnloaded(int cx, int cz, World world) {
  87. ChunkletUnloader.addToList(cx, cx, world);
  88. }
  89. @Override
  90. public void saveWorld(World world) {
  91. String worldName = world.getName();
  92. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  93. if(!dataDir.exists())
  94. dataDir.mkdirs();
  95. for(String key : store.keySet()) {
  96. String[] info = key.split(",");
  97. if(worldName.equals(info[0])) {
  98. File cxDir = new File(dataDir, "" + info[1]);
  99. if(!cxDir.exists()) cxDir.mkdir();
  100. File czDir = new File(cxDir, "" + info[2]);
  101. if(!czDir.exists()) czDir.mkdir();
  102. File yFile = new File(czDir, "" + info[3]);
  103. serializeChunkletStore(store.get(key), yFile);
  104. }
  105. }
  106. }
  107. @Override
  108. public void unloadWorld(World world) {
  109. saveWorld(world);
  110. String worldName = world.getName();
  111. for(String key : store.keySet()) {
  112. String tempWorldName = key.split(",")[0];
  113. if(tempWorldName.equals(worldName)) {
  114. store.remove(key);
  115. return;
  116. }
  117. }
  118. }
  119. @Override
  120. public void loadWorld(World world) {
  121. //for(Chunk chunk : world.getLoadedChunks()) {
  122. // this.chunkLoaded(chunk.getX(), chunk.getZ(), world);
  123. //}
  124. }
  125. @Override
  126. public void saveAll() {
  127. for(World world : Bukkit.getWorlds()) {
  128. saveWorld(world);
  129. }
  130. }
  131. @Override
  132. public void unloadAll() {
  133. saveAll();
  134. for(World world : Bukkit.getWorlds()) {
  135. unloadWorld(world);
  136. }
  137. }
  138. @Override
  139. public boolean isTrue(int x, int y, int z, World world) {
  140. int cx = x / 16;
  141. int cz = z / 16;
  142. int cy = y / 64;
  143. String key = world.getName() + "," + cx + "," + cz + "," + cy;
  144. if (!store.containsKey(key)) {
  145. loadChunklet(cx, cy, cz, world);
  146. }
  147. if (!store.containsKey(key)) {
  148. return false;
  149. }
  150. ChunkletStore check = store.get(world.getName() + "," + cx + "," + cz + "," + cy);
  151. int ix = Math.abs(x) % 16;
  152. int iz = Math.abs(z) % 16;
  153. int iy = Math.abs(y) % 64;
  154. return check.isTrue(ix, iy, iz);
  155. }
  156. @Override
  157. public boolean isTrue(Block block) {
  158. return isTrue(block.getX(), block.getY(), block.getZ(), block.getWorld());
  159. }
  160. @Override
  161. public void setTrue(int x, int y, int z, World world) {
  162. int cx = x / 16;
  163. int cz = z / 16;
  164. int cy = y / 64;
  165. int ix = Math.abs(x) % 16;
  166. int iz = Math.abs(z) % 16;
  167. int iy = Math.abs(y) % 64;
  168. String key = world.getName() + "," + cx + "," + cz + "," + cy;
  169. if (!store.containsKey(key)) {
  170. loadChunklet(cx, cy, cz, world);
  171. }
  172. ChunkletStore cStore = store.get(key);
  173. if (cStore == null) {
  174. cStore = ChunkletStoreFactory.getChunkletStore();
  175. store.put(world.getName() + "," + cx + "," + cz + "," + cy, cStore);
  176. }
  177. cStore.setTrue(ix, iy, iz);
  178. }
  179. @Override
  180. public void setTrue(Block block) {
  181. setTrue(block.getX(), block.getY(), block.getZ(), block.getWorld());
  182. }
  183. @Override
  184. public void setFalse(int x, int y, int z, World world) {
  185. int cx = x / 16;
  186. int cz = z / 16;
  187. int cy = y / 64;
  188. int ix = Math.abs(x) % 16;
  189. int iz = Math.abs(z) % 16;
  190. int iy = Math.abs(y) % 64;
  191. String key = world.getName() + "," + cx + "," + cz + "," + cy;
  192. if (!store.containsKey(key)) {
  193. loadChunklet(cx, cy, cz, world);
  194. }
  195. ChunkletStore cStore = store.get(key);
  196. if (cStore == null) {
  197. return; //No need to make a store for something we will be setting to false
  198. }
  199. cStore.setFalse(ix, iy, iz);
  200. }
  201. @Override
  202. public void setFalse(Block block) {
  203. setFalse(block.getX(), block.getY(), block.getZ(), block.getWorld());
  204. }
  205. @Override
  206. public void cleanUp() {
  207. for(String key : store.keySet()) {
  208. if(store.get(key).isEmpty()) {
  209. String[] info = key.split(",");
  210. File dataDir = new File(Bukkit.getWorld(info[0]).getWorldFolder(), "mcmmo_data");
  211. File cxDir = new File(dataDir, "" + info[1]);
  212. if(!cxDir.exists()) continue;
  213. File czDir = new File(cxDir, "" + info[2]);
  214. if(!czDir.exists()) continue;
  215. File yFile = new File(czDir, "" + info[3]);
  216. yFile.delete();
  217. //Delete empty directories
  218. if(czDir.list().length == 0) czDir.delete();
  219. if(cxDir.list().length == 0) cxDir.delete();
  220. }
  221. }
  222. }
  223. /**
  224. * @param cStore ChunkletStore to save
  225. * @param location Where on the disk to put it
  226. */
  227. private void serializeChunkletStore(ChunkletStore cStore, File location) {
  228. FileOutputStream fileOut = null;
  229. ObjectOutputStream objOut = null;
  230. try {
  231. if(!location.exists())
  232. location.createNewFile();
  233. fileOut = new FileOutputStream(location);
  234. objOut = new ObjectOutputStream(fileOut);
  235. objOut.writeObject(cStore);
  236. }
  237. catch (IOException ex) {
  238. ex.printStackTrace();
  239. }
  240. finally {
  241. if (objOut != null) {
  242. try {
  243. objOut.flush();
  244. objOut.close();
  245. }
  246. catch (IOException ex) {
  247. ex.printStackTrace();
  248. }
  249. }
  250. if (fileOut != null) {
  251. try {
  252. fileOut.close();
  253. }
  254. catch (IOException ex) {
  255. ex.printStackTrace();
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. * @param location Where on the disk to read from
  262. * @return ChunkletStore from the specified location
  263. */
  264. private ChunkletStore deserializeChunkletStore(File location) {
  265. ChunkletStore storeIn = null;
  266. FileInputStream fileIn = null;
  267. ObjectInputStream objIn = null;
  268. try {
  269. fileIn = new FileInputStream(location);
  270. objIn = new ObjectInputStream(fileIn);
  271. storeIn = (ChunkletStore) objIn.readObject();
  272. }
  273. catch (IOException ex) {
  274. if (ex instanceof EOFException) {
  275. // EOF should only happen on Chunklets that somehow have been corrupted.
  276. mcMMO.p.getLogger().severe("Chunklet data at " + location.toString() + " could not be read due to an EOFException, data in this area will be lost.");
  277. return ChunkletStoreFactory.getChunkletStore();
  278. }
  279. else if (ex instanceof StreamCorruptedException) {
  280. // StreamCorrupted happens when the Chunklet is no good.
  281. mcMMO.p.getLogger().severe("Chunklet data at " + location.toString() + " is corrupted, data in this area will be lost.");
  282. return ChunkletStoreFactory.getChunkletStore();
  283. }
  284. else if (ex instanceof UTFDataFormatException) {
  285. // UTF happens when the Chunklet cannot be read or is corrupted
  286. mcMMO.p.getLogger().severe("Chunklet data at " + location.toString() + " could not be read due to an UTFDataFormatException, data in this area will be lost.");
  287. return ChunkletStoreFactory.getChunkletStore();
  288. }
  289. ex.printStackTrace();
  290. }
  291. catch (ClassNotFoundException ex) {
  292. ex.printStackTrace();
  293. }
  294. finally {
  295. if (objIn != null) {
  296. try {
  297. objIn.close();
  298. }
  299. catch (IOException ex) {
  300. ex.printStackTrace();
  301. }
  302. }
  303. if (fileIn != null) {
  304. try {
  305. fileIn.close();
  306. }
  307. catch (IOException ex) {
  308. ex.printStackTrace();
  309. }
  310. }
  311. }
  312. // TODO: Make this less messy, as it is, it's kinda... depressing to do it like this.
  313. // Might also make a mess when we move to stacks, but at that point I think I will write a new Manager...
  314. // IMPORTANT! If ChunkletStoreFactory is going to be returning something other than PrimitiveEx we need to remove this, as it will be breaking time for old maps
  315. /*
  316. if(!(storeIn instanceof PrimitiveExChunkletStore)) {
  317. ChunkletStore tempStore = ChunkletStoreFactory.getChunkletStore();
  318. if(storeIn != null) {
  319. tempStore.copyFrom(storeIn);
  320. }
  321. storeIn = tempStore;
  322. }
  323. */
  324. return storeIn;
  325. }
  326. }