Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package net.pixelcade.miner.objs;
  2.  
  3. import java.util.UUID;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity;
  10. import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
  11. import org.bukkit.entity.Entity;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.block.BlockBreakEvent;
  14. import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
  15. import org.bukkit.scheduler.BukkitTask;
  16.  
  17. import net.citizensnpcs.api.npc.NPC;
  18. import net.md_5.bungee.api.ChatColor;
  19. import net.minecraft.server.v1_12_R1.PacketPlayOutAnimation;
  20. import net.pixelcade.miner.Driver;
  21.  
  22. public class Miner {
  23.  
  24. private NPC npc;
  25. private long timeLeft;
  26. private UUID uuid;
  27. private BukkitTask id;
  28.  
  29. public Miner(UUID uuid, NPC npc, long timeLeft) {
  30. this.uuid = uuid;
  31. this.npc = npc;
  32. this.timeLeft = timeLeft;
  33. }
  34.  
  35. public NPC getNpc() {
  36. return npc;
  37. }
  38.  
  39. public void setNpc(NPC npc) {
  40. this.npc = npc;
  41. }
  42.  
  43. public long getTimeLeft() {
  44. return timeLeft;
  45. }
  46.  
  47. public void setTimeLeft(long timeLeft) {
  48. this.timeLeft = timeLeft;
  49. }
  50.  
  51. public UUID getUuid() {
  52. return uuid;
  53. }
  54.  
  55. public void setUuid(UUID uuid) {
  56. this.uuid = uuid;
  57. }
  58.  
  59. public void stopMining(Driver plugin) {
  60. if (this.id != null) {
  61. plugin.getServer().getScheduler().cancelTask(this.id.getTaskId());
  62. plugin.getMinerManger().getJsonMinerManager().saveMiner(getThis());
  63. }
  64. }
  65.  
  66. public Miner getThis() {
  67. return this;
  68. }
  69.  
  70. public void startMining(Driver plugin) {
  71. this.id = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
  72.  
  73. int counter = 0;
  74.  
  75. public void run() {
  76.  
  77. setTimeLeft(getTimeLeft() - 250);
  78.  
  79. if (counter >= Integer.MAX_VALUE - 1000) {
  80. counter = 0;
  81. }
  82.  
  83. counter += 5;
  84.  
  85. if (counter % 1200 == 0) {
  86. plugin.getMinerManger().getJsonMinerManager().saveMiner(getThis());
  87. }
  88.  
  89. if (getTimeLeft() <= 0) {
  90. setTimeLeft(0);
  91. plugin.getMinerManger().getJsonMinerManager().saveMiner(getThis());
  92. stopMining(plugin);
  93. npc.despawn();
  94. npc.destroy();
  95. Bukkit.getPlayer(uuid).sendMessage(ChatColor.RED + "You are all out of mining time for today.");
  96. return;
  97. }
  98.  
  99. int radius = 5;
  100. int cx = npc.getEntity().getLocation().getBlockX();
  101. int cy = npc.getEntity().getLocation().getBlockY();
  102. int cz = npc.getEntity().getLocation().getBlockZ();
  103.  
  104. for (int x = cx - radius; x <= cx + radius; x++) {
  105. for (int z = cz - radius; z <= cz + radius; z++) {
  106. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + ((cy - cy) * (cy - cy));
  107.  
  108. if (dist < radius * radius) {
  109. Location l = new Location(npc.getEntity().getWorld(), x, cy, z);
  110.  
  111. if (l.getBlock() != null) {
  112. if (plugin.getMinesPlugin().getAPI().getMinesByLocation(l).size() >= 1) {
  113. if (!(l.getBlock().getType().equals(Material.BEDROCK))) {
  114. if (!(l.getBlock().getType().equals(Material.AIR))) {
  115. npc.getNavigator().setTarget(l);
  116. BlockBreakEvent event = new BlockBreakEvent(l.getBlock(),
  117. Bukkit.getPlayer(uuid));
  118. Bukkit.getServer().getPluginManager().callEvent(event);
  119. sendBreak(Bukkit.getPlayer(uuid), npc.getEntity());
  120. return;
  121. }
  122. }
  123. }
  124. }
  125.  
  126. // mine out the blocks homieg.
  127.  
  128. }
  129. }
  130. }
  131.  
  132. // if we got here we didn't mine. Move down one.
  133. if (!npc.getNavigator().isNavigating()) {
  134. Block below = npc.getEntity().getLocation().subtract(0, 1, 0).getBlock();
  135. if (below != null) {
  136. if (plugin.getMinesPlugin().getAPI().getMinesByLocation(below.getLocation()).size() >= 1) {
  137. if (!(below.getType().equals(Material.BEDROCK))) {
  138. if (!(below.getType().equals(Material.AIR))) {
  139. npc.getNavigator().setTarget(below.getLocation());
  140. npc.teleport(below.getLocation(), TeleportCause.PLUGIN);
  141. below.setType(Material.AIR);
  142. sendBreak(Bukkit.getPlayer(uuid), npc.getEntity());
  143. return;
  144. } else {
  145. npc.teleport(below.getLocation(), TeleportCause.PLUGIN);
  146. }
  147. }
  148. } else {
  149. if (plugin.getMinesPlugin().getAPI().getMinesByLocation(npc.getEntity().getLocation())
  150. .size() >= 1) {
  151. int y = plugin.getMinesPlugin().getAPI()
  152. .getMinesByLocation(npc.getEntity().getLocation()).get(0).getMineRegion()
  153. .getMaxPoint().getBlockY();
  154. Location location = npc.getEntity().getLocation();
  155. location.setY(y + 1);
  156. npc.getNavigator().setTarget(npc.getEntity().getLocation());
  157. npc.teleport(location, TeleportCause.PLUGIN);
  158. }
  159. }
  160. }
  161. }
  162.  
  163. }
  164.  
  165. }, 0, 5);
  166. }
  167.  
  168. public void sendBreak(Player player, Entity miner) {
  169. Player targetPlayer = player;
  170. PacketPlayOutAnimation animationPacket = new PacketPlayOutAnimation(((CraftEntity) miner).getHandle(), 0);
  171. ((CraftPlayer) targetPlayer).getHandle().playerConnection.sendPacket(animationPacket);
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement