NPC moved to mongodb
This commit is contained in:
parent
bf8681a385
commit
07053c9b5f
8 changed files with 80 additions and 52 deletions
4
pom.xml
4
pom.xml
|
@ -26,8 +26,8 @@
|
|||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>15</source>
|
||||
<target>15</target>
|
||||
<source>16</source>
|
||||
<target>16</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,6 +12,7 @@ public class DatabaseManager {
|
|||
private static MongoCollection<Document> users;
|
||||
private static MongoCollection<Document> custom_weapons;
|
||||
public static MongoCollection<Document> ranks;
|
||||
public static MongoCollection<Document> 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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<EntityPlayer> 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;
|
||||
|
|
Loading…
Reference in a new issue