From c3af2d931183aeda70ae6be9e893877d05b7010b Mon Sep 17 00:00:00 2001 From: NK Date: Tue, 15 Nov 2022 18:32:45 +0000 Subject: [PATCH] persistence data container commands --- pom.xml | 4 +- .../nullvalkyrie/commands/BetaCommand.java | 65 +++++++++++++++---- .../nullvalkyrie/commands/WeaponCommand.java | 39 +++++------ .../night/nullvalkyrie/database/Client.java | 2 +- .../nullvalkyrie/events/CustomItemEvents.java | 22 ++++++- .../nullvalkyrie/items/CustomItemManager.java | 30 +++++++-- src/main/resources/config.yml | 3 + 7 files changed, 119 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index 6672dd5..4047b74 100644 --- a/pom.xml +++ b/pom.xml @@ -26,8 +26,8 @@ maven-compiler-plugin 3.8.1 - ${java.version} - ${java.version} + 15 + 15 diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index b2222ee..0c2de96 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -5,19 +5,22 @@ import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import net.minecraft.network.protocol.game.*; import net.minecraft.server.level.EntityPlayer; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitScheduler; -import java.util.ArrayList; +import org.apache.commons.lang3.time.StopWatch; import java.util.List; +import java.util.concurrent.TimeUnit; +import static java.lang.Thread.sleep; import static me.night.nullvalkyrie.database.Client.getUser; public class BetaCommand extends Command { private Main main; + private BukkitScheduler scheduler; public BetaCommand(Main main) { super("beta", new String[]{"b"}, "Beta", ""); @@ -26,23 +29,57 @@ public class BetaCommand extends Command { @Override public void onCommand(CommandSender sender, String[] args) { - getUser("Kaly15"); +// getUser("Kaly15"); if (sender instanceof Player) { Player pa = (Player) sender; - CraftPlayer cp = (CraftPlayer) sender; - EntityPlayer ep = cp.getHandle(); - Entity e = (Entity) ep; - PacketPlayOutUpdateHealth packet = new PacketPlayOutUpdateHealth(20f, 20, 5.0f); // health, food, food - // saturation - ep.b.a(packet); // Sends the Packet +// CraftPlayer cp = (CraftPlayer) sender; +// EntityPlayer ep = cp.getHandle(); +// PacketPlayOutUpdateHealth packet = new PacketPlayOutUpdateHealth(20f, 20, 5.0f); // health, food, food saturation +// ep.b.a(packet); // Sends the Packet - new BukkitRunnable() { +// new BukkitRunnable() { +// @Override +// public void run() { +// pa.spigot().sendMessage(ChatMessageType.ACTION_BAR, +// TextComponent.fromLegacyText("§1NOT ENOUGH MANNER")); +// } +// }.runTaskTimer(main, 0L, 10); + + Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() { + int ms = 1; @Override public void run() { - pa.spigot().sendMessage(ChatMessageType.ACTION_BAR, - TextComponent.fromLegacyText("§1NOT ENOUGH MANNER")); + while (true) { + try { + sleep(10); // will run every 0.1 seconds + long minutes = (ms / 1000) / 60; + long seconds = (ms / 100) % 60; + long milliseconds = (ms /10); + String secondsStr = Long.toString(seconds); + String secs; + if (secondsStr.length() >= 2) { + secs = secondsStr.substring(0, 2); + } else { + secs = "0" + secondsStr; + } + String minsStr = Long.toString(minutes); + String mins; + if (minsStr.length() >= 2) { + mins = minsStr.substring(0, 2); + } else { + mins = "0" + minsStr; + } + String sw = mins + ":" + secs + ":" + milliseconds; + System.out.println(sw); + pa.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§1"+ sw)); + ms++; + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + + } } - }.runTaskTimer(main, 0L, 10); + }, 0L, 1); } } diff --git a/src/main/java/me/night/nullvalkyrie/commands/WeaponCommand.java b/src/main/java/me/night/nullvalkyrie/commands/WeaponCommand.java index 6fccfcf..15d7785 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/WeaponCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/WeaponCommand.java @@ -1,6 +1,5 @@ package me.night.nullvalkyrie.commands; -import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.items.CustomItemManager; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -15,49 +14,43 @@ import java.util.List; import java.util.Objects; public class WeaponCommand extends Command { - private Main main; - public WeaponCommand(Main main) { - super( - "weapon", - new String[]{}, - "Give you a weapon", - "" - - ); - this.main = main; + public WeaponCommand() { + super("weapon", new String[]{}, "Give you a weapon", ""); } - @Override public void onCommand(CommandSender sender, String[] args) { Player player = (Player) sender; StringBuilder s = new StringBuilder(); List b = Arrays.asList(args); - for(String a: args) { - if(a.equals(b.get(b.size() - 1))) { + for (String a : args) { + if (a.equals(b.get(b.size() - 1))) { s.append(a); } else { s.append(a); s.append(" "); } - } - ItemStack item = main.getCustomItemManager().getItem(s.toString()); - if(item.hasItemMeta()) { - player.getInventory().addItem(item); - } else { + if (s.isEmpty()) { player.sendMessage(ChatColor.RED + "This item doesn't exist"); + } else { + ItemStack item = CustomItemManager.getItem(s.toString()); + if (item.hasItemMeta()) { + player.getInventory().addItem(item); + } else { + player.sendMessage(ChatColor.RED + "This item doesn't exist"); + } } } @Override public List onTabComplete(CommandSender sender, String[] args) { - if(args.length == 1) { + if (args.length == 1) { List hh = CustomItemManager.getAllFilesFromDirectory("ItemData"); ArrayList cc = new ArrayList<>(); - for(int kk = 0; kk < hh.size(); kk++) { - FileConfiguration c = CustomItemManager.loadConfig("ItemData/" + hh.get(kk)); - if(Objects.equals(c.getString("type"), "Weapon")) { + for (String s : hh) { + FileConfiguration c = CustomItemManager.loadConfig("ItemData/" + s); + if (Objects.equals(c.getString("type"), "Weapon")) { cc.add(c.getString("name")); } } diff --git a/src/main/java/me/night/nullvalkyrie/database/Client.java b/src/main/java/me/night/nullvalkyrie/database/Client.java index 5e24f35..389902a 100644 --- a/src/main/java/me/night/nullvalkyrie/database/Client.java +++ b/src/main/java/me/night/nullvalkyrie/database/Client.java @@ -43,7 +43,7 @@ public class Client { Document doc = (Document) cursor.next(); for(String a : doc.keySet()) { if(!a.equals("_id")) { - System.out.println(a + ": " + doc.get(a)); +// System.out.println(a + ": " + doc.get(a)); } } } diff --git a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java index b559c3e..0550b52 100644 --- a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java @@ -4,6 +4,8 @@ import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.items.Rarity; import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.commands.SpawnCommand; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.entity.*; @@ -18,10 +20,14 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Merchant; import org.bukkit.inventory.MerchantRecipe; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import org.bukkit.scheduler.BukkitRunnable; import java.util.*; + public class CustomItemEvents implements Listener { private Main main; @@ -98,6 +104,20 @@ public class CustomItemEvents implements Listener { } else if (name.equalsIgnoreCase(Rarity.ULTRA.getColor() + "Snow Gun")) { Snowball s = player.launchProjectile(Snowball.class, player.getLocation().getDirection()); s.setVelocity(player.getLocation().getDirection().multiply(10)); + + ItemStack weapon = player.getInventory().getItemInMainHand(); + ItemMeta weaponMeta = weapon.getItemMeta(); + PersistentDataContainer container = weaponMeta.getPersistentDataContainer(); + System.out.println(name); + if(container != null) { + int ammo = container.get(CustomItemManager.keys.get("Snow Gun" + "." + "ammo"), PersistentDataType.INTEGER); + container.set(CustomItemManager.keys.get("Snow Gun" + "." + "ammo"), PersistentDataType.INTEGER, ammo - 1); + int maxload = container.get(CustomItemManager.keys.get("Snow Gun" + "." + "maxload"), PersistentDataType.INTEGER); + weapon.setItemMeta(weaponMeta); + e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&6AK-47 ( " + (ammo - 1) + "/ " + maxload + " )"))); + + } + } else if (name.equalsIgnoreCase(Rarity.MYTHIC.getColor() + "Terminator")) { Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); arrow.setVelocity(arrow.getVelocity().multiply(5)); @@ -226,7 +246,7 @@ public class CustomItemEvents implements Listener { if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite Water Bucket")) { e.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.WATER); e.setCancelled(true); - } else if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite LAVA Bucket")) { + } else if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite Lava Bucket")) { e.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.LAVA); e.setCancelled(true); } diff --git a/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java b/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java index c5d52cb..76f7a73 100644 --- a/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java +++ b/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java @@ -13,6 +13,8 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import java.io.File; import java.io.IOException; @@ -21,12 +23,16 @@ import java.util.*; public class CustomItemManager { private static HashMap a = new HashMap<>(); + public static HashMap keys = new HashMap<>(); private static Main main; public CustomItemManager(Main main) { this.main = main; + main.getConfig().options().copyDefaults(); + main.saveDefaultConfig(); if(!main.getDataFolder().exists()) { main.getDataFolder().mkdir(); } + createDirectoryInPluginFolder("ItemData"); createFilesFromConfig(main.getConfig()); register(); @@ -36,10 +42,10 @@ public class CustomItemManager { for(int kk = 0; kk < hh.size(); kk++) { FileConfiguration c = loadConfig("ItemData/" + hh.get(kk)); ItemStack i = new ItemStack(Material.matchMaterial(c.getString("material"))); - HashMap> d = new HashMap<>(); List pr = new ArrayList<>(); List ia = new ArrayList<>(); HashMap aa = new HashMap<>(); + HashMap pdc = new HashMap<>(); for (String key : c.getKeys(true)) { if (key.startsWith("enchants.")) { i.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(Arrays.asList(key.split("\\.")).get(1))), c.getInt(key)); @@ -67,14 +73,11 @@ public class CustomItemManager { } } - d.put("properties", pr); - d.put("ability", ia); - d.put("attributes", pr); ItemMeta im = i.getItemMeta(); im.setDisplayName(Rarity.getRarity(c.getString("rarity")).getColor() + c.getString("name")); im.setUnbreakable(true); ArrayList l = new ArrayList<>(); - l.addAll(d.get("properties")); + l.addAll(pr); l.add(""); ArrayList e = new ArrayList<>(); for (Enchantment ee : i.getEnchantments().keySet()) { @@ -109,6 +112,23 @@ public class CustomItemManager { } } im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS); + for (String key : c.getKeys(true)) { + if (key.startsWith("pdc.")) { + String property = Arrays.asList(key.split("\\.")).get(1); + if (property.equals("ammo")) { + PersistentDataContainer container = im.getPersistentDataContainer(); + NamespacedKey key1 = new NamespacedKey(main, "ammo"); + keys.put(c.getString("name") + "." + property, key1); + container.set(key1, PersistentDataType.INTEGER, c.getInt(key)); + } + else if (property.equals("maxload")) { + PersistentDataContainer container = im.getPersistentDataContainer(); + NamespacedKey key2 = new NamespacedKey(main, "maxload"); + keys.put(c.getString("name") + "." + property, key2); + container.set(key2, PersistentDataType.INTEGER, c.getInt(key)); + } + } + } i.setItemMeta(im); a.put(c.getString("name"), i); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 38add75..3b14b09 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -61,6 +61,9 @@ SnowGun: attributes: damage: 25.0 moveSpeed: 0.2 + pdc: + ammo: 30 + maxload: 121 TeleportDoor: name: "Teleport Door" material: "DIAMOND_SHOVEL"