Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. package org.spigotmc;
  2.  
  3. import gnu.trove.set.TByteSet;
  4. import gnu.trove.set.hash.TByteHashSet;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import net.minecraft.server.v1_8_R3.Block;
  8. import net.minecraft.server.v1_8_R3.BlockPosition;
  9. import net.minecraft.server.v1_8_R3.Blocks;
  10. import net.minecraft.server.v1_8_R3.IBlockData;
  11. import net.minecraft.server.v1_8_R3.World;
  12. import org.bukkit.craftbukkit.v1_8_R3.util.CraftMagicNumbers;
  13.  
  14. public class AntiXray
  15. {
  16. private static final CustomTimingsHandler update = new CustomTimingsHandler("xray - update");
  17. private static final CustomTimingsHandler obfuscate = new CustomTimingsHandler("xray - obfuscate");
  18. private final boolean[] obfuscateBlocks = new boolean['?'];
  19. private final byte[] replacementOres;
  20.  
  21. public AntiXray(SpigotWorldConfig config)
  22. {
  23. for (Iterator localIterator1 = (config.engineMode == 1 ? config.hiddenBlocks : config.replaceBlocks).iterator(); localIterator1.hasNext();)
  24. {
  25. int id = ((Integer)localIterator1.next()).intValue();
  26.  
  27. this.obfuscateBlocks[id] = true;
  28. }
  29. TByteSet blocks = new TByteHashSet();
  30. for (Integer i : config.hiddenBlocks)
  31. {
  32. Block block = Block.getById(i.intValue());
  33. if ((block != null) && (!block.isTileEntity())) {
  34. blocks.add((byte)i.intValue());
  35. }
  36. }
  37. this.replacementOres = blocks.toArray();
  38. }
  39.  
  40. public void updateNearbyBlocks(World world, BlockPosition position)
  41. {
  42. if (world.spigotConfig.antiXray)
  43. {
  44. update.startTiming();
  45. updateNearbyBlocks(world, position, 2, false);
  46. update.stopTiming();
  47. }
  48. }
  49.  
  50. public void obfuscateSync(int chunkX, int chunkY, int bitmask, byte[] buffer, World world)
  51. {
  52. if (world.spigotConfig.antiXray)
  53. {
  54. obfuscate.startTiming();
  55. obfuscate(chunkX, chunkY, bitmask, buffer, world);
  56. obfuscate.stopTiming();
  57. }
  58. }
  59.  
  60. public void obfuscate(int chunkX, int chunkY, int bitmask, byte[] buffer, World world)
  61. {
  62. if (world.spigotConfig.antiXray)
  63. {
  64. int initialRadius = 1;
  65.  
  66. int index = 0;
  67.  
  68. int randomOre = 0;
  69.  
  70. int startX = chunkX << 4;
  71. int startZ = chunkY << 4;
  72. byte replaceWithTypeId;
  73. byte replaceWithTypeId;
  74. byte replaceWithTypeId;
  75. switch (world.getWorld().getEnvironment())
  76. {
  77. case NORMAL:
  78. replaceWithTypeId = (byte)CraftMagicNumbers.getId(Blocks.NETHERRACK);
  79. break;
  80. case THE_END:
  81. replaceWithTypeId = (byte)CraftMagicNumbers.getId(Blocks.END_STONE);
  82. break;
  83. default:
  84. replaceWithTypeId = (byte)CraftMagicNumbers.getId(Blocks.STONE);
  85. }
  86. for (int i = 0; i < 16; i++) {
  87. if ((bitmask & 1 << i) != 0) {
  88. for (int y = 0; y < 16; y++) {
  89. for (int z = 0; z < 16; z++) {
  90. for (int x = 0; x < 16; x++) {
  91. if (index >= buffer.length)
  92. {
  93. index++;
  94. }
  95. else
  96. {
  97. int blockId = buffer[(index << 1)] & 0xFF |
  98. (buffer[((index << 1) + 1)] & 0xFF) << 8;
  99. blockId >>>= 4;
  100. if (this.obfuscateBlocks[blockId] != 0)
  101. {
  102. if (!isLoaded(world, new BlockPosition(startX + x, (i << 4) + y, startZ + z), initialRadius))
  103. {
  104. index++;
  105. continue;
  106. }
  107. if (!hasTransparentBlockAdjacent(world, new BlockPosition(startX + x, (i << 4) + y, startZ + z), initialRadius))
  108. {
  109. int newId = blockId;
  110. switch (world.spigotConfig.engineMode)
  111. {
  112. case 1:
  113. newId = replaceWithTypeId & 0xFF;
  114. break;
  115. case 2:
  116. if (randomOre >= this.replacementOres.length) {
  117. randomOre = 0;
  118. }
  119. newId = this.replacementOres[(randomOre++)] & 0xFF;
  120. }
  121. newId = newId << 4;
  122. buffer[(index << 1)] = ((byte)(newId & 0xFF));
  123. buffer[((index << 1) + 1)] = ((byte)(newId >> 8 & 0xFF));
  124. }
  125. }
  126. index++;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135.  
  136. private void updateNearbyBlocks(World world, BlockPosition position, int radius, boolean updateSelf)
  137. {
  138. if (world.isLoaded(position))
  139. {
  140. Block block = world.getType(position).getBlock();
  141. if ((updateSelf) && (this.obfuscateBlocks[Block.getId(block)] != 0)) {
  142. world.notify(position);
  143. }
  144. if (radius > 0)
  145. {
  146. updateNearbyBlocks(world, position.east(), radius - 1, true);
  147. updateNearbyBlocks(world, position.west(), radius - 1, true);
  148. updateNearbyBlocks(world, position.up(), radius - 1, true);
  149. updateNearbyBlocks(world, position.down(), radius - 1, true);
  150. updateNearbyBlocks(world, position.south(), radius - 1, true);
  151. updateNearbyBlocks(world, position.north(), radius - 1, true);
  152. }
  153. }
  154. }
  155.  
  156. private static boolean isLoaded(World world, BlockPosition position, int radius)
  157. {
  158. return (world.isLoaded(position)) && (
  159. (radius == 0) || (
  160. (isLoaded(world, position.east(), radius - 1)) &&
  161. (isLoaded(world, position.west(), radius - 1)) &&
  162. (isLoaded(world, position.up(), radius - 1)) &&
  163. (isLoaded(world, position.down(), radius - 1)) &&
  164. (isLoaded(world, position.south(), radius - 1)) &&
  165. (isLoaded(world, position.north(), radius - 1))));
  166. }
  167.  
  168. private static boolean hasTransparentBlockAdjacent(World world, BlockPosition position, int radius)
  169. {
  170. return (!isSolidBlock(world.getType(position, false).getBlock())) || (
  171. (radius > 0) && (
  172. (hasTransparentBlockAdjacent(world, position.east(), radius - 1)) ||
  173. (hasTransparentBlockAdjacent(world, position.west(), radius - 1)) ||
  174. (hasTransparentBlockAdjacent(world, position.up(), radius - 1)) ||
  175. (hasTransparentBlockAdjacent(world, position.down(), radius - 1)) ||
  176. (hasTransparentBlockAdjacent(world, position.south(), radius - 1)) ||
  177. (hasTransparentBlockAdjacent(world, position.north(), radius - 1))));
  178. }
  179.  
  180. private static boolean isSolidBlock(Block block)
  181. {
  182. return (block.isOccluding()) && (block != Blocks.MOB_SPAWNER) && (block != Blocks.BARRIER);
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement