miners using mongodb

This commit is contained in:
NK 2022-11-29 00:26:04 +00:00
parent f06475d62b
commit c286d631e9
6 changed files with 82 additions and 94 deletions

View file

@ -24,7 +24,7 @@ public final class Main extends JavaPlugin {
new CustomItemManager(this); new CustomItemManager(this);
new FileManager(); new FileManager();
env = Dotenv.configure().directory("E:\\Files\\SB\\plugins\\NullValkyrie").filename(".env").load(); env = Dotenv.configure().directory("E:\\Files\\SB\\plugins\\NullValkyrie").filename(".env").load();
new CommandManager(this); new CommandManager();
Bukkit.getPluginManager().registerEvents(new ServerEvents(), this); Bukkit.getPluginManager().registerEvents(new ServerEvents(), this);
Bukkit.getPluginManager().registerEvents(new MenuListener(), this); Bukkit.getPluginManager().registerEvents(new MenuListener(), this);
Bukkit.getPluginManager().registerEvents(new ScoreboardListener(this), this); Bukkit.getPluginManager().registerEvents(new ScoreboardListener(this), this);

View file

@ -1,9 +1,7 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.Main;
public class CommandManager { public class CommandManager {
public CommandManager(Main main) { public CommandManager() {
new VanishCommand(); new VanishCommand();
new TestCommand(); new TestCommand();
new AnvilCommand(); new AnvilCommand();
@ -18,6 +16,6 @@ public class CommandManager {
new UtilCommand(); new UtilCommand();
new ShopCommand(); new ShopCommand();
new BetaCommand(); new BetaCommand();
new MinerCommand(main); new MinerCommand();
} }
} }

View file

@ -1,7 +1,6 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.database.MinerDataManager;
import me.night.nullvalkyrie.miners.CryptoMiner;
import me.night.nullvalkyrie.miners.MinerGUI; import me.night.nullvalkyrie.miners.MinerGUI;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -11,19 +10,16 @@ import java.util.Date;
import java.util.List; import java.util.List;
import static me.night.nullvalkyrie.miners.CryptoMiner.generate; import static me.night.nullvalkyrie.miners.CryptoMiner.generate;
import static me.night.nullvalkyrie.miners.CryptoMiner.getMiner;
public class MinerCommand extends Command { public class MinerCommand extends Command {
private final Main main;
public MinerCommand(Main main) { public MinerCommand() {
super( super(
"miner", "miner",
new String[]{"m", "miners"}, new String[]{"m", "miners"},
"Miner list", "Miner list",
"" ""
); );
this.main = main;
} }
@Override @Override
@ -31,22 +27,20 @@ public class MinerCommand extends Command {
if (sender instanceof Player) { if (sender instanceof Player) {
if (args.length == 0) { if (args.length == 0) {
Player player = (Player) sender; Player player = (Player) sender;
new MinerGUI(main, player); new MinerGUI(player);
int seconds = (int) (new Date().getTime() - getMiner("0").getLastclaim()) / 1000; int seconds = (int) (new Date().getTime() - MinerDataManager.getLastClaim(1)) / 1000;
System.out.println("Seconds" + seconds); System.out.println("Seconds: " + seconds);
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[1];
Material pick = Material.STONE_PICKAXE; Material pick = Material.NETHERITE_AXE;
int level = 20; int level = 20;
double rate = 0.4; double rate = 0.4;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
CryptoMiner miner = new CryptoMiner(main, name, pick, level, rate, time); MinerDataManager.setNPC(name, pick, level, rate, true, time);
miner.setMiner(name, pick.name(), level, rate, time);
} else if (args[0].equalsIgnoreCase("claim")) { } else if (args[0].equalsIgnoreCase("claim")) {
String minerIndex = args[1]; String minerIndex = args[1];
CryptoMiner miner = getMiner(minerIndex); MinerDataManager.setLastClaim(Long.parseLong(minerIndex));
miner.setLastClaim(minerIndex, System.currentTimeMillis());
System.out.println("Done"); System.out.println("Done");
} }

View file

@ -1,9 +1,14 @@
package me.night.nullvalkyrie.database; package me.night.nullvalkyrie.database;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import me.night.nullvalkyrie.miners.CryptoMiner;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.bukkit.Material; import org.bukkit.Material;
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 setNPC(String name, Material material, int level, double rate, boolean enabled, long lastclaim) {
Document newDocument = new Document(); Document newDocument = new Document();
@ -16,7 +21,8 @@ public class MinerDataManager {
newDocument.put("LastClaim", lastclaim); newDocument.put("LastClaim", lastclaim);
DatabaseManager.miners.insertOne(newDocument); DatabaseManager.miners.insertOne(newDocument);
} }
public static void setLastclaim(int id) {
public static void setLastClaim(long id) {
Document document = DatabaseManager.miners.find(new Document("ID", id)).first(); Document document = DatabaseManager.miners.find(new Document("ID", id)).first();
if (document != null) { if (document != null) {
Bson updated = new Document("LastClaim", System.currentTimeMillis()); Bson updated = new Document("LastClaim", System.currentTimeMillis());
@ -24,5 +30,39 @@ public class MinerDataManager {
DatabaseManager.miners.updateOne(document, update); DatabaseManager.miners.updateOne(document, update);
} }
} }
public static long getLastClaim(long id) {
try (MongoCursor<Document> cursor = DatabaseManager.miners.find(Filters.eq("ID", id)).cursor()) {
while (cursor.hasNext()) {
Document doc = cursor.next();
for (String key : doc.keySet()) {
if (key.equals("LastClaim")) return (long) doc.get(key);
}
}
}
return 0;
}
public static CryptoMiner getMiner(long id) {
try (MongoCursor<Document> cursor = DatabaseManager.miners.find(Filters.eq("ID", id)).cursor()) {
while (cursor.hasNext()) {
Document doc = cursor.next();
return new CryptoMiner((String) doc.get("Name"), Material.matchMaterial((String) doc.get("Material")), (int) doc.get("Level"), (double) doc.get("Rate"), (long) doc.get("LastClaim"));
}
}
return null;
}
public static HashMap<Long, CryptoMiner> getMiners() {
HashMap<Long, CryptoMiner> list = new HashMap<>();
try (MongoCursor<Document> cursor = DatabaseManager.miners.find().cursor()) {
while (cursor.hasNext()) {
Document doc = cursor.next();
list.put((long) doc.get("ID"), new CryptoMiner((String) doc.get("Name"), Material.matchMaterial((String) doc.get("Material")), (int) doc.get("Level"), (double) doc.get("Rate"), (long) doc.get("LastClaim")));
}
return list;
}
}
} }

View file

@ -1,27 +1,17 @@
package me.night.nullvalkyrie.miners; package me.night.nullvalkyrie.miners;
import me.night.nullvalkyrie.Main;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static me.night.nullvalkyrie.items.CustomItemManager.loadConfig;
import static me.night.nullvalkyrie.items.CustomItemManager.loadFile;
public class CryptoMiner { public class CryptoMiner {
protected static Main main;
protected String name; protected String name;
protected Material type; protected Material type;
protected int level; protected int level;
protected double rate; protected double rate;
protected static int generated;
protected long lastclaim; protected long lastclaim;
public CryptoMiner(Main main, String name, Material type, int level, double rate, long lastclaim) {
CryptoMiner.main = main; public CryptoMiner(String name, Material 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; // Material to mine
this.level = level; this.level = level;
@ -33,77 +23,46 @@ public class CryptoMiner {
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public double getRate() { public double getRate() {
return rate; return rate;
} }
public void setRate(double rate) { public void setRate(double rate) {
this.rate = rate; this.rate = rate;
} }
public Material getType() { public Material getType() {
return type; return type;
} }
public void setType(Material type) { public void setType(Material type) {
this.type = type; this.type = type;
} }
public int getLevel() { public int getLevel() {
return level; return level;
} }
public void setLevel(int level) { this.level = level; }
public void setLevel(int level) {
this.level = level;
}
public long getLastclaim() { public long getLastclaim() {
return lastclaim; return lastclaim;
} }
public void setLastClaim(String index, long lastclaim) {
this.lastclaim = lastclaim;
FileConfiguration file = loadConfig("miners.yml");
file.set(index + ".last-claim", lastclaim);
try {
file.save(loadFile("miners.yml"));
} catch (IOException ignored) {
}
}
public static void generate(int pp, int times) { public static void generate(int pp, int times) {
int generated = 0;
for (int counter = 0; counter < times; counter++) { for (int counter = 0; counter < times; counter++) {
int count = ThreadLocalRandom.current().nextInt(100); int count = ThreadLocalRandom.current().nextInt(100);
if (count > pp) generated++; if (count > pp) generated++;
} }
System.out.println(generated); System.out.println(generated);
} }
public List<CryptoMiner> getMiners() {
List<CryptoMiner> arr = new ArrayList<>();
FileConfiguration file = loadConfig("miners.yml");
for(String c : file.getKeys(false)) {
arr.add(new CryptoMiner(main, file.getString(c + ".name"), Material.matchMaterial(file.getString(c + ".material")), file.getInt(c + ".level"), file.getDouble(c + ".rate"), file.getLong(c + ".last-claim")));
}
return arr;
}
public static CryptoMiner getMiner(String index) {
FileConfiguration file = loadConfig("miners.yml");
String name = file.getString(index + ".name");
Material material = Material.matchMaterial(file.getString(index + ".material"));
int level = file.getInt(index + ".level");
double rate = file.getDouble(index + ".rate");
long lastclaim = file.getLong(index + ".last-claim");
return new CryptoMiner(main, name, material, level, rate, lastclaim);
}
public void setMiner(String name, String material, int level, double rate, long time) {
FileConfiguration file = loadConfig("miners.yml");
String index = Integer.toString(file.getKeys(false).size());
file.createSection(index);
file.set(index + ".name", name);
file.set(index + ".material", material);
file.set(index + ".level", level);
file.set(index + ".rate", rate);
file.set(index + ".enabled", true);
file.set(index + ".last-claim", time);
try {
file.save(loadFile("miners.yml"));
} catch (IOException e) {
e.printStackTrace();
}
}
} }

View file

@ -1,11 +1,9 @@
package me.night.nullvalkyrie.miners; package me.night.nullvalkyrie.miners;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.database.MinerDataManager;
import me.night.nullvalkyrie.items.CustomItemManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -17,14 +15,13 @@ import java.util.Date;
import java.util.List; import java.util.List;
public class MinerGUI { public class MinerGUI {
private final FileConfiguration file;
private Inventory inv; private Inventory inv;
public MinerGUI(Main main, Player player) {
if(!main.getDataFolder().exists()) main.getDataFolder().mkdir(); public MinerGUI(Player player) {
file = CustomItemManager.loadConfig("miners.yml");
createUI(); createUI();
player.openInventory(inv); player.openInventory(inv);
} }
public void createUI() { public void createUI() {
inv = Bukkit.createInventory(null, 45, ChatColor.DARK_AQUA + "Crypto Miners"); inv = Bukkit.createInventory(null, 45, ChatColor.DARK_AQUA + "Crypto Miners");
ItemStack frame = new ItemStack(Material.BLUE_STAINED_GLASS_PANE); ItemStack frame = new ItemStack(Material.BLUE_STAINED_GLASS_PANE);
@ -39,16 +36,16 @@ public class MinerGUI {
inv.setItem(0, close); inv.setItem(0, close);
int[] a = new int[]{10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34}; int[] a = new int[]{10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34};
int ind = 0; int ind = 0;
for(String c : file.getKeys(false)) {
for (CryptoMiner c : MinerDataManager.getMiners().values()) {
if (ind <= 20) { if (ind <= 20) {
ItemStack item = new ItemStack(Material.matchMaterial(file.getString(c + ".material"))); ItemStack item = new ItemStack(c.getType());
ItemMeta itemMeta = item.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
itemMeta.setDisplayName(file.getString(c + ".name")); itemMeta.setDisplayName(c.getName());
List<String> lore = new ArrayList<>(); List<String> lore = new ArrayList<>();
lore.add("Level: " + file.getString(c + ".level")); lore.add("Level: " + c.getLevel());
lore.add("Rate: " + file.getString(c + ".rate")); lore.add("Rate: " + c.getRate());
lore.add("Last Claim: " + new SimpleDateFormat("d MMM yyyy HH:mm:ss").format(new Date(file.getLong(c + ".last-claim")))); lore.add("Last Claim: " + new SimpleDateFormat("d MMM yyyy HH:mm:ss").format(new Date(c.getLastclaim())));
boolean b = file.getBoolean(c + ".rate") ? lore.add(ChatColor.GRAY + "Click to enable miner!") : lore.add(ChatColor.RED + "Click to disable miner!");
itemMeta.setLore(lore); itemMeta.setLore(lore);
item.setItemMeta(itemMeta); item.setItemMeta(itemMeta);
inv.setItem(a[ind], item); inv.setItem(a[ind], item);