persistence data container commands

This commit is contained in:
NK 2022-11-15 18:32:45 +00:00
parent 1a0f33dcdb
commit c3af2d9311
7 changed files with 119 additions and 46 deletions

View file

@ -26,8 +26,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>${java.version}</source> <source>15</source>
<target>${java.version}</target> <target>15</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View file

@ -5,19 +5,22 @@ import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.minecraft.network.protocol.game.*; import net.minecraft.network.protocol.game.*;
import net.minecraft.server.level.EntityPlayer; import net.minecraft.server.level.EntityPlayer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; 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.List;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
import static me.night.nullvalkyrie.database.Client.getUser; import static me.night.nullvalkyrie.database.Client.getUser;
public class BetaCommand extends Command { public class BetaCommand extends Command {
private Main main; private Main main;
private BukkitScheduler scheduler;
public BetaCommand(Main main) { public BetaCommand(Main main) {
super("beta", new String[]{"b"}, "Beta", ""); super("beta", new String[]{"b"}, "Beta", "");
@ -26,23 +29,57 @@ public class BetaCommand extends Command {
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
getUser("Kaly15"); // getUser("Kaly15");
if (sender instanceof Player) { if (sender instanceof Player) {
Player pa = (Player) sender; Player pa = (Player) sender;
CraftPlayer cp = (CraftPlayer) sender; // CraftPlayer cp = (CraftPlayer) sender;
EntityPlayer ep = cp.getHandle(); // EntityPlayer ep = cp.getHandle();
Entity e = (Entity) ep; // PacketPlayOutUpdateHealth packet = new PacketPlayOutUpdateHealth(20f, 20, 5.0f); // health, food, food saturation
PacketPlayOutUpdateHealth packet = new PacketPlayOutUpdateHealth(20f, 20, 5.0f); // health, food, food // ep.b.a(packet); // Sends the Packet
// 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 @Override
public void run() { public void run() {
pa.spigot().sendMessage(ChatMessageType.ACTION_BAR, while (true) {
TextComponent.fromLegacyText("§1NOT ENOUGH MANNER")); 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);
} }
} }

View file

@ -1,6 +1,5 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.items.CustomItemManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -15,49 +14,43 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class WeaponCommand extends Command { public class WeaponCommand extends Command {
private Main main; public WeaponCommand() {
public WeaponCommand(Main main) { super("weapon", new String[]{}, "Give you a weapon", "");
super(
"weapon",
new String[]{},
"Give you a weapon",
""
);
this.main = main;
} }
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
Player player = (Player) sender; Player player = (Player) sender;
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
List<String> b = Arrays.asList(args); List<String> b = Arrays.asList(args);
for(String a: args) { for (String a : args) {
if(a.equals(b.get(b.size() - 1))) { if (a.equals(b.get(b.size() - 1))) {
s.append(a); s.append(a);
} else { } else {
s.append(a); s.append(a);
s.append(" "); s.append(" ");
} }
} }
ItemStack item = main.getCustomItemManager().getItem(s.toString()); if (s.isEmpty()) {
if(item.hasItemMeta()) {
player.getInventory().addItem(item);
} else {
player.sendMessage(ChatColor.RED + "This item doesn't exist"); 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 @Override
public List<String> onTabComplete(CommandSender sender, String[] args) { public List<String> onTabComplete(CommandSender sender, String[] args) {
if(args.length == 1) { if (args.length == 1) {
List<String> hh = CustomItemManager.getAllFilesFromDirectory("ItemData"); List<String> hh = CustomItemManager.getAllFilesFromDirectory("ItemData");
ArrayList<String> cc = new ArrayList<>(); ArrayList<String> cc = new ArrayList<>();
for(int kk = 0; kk < hh.size(); kk++) { for (String s : hh) {
FileConfiguration c = CustomItemManager.loadConfig("ItemData/" + hh.get(kk)); FileConfiguration c = CustomItemManager.loadConfig("ItemData/" + s);
if(Objects.equals(c.getString("type"), "Weapon")) { if (Objects.equals(c.getString("type"), "Weapon")) {
cc.add(c.getString("name")); cc.add(c.getString("name"));
} }
} }

View file

@ -43,7 +43,7 @@ public class Client {
Document doc = (Document) cursor.next(); Document doc = (Document) cursor.next();
for(String a : doc.keySet()) { for(String a : doc.keySet()) {
if(!a.equals("_id")) { if(!a.equals("_id")) {
System.out.println(a + ": " + doc.get(a)); // System.out.println(a + ": " + doc.get(a));
} }
} }
} }

View file

@ -4,6 +4,8 @@ import me.night.nullvalkyrie.items.CustomItemManager;
import me.night.nullvalkyrie.items.Rarity; import me.night.nullvalkyrie.items.Rarity;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.commands.SpawnCommand; 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.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.*; import org.bukkit.entity.*;
@ -18,10 +20,14 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant; import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe; 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 org.bukkit.scheduler.BukkitRunnable;
import java.util.*; import java.util.*;
public class CustomItemEvents implements Listener { public class CustomItemEvents implements Listener {
private Main main; private Main main;
@ -98,6 +104,20 @@ public class CustomItemEvents implements Listener {
} else if (name.equalsIgnoreCase(Rarity.ULTRA.getColor() + "Snow Gun")) { } else if (name.equalsIgnoreCase(Rarity.ULTRA.getColor() + "Snow Gun")) {
Snowball s = player.launchProjectile(Snowball.class, player.getLocation().getDirection()); Snowball s = player.launchProjectile(Snowball.class, player.getLocation().getDirection());
s.setVelocity(player.getLocation().getDirection().multiply(10)); 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")) { } else if (name.equalsIgnoreCase(Rarity.MYTHIC.getColor() + "Terminator")) {
Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection());
arrow.setVelocity(arrow.getVelocity().multiply(5)); arrow.setVelocity(arrow.getVelocity().multiply(5));
@ -226,7 +246,7 @@ public class CustomItemEvents implements Listener {
if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite Water Bucket")) { if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite Water Bucket")) {
e.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.WATER); e.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.WATER);
e.setCancelled(true); 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.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.LAVA);
e.setCancelled(true); e.setCancelled(true);
} }

View file

@ -13,6 +13,8 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -21,12 +23,16 @@ import java.util.*;
public class CustomItemManager { public class CustomItemManager {
private static HashMap<String, ItemStack> a = new HashMap<>(); private static HashMap<String, ItemStack> a = new HashMap<>();
public static HashMap<String, NamespacedKey> keys = new HashMap<>();
private static Main main; private static Main main;
public CustomItemManager(Main main) { public CustomItemManager(Main main) {
this.main = main; this.main = main;
main.getConfig().options().copyDefaults();
main.saveDefaultConfig();
if(!main.getDataFolder().exists()) { if(!main.getDataFolder().exists()) {
main.getDataFolder().mkdir(); main.getDataFolder().mkdir();
} }
createDirectoryInPluginFolder("ItemData"); createDirectoryInPluginFolder("ItemData");
createFilesFromConfig(main.getConfig()); createFilesFromConfig(main.getConfig());
register(); register();
@ -36,10 +42,10 @@ public class CustomItemManager {
for(int kk = 0; kk < hh.size(); kk++) { for(int kk = 0; kk < hh.size(); kk++) {
FileConfiguration c = loadConfig("ItemData/" + hh.get(kk)); FileConfiguration c = loadConfig("ItemData/" + hh.get(kk));
ItemStack i = new ItemStack(Material.matchMaterial(c.getString("material"))); ItemStack i = new ItemStack(Material.matchMaterial(c.getString("material")));
HashMap<String, List<String>> d = new HashMap<>();
List<String> pr = new ArrayList<>(); List<String> pr = new ArrayList<>();
List<String> ia = new ArrayList<>(); List<String> ia = new ArrayList<>();
HashMap<String, Double> aa = new HashMap<>(); HashMap<String, Double> aa = new HashMap<>();
HashMap<String, String> pdc = new HashMap<>();
for (String key : c.getKeys(true)) { for (String key : c.getKeys(true)) {
if (key.startsWith("enchants.")) { if (key.startsWith("enchants.")) {
i.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(Arrays.asList(key.split("\\.")).get(1))), c.getInt(key)); 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(); ItemMeta im = i.getItemMeta();
im.setDisplayName(Rarity.getRarity(c.getString("rarity")).getColor() + c.getString("name")); im.setDisplayName(Rarity.getRarity(c.getString("rarity")).getColor() + c.getString("name"));
im.setUnbreakable(true); im.setUnbreakable(true);
ArrayList<String> l = new ArrayList<>(); ArrayList<String> l = new ArrayList<>();
l.addAll(d.get("properties")); l.addAll(pr);
l.add(""); l.add("");
ArrayList<String> e = new ArrayList<>(); ArrayList<String> e = new ArrayList<>();
for (Enchantment ee : i.getEnchantments().keySet()) { for (Enchantment ee : i.getEnchantments().keySet()) {
@ -109,6 +112,23 @@ public class CustomItemManager {
} }
} }
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS); 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); i.setItemMeta(im);
a.put(c.getString("name"), i); a.put(c.getString("name"), i);
} }

View file

@ -61,6 +61,9 @@ SnowGun:
attributes: attributes:
damage: 25.0 damage: 25.0
moveSpeed: 0.2 moveSpeed: 0.2
pdc:
ammo: 30
maxload: 121
TeleportDoor: TeleportDoor:
name: "Teleport Door" name: "Teleport Door"
material: "DIAMOND_SHOVEL" material: "DIAMOND_SHOVEL"