Guest User

Untitled

a guest
Sep 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Queue;
  3.  
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.World;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  10. import org.bukkit.material.MaterialData;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12. import org.bukkit.scheduler.BukkitTask;
  13.  
  14. import net.minecraft.server.v1_8_R3.BlockPosition;
  15. import net.minecraft.server.v1_8_R3.IBlockData;
  16.  
  17. /**
  18. * High Efficiency Multi Block Changer
  19. * @author LeeGod
  20. *
  21. * my first util like this, hope its great :P
  22. */
  23. public class MultiBlockChanger {
  24.  
  25. private String worldName;
  26.  
  27. private int maxChanges = 100;
  28.  
  29. private boolean async = false;
  30.  
  31. /* not sure does this thing have to exists at here, so i didnt code this */
  32. private boolean javaThread = false;
  33.  
  34. private Runnable callback = null;
  35.  
  36. private long tick = 5L;
  37.  
  38. private final Queue<BlockChange> blockChanges = new ArrayDeque<>();
  39.  
  40. public MultiBlockChanger(World world) {
  41. this.worldName = world.getName();
  42. }
  43.  
  44. public MultiBlockChanger(String worldName) {
  45. this.worldName = worldName;
  46. }
  47.  
  48. public String getWorldName() {
  49. return worldName;
  50. }
  51.  
  52. public int getMaxChanges() {
  53. return maxChanges;
  54. }
  55.  
  56. public boolean isAsync() {
  57. return async;
  58. }
  59.  
  60. public boolean isJavaThread() {
  61. return javaThread;
  62. }
  63.  
  64. public Runnable getCallback() {
  65. return callback;
  66. }
  67.  
  68. public long getTick() {
  69. return tick;
  70. }
  71.  
  72. public Queue<BlockChange> getBlockChanges(){
  73. return blockChanges;
  74. }
  75.  
  76. public MultiBlockChanger setWorldName(String worldName) {
  77. this.worldName = worldName;
  78. return this;
  79. }
  80.  
  81. public MultiBlockChanger setMaxChanges(int maxChanges) {
  82. this.maxChanges = maxChanges;
  83. return this;
  84. }
  85.  
  86. public MultiBlockChanger async() {
  87. this.async = true;
  88. return this;
  89. }
  90.  
  91. public MultiBlockChanger javaThread() {
  92. this.javaThread = true;
  93. return this;
  94. }
  95.  
  96. public MultiBlockChanger tick(long tick) {
  97. this.tick = tick;
  98. return this;
  99. }
  100.  
  101. public MultiBlockChanger callback(Runnable callback) {
  102. this.callback = callback;
  103. return this;
  104. }
  105.  
  106. public MultiBlockChanger addBlockChanges(Block block, MaterialData materialData) {
  107. this.blockChanges.add(new BlockChange(BlockVector.toBlockVector(block), materialData));
  108. return this;
  109. }
  110.  
  111. public MultiBlockChanger addBlockChanges(Location location, MaterialData materialData) {
  112. this.blockChanges.add(new BlockChange(BlockVector.toBlockVector(location), materialData));
  113. return this;
  114. }
  115.  
  116. public MultiBlockChanger addBlockChanges(Block block, Material material, byte data) {
  117. this.blockChanges.add(new BlockChange(BlockVector.toBlockVector(block), material, data));
  118. return this;
  119. }
  120.  
  121. public MultiBlockChanger addBlockChanges(Location location, Material material, byte data) {
  122. this.blockChanges.add(new BlockChange(BlockVector.toBlockVector(location), material, data));
  123. return this;
  124. }
  125.  
  126. public MultiBlockChanger addBlockChanges(Material material, byte data, Location... locations) {
  127. for (final Location location : locations) {
  128. this.blockChanges.add(new BlockChange(BlockVector.toBlockVector(location), material, data));
  129. }
  130. return this;
  131. }
  132.  
  133. public MultiBlockChanger addBlockChanges(Material material, byte data, Block... blocks) {
  134. for (final Block block : blocks) {
  135. this.blockChanges.add(new BlockChange(BlockVector.toBlockVector(block), material, data));
  136. }
  137. return this;
  138. }
  139.  
  140. private BukkitTask bukkitTask = null;
  141.  
  142. public void start(JavaPlugin plugin) {
  143.  
  144. final World world = Bukkit.getWorld(worldName);
  145.  
  146. final Runnable runnable = new Runnable() {
  147. private final Queue<SimpleChunk> chunksToRefresh = new ArrayDeque<SimpleChunk>() {
  148. private static final long serialVersionUID = 1L;
  149.  
  150. @Override
  151. public boolean add(SimpleChunk simpleChunk) {
  152. if (this.contains(simpleChunk)) {
  153. return false;
  154. }
  155. return super.add(simpleChunk);
  156. }
  157.  
  158. };
  159. private final Queue<BlockChange> blockChanges = new ArrayDeque<>(MultiBlockChanger.this.blockChanges);
  160.  
  161. private boolean blockSetDone = false;
  162.  
  163. @Override
  164. public void run() {
  165.  
  166. if (!blockChanges.isEmpty()) {
  167. for (int i = 0; i < maxChanges; i++) {
  168.  
  169. if (blockChanges.isEmpty()) {
  170. blockSetDone = true;
  171. return;
  172. }
  173.  
  174. final BlockChange blockChange = blockChanges.poll();
  175. final BlockVector blockVector = blockChange.getBlockVector();
  176. chunksToRefresh.add(new SimpleChunk(blockVector.getX() >> 4, blockVector.getZ() >> 4));
  177.  
  178. /*
  179. i make the block set to nms because its pertty great for performance
  180. you can change it to bukkit api if you want
  181. */
  182. final net.minecraft.server.v1_8_R3.World w = ((CraftWorld) world).getHandle();
  183. final net.minecraft.server.v1_8_R3.Chunk chunk = w.getChunkAt(blockVector.getX() >> 4, blockVector.getZ() >> 4);
  184. final BlockPosition bp = new BlockPosition(blockVector.getX(), blockVector.getY(), blockVector.getZ());
  185. final int combined = blockChange.getMaterialData().getItemTypeId() + (blockChange.getMaterialData().getData() << 12);
  186. final IBlockData ibd = net.minecraft.server.v1_8_R3.Block.getByCombinedId(combined);
  187. w.setTypeAndData(bp, ibd, 2);
  188. chunk.a(bp, ibd);
  189.  
  190. }
  191. }
  192.  
  193. if (!blockSetDone) {
  194. return;
  195. }
  196.  
  197. while (!chunksToRefresh.isEmpty()) {
  198. final SimpleChunk simpleChunk = chunksToRefresh.poll();
  199. world.refreshChunk(simpleChunk.getX(), simpleChunk.getZ());
  200. }
  201.  
  202. if (callback != null) {
  203. callback.run();
  204. }
  205. bukkitTask.cancel();
  206. return;
  207. }
  208. };
  209.  
  210. if (async) {
  211. bukkitTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, 0L, tick);
  212. return;
  213. }
  214. bukkitTask = Bukkit.getScheduler().runTaskTimer(plugin, runnable, 0L, tick);
  215. return;
  216. }
  217.  
  218. private static class BlockVector {
  219.  
  220. int x = 0, y = 0, z = 0;
  221.  
  222. private BlockVector() {}
  223.  
  224. public BlockVector(int x, int y, int z) {
  225. this.x = x;
  226. this.y = y;
  227. this.z = z;
  228. }
  229.  
  230. public int getX() {
  231. return x;
  232. }
  233.  
  234. public int getY() {
  235. return y;
  236. }
  237.  
  238. public int getZ() {
  239. return z;
  240. }
  241.  
  242. private static BlockVector toBlockVector(Block block) {
  243. return new BlockVector(block.getX(), block.getY(), block.getZ());
  244. }
  245.  
  246. private static BlockVector toBlockVector(Location location) {
  247. return new BlockVector(location.getBlockX(), location.getBlockY(), location.getBlockZ());
  248. }
  249.  
  250. }
  251.  
  252. private static class BlockChange {
  253.  
  254. private BlockVector blockVector;
  255. private MaterialData materialData;
  256.  
  257. private BlockChange() {}
  258.  
  259. public BlockChange(BlockVector blockVector, MaterialData materialData) {
  260. this.blockVector = blockVector;
  261. this.materialData = materialData;
  262. }
  263.  
  264. public BlockChange(BlockVector blockVector, Material material, byte data) {
  265. this.blockVector = blockVector;
  266. this.materialData = new MaterialData(material, data);
  267. }
  268.  
  269. public BlockVector getBlockVector() {
  270. return blockVector;
  271. }
  272.  
  273. public MaterialData getMaterialData() {
  274. return materialData;
  275. }
  276.  
  277. }
  278.  
  279. private static class SimpleChunk {
  280.  
  281. private int x = 0, z = 0;
  282.  
  283. private SimpleChunk() {}
  284.  
  285. public SimpleChunk(int x, int z) {
  286. this.x = x;
  287. this.z = z;
  288. }
  289.  
  290. public int getX() {
  291. return x;
  292. }
  293.  
  294. public int getZ() {
  295. return z;
  296. }
  297.  
  298. @Override
  299. public boolean equals(Object object) {
  300. if (!(object instanceof SimpleChunk)) {
  301. return false;
  302. }
  303. return ((SimpleChunk) object).getX() == this.x && ((SimpleChunk) object).getZ() == this.z;
  304. }
  305. }
  306.  
  307. }
Add Comment
Please, Sign In to add comment