miners using mongodb
This commit is contained in:
parent
f06475d62b
commit
c286d631e9
6 changed files with 82 additions and 94 deletions
|
@ -24,7 +24,7 @@ public final class Main extends JavaPlugin {
|
|||
new CustomItemManager(this);
|
||||
new FileManager();
|
||||
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 MenuListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new ScoreboardListener(this), this);
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package me.night.nullvalkyrie.commands;
|
||||
|
||||
import me.night.nullvalkyrie.Main;
|
||||
|
||||
public class CommandManager {
|
||||
public CommandManager(Main main) {
|
||||
public CommandManager() {
|
||||
new VanishCommand();
|
||||
new TestCommand();
|
||||
new AnvilCommand();
|
||||
|
@ -18,6 +16,6 @@ public class CommandManager {
|
|||
new UtilCommand();
|
||||
new ShopCommand();
|
||||
new BetaCommand();
|
||||
new MinerCommand(main);
|
||||
new MinerCommand();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package me.night.nullvalkyrie.commands;
|
||||
|
||||
import me.night.nullvalkyrie.Main;
|
||||
import me.night.nullvalkyrie.miners.CryptoMiner;
|
||||
import me.night.nullvalkyrie.database.MinerDataManager;
|
||||
import me.night.nullvalkyrie.miners.MinerGUI;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
@ -11,19 +10,16 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
|
||||
import static me.night.nullvalkyrie.miners.CryptoMiner.generate;
|
||||
import static me.night.nullvalkyrie.miners.CryptoMiner.getMiner;
|
||||
|
||||
public class MinerCommand extends Command {
|
||||
private final Main main;
|
||||
|
||||
public MinerCommand(Main main) {
|
||||
public MinerCommand() {
|
||||
super(
|
||||
"miner",
|
||||
new String[]{"m", "miners"},
|
||||
"Miner list",
|
||||
""
|
||||
);
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,22 +27,20 @@ public class MinerCommand extends Command {
|
|||
if (sender instanceof Player) {
|
||||
if (args.length == 0) {
|
||||
Player player = (Player) sender;
|
||||
new MinerGUI(main, player);
|
||||
int seconds = (int) (new Date().getTime() - getMiner("0").getLastclaim()) / 1000;
|
||||
System.out.println("Seconds" + seconds);
|
||||
new MinerGUI(player);
|
||||
int seconds = (int) (new Date().getTime() - MinerDataManager.getLastClaim(1)) / 1000;
|
||||
System.out.println("Seconds: " + seconds);
|
||||
generate(50, seconds);
|
||||
} else if (args[0].equalsIgnoreCase("new")) {
|
||||
String name = args[1];
|
||||
Material pick = Material.STONE_PICKAXE;
|
||||
Material pick = Material.NETHERITE_AXE;
|
||||
int level = 20;
|
||||
double rate = 0.4;
|
||||
long time = System.currentTimeMillis();
|
||||
CryptoMiner miner = new CryptoMiner(main, name, pick, level, rate, time);
|
||||
miner.setMiner(name, pick.name(), level, rate, time);
|
||||
MinerDataManager.setNPC(name, pick, level, rate, true, time);
|
||||
} else if (args[0].equalsIgnoreCase("claim")) {
|
||||
String minerIndex = args[1];
|
||||
CryptoMiner miner = getMiner(minerIndex);
|
||||
miner.setLastClaim(minerIndex, System.currentTimeMillis());
|
||||
MinerDataManager.setLastClaim(Long.parseLong(minerIndex));
|
||||
System.out.println("Done");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
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.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) {
|
||||
Document newDocument = new Document();
|
||||
|
@ -16,7 +21,8 @@ public class MinerDataManager {
|
|||
newDocument.put("LastClaim", lastclaim);
|
||||
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();
|
||||
if (document != null) {
|
||||
Bson updated = new Document("LastClaim", System.currentTimeMillis());
|
||||
|
@ -24,5 +30,39 @@ public class MinerDataManager {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,27 +1,17 @@
|
|||
package me.night.nullvalkyrie.miners;
|
||||
|
||||
import me.night.nullvalkyrie.Main;
|
||||
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 static me.night.nullvalkyrie.items.CustomItemManager.loadConfig;
|
||||
import static me.night.nullvalkyrie.items.CustomItemManager.loadFile;
|
||||
|
||||
public class CryptoMiner {
|
||||
protected static Main main;
|
||||
protected String name;
|
||||
protected Material type;
|
||||
protected int level;
|
||||
protected double rate;
|
||||
protected static int generated;
|
||||
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.type = type; // Material to mine
|
||||
this.level = level;
|
||||
|
@ -33,77 +23,46 @@ public class CryptoMiner {
|
|||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getRate() {
|
||||
return rate;
|
||||
}
|
||||
|
||||
public void setRate(double rate) {
|
||||
this.rate = rate;
|
||||
}
|
||||
|
||||
public Material getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Material type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
public void setLevel(int level) { this.level = level; }
|
||||
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public long getLastclaim() {
|
||||
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) {
|
||||
int generated = 0;
|
||||
for (int counter = 0; counter < times; counter++) {
|
||||
int count = ThreadLocalRandom.current().nextInt(100);
|
||||
if (count > pp) 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package me.night.nullvalkyrie.miners;
|
||||
|
||||
import me.night.nullvalkyrie.Main;
|
||||
import me.night.nullvalkyrie.items.CustomItemManager;
|
||||
import me.night.nullvalkyrie.database.MinerDataManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
@ -17,14 +15,13 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
|
||||
public class MinerGUI {
|
||||
private final FileConfiguration file;
|
||||
private Inventory inv;
|
||||
public MinerGUI(Main main, Player player) {
|
||||
if(!main.getDataFolder().exists()) main.getDataFolder().mkdir();
|
||||
file = CustomItemManager.loadConfig("miners.yml");
|
||||
|
||||
public MinerGUI(Player player) {
|
||||
createUI();
|
||||
player.openInventory(inv);
|
||||
}
|
||||
|
||||
public void createUI() {
|
||||
inv = Bukkit.createInventory(null, 45, ChatColor.DARK_AQUA + "Crypto Miners");
|
||||
ItemStack frame = new ItemStack(Material.BLUE_STAINED_GLASS_PANE);
|
||||
|
@ -39,16 +36,16 @@ public class MinerGUI {
|
|||
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 ind = 0;
|
||||
for(String c : file.getKeys(false)) {
|
||||
|
||||
for (CryptoMiner c : MinerDataManager.getMiners().values()) {
|
||||
if (ind <= 20) {
|
||||
ItemStack item = new ItemStack(Material.matchMaterial(file.getString(c + ".material")));
|
||||
ItemStack item = new ItemStack(c.getType());
|
||||
ItemMeta itemMeta = item.getItemMeta();
|
||||
itemMeta.setDisplayName(file.getString(c + ".name"));
|
||||
itemMeta.setDisplayName(c.getName());
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add("Level: " + file.getString(c + ".level"));
|
||||
lore.add("Rate: " + file.getString(c + ".rate"));
|
||||
lore.add("Last Claim: " + new SimpleDateFormat("d MMM yyyy HH:mm:ss").format(new Date(file.getLong(c + ".last-claim"))));
|
||||
boolean b = file.getBoolean(c + ".rate") ? lore.add(ChatColor.GRAY + "Click to enable miner!") : lore.add(ChatColor.RED + "Click to disable miner!");
|
||||
lore.add("Level: " + c.getLevel());
|
||||
lore.add("Rate: " + c.getRate());
|
||||
lore.add("Last Claim: " + new SimpleDateFormat("d MMM yyyy HH:mm:ss").format(new Date(c.getLastclaim())));
|
||||
itemMeta.setLore(lore);
|
||||
item.setItemMeta(itemMeta);
|
||||
inv.setItem(a[ind], item);
|
||||
|
|
Loading…
Reference in a new issue