persistence data container commands
This commit is contained in:
parent
1a0f33dcdb
commit
c3af2d9311
7 changed files with 119 additions and 46 deletions
4
pom.xml
4
pom.xml
|
@ -26,8 +26,8 @@
|
|||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<source>15</source>
|
||||
<target>15</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}.runTaskTimer(main, 0L, 10);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}, 0L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,19 +14,10 @@ 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;
|
||||
|
@ -40,23 +30,26 @@ public class WeaponCommand extends Command {
|
|||
s.append(a);
|
||||
s.append(" ");
|
||||
}
|
||||
|
||||
}
|
||||
ItemStack item = main.getCustomItemManager().getItem(s.toString());
|
||||
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<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
if (args.length == 1) {
|
||||
List<String> hh = CustomItemManager.getAllFilesFromDirectory("ItemData");
|
||||
ArrayList<String> cc = new ArrayList<>();
|
||||
for(int kk = 0; kk < hh.size(); kk++) {
|
||||
FileConfiguration c = CustomItemManager.loadConfig("ItemData/" + hh.get(kk));
|
||||
for (String s : hh) {
|
||||
FileConfiguration c = CustomItemManager.loadConfig("ItemData/" + s);
|
||||
if (Objects.equals(c.getString("type"), "Weapon")) {
|
||||
cc.add(c.getString("name"));
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<String, ItemStack> a = new HashMap<>();
|
||||
public static HashMap<String, NamespacedKey> 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<String, List<String>> d = new HashMap<>();
|
||||
List<String> pr = new ArrayList<>();
|
||||
List<String> ia = new ArrayList<>();
|
||||
HashMap<String, Double> aa = new HashMap<>();
|
||||
HashMap<String, String> 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<String> l = new ArrayList<>();
|
||||
l.addAll(d.get("properties"));
|
||||
l.addAll(pr);
|
||||
l.add("");
|
||||
ArrayList<String> 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);
|
||||
}
|
||||
|
|
|
@ -61,6 +61,9 @@ SnowGun:
|
|||
attributes:
|
||||
damage: 25.0
|
||||
moveSpeed: 0.2
|
||||
pdc:
|
||||
ammo: 30
|
||||
maxload: 121
|
||||
TeleportDoor:
|
||||
name: "Teleport Door"
|
||||
material: "DIAMOND_SHOVEL"
|
||||
|
|
Loading…
Reference in a new issue