From 4a3ec6bc657e1c877ea5ee2d82a5fe8ff29d15f2 Mon Sep 17 00:00:00 2001 From: NK Date: Tue, 20 Dec 2022 15:17:47 +0000 Subject: [PATCH] improvements for miners --- .../nullvalkyrie/commands/BetaCommand.java | 10 +- .../nullvalkyrie/commands/MinerCommand.java | 49 +++-- .../database/MinerDataManager.java | 14 +- .../entities/miners/CryptoMiner.java | 173 +++++++++--------- .../night/nullvalkyrie/enums/MinerType.java | 36 +++- 5 files changed, 173 insertions(+), 109 deletions(-) diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index 2b9497b..342686c 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -1,6 +1,8 @@ package me.night.nullvalkyrie.commands; +//import me.night.nullvalkyrie.entities.pets.ZombiePet; import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; import org.bukkit.entity.Player; import java.util.List; @@ -8,16 +10,16 @@ import java.util.List; public class BetaCommand extends Command { public BetaCommand() { - super("beta", new String[]{"b", "npc"}, "Beta", ""); + super("beta", new String[]{"b"}, "Beta", ""); } @Override public void onCommand(CommandSender sender, String[] args) { if (sender instanceof Player player) { -// CryptoMiner miner = new CryptoMiner(args[0], Material.DIAMOND_ORE, 1, 0.5, System.currentTimeMillis()); -// miner.spawn(player, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"); -// new LuckyDraw().UI(player); + + //ZombiePet a = new ZombiePet(player.getLocation(), player); + //((CraftWorld) player.getWorld()).getHandle().b(a); } } diff --git a/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java b/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java index 678a0a2..cfed762 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java @@ -1,47 +1,72 @@ package me.night.nullvalkyrie.commands; import me.night.nullvalkyrie.database.MinerDataManager; +import me.night.nullvalkyrie.entities.miners.CryptoMiner; +import me.night.nullvalkyrie.enums.MinerType; import me.night.nullvalkyrie.ui.inventory.Miner; -import org.bukkit.Material; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; -import java.util.Date; -import java.util.List; +import java.util.*; import static me.night.nullvalkyrie.entities.miners.CryptoMiner.generate; public class MinerCommand extends Command { public MinerCommand() { - super("miner", new String[]{"m", "miners"}, "Miner list", ""); + super("miner", new String[]{"miners"}, "Miners", ""); } @Override public void onCommand(CommandSender sender, String[] args) { if (sender instanceof Player player) { if (args.length == 0) { + player.sendMessage(ChatColor.RED + "Invalid command"); + return; + } + if (args[0].equalsIgnoreCase("list")) { new Miner().UI(player); int seconds = (int) (new Date().getTime() - MinerDataManager.getLastClaim(1)) / 1000; generate(50, seconds); } else if (args[0].equalsIgnoreCase("new")) { - String name = args[1]; - Material pick = Material.NETHERITE_AXE; + String name = args[2]; + MinerType type = MinerType.getByName(args[1]); int level = 20; double rate = 0.4; long time = System.currentTimeMillis(); - MinerDataManager.setNPC(name, pick, level, rate, true, time); + assert type != null; + MinerDataManager.setMiner(name, type, level, rate, true, time); + CryptoMiner miner = new CryptoMiner(name, type, level, rate, time); + miner.spawn(player); } else if (args[0].equalsIgnoreCase("claim")) { - String minerIndex = args[1]; - MinerDataManager.setLastClaim(Long.parseLong(minerIndex)); - player.sendMessage("Claimed"); + MinerDataManager.setLastClaim(args[1]); + player.sendMessage(ChatColor.GREEN + "Claimed"); } - } } @Override public List onTabComplete(CommandSender sender, String[] args) { - return null; + if (args.length == 1) { + List cc = List.of("new", "claim", "list"); + return StringUtil.copyPartialMatches(args[0], cc, new ArrayList<>()); + } else if (args.length == 2) { + if (args[0].equalsIgnoreCase("new")) { + List choices = new ArrayList<>(); + for (MinerType type : MinerType.values()) { + choices.add(type.getName()); + } + return StringUtil.copyPartialMatches(args[1], choices, new ArrayList<>()); + } else if (args[0].equalsIgnoreCase("claim")) { + List choices = new ArrayList<>(); + for (CryptoMiner miner : MinerDataManager.getMiners().values()) { + choices.add(miner.getName()); + } + return StringUtil.copyPartialMatches(args[1], choices, new ArrayList<>()); + } + } + return new ArrayList<>(); } } diff --git a/src/main/java/me/night/nullvalkyrie/database/MinerDataManager.java b/src/main/java/me/night/nullvalkyrie/database/MinerDataManager.java index d916d19..1b9f740 100644 --- a/src/main/java/me/night/nullvalkyrie/database/MinerDataManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/MinerDataManager.java @@ -2,18 +2,18 @@ package me.night.nullvalkyrie.database; import com.mongodb.client.MongoCursor; import me.night.nullvalkyrie.entities.miners.CryptoMiner; +import me.night.nullvalkyrie.enums.MinerType; import org.bson.Document; import org.bson.conversions.Bson; -import org.bukkit.Material; import java.util.HashMap; public class MinerDataManager { - public static void setNPC(String name, Material material, int level, double rate, boolean enabled, long lastclaim) { + public static void setMiner(String name, MinerType type, int level, double rate, boolean enabled, long lastclaim) { Document newDocument = new Document(); newDocument.put("ID", DatabaseManager.getMinersDB().countDocuments() + 1); newDocument.put("Name", name); - newDocument.put("Material", material.name()); + newDocument.put("Material", type.getName()); newDocument.put("Level", level); newDocument.put("Rate", rate); newDocument.put("Enabled", enabled); @@ -21,8 +21,8 @@ public class MinerDataManager { DatabaseManager.getMinersDB().insertOne(newDocument); } - public static void setLastClaim(long id) { - Document document = DatabaseManager.getMinersDB().find(new Document("ID", id)).first(); + public static void setLastClaim(String name) { + Document document = DatabaseManager.getMinersDB().find(new Document("Name", name)).first(); if (document != null) { Bson updated = new Document("LastClaim", System.currentTimeMillis()); Bson update = new Document("$set", updated); @@ -43,7 +43,7 @@ public class MinerDataManager { public static CryptoMiner getMiner(long id) { Document doc = DatabaseManager.getMinersDB().find(new Document("ID", id)).first(); if (doc != null) { - return new CryptoMiner(doc.getString("Name"), Material.matchMaterial(doc.getString("Material")), doc.getInteger("Level"), doc.getDouble("Rate"), doc.getLong("LastClaim")); + return new CryptoMiner(doc.getString("Name"), MinerType.getByName(doc.getString("Material")), doc.getInteger("Level"), doc.getDouble("Rate"), doc.getLong("LastClaim")); } return null; } @@ -53,7 +53,7 @@ public class MinerDataManager { try (MongoCursor cursor = DatabaseManager.getMinersDB().find().cursor()) { while (cursor.hasNext()) { Document doc = cursor.next(); - list.put(doc.getLong("ID"), new CryptoMiner(doc.getString("Name"), Material.matchMaterial(doc.getString("Material")), doc.getInteger("Level"), doc.getDouble("Rate"), doc.getLong("LastClaim"))); + list.put(doc.getLong("ID"), new CryptoMiner(doc.getString("Name"), MinerType.getByName(doc.getString("Material")), doc.getInteger("Level"), doc.getDouble("Rate"), doc.getLong("LastClaim"))); } return list; } diff --git a/src/main/java/me/night/nullvalkyrie/entities/miners/CryptoMiner.java b/src/main/java/me/night/nullvalkyrie/entities/miners/CryptoMiner.java index 5a58507..34cf819 100644 --- a/src/main/java/me/night/nullvalkyrie/entities/miners/CryptoMiner.java +++ b/src/main/java/me/night/nullvalkyrie/entities/miners/CryptoMiner.java @@ -2,6 +2,8 @@ package me.night.nullvalkyrie.entities.miners; import com.mojang.authlib.GameProfile; import com.mojang.authlib.properties.Property; + +import me.night.nullvalkyrie.enums.MinerType; import me.night.nullvalkyrie.util.Skin; import me.night.nullvalkyrie.util.Util; import net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo; @@ -30,14 +32,14 @@ import java.util.concurrent.ThreadLocalRandom; public class CryptoMiner { protected String name; - protected Material type; + protected MinerType type; protected int level; protected double rate; protected final long lastclaim; - public CryptoMiner(String name, Material type, int level, double rate, long lastclaim) { + public CryptoMiner(String name, MinerType type, int level, double rate, long lastclaim) { this.name = name; // Name of the miner - this.type = type; // Material to mine + this.type = type; // Type of the miner this.level = level; this.rate = rate; // Percentage generate chance in each tick 20tick per sec this.lastclaim = lastclaim; @@ -60,10 +62,10 @@ public class CryptoMiner { } public Material getType() { - return type; + return this.type.getMaterial(); } - public void setType(Material type) { + public void setType(MinerType type) { this.type = type; } @@ -88,7 +90,7 @@ public class CryptoMiner { System.out.println(generated); } - public void spawn(Player player, String url) { + public void spawn(Player player) { Location loc = player.getLocation().getWorld().getBlockAt(player.getLocation()).getLocation().add(0.5, 0, 0.5); if (player.getLocation().getWorld() == null) return; ArmorStand stand = player.getLocation().getWorld().spawn(loc, ArmorStand.class); @@ -101,7 +103,7 @@ public class CryptoMiner { SkullMeta meta = (SkullMeta) head.getItemMeta(); if (meta == null) return; GameProfile profile = new GameProfile(UUID.randomUUID(), null); - byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", url).getBytes()); + byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", this.type.getHeadTexture()).getBytes()); profile.getProperties().put("textures", new Property("textures", new String(encodedData))); try { Util.setFieldValue(meta, "profile", profile); @@ -145,89 +147,92 @@ public class CryptoMiner { for (int x = (int) stand.getLocation().getX() - 3; x <= stand.getLocation().getX() + 2; x++) { for (int z = (int) stand.getLocation().getZ() - 2; z <= stand.getLocation().getZ() + 2; z++) { for (int y = (int) stand.getLocation().getY() - 1; y <= stand.getLocation().getY() - 1; y++) { - if (world.getBlockAt(x, y, z).getType() == this.type) + if (world.getBlockAt(x, y, z).getType() == this.getType()) locs.add(world.getBlockAt(x, y, z).getLocation()); } } } locs.remove(world.getBlockAt(stand.getLocation().subtract(0, -1, 0)).getLocation()); - Location closest = locs.get(0); - for (Location location : locs) { - if (location.distance(stand.getLocation()) < closest.distance(stand.getLocation())) closest = location; - } - ArrayList items = new ArrayList<>(); - ThreadLocalRandom random = ThreadLocalRandom.current(); - if (closest.getBlock().getType() == this.type) { - closest.getBlock().getDrops().clear(); - int lower = 0; - int upper = 0; - if (this.level == 1) { - lower = 1; - upper = 3; - } else if (this.level == 2) { - lower = 2; - upper = 5; - } else if (this.level == 3) { - lower = 3; - upper = 7; - } else if (this.level == 4) { - lower = 4; - upper = 9; - } else if (this.level == 5) { - lower = 5; - upper = 11; - } else if (this.level == 6) { - lower = 6; - upper = 13; - } else if (this.level == 7) { - lower = 7; - upper = 15; - } else if (this.level == 8) { - lower = 8; - upper = 17; - } else if (this.level == 9) { - lower = 9; - upper = 19; - } else if (this.level == 10) { - lower = 10; - upper = 21; - } else if (this.level == 11) { - lower = 11; - upper = 23; - } else if (this.level == 12) { - lower = 12; - upper = 25; - } else if (this.level == 13) { - lower = 13; - upper = 27; - } else if (this.level == 14) { - lower = 14; - upper = 29; - } else if (this.level == 15) { - lower = 15; - upper = 31; - } else if (this.level == 16) { - lower = 16; - upper = 33; - } else if (this.level == 17) { - lower = 17; - upper = 35; - } else if (this.level == 18) { - lower = 18; - upper = 37; - } else if (this.level == 19) { - lower = 19; - upper = 39; - } else if (this.level == 20) { - lower = 20; - upper = 41; + if (locs.size() != 0) { + Location closest = locs.get(0); + for (Location location : locs) { + if (location.distance(stand.getLocation()) < closest.distance(stand.getLocation())) closest = location; + } + ArrayList items = new ArrayList<>(); + ThreadLocalRandom random = ThreadLocalRandom.current(); + if (closest.getBlock().getType() == this.getType()) { + closest.getBlock().getDrops().clear(); + int lower = 0; + int upper = 0; + if (this.level == 1) { + lower = 1; + upper = 3; + } else if (this.level == 2) { + lower = 2; + upper = 5; + } else if (this.level == 3) { + lower = 3; + upper = 7; + } else if (this.level == 4) { + lower = 4; + upper = 9; + } else if (this.level == 5) { + lower = 5; + upper = 11; + } else if (this.level == 6) { + lower = 6; + upper = 13; + } else if (this.level == 7) { + lower = 7; + upper = 15; + } else if (this.level == 8) { + lower = 8; + upper = 17; + } else if (this.level == 9) { + lower = 9; + upper = 19; + } else if (this.level == 10) { + lower = 10; + upper = 21; + } else if (this.level == 11) { + lower = 11; + upper = 23; + } else if (this.level == 12) { + lower = 12; + upper = 25; + } else if (this.level == 13) { + lower = 13; + upper = 27; + } else if (this.level == 14) { + lower = 14; + upper = 29; + } else if (this.level == 15) { + lower = 15; + upper = 31; + } else if (this.level == 16) { + lower = 16; + upper = 33; + } else if (this.level == 17) { + lower = 17; + upper = 35; + } else if (this.level == 18) { + lower = 18; + upper = 37; + } else if (this.level == 19) { + lower = 19; + upper = 39; + } else if (this.level == 20) { + lower = 20; + upper = 41; + } + items.add(new ItemStack(this.getType(), random.nextInt(lower, upper))); + closest.getBlock().setType(Material.AIR); + } + // drop the items + for (ItemStack item : items) { + world.dropItemNaturally(closest, item); } - items.add(new ItemStack(this.type, random.nextInt(lower, upper))); - closest.getBlock().setType(Material.AIR); - } - // drop the items - for (ItemStack item : items) { - world.dropItemNaturally(closest, item); } + } } diff --git a/src/main/java/me/night/nullvalkyrie/enums/MinerType.java b/src/main/java/me/night/nullvalkyrie/enums/MinerType.java index 8c970ad..724deee 100644 --- a/src/main/java/me/night/nullvalkyrie/enums/MinerType.java +++ b/src/main/java/me/night/nullvalkyrie/enums/MinerType.java @@ -1,13 +1,45 @@ package me.night.nullvalkyrie.enums; +import org.bukkit.Material; + public enum MinerType { - DIAMOND("Diamond"), EMERALD("Emerald"), GOLD("Gold"), IRON("Iron"), REDSTONE("Redstone"), COAL("Coal"), LAPIS("Lapis"), QUARTZ("Quartz"), OBSIDIAN("Obsidian"), NETHERITE("Netherite"); + DIAMOND("Diamond", Material.DIAMOND_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + EMERALD("Emerald", Material.EMERALD_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + GOLD("Gold", Material.GOLD_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + IRON("Iron", Material.IRON_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + REDSTONE("Redstone", Material.REDSTONE_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + COAL("Coal", Material.COAL_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + LAPIS("Lapis", Material.LAPIS_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + QUARTZ("Quartz", Material.NETHER_QUARTZ_ORE, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + OBSIDIAN("Obsidian", Material.OBSIDIAN, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"), + NETHERITE("Netherite", Material.ANCIENT_DEBRIS, "https://textures.minecraft.net/texture/c09cc3c75bd13c59602040b5970f30dbc76825c0e817da815a65d76ab0e82198"); private final String name; - MinerType(String name) { + private final Material material; + private final String headTexture; + MinerType(String name, Material material, String headTexture) { this.name = name; + this.material = material; + this.headTexture = headTexture; } public String getName() { return name; } + + public Material getMaterial() { + return material; + } + + public String getHeadTexture() { + return headTexture; + } + + public static MinerType getByName(String name) { + for (MinerType type : MinerType.values()) { + if (type.getName().equalsIgnoreCase(name)) { + return type; + } + } + return null; + } }