nms comments

This commit is contained in:
NK 2022-12-20 14:14:39 +00:00
parent 0f39c1bc73
commit 5b6e509cbf
4 changed files with 41 additions and 31 deletions

View file

@ -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<HashMap<String, Object>> npcList = new ArrayList<>();
try (MongoCursor<Document> cursor = DatabaseManager.getNPCsDB().find().cursor()) {
while (cursor.hasNext()) {
Document document = cursor.next();
HashMap<String, Object> 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);
}
}

View file

@ -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<EntityPlayer> 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<HashMap<String, Object>> npcs) {
for (HashMap<String, Object> 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);
}
}
}

View file

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

View file

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