HashChunkletManager.java 7.1 KB

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