HashChunkletManager.java 7.7 KB

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