HashChunkletManager.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.Chunk;
  14. import org.bukkit.World;
  15. import org.bukkit.block.Block;
  16. import com.gmail.nossr50.mcMMO;
  17. public class HashChunkletManager implements ChunkletManager {
  18. private HashMap<String, ChunkletStore> store = new HashMap<String, ChunkletStore>();
  19. @Override
  20. public void chunkLoaded(int cx, 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. for(int y = 0; y < 4; y++) {
  27. File yFile = new File(czDir, "" + y);
  28. if(!yFile.exists()) {
  29. continue;
  30. } else {
  31. ChunkletStore in = deserializeChunkletStore(yFile);
  32. if(in != null) {
  33. store.put(world.getName() + "," + cx + "," + cz + "," + y, in);
  34. }
  35. }
  36. }
  37. }
  38. @Override
  39. public void chunkUnloaded(int cx, int cz, World world) {
  40. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  41. for(int y = 0; y < 4; y++) {
  42. if(store.containsKey(world.getName() + "," + cx + "," + cz + "," + y)) {
  43. File cxDir = new File(dataDir, "" + cx);
  44. if(!cxDir.exists()) cxDir.mkdir();
  45. File czDir = new File(cxDir, "" + cz);
  46. if(!czDir.exists()) czDir.mkdir();
  47. File yFile = new File(czDir, "" + y);
  48. ChunkletStore out = store.get(world.getName() + "," + cx + "," + cz + "," + y);
  49. serializeChunkletStore(out, yFile);
  50. store.remove(world.getName() + "," + cx + "," + cz + "," + y);
  51. }
  52. }
  53. }
  54. @Override
  55. public void saveWorld(World world) {
  56. String worldName = world.getName();
  57. File dataDir = new File(world.getWorldFolder(), "mcmmo_data");
  58. for(String key : store.keySet()) {
  59. String[] info = key.split(",");
  60. if(worldName.equals(info[0])) {
  61. File cxDir = new File(dataDir, "" + info[1]);
  62. if(!cxDir.exists()) cxDir.mkdir();
  63. File czDir = new File(cxDir, "" + info[2]);
  64. if(!czDir.exists()) czDir.mkdir();
  65. File yFile = new File(czDir, "" + info[3]);
  66. serializeChunkletStore(store.get(key), yFile);
  67. }
  68. }
  69. }
  70. @Override
  71. public void unloadWorld(World world) {
  72. saveWorld(world);
  73. String worldName = world.getName();
  74. for(String key : store.keySet()) {
  75. String tempWorldName = key.split(",")[0];
  76. if(tempWorldName.equals(worldName)) {
  77. store.remove(key);
  78. }
  79. }
  80. }
  81. @Override
  82. public void loadWorld(World world) {
  83. for(Chunk chunk : world.getLoadedChunks()) {
  84. this.chunkLoaded(chunk.getX(), chunk.getZ(), world);
  85. }
  86. }
  87. @Override
  88. public void saveAll() {
  89. for(World world : Bukkit.getWorlds()) {
  90. saveWorld(world);
  91. }
  92. }
  93. @Override
  94. public void unloadAll() {
  95. saveAll();
  96. for(World world : Bukkit.getWorlds()) {
  97. unloadWorld(world);
  98. }
  99. }
  100. @Override
  101. public boolean isTrue(int x, int y, int z, World world) {
  102. int cx = x / 16;
  103. int cz = z / 16;
  104. int cy = y / 64;
  105. if(!store.containsKey(world.getName() + "," + cx + "," + cz + "," + cy)) return false;
  106. ChunkletStore check = store.get(world.getName() + "," + cx + "," + cz + "," + cy);
  107. int ix = Math.abs(x) % 16;
  108. int iz = Math.abs(z) % 16;
  109. int iy = Math.abs(y) % 64;
  110. return check.isTrue(ix, iy, iz);
  111. }
  112. @Override
  113. public boolean isTrue(Block block) {
  114. return isTrue(block.getX(), block.getY(), block.getZ(), block.getWorld());
  115. }
  116. @Override
  117. public void setTrue(int x, int y, int z, World world) {
  118. int cx = x / 16;
  119. int cz = z / 16;
  120. int cy = y / 64;
  121. int ix = Math.abs(x) % 16;
  122. int iz = Math.abs(z) % 16;
  123. int iy = Math.abs(y) % 64;
  124. ChunkletStore cStore;
  125. if(!store.containsKey(world.getName() + "," + cx + "," + cz + "," + cy)) {
  126. cStore = ChunkletStoreFactory.getChunkletStore();
  127. store.put(world.getName() + "," + cx + "," + cz + "," + cy, cStore);
  128. }
  129. cStore = store.get(world.getName() + "," + cx + "," + cz + "," + cy);
  130. cStore.setTrue(ix, iy, iz);
  131. }
  132. @Override
  133. public void setTrue(Block block) {
  134. setTrue(block.getX(), block.getY(), block.getZ(), block.getWorld());
  135. }
  136. @Override
  137. public void setFalse(int x, int y, int z, World world) {
  138. int cx = x / 16;
  139. int cz = z / 16;
  140. int cy = y / 64;
  141. int ix = Math.abs(x) % 16;
  142. int iz = Math.abs(z) % 16;
  143. int iy = Math.abs(y) % 64;
  144. ChunkletStore cStore;
  145. if(!store.containsKey(world.getName() + "," + cx + "," + cz + "," + cy)) {
  146. return; // No need to make a store for something we will be setting to false
  147. }
  148. cStore = store.get(world.getName() + "," + cx + "," + cz + "," + cy);
  149. cStore.setFalse(ix, iy, iz);
  150. }
  151. @Override
  152. public void setFalse(Block block) {
  153. setFalse(block.getX(), block.getY(), block.getZ(), block.getWorld());
  154. }
  155. @Override
  156. public void cleanUp() {
  157. for(String key : store.keySet()) {
  158. if(store.get(key).isEmpty()) {
  159. String[] info = key.split(",");
  160. File dataDir = new File(Bukkit.getWorld(info[0]).getWorldFolder(), "mcmmo_data");
  161. File cxDir = new File(dataDir, "" + info[1]);
  162. if(!cxDir.exists()) continue;
  163. File czDir = new File(cxDir, "" + info[2]);
  164. if(!czDir.exists()) continue;
  165. File yFile = new File(czDir, "" + info[3]);
  166. yFile.delete();
  167. //Delete empty directories
  168. if(czDir.list().length == 0) czDir.delete();
  169. if(cxDir.list().length == 0) cxDir.delete();
  170. }
  171. }
  172. }
  173. /**
  174. * @param cStore ChunkletStore to save
  175. * @param location Where on the disk to put it
  176. */
  177. private void serializeChunkletStore(ChunkletStore cStore, File location) {
  178. try {
  179. FileOutputStream fileOut = new FileOutputStream(location);
  180. ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
  181. objOut.writeObject(cStore);
  182. objOut.close();
  183. fileOut.close();
  184. } catch (IOException ex) {
  185. ex.printStackTrace();
  186. }
  187. }
  188. /**
  189. * @param location Where on the disk to read from
  190. * @return ChunkletStore from the specified location
  191. */
  192. private ChunkletStore deserializeChunkletStore(File location) {
  193. ChunkletStore storeIn = null;
  194. try {
  195. FileInputStream fileIn = new FileInputStream(location);
  196. ObjectInputStream objIn = new ObjectInputStream(fileIn);
  197. storeIn = (ChunkletStore) objIn.readObject();
  198. objIn.close();
  199. fileIn.close();
  200. }
  201. catch (IOException ex) {
  202. if (ex instanceof EOFException) {
  203. // EOF should only happen on Chunklets that somehow have been corrupted.
  204. mcMMO.p.getLogger().severe("Chunklet data at " + location.toString() + " could not be read due to an EOFException, data in this area will be lost.");
  205. return ChunkletStoreFactory.getChunkletStore();
  206. }
  207. else if (ex instanceof StreamCorruptedException) {
  208. // StreamCorrupted happens when the Chunklet is no good.
  209. mcMMO.p.getLogger().severe("Chunklet data at " + location.toString() + " is corrupted, data in this area will be lost.");
  210. return ChunkletStoreFactory.getChunkletStore();
  211. }
  212. else if (ex instanceof UTFDataFormatException) {
  213. // UTF happens when the Chunklet cannot be read or is corrupted
  214. mcMMO.p.getLogger().severe("Chunklet data at " + location.toString() + " could not be read due to an UTFDataFormatException, data in this area will be lost.");
  215. return ChunkletStoreFactory.getChunkletStore();
  216. }
  217. ex.printStackTrace();
  218. }
  219. catch (ClassNotFoundException ex) {
  220. ex.printStackTrace();
  221. }
  222. return storeIn;
  223. }
  224. }