NullValkyrie

Minecraft 1.19 multipurpose plugin for spigot servers with a lot of features where most modern servers have.
git clone https://codeberg.org/night0721/NullValkyrie
Log | Files | Refs | README | LICENSE

CryptoMiner.java (10074B)


      1 package me.night.nullvalkyrie.entities.miners;
      2 
      3 import com.mojang.authlib.GameProfile;
      4 import com.mojang.authlib.properties.Property;
      5 import com.mojang.datafixers.util.Pair;
      6 import me.night.nullvalkyrie.NullValkyrie;
      7 import me.night.nullvalkyrie.database.MinerDataManager;
      8 import me.night.nullvalkyrie.packets.protocol.PacketPlayOutEntityMetadata;
      9 import me.night.nullvalkyrie.util.Util;
     10 import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
     11 import net.minecraft.network.protocol.game.ClientboundPlayerInfoPacket;
     12 import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
     13 import net.minecraft.network.syncher.EntityDataAccessor;
     14 import net.minecraft.network.syncher.EntityDataSerializers;
     15 import net.minecraft.network.syncher.SynchedEntityData;
     16 import net.minecraft.server.MinecraftServer;
     17 import net.minecraft.server.level.ServerLevel;
     18 import net.minecraft.server.level.ServerPlayer;
     19 import net.minecraft.server.network.ServerGamePacketListenerImpl;
     20 import net.minecraft.world.entity.EquipmentSlot;
     21 import net.minecraft.world.entity.decoration.ArmorStand;
     22 import org.bukkit.Bukkit;
     23 import org.bukkit.Location;
     24 import org.bukkit.Material;
     25 import org.bukkit.craftbukkit.v1_19_R1.CraftServer;
     26 import org.bukkit.craftbukkit.v1_19_R1.CraftWorld;
     27 import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;
     28 import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack;
     29 import org.bukkit.entity.Player;
     30 import org.bukkit.inventory.ItemStack;
     31 import org.bukkit.inventory.meta.LeatherArmorMeta;
     32 import org.bukkit.inventory.meta.SkullMeta;
     33 import org.bukkit.scheduler.BukkitRunnable;
     34 
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 import java.util.UUID;
     38 import java.util.concurrent.ThreadLocalRandom;
     39 
     40 public class CryptoMiner {
     41     protected String name;
     42     protected MinerType type;
     43     protected int level;
     44     protected double rate;
     45     protected final long lastclaim;
     46     protected final double x;
     47     protected final double y;
     48     protected final double z;
     49     protected final Location location;
     50 
     51     public CryptoMiner(String name, MinerType type, int level, double rate, long lastclaim, Location location) {
     52         this.name = name; // Name of the miner
     53         this.type = type; // Type of the miner
     54         this.level = level;
     55         this.rate = rate; // Percentage generate chance in each tick 20tick per sec
     56         this.lastclaim = lastclaim;
     57         this.x = location.getX();
     58         this.y = location.getY();
     59         this.z = location.getZ();
     60         this.location = location;
     61     }
     62 
     63     public String getName() {
     64         return name;
     65     }
     66 
     67     public void setName(String name) {
     68         this.name = name;
     69     }
     70 
     71     public double getRate() {
     72         return rate;
     73     }
     74 
     75     public Material getType() {
     76         return this.type.getMaterial();
     77     }
     78 
     79     public MinerType getMinerType() {
     80         return this.type;
     81     }
     82 
     83     public int getLevel() {
     84         return level;
     85     }
     86 
     87     public void setLevel(int level) {
     88         this.level = level;
     89     }
     90 
     91     public long getLastclaim() {
     92         return lastclaim;
     93     }
     94 
     95     public Location getLocation() {
     96         return location;
     97     }
     98 
     99     public static void generate(int pp, int times) {
    100         int generated = 0;
    101         for (int counter = 0; counter < times; counter++) {
    102             int count = ThreadLocalRandom.current().nextInt(100);
    103             if (count > pp) generated++;
    104         }
    105         System.out.println(generated);
    106     }
    107     public void spawn(Player player) {
    108         ArmorStand stand = new ArmorStand(((CraftWorld) this.getLocation().getWorld()).getHandle(), this.getLocation().getX() + 0.5, this.getLocation().getY(), this.getLocation().getZ() + 0.5);
    109         stand.setInvulnerable(true);
    110         ItemStack head = new ItemStack(Material.PLAYER_HEAD, 1);
    111         SkullMeta meta = (SkullMeta) head.getItemMeta();
    112         if (meta == null) return;
    113         GameProfile profile = new GameProfile(UUID.randomUUID(), null);
    114         // url method: new String(Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", this.getMinerType().getHeadTexture()).getBytes()));
    115         profile.getProperties().put("textures", new Property("textures", this.getMinerType().getHeadTexture()));
    116         try {
    117             Util.setFieldValue(meta, "profile", profile);
    118         } catch (Exception e) {
    119             throw new RuntimeException(e);
    120         }
    121         head.setItemMeta(meta);
    122         ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE);
    123         LeatherArmorMeta chestdata = (LeatherArmorMeta) chest.getItemMeta();
    124         if (chestdata == null) return;
    125         chestdata.setColor(org.bukkit.Color.fromRGB(2, 2, 58));
    126         chest.setItemMeta(chestdata);
    127         ItemStack leg = new ItemStack(Material.LEATHER_LEGGINGS);
    128         LeatherArmorMeta legdata = (LeatherArmorMeta) leg.getItemMeta();
    129         if (legdata == null) return;
    130         legdata.setColor(org.bukkit.Color.fromRGB(2, 2, 58));
    131         leg.setItemMeta(legdata);
    132         ItemStack boot = new ItemStack(Material.LEATHER_BOOTS);
    133         LeatherArmorMeta bootdata = (LeatherArmorMeta) boot.getItemMeta();
    134         if (bootdata == null) return;
    135         bootdata.setColor(org.bukkit.Color.fromRGB(2, 2, 58));
    136         boot.setItemMeta(bootdata);
    137         ItemStack pick = new ItemStack(Material.GOLDEN_PICKAXE);
    138         List<Pair<EquipmentSlot, net.minecraft.world.item.ItemStack>> list = new ArrayList<>();
    139         list.add(new Pair<>(EquipmentSlot.HEAD, CraftItemStack.asNMSCopy(head)));
    140         list.add(new Pair<>(EquipmentSlot.CHEST, CraftItemStack.asNMSCopy(chest)));
    141         list.add(new Pair<>(EquipmentSlot.LEGS, CraftItemStack.asNMSCopy(leg)));
    142         list.add(new Pair<>(EquipmentSlot.FEET, CraftItemStack.asNMSCopy(boot)));
    143         list.add(new Pair<>(EquipmentSlot.MAINHAND, CraftItemStack.asNMSCopy(pick)));
    144 
    145         GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Util.color(this.name));
    146         gameProfile.getProperties().put("textures", new Property("textures", this.type.getHeadTexture()));
    147         MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
    148         ServerLevel w = ((CraftWorld) player.getLocation().getWorld()).getHandle();
    149         ServerPlayer m = new ServerPlayer(server, w, gameProfile, null);
    150         ServerGamePacketListenerImpl pc = ((CraftPlayer) player).getHandle().connection;
    151         pc.send(new ClientboundAddEntityPacket(stand));
    152         pc.send(new ClientboundSetEquipmentPacket(stand.getId(), list));
    153         pc.send(new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.ADD_PLAYER, m));
    154         SynchedEntityData watcher = stand.getEntityData();
    155         watcher.set(new EntityDataAccessor<>(0, EntityDataSerializers.BYTE), (byte) 20);
    156         watcher.set(new EntityDataAccessor<>(5, EntityDataSerializers.BOOLEAN), true);
    157         watcher.set(new EntityDataAccessor<>(15, EntityDataSerializers.BYTE), (byte) 13);
    158         new PacketPlayOutEntityMetadata(player, stand, watcher);
    159         new BukkitRunnable() {
    160             @Override
    161             public void run() {
    162                 generate();
    163             }
    164         }.runTaskTimer(NullValkyrie.getPlugin(NullValkyrie.class), 0, 40);
    165     }
    166     public void generate() {
    167         List<Location> locs = new ArrayList<>();
    168         for (int x = (int) this.getLocation().getX() - 2; x <= this.getLocation().getX() + 2; x++) {
    169             for (int z = (int) this.getLocation().getZ() - 2; z <= this.getLocation().getZ() + 2; z++) {
    170                 for (int y = (int) this.getLocation().getY() - 1; y <= this.getLocation().getY() - 1; y++) {
    171                     this.getLocation().setY(17.0F);
    172                     if (this.getLocation().getWorld().getBlockAt(x, y, z).getType() == this.getType()) {
    173                         locs.add(this.getLocation().getWorld().getBlockAt(x, y, z).getLocation());
    174                     }
    175                 }
    176             }
    177         }
    178         if (locs.size() != 0) {
    179             Location closest = locs.get(0);
    180             for (Location location : locs)
    181                 if (location.distance(this.getLocation()) < closest.distance(this.getLocation()))
    182                     closest = location;
    183             ArrayList<ItemStack> items = new ArrayList<>();
    184             ThreadLocalRandom random = ThreadLocalRandom.current();
    185             closest.getBlock().getDrops().clear();
    186             List<int[]> levels = List.of(new int[]{1, 3}, new int[]{2, 5}, new int[]{3, 7}, new int[]{4, 9}, new int[]{5, 11}, new int[]{6, 13}, new int[]{7, 15}, new int[]{8, 17}, new int[]{9, 19}, new int[]{10, 21}, new int[]{11, 23}, new int[]{12, 25}, new int[]{13, 27}, new int[]{14, 29}, new int[]{15, 31}, new int[]{16, 33}, new int[]{17, 35}, new int[]{18, 37}, new int[]{19, 39}, new int[]{20, 41}, new int[]{21, 43}, new int[]{22, 45}, new int[]{23, 47}, new int[]{24, 49}, new int[]{25, 51}, new int[]{26, 53}, new int[]{27, 55}, new int[]{28, 57}, new int[]{29, 59}, new int[]{30, 61}, new int[]{31, 63}, new int[]{32, 65}, new int[]{33, 67}, new int[]{34, 69}, new int[]{35, 71}, new int[]{36, 73}, new int[]{37, 75}, new int[]{38, 77}, new int[]{39, 79}, new int[]{40, 81}, new int[]{41, 83}, new int[]{42, 85}, new int[]{43, 87}, new int[]{44, 89}, new int[]{45, 91}, new int[]{46, 93}, new int[]{47, 95}, new int[]{48, 97}, new int[]{49, 99}, new int[]{50, 100});
    187             items.add(new ItemStack(this.getType(), random.nextInt(levels.get(this.getLevel())[0], levels.get(this.getLevel())[1])));
    188             closest.getBlock().setType(Material.AIR);
    189             // drop the items
    190             for (ItemStack item : items) {
    191                 this.getLocation().add(0, 2, 0).getWorld().dropItemNaturally(closest, item);
    192             }
    193         }
    194     }
    195     public static void reloadMiner() {
    196         for (Player player : Bukkit.getOnlinePlayers())
    197             for (CryptoMiner miner : MinerDataManager.getMiners().values()) {
    198                 miner.spawn(player);
    199             }
    200     }
    201 
    202     public static void onJoin(Player player) {
    203         for (CryptoMiner miner : MinerDataManager.getMiners().values()) {
    204             miner.spawn(player);
    205         }
    206     }
    207 }