NPC moved to mongodb

This commit is contained in:
NK 2022-11-28 11:54:21 +00:00
parent bf8681a385
commit 07053c9b5f
8 changed files with 80 additions and 52 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>15</source> <source>16</source>
<target>15</target> <target>16</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View file

@ -2,6 +2,7 @@ package me.night.nullvalkyrie;
import io.github.cdimascio.dotenv.Dotenv; import io.github.cdimascio.dotenv.Dotenv;
import me.night.nullvalkyrie.chests.MenuListener; import me.night.nullvalkyrie.chests.MenuListener;
import me.night.nullvalkyrie.database.NPCDataManager;
import me.night.nullvalkyrie.discord.DiscordClientManager; import me.night.nullvalkyrie.discord.DiscordClientManager;
import me.night.nullvalkyrie.enchantments.EnchantmentManager; import me.night.nullvalkyrie.enchantments.EnchantmentManager;
import me.night.nullvalkyrie.events.*; import me.night.nullvalkyrie.events.*;
@ -32,6 +33,6 @@ public final class Main extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new NPCEvents(), this); Bukkit.getPluginManager().registerEvents(new NPCEvents(), this);
new DiscordClientManager(); new DiscordClientManager();
new DatabaseManager(); new DatabaseManager();
NPCManager.reloadNPC(); NPCDataManager.reloadNPC();
} }
} }

View file

@ -1,5 +1,6 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.npc.NPCManager;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,6 +15,7 @@ public class BetaCommand extends Command {
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
if (sender instanceof Player) { if (sender instanceof Player) {
NPCManager.createNPC((Player) sender, args[0]);
} }
} }
@Override @Override

View file

@ -12,6 +12,7 @@ public class DatabaseManager {
private static MongoCollection<Document> users; private static MongoCollection<Document> users;
private static MongoCollection<Document> custom_weapons; private static MongoCollection<Document> custom_weapons;
public static MongoCollection<Document> ranks; public static MongoCollection<Document> ranks;
public static MongoCollection<Document> npcs;
public MongoClient client; public MongoClient client;
public static MongoDatabase database; public static MongoDatabase database;
@ -21,6 +22,7 @@ public class DatabaseManager {
users = database.getCollection("users"); users = database.getCollection("users");
custom_weapons = database.getCollection("custom_weapons"); custom_weapons = database.getCollection("custom_weapons");
ranks = database.getCollection("ranks"); ranks = database.getCollection("ranks");
npcs = database.getCollection("npcs");
} }
public static void createUserSchema(String username) { public static void createUserSchema(String username) {
Document document = new Document(); Document document = new Document();

View file

@ -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<Document> 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);
}
}
}
}

View file

@ -5,8 +5,6 @@ import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition; 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.CustomItemManager;
import me.night.nullvalkyrie.items.Pickaxe; import me.night.nullvalkyrie.items.Pickaxe;
import me.night.nullvalkyrie.items.Rarity; import me.night.nullvalkyrie.items.Rarity;
@ -30,7 +28,6 @@ import org.bukkit.persistence.PersistentDataType;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
public class CustomItemEvents implements Listener { public class CustomItemEvents implements Listener {
private final Main main; private final Main main;

View file

@ -19,6 +19,7 @@ public class NPCEvents implements Listener {
player.sendMessage(Util.color("Hi")); player.sendMessage(Util.color("Hi"));
} }
} }
@EventHandler @EventHandler
public void onMove(PlayerMoveEvent e) { public void onMove(PlayerMoveEvent e) {
NPCManager.getNPCs().forEach(npc -> { NPCManager.getNPCs().forEach(npc -> {
@ -27,8 +28,8 @@ public class NPCEvents implements Listener {
float yaw = location.getYaw(); float yaw = location.getYaw();
float pitch = location.getPitch(); float pitch = location.getPitch();
PlayerConnection con = ((CraftPlayer) e.getPlayer()).getHandle().b; PlayerConnection con = ((CraftPlayer) e.getPlayer()).getHandle().b;
con.a(new PacketPlayOutEntityHeadRotation(npc, (byte) ((yaw%360)*256/360))); 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 PacketPlayOutEntity.PacketPlayOutEntityLook(npc.ae(), (byte) ((yaw % 360) * 256 / 360), (byte) ((pitch % 360) * 256 / 360), false));
}); });
} }
} }

View file

@ -4,7 +4,7 @@ import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.Property;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.database.NPCDataManager;
import me.night.nullvalkyrie.util.Util; import me.night.nullvalkyrie.util.Util;
import net.minecraft.network.protocol.game.*; import net.minecraft.network.protocol.game.*;
import net.minecraft.network.syncher.DataWatcher; import net.minecraft.network.syncher.DataWatcher;
@ -18,18 +18,14 @@ import net.minecraft.world.entity.EnumItemSlot;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; 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.CraftWorld;
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class NPCManager { public class NPCManager {
@ -38,9 +34,6 @@ public class NPCManager {
public static List<EntityPlayer> getNPCs() { public static List<EntityPlayer> getNPCs() {
return NPCs; 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 ** public static void createNPC(Player player, String name) { //name must be less than 16 characters including color codes **
EntityPlayer sp = ((CraftPlayer) player).getHandle(); EntityPlayer sp = ((CraftPlayer) player).getHandle();
MinecraftServer server = sp.c; MinecraftServer server = sp.c;
@ -53,43 +46,8 @@ public class NPCManager {
npc.a(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); npc.a(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
addNPCPacket(npc); addNPCPacket(npc);
NPCs.add(npc); NPCs.add(npc);
int var = 1; 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]);
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();
}
} }
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) { public static void addNPCPacket(EntityPlayer npc) {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
PlayerConnection pc = ((CraftPlayer) player).getHandle().b; PlayerConnection pc = ((CraftPlayer) player).getHandle().b;