Advertisement
tahg

Untitled

Jul 23rd, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. package org.bukkit.craftbukkit.util;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.DataInputStream;
  5. import java.io.IOException;
  6. import java.util.BitSet;
  7.  
  8. import net.minecraft.server.Chunk;
  9.  
  10. public class TickQueue {
  11. public BitSet blockCache = new BitSet(32768);
  12. public BitSet columnCache = new BitSet(256);
  13. public LongHeap heap = new LongHeap();
  14.  
  15. /**
  16. * Each long has on the inside:
  17. * 32bit time
  18. * 1bit isColumn
  19. * 4bit x
  20. * 4bit z
  21. * 7bit y
  22. * 16bit data
  23. */
  24.  
  25. public synchronized void insert(long entry) {
  26. int coord = (int) ((entry >> 16) & 0xFFFF);
  27.  
  28. BitSet cache = blockCache;
  29. int cacheEntry = coord & 0x7FFF;
  30.  
  31. // isColumn
  32. if (((entry >> 31) & 0x1) == 1) {
  33. cache = columnCache;
  34. cacheEntry = (coord >> 7) & 0xFF;
  35. }
  36.  
  37. if (cache.get(cacheEntry)) {
  38. heap.removeByCoord(coord);
  39. } else {
  40. cache.set(cacheEntry);
  41. }
  42.  
  43. heap.insert(entry);
  44. }
  45.  
  46. public synchronized long pop() {
  47. long entry = heap.popHead();
  48.  
  49. // isColumn
  50. if (((entry >> 31) & 0x1) == 1) {
  51. columnCache.clear((int) ((entry >> 7) & 0xFF));
  52. } else {
  53. blockCache.clear((int) ((entry >> 16) & 0x7FFF));
  54. }
  55.  
  56. return entry;
  57. }
  58.  
  59. public long peek() {
  60. if (heap.isEmpty()) return Long.MAX_VALUE;
  61. return heap.peekHead();
  62. }
  63.  
  64. public void fromByteArray(byte[] data, boolean hasRestarted, int deltaTime) {
  65. long value;
  66. int newTime;
  67. ByteArrayInputStream bis = new ByteArrayInputStream(data);
  68. DataInputStream dis = new DataInputStream(bis);
  69.  
  70. try {
  71. while(dis.available() > 0) {
  72. value = dis.readLong();
  73. newTime = (int) (value >>> 32);
  74. if (hasRestarted) {
  75. newTime += deltaTime;
  76. if (newTime < 0) newTime = 0;
  77.  
  78. value = (value & 0xFFFFFFFF) | (((long) newTime) << 32);
  79. }
  80.  
  81. // Due to an early bug; only queue stuff that is supposed to happen within the next hour
  82. if (newTime < 20 * 60 * 60)
  83. this.insert(value);
  84. }
  85. } catch (IOException e) {
  86. // yeah blah memory, i dont care.
  87. } finally {
  88. try {
  89. dis.close();
  90. bis.close();
  91. } catch (IOException e) {
  92. }
  93. }
  94. }
  95.  
  96. public byte[] toByteArray() {
  97. return heap.toByteArray();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement