From 5b6e509cbf5d69ab1fe45e8ddd106d58054f09a3 Mon Sep 17 00:00:00 2001 From: NK Date: Tue, 20 Dec 2022 14:14:39 +0000 Subject: [PATCH] nms comments --- .../nullvalkyrie/database/NPCDataManager.java | 41 ++++++++----------- .../entities/npcs/NPCManager.java | 17 +++++++- .../events/listeners/ServerEvents.java | 6 +++ .../nullvalkyrie/packets/PacketInjector.java | 8 +--- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java b/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java index f3bf348..1238f6d 100644 --- a/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/NPCDataManager.java @@ -1,21 +1,12 @@ 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 me.night.nullvalkyrie.entities.npcs.NPCManager; 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.entities.npcs.NPCManager.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; public class NPCDataManager { @@ -39,9 +30,11 @@ public class NPCDataManager { } public static void reloadNPC() { + List> npcList = new ArrayList<>(); try (MongoCursor cursor = DatabaseManager.getNPCsDB().find().cursor()) { while (cursor.hasNext()) { Document document = cursor.next(); + HashMap npc = new HashMap<>(); String name = document.getString("Name"); int x = document.getInteger("x"); int y = document.getInteger("y"); @@ -51,18 +44,18 @@ public class NPCDataManager { String world = document.getString("world"); String texture = document.getString("texture"); String signature = document.getString("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); + npc.put("name", name); + npc.put("x", x); + npc.put("y", y); + npc.put("z", z); + npc.put("pitch", pitch); + npc.put("yaw", yaw); + npc.put("world", world); + npc.put("texture", texture); + npc.put("signature", signature); + npcList.add(npc); } } + NPCManager.reloadNPC(npcList); } } \ No newline at end of file diff --git a/src/main/java/me/night/nullvalkyrie/entities/npcs/NPCManager.java b/src/main/java/me/night/nullvalkyrie/entities/npcs/NPCManager.java index 04b3255..6da3c9a 100644 --- a/src/main/java/me/night/nullvalkyrie/entities/npcs/NPCManager.java +++ b/src/main/java/me/night/nullvalkyrie/entities/npcs/NPCManager.java @@ -16,6 +16,7 @@ import net.minecraft.world.entity.EnumItemSlot; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +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; @@ -23,6 +24,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -32,7 +34,7 @@ public class NPCManager { public static List getNPCs() { return NPCs; } - 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(); MinecraftServer server = sp.c; WorldServer level = ((CraftWorld) player.getLocation().getWorld()).getHandle(); @@ -83,5 +85,18 @@ public class NPCManager { pc.a(new PacketPlayOutEntityEquipment(npc.ae(), list)); } } + public static void reloadNPC(List> npcs) { + for (HashMap npc : npcs) { + Location location = new Location(Bukkit.getWorld((String) npc.get("world")), (int) npc.get("x"), (int) npc.get("y"), (int) npc.get("z"), (int) npc.get("yaw"), (int) npc.get("pitch")); + GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Util.color((String) npc.get("name"))); + gameProfile.getProperties().put("textures", new Property("textures", (String) npc.get("texture"), (String) npc.get("signature"))); + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + WorldServer w = ((CraftWorld) location.getWorld()).getHandle(); + EntityPlayer ep = new EntityPlayer(server, w, gameProfile, null); + ep.a(location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getPitch()); // NMS: 1.19.2 https://nms.screamingsandals.org/1.19.2/net/minecraft/world/entity/Entity.html absMoveTo + addNPCPacket(ep); + NPCs.add(ep); + } + } } diff --git a/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java b/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java index c37fbc6..08d70fe 100644 --- a/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java @@ -9,6 +9,7 @@ import org.bukkit.boss.*; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.event.weather.WeatherChangeEvent; @@ -26,6 +27,11 @@ public class ServerEvents implements Listener { bossbar.addPlayer(e.getPlayer()); injector.addPlayer(e.getPlayer()); } + + @EventHandler + public void onQuit(PlayerQuitEvent e) { + injector.removePlayer(e.getPlayer()); + } @EventHandler public void onPing(ServerListPingEvent e) { e.setMaxPlayers(8964); diff --git a/src/main/java/me/night/nullvalkyrie/packets/PacketInjector.java b/src/main/java/me/night/nullvalkyrie/packets/PacketInjector.java index ca76cb8..4e18f42 100644 --- a/src/main/java/me/night/nullvalkyrie/packets/PacketInjector.java +++ b/src/main/java/me/night/nullvalkyrie/packets/PacketInjector.java @@ -1,14 +1,13 @@ package me.night.nullvalkyrie.packets; import io.netty.channel.Channel; -import net.minecraft.server.level.EntityPlayer; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.entity.Player; public class PacketInjector { public void addPlayer(Player p) { try { - Channel ch = nms(p).b.b.m; + Channel ch = ((CraftPlayer) p).getHandle().b.b.m; if (ch.pipeline().get("PacketInjector") == null) { PacketHandler h = new PacketHandler(p); ch.pipeline().addBefore("packet_handler", "PacketInjector", h); @@ -20,7 +19,7 @@ public class PacketInjector { public void removePlayer(Player p) { try { - Channel ch = nms(p).b.b.m; + Channel ch = ((CraftPlayer) p).getHandle().b.b.m; // NMS: 1.19.2 https://nms.screamingsandals.org/1.19.2/net/minecraft/server/network/ServerGamePacketListenerImpl.html PlayerConnection -> NetworkManager -> Channel if (ch.pipeline().get("PacketInjector") != null) { ch.pipeline().remove("PacketInjector"); } @@ -29,7 +28,4 @@ public class PacketInjector { } } - public static EntityPlayer nms(Player p) { - return ((CraftPlayer) p).getHandle(); - } }