From 07053c9b5f39dbdaa8eaffe611da96506a330561 Mon Sep 17 00:00:00 2001 From: NK Date: Mon, 28 Nov 2022 11:54:21 +0000 Subject: [PATCH] NPC moved to mongodb --- pom.xml | 4 +- src/main/java/me/night/nullvalkyrie/Main.java | 3 +- .../nullvalkyrie/commands/BetaCommand.java | 2 + .../database/DatabaseManager.java | 2 + .../nullvalkyrie/database/NPCDataManager.java | 67 +++++++++++++++++++ .../nullvalkyrie/events/CustomItemEvents.java | 3 - .../me/night/nullvalkyrie/npc/NPCEvents.java | 5 +- .../me/night/nullvalkyrie/npc/NPCManager.java | 46 +------------ 8 files changed, 80 insertions(+), 52 deletions(-) create mode 100644 src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java diff --git a/pom.xml b/pom.xml index bcdbb17..029ac7e 100644 --- a/pom.xml +++ b/pom.xml @@ -26,8 +26,8 @@ maven-compiler-plugin 3.8.1 - 15 - 15 + 16 + 16 diff --git a/src/main/java/me/night/nullvalkyrie/Main.java b/src/main/java/me/night/nullvalkyrie/Main.java index bc0b9b2..4b54206 100644 --- a/src/main/java/me/night/nullvalkyrie/Main.java +++ b/src/main/java/me/night/nullvalkyrie/Main.java @@ -2,6 +2,7 @@ package me.night.nullvalkyrie; import io.github.cdimascio.dotenv.Dotenv; import me.night.nullvalkyrie.chests.MenuListener; +import me.night.nullvalkyrie.database.NPCDataManager; import me.night.nullvalkyrie.discord.DiscordClientManager; import me.night.nullvalkyrie.enchantments.EnchantmentManager; import me.night.nullvalkyrie.events.*; @@ -32,6 +33,6 @@ public final class Main extends JavaPlugin { Bukkit.getPluginManager().registerEvents(new NPCEvents(), this); new DiscordClientManager(); new DatabaseManager(); - NPCManager.reloadNPC(); + NPCDataManager.reloadNPC(); } } diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index 48423d6..dcaa9a4 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -1,5 +1,6 @@ package me.night.nullvalkyrie.commands; +import me.night.nullvalkyrie.npc.NPCManager; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -14,6 +15,7 @@ public class BetaCommand extends Command { @Override public void onCommand(CommandSender sender, String[] args) { if (sender instanceof Player) { + NPCManager.createNPC((Player) sender, args[0]); } } @Override diff --git a/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java b/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java index 3bc1264..eb435b9 100644 --- a/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java @@ -12,6 +12,7 @@ public class DatabaseManager { private static MongoCollection users; private static MongoCollection custom_weapons; public static MongoCollection ranks; + public static MongoCollection npcs; public MongoClient client; public static MongoDatabase database; @@ -21,6 +22,7 @@ public class DatabaseManager { users = database.getCollection("users"); custom_weapons = database.getCollection("custom_weapons"); ranks = database.getCollection("ranks"); + npcs = database.getCollection("npcs"); } public static void createUserSchema(String username) { Document document = new Document(); diff --git a/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java b/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java new file mode 100644 index 0000000..3a00995 --- /dev/null +++ b/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java @@ -0,0 +1,67 @@ +package me.night.nullvalkyrie.database; + +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.properties.Property; +import com.mongodb.client.MongoCursor; +import me.night.nullvalkyrie.util.Util; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.EntityPlayer; +import net.minecraft.server.level.WorldServer; +import org.bson.Document; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_19_R1.CraftServer; +import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; + +import java.util.UUID; + +import static me.night.nullvalkyrie.npc.NPCManager.*; + + +public class NPCDataManager { + public static void setNPC(String name, int x, int y, int z, int pitch, int yaw, String world, String texture, String signature) { + Document document = DatabaseManager.npcs.find(new Document("Name", name)).first(); + if(document != null) { + System.out.println("A NPC with this name already exist"); + } else { + Document newDocument = new Document(); + newDocument.put("Name", name); + newDocument.put("x", x); + newDocument.put("y", y); + newDocument.put("z", z); + newDocument.put("pitch", pitch); + newDocument.put("yaw", yaw); + newDocument.put("world", world); + newDocument.put("texture", texture); + newDocument.put("signature", signature); + DatabaseManager.npcs.insertOne(newDocument); + } + } + public static void reloadNPC() { + try (MongoCursor cursor = DatabaseManager.npcs.find().cursor()){ + while(cursor.hasNext()) { + Document document = cursor.next(); + String name = (String) document.get("Name"); + int x = (int) document.get("x"); + int y = (int) document.get("y"); + int z = (int) document.get("z"); + int pitch = (int) document.get("pitch"); + int yaw = (int) document.get("yaw"); + String world = (String) document.get("world"); + String texture = (String) document.get("texture"); + String signature = (String) document.get("signature"); + Location location = new Location(Bukkit.getWorld(world), x ,y ,z); + location.setPitch((float) pitch); + location.setYaw((float) yaw); + GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Util.color(name)); + gameProfile.getProperties().put("textures", new Property("textures", texture, signature)); + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + WorldServer w = ((CraftWorld) location.getWorld()).getHandle(); + EntityPlayer npc = new EntityPlayer(server, w, gameProfile, null); + npc.a(location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getPitch()); + addNPCPacket(npc); + getNPCs().add(npc); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java index 2f7f6a0..d5b0190 100644 --- a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java @@ -5,8 +5,6 @@ import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.BlockPosition; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.items.Pickaxe; import me.night.nullvalkyrie.items.Rarity; @@ -30,7 +28,6 @@ import org.bukkit.persistence.PersistentDataType; import java.lang.reflect.InvocationTargetException; import java.util.*; -import java.util.concurrent.TimeUnit; public class CustomItemEvents implements Listener { private final Main main; diff --git a/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java b/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java index 1b2c2a7..1d146a9 100644 --- a/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java +++ b/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java @@ -19,6 +19,7 @@ public class NPCEvents implements Listener { player.sendMessage(Util.color("Hi")); } } + @EventHandler public void onMove(PlayerMoveEvent e) { NPCManager.getNPCs().forEach(npc -> { @@ -27,8 +28,8 @@ public class NPCEvents implements Listener { float yaw = location.getYaw(); float pitch = location.getPitch(); PlayerConnection con = ((CraftPlayer) e.getPlayer()).getHandle().b; - con.a(new PacketPlayOutEntityHeadRotation(npc, (byte) ((yaw%360)*256/360))); - con.a(new PacketPlayOutEntity.PacketPlayOutEntityLook(npc.ae(), (byte) ((yaw%360)*256/360), (byte) ((pitch%360)*256/360), false)); + con.a(new PacketPlayOutEntityHeadRotation(npc, (byte) ((yaw % 360) * 256 / 360))); + con.a(new PacketPlayOutEntity.PacketPlayOutEntityLook(npc.ae(), (byte) ((yaw % 360) * 256 / 360), (byte) ((pitch % 360) * 256 / 360), false)); }); } } diff --git a/src/main/java/me/night/nullvalkyrie/npc/NPCManager.java b/src/main/java/me/night/nullvalkyrie/npc/NPCManager.java index 3bc164d..ccc41e1 100644 --- a/src/main/java/me/night/nullvalkyrie/npc/NPCManager.java +++ b/src/main/java/me/night/nullvalkyrie/npc/NPCManager.java @@ -4,7 +4,7 @@ import com.mojang.authlib.GameProfile; import com.mojang.authlib.properties.Property; import com.mojang.datafixers.util.Pair; import me.night.nullvalkyrie.Main; -import me.night.nullvalkyrie.items.CustomItemManager; +import me.night.nullvalkyrie.database.NPCDataManager; import me.night.nullvalkyrie.util.Util; import net.minecraft.network.protocol.game.*; import net.minecraft.network.syncher.DataWatcher; @@ -18,18 +18,14 @@ import net.minecraft.world.entity.EnumItemSlot; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.craftbukkit.v1_19_R1.CraftServer; import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.UUID; public class NPCManager { @@ -38,9 +34,6 @@ public class NPCManager { public static List getNPCs() { return NPCs; } - public static void reloadNPC() { - loadNPC(CustomItemManager.loadConfig("npcs.yml")); - } public static void createNPC(Player player, String name) { //name must be less than 16 characters including color codes ** EntityPlayer sp = ((CraftPlayer) player).getHandle(); MinecraftServer server = sp.c; @@ -53,43 +46,8 @@ public class NPCManager { npc.a(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); addNPCPacket(npc); NPCs.add(npc); - int var = 1; - FileConfiguration npcFile = CustomItemManager.loadConfig("npcs.yml"); - if (npcFile.contains("data")) var = npcFile.getConfigurationSection("data").getKeys(false).size() + 1; - npcFile.set("data." + var + ".x", (int) player.getLocation().getX()); - npcFile.set("data." + var + ".y", (int) player.getLocation().getY()); - npcFile.set("data." + var + ".z", (int) player.getLocation().getZ()); - npcFile.set("data." + var + ".pitch", (int) player.getLocation().getPitch()); - npcFile.set("data." + var + ".yaw", (int) player.getLocation().getYaw()); - npcFile.set("data." + var + ".world", player.getLocation().getWorld().getName()); - npcFile.set("data." + var + ".name", name); - npcFile.set("data." + var + ".texture", skin[0]); - npcFile.set("data." + var + ".signature", skin[1]); - try { - npcFile.save(CustomItemManager.loadFile("npcs.yml")); - } catch (IOException e) { - e.printStackTrace(); - } + NPCDataManager.setNPC(name, (int) player.getLocation().getX(), (int) player.getLocation().getY(), (int) player.getLocation().getZ(), (int) player.getLocation().getPitch(), (int) player.getLocation().getYaw(), player.getLocation().getWorld().getName(), skin[0], skin[1]); } - - public static void loadNPC(FileConfiguration npcFile) { - npcFile.getConfigurationSection("data").getKeys(false).forEach(npc -> { - Location location = new Location(Bukkit.getWorld(Objects.requireNonNull(npcFile.getString("data." + npc + ".world"))), npcFile.getInt("data." + npc + ".x"), npcFile.getInt("data." + npc + ".y"), npcFile.getInt("data." + npc + ".z")); - location.setPitch((float) npcFile.getDouble("data." + npc + ".pitch")); - location.setYaw((float) npcFile.getDouble("data." + npc + ".yaw")); - String name = npcFile.getString("data." + npc + ".name"); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Util.color(name)); - gameProfile.getProperties().put("textures", new Property("textures", npcFile.getString("data." + npc + ".texture"), npcFile.getString("data." + npc + ".signature"))); - MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); - WorldServer world = ((CraftWorld) location.getWorld()).getHandle(); - EntityPlayer npcs = new EntityPlayer(server, world, gameProfile, null); - npcs.a(location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getPitch()); - addNPCPacket(npcs); - NPCs.add(npcs); - }); - - } - public static void addNPCPacket(EntityPlayer npc) { for (Player player : Bukkit.getOnlinePlayers()) { PlayerConnection pc = ((CraftPlayer) player).getHandle().b;