Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.pixelcade.miner.objs;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity;
- import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
- import org.bukkit.scheduler.BukkitTask;
- import net.citizensnpcs.api.npc.NPC;
- import net.md_5.bungee.api.ChatColor;
- import net.minecraft.server.v1_12_R1.PacketPlayOutAnimation;
- import net.pixelcade.miner.Driver;
- public class Miner {
- private NPC npc;
- private long timeLeft;
- private UUID uuid;
- private BukkitTask id;
- public Miner(UUID uuid, NPC npc, long timeLeft) {
- this.uuid = uuid;
- this.npc = npc;
- this.timeLeft = timeLeft;
- }
- public NPC getNpc() {
- return npc;
- }
- public void setNpc(NPC npc) {
- this.npc = npc;
- }
- public long getTimeLeft() {
- return timeLeft;
- }
- public void setTimeLeft(long timeLeft) {
- this.timeLeft = timeLeft;
- }
- public UUID getUuid() {
- return uuid;
- }
- public void setUuid(UUID uuid) {
- this.uuid = uuid;
- }
- public void stopMining(Driver plugin) {
- if (this.id != null) {
- plugin.getServer().getScheduler().cancelTask(this.id.getTaskId());
- plugin.getMinerManger().getJsonMinerManager().saveMiner(getThis());
- }
- }
- public Miner getThis() {
- return this;
- }
- public void startMining(Driver plugin) {
- this.id = Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
- int counter = 0;
- public void run() {
- setTimeLeft(getTimeLeft() - 250);
- if (counter >= Integer.MAX_VALUE - 1000) {
- counter = 0;
- }
- counter += 5;
- if (counter % 1200 == 0) {
- plugin.getMinerManger().getJsonMinerManager().saveMiner(getThis());
- }
- if (getTimeLeft() <= 0) {
- setTimeLeft(0);
- plugin.getMinerManger().getJsonMinerManager().saveMiner(getThis());
- stopMining(plugin);
- npc.despawn();
- npc.destroy();
- Bukkit.getPlayer(uuid).sendMessage(ChatColor.RED + "You are all out of mining time for today.");
- return;
- }
- int radius = 5;
- int cx = npc.getEntity().getLocation().getBlockX();
- int cy = npc.getEntity().getLocation().getBlockY();
- int cz = npc.getEntity().getLocation().getBlockZ();
- for (int x = cx - radius; x <= cx + radius; x++) {
- for (int z = cz - radius; z <= cz + radius; z++) {
- double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + ((cy - cy) * (cy - cy));
- if (dist < radius * radius) {
- Location l = new Location(npc.getEntity().getWorld(), x, cy, z);
- if (l.getBlock() != null) {
- if (plugin.getMinesPlugin().getAPI().getMinesByLocation(l).size() >= 1) {
- if (!(l.getBlock().getType().equals(Material.BEDROCK))) {
- if (!(l.getBlock().getType().equals(Material.AIR))) {
- npc.getNavigator().setTarget(l);
- BlockBreakEvent event = new BlockBreakEvent(l.getBlock(),
- Bukkit.getPlayer(uuid));
- Bukkit.getServer().getPluginManager().callEvent(event);
- sendBreak(Bukkit.getPlayer(uuid), npc.getEntity());
- return;
- }
- }
- }
- }
- // mine out the blocks homieg.
- }
- }
- }
- // if we got here we didn't mine. Move down one.
- if (!npc.getNavigator().isNavigating()) {
- Block below = npc.getEntity().getLocation().subtract(0, 1, 0).getBlock();
- if (below != null) {
- if (plugin.getMinesPlugin().getAPI().getMinesByLocation(below.getLocation()).size() >= 1) {
- if (!(below.getType().equals(Material.BEDROCK))) {
- if (!(below.getType().equals(Material.AIR))) {
- npc.getNavigator().setTarget(below.getLocation());
- npc.teleport(below.getLocation(), TeleportCause.PLUGIN);
- below.setType(Material.AIR);
- sendBreak(Bukkit.getPlayer(uuid), npc.getEntity());
- return;
- } else {
- npc.teleport(below.getLocation(), TeleportCause.PLUGIN);
- }
- }
- } else {
- if (plugin.getMinesPlugin().getAPI().getMinesByLocation(npc.getEntity().getLocation())
- .size() >= 1) {
- int y = plugin.getMinesPlugin().getAPI()
- .getMinesByLocation(npc.getEntity().getLocation()).get(0).getMineRegion()
- .getMaxPoint().getBlockY();
- Location location = npc.getEntity().getLocation();
- location.setY(y + 1);
- npc.getNavigator().setTarget(npc.getEntity().getLocation());
- npc.teleport(location, TeleportCause.PLUGIN);
- }
- }
- }
- }
- }
- }, 0, 5);
- }
- public void sendBreak(Player player, Entity miner) {
- Player targetPlayer = player;
- PacketPlayOutAnimation animationPacket = new PacketPlayOutAnimation(((CraftEntity) miner).getHandle(), 0);
- ((CraftPlayer) targetPlayer).getHandle().playerConnection.sendPacket(animationPacket);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement