improvements for miners

This commit is contained in:
NK 2022-12-20 15:17:47 +00:00
parent 5b6e509cbf
commit 4a3ec6bc65
5 changed files with 173 additions and 109 deletions

View file

@ -1,6 +1,8 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
//import me.night.nullvalkyrie.entities.pets.ZombiePet;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_19_R1.CraftWorld;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
@ -8,16 +10,16 @@ import java.util.List;
public class BetaCommand extends Command { public class BetaCommand extends Command {
public BetaCommand() { public BetaCommand() {
super("beta", new String[]{"b", "npc"}, "Beta", ""); super("beta", new String[]{"b"}, "Beta", "");
} }
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
if (sender instanceof Player player) { 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"); //ZombiePet a = new ZombiePet(player.getLocation(), player);
// new LuckyDraw().UI(player); //((CraftWorld) player.getWorld()).getHandle().b(a);
} }
} }

View file

@ -1,47 +1,72 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.database.MinerDataManager; 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 me.night.nullvalkyrie.ui.inventory.Miner;
import org.bukkit.Material; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import java.util.Date; import java.util.*;
import java.util.List;
import static me.night.nullvalkyrie.entities.miners.CryptoMiner.generate; import static me.night.nullvalkyrie.entities.miners.CryptoMiner.generate;
public class MinerCommand extends Command { public class MinerCommand extends Command {
public MinerCommand() { public MinerCommand() {
super("miner", new String[]{"m", "miners"}, "Miner list", ""); super("miner", new String[]{"miners"}, "Miners", "");
} }
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
if (sender instanceof Player player) { if (sender instanceof Player player) {
if (args.length == 0) { if (args.length == 0) {
player.sendMessage(ChatColor.RED + "Invalid command");
return;
}
if (args[0].equalsIgnoreCase("list")) {
new Miner().UI(player); new Miner().UI(player);
int seconds = (int) (new Date().getTime() - MinerDataManager.getLastClaim(1)) / 1000; int seconds = (int) (new Date().getTime() - MinerDataManager.getLastClaim(1)) / 1000;
generate(50, seconds); generate(50, seconds);
} else if (args[0].equalsIgnoreCase("new")) { } else if (args[0].equalsIgnoreCase("new")) {
String name = args[1]; String name = args[2];
Material pick = Material.NETHERITE_AXE; MinerType type = MinerType.getByName(args[1]);
int level = 20; int level = 20;
double rate = 0.4; double rate = 0.4;
long time = System.currentTimeMillis(); 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")) { } else if (args[0].equalsIgnoreCase("claim")) {
String minerIndex = args[1]; MinerDataManager.setLastClaim(args[1]);
MinerDataManager.setLastClaim(Long.parseLong(minerIndex)); player.sendMessage(ChatColor.GREEN + "Claimed");
player.sendMessage("Claimed");
} }
} }
} }
@Override @Override
public List<String> onTabComplete(CommandSender sender, String[] args) { public List<String> onTabComplete(CommandSender sender, String[] args) {
return null; if (args.length == 1) {
List<String> 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<String> 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<String> choices = new ArrayList<>();
for (CryptoMiner miner : MinerDataManager.getMiners().values()) {
choices.add(miner.getName());
}
return StringUtil.copyPartialMatches(args[1], choices, new ArrayList<>());
}
}
return new ArrayList<>();
} }
} }

View file

@ -2,18 +2,18 @@ package me.night.nullvalkyrie.database;
import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoCursor;
import me.night.nullvalkyrie.entities.miners.CryptoMiner; import me.night.nullvalkyrie.entities.miners.CryptoMiner;
import me.night.nullvalkyrie.enums.MinerType;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.bukkit.Material;
import java.util.HashMap; import java.util.HashMap;
public class MinerDataManager { 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(); Document newDocument = new Document();
newDocument.put("ID", DatabaseManager.getMinersDB().countDocuments() + 1); newDocument.put("ID", DatabaseManager.getMinersDB().countDocuments() + 1);
newDocument.put("Name", name); newDocument.put("Name", name);
newDocument.put("Material", material.name()); newDocument.put("Material", type.getName());
newDocument.put("Level", level); newDocument.put("Level", level);
newDocument.put("Rate", rate); newDocument.put("Rate", rate);
newDocument.put("Enabled", enabled); newDocument.put("Enabled", enabled);
@ -21,8 +21,8 @@ public class MinerDataManager {
DatabaseManager.getMinersDB().insertOne(newDocument); DatabaseManager.getMinersDB().insertOne(newDocument);
} }
public static void setLastClaim(long id) { public static void setLastClaim(String name) {
Document document = DatabaseManager.getMinersDB().find(new Document("ID", id)).first(); Document document = DatabaseManager.getMinersDB().find(new Document("Name", name)).first();
if (document != null) { if (document != null) {
Bson updated = new Document("LastClaim", System.currentTimeMillis()); Bson updated = new Document("LastClaim", System.currentTimeMillis());
Bson update = new Document("$set", updated); Bson update = new Document("$set", updated);
@ -43,7 +43,7 @@ public class MinerDataManager {
public static CryptoMiner getMiner(long id) { public static CryptoMiner getMiner(long id) {
Document doc = DatabaseManager.getMinersDB().find(new Document("ID", id)).first(); Document doc = DatabaseManager.getMinersDB().find(new Document("ID", id)).first();
if (doc != null) { 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; return null;
} }
@ -53,7 +53,7 @@ public class MinerDataManager {
try (MongoCursor<Document> cursor = DatabaseManager.getMinersDB().find().cursor()) { try (MongoCursor<Document> cursor = DatabaseManager.getMinersDB().find().cursor()) {
while (cursor.hasNext()) { while (cursor.hasNext()) {
Document doc = cursor.next(); 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; return list;
} }

View file

@ -2,6 +2,8 @@ package me.night.nullvalkyrie.entities.miners;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.Property;
import me.night.nullvalkyrie.enums.MinerType;
import me.night.nullvalkyrie.util.Skin; import me.night.nullvalkyrie.util.Skin;
import me.night.nullvalkyrie.util.Util; import me.night.nullvalkyrie.util.Util;
import net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo; import net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo;
@ -30,14 +32,14 @@ import java.util.concurrent.ThreadLocalRandom;
public class CryptoMiner { public class CryptoMiner {
protected String name; protected String name;
protected Material type; protected MinerType type;
protected int level; protected int level;
protected double rate; protected double rate;
protected final long lastclaim; 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.name = name; // Name of the miner
this.type = type; // Material to mine this.type = type; // Type of the miner
this.level = level; this.level = level;
this.rate = rate; // Percentage generate chance in each tick 20tick per sec this.rate = rate; // Percentage generate chance in each tick 20tick per sec
this.lastclaim = lastclaim; this.lastclaim = lastclaim;
@ -60,10 +62,10 @@ public class CryptoMiner {
} }
public Material getType() { public Material getType() {
return type; return this.type.getMaterial();
} }
public void setType(Material type) { public void setType(MinerType type) {
this.type = type; this.type = type;
} }
@ -88,7 +90,7 @@ public class CryptoMiner {
System.out.println(generated); 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); Location loc = player.getLocation().getWorld().getBlockAt(player.getLocation()).getLocation().add(0.5, 0, 0.5);
if (player.getLocation().getWorld() == null) return; if (player.getLocation().getWorld() == null) return;
ArmorStand stand = player.getLocation().getWorld().spawn(loc, ArmorStand.class); ArmorStand stand = player.getLocation().getWorld().spawn(loc, ArmorStand.class);
@ -101,7 +103,7 @@ public class CryptoMiner {
SkullMeta meta = (SkullMeta) head.getItemMeta(); SkullMeta meta = (SkullMeta) head.getItemMeta();
if (meta == null) return; if (meta == null) return;
GameProfile profile = new GameProfile(UUID.randomUUID(), null); 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))); profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
try { try {
Util.setFieldValue(meta, "profile", profile); 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 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 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++) { 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.add(world.getBlockAt(x, y, z).getLocation());
} }
} }
} }
locs.remove(world.getBlockAt(stand.getLocation().subtract(0, -1, 0)).getLocation()); locs.remove(world.getBlockAt(stand.getLocation().subtract(0, -1, 0)).getLocation());
Location closest = locs.get(0); if (locs.size() != 0) {
for (Location location : locs) { Location closest = locs.get(0);
if (location.distance(stand.getLocation()) < closest.distance(stand.getLocation())) closest = location; for (Location location : locs) {
} if (location.distance(stand.getLocation()) < closest.distance(stand.getLocation())) closest = location;
ArrayList<ItemStack> items = new ArrayList<>(); }
ThreadLocalRandom random = ThreadLocalRandom.current(); ArrayList<ItemStack> items = new ArrayList<>();
if (closest.getBlock().getType() == this.type) { ThreadLocalRandom random = ThreadLocalRandom.current();
closest.getBlock().getDrops().clear(); if (closest.getBlock().getType() == this.getType()) {
int lower = 0; closest.getBlock().getDrops().clear();
int upper = 0; int lower = 0;
if (this.level == 1) { int upper = 0;
lower = 1; if (this.level == 1) {
upper = 3; lower = 1;
} else if (this.level == 2) { upper = 3;
lower = 2; } else if (this.level == 2) {
upper = 5; lower = 2;
} else if (this.level == 3) { upper = 5;
lower = 3; } else if (this.level == 3) {
upper = 7; lower = 3;
} else if (this.level == 4) { upper = 7;
lower = 4; } else if (this.level == 4) {
upper = 9; lower = 4;
} else if (this.level == 5) { upper = 9;
lower = 5; } else if (this.level == 5) {
upper = 11; lower = 5;
} else if (this.level == 6) { upper = 11;
lower = 6; } else if (this.level == 6) {
upper = 13; lower = 6;
} else if (this.level == 7) { upper = 13;
lower = 7; } else if (this.level == 7) {
upper = 15; lower = 7;
} else if (this.level == 8) { upper = 15;
lower = 8; } else if (this.level == 8) {
upper = 17; lower = 8;
} else if (this.level == 9) { upper = 17;
lower = 9; } else if (this.level == 9) {
upper = 19; lower = 9;
} else if (this.level == 10) { upper = 19;
lower = 10; } else if (this.level == 10) {
upper = 21; lower = 10;
} else if (this.level == 11) { upper = 21;
lower = 11; } else if (this.level == 11) {
upper = 23; lower = 11;
} else if (this.level == 12) { upper = 23;
lower = 12; } else if (this.level == 12) {
upper = 25; lower = 12;
} else if (this.level == 13) { upper = 25;
lower = 13; } else if (this.level == 13) {
upper = 27; lower = 13;
} else if (this.level == 14) { upper = 27;
lower = 14; } else if (this.level == 14) {
upper = 29; lower = 14;
} else if (this.level == 15) { upper = 29;
lower = 15; } else if (this.level == 15) {
upper = 31; lower = 15;
} else if (this.level == 16) { upper = 31;
lower = 16; } else if (this.level == 16) {
upper = 33; lower = 16;
} else if (this.level == 17) { upper = 33;
lower = 17; } else if (this.level == 17) {
upper = 35; lower = 17;
} else if (this.level == 18) { upper = 35;
lower = 18; } else if (this.level == 18) {
upper = 37; lower = 18;
} else if (this.level == 19) { upper = 37;
lower = 19; } else if (this.level == 19) {
upper = 39; lower = 19;
} else if (this.level == 20) { upper = 39;
lower = 20; } else if (this.level == 20) {
upper = 41; 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);
} }
} }
} }

View file

@ -1,13 +1,45 @@
package me.night.nullvalkyrie.enums; package me.night.nullvalkyrie.enums;
import org.bukkit.Material;
public enum MinerType { 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; 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.name = name;
this.material = material;
this.headTexture = headTexture;
} }
public String getName() { public String getName() {
return name; 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;
}
} }