move a lot of packet sending to PacketEvents

This commit is contained in:
Pyrbu 2023-04-23 01:34:34 +01:00
parent 0efab9d77d
commit 74c954c553
15 changed files with 58 additions and 223 deletions

@ -1,5 +1,6 @@
package io.github.znetworkw.znpcservers.commands.list; package io.github.znetworkw.znpcservers.commands.list;
import com.github.retrooper.packetevents.protocol.player.EquipmentSlot;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -180,9 +181,7 @@ public class DefaultCommand extends Command {
Configuration.MESSAGES.sendMessage(sender.getCommandSender(), ConfigurationValue.NPC_NOT_FOUND); Configuration.MESSAGES.sendMessage(sender.getCommandSender(), ConfigurationValue.NPC_NOT_FOUND);
return; return;
} }
foundNPC.getNpcPojo().getNpcEquip().put( foundNPC.getNpcPojo().getNpcEquip().put(EquipmentSlot.valueOf(args.get("slot").toUpperCase()), sender.getPlayer().getInventory().getItemInHand());
ItemSlot.valueOf(args.get("slot").toUpperCase()), sender
.getPlayer().getInventory().getItemInHand());
foundNPC.getPackets().flushCache("equipPackets"); foundNPC.getPackets().flushCache("equipPackets");
Objects.requireNonNull(foundNPC); Objects.requireNonNull(foundNPC);
Objects.requireNonNull(foundNPC); Objects.requireNonNull(foundNPC);

@ -1,5 +1,7 @@
package io.github.znetworkw.znpcservers.hologram; package io.github.znetworkw.znpcservers.hologram;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDestroyEntities;
import io.github.znetworkw.znpcservers.UnexpectedCallException; import io.github.znetworkw.znpcservers.UnexpectedCallException;
import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.configuration.Configuration; import io.github.znetworkw.znpcservers.configuration.Configuration;
@ -65,13 +67,7 @@ public class Hologram {
} }
public void delete(ZUser user) { public void delete(ZUser user) {
this.hologramLines.forEach(hologramLine -> { this.hologramLines.forEach(hologramLine -> PacketEvents.getAPI().getPlayerManager().sendPacket(user.toPlayer(), new WrapperPlayServerDestroyEntities(hologramLine.id)));
try {
Utils.sendPackets(user, this.npc.getPackets().getNms().createEntityDestroyPacket(hologramLine.id));
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException);
}
});
} }
public void updateNames(ZUser user) { public void updateNames(ZUser user) {

@ -2,17 +2,14 @@ package io.github.znetworkw.znpcservers.nms;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.npc.FunctionFactory; import io.github.znetworkw.znpcservers.npc.FunctionFactory;
import io.github.znetworkw.znpcservers.npc.ItemSlot;
import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.npc.NPC;
import io.github.znetworkw.znpcservers.npc.NPCType; import io.github.znetworkw.znpcservers.npc.NPCType;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.utility.ReflectionUtils; import io.github.znetworkw.znpcservers.utility.ReflectionUtils;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.Utils;
import org.bukkit.inventory.ItemStack;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
public interface NMS { public interface NMS {
int version(); int version();
@ -20,41 +17,11 @@ public interface NMS {
@PacketValue(keyName = "playerPacket") @PacketValue(keyName = "playerPacket")
Object createPlayer(Object paramObject, GameProfile paramGameProfile) throws ReflectiveOperationException; Object createPlayer(Object paramObject, GameProfile paramGameProfile) throws ReflectiveOperationException;
@PacketValue(keyName = "spawnPacket")
Object createSpawnPacket(Object paramObject, boolean paramBoolean) throws ReflectiveOperationException;
Object createEntityEquipmentPacket(int paramInt, ItemSlot paramItemSlot, ItemStack paramItemStack) throws ReflectiveOperationException;
Object createMetadataPacket(int paramInt, Object paramObject) throws ReflectiveOperationException; Object createMetadataPacket(int paramInt, Object paramObject) throws ReflectiveOperationException;
@PacketValue(keyName = "hologramSpawnPacket", valueType = ValueType.ARGUMENTS) @PacketValue(keyName = "hologramSpawnPacket", valueType = ValueType.ARGUMENTS)
Object createArmorStandSpawnPacket(Object paramObject) throws ReflectiveOperationException; Object createArmorStandSpawnPacket(Object paramObject) throws ReflectiveOperationException;
@SuppressWarnings("SuspiciousTernaryOperatorInVarargsCall")
@PacketValue(keyName = "destroyPacket", valueType = ValueType.ARGUMENTS)
default Object createEntityDestroyPacket(int entityId) throws ReflectiveOperationException {
return Reflections.PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR.get().newInstance(Reflections.PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR.get().getParameterTypes()[0].isArray() ? new int[] {entityId} : entityId);
}
@PacketValue(keyName = "enumSlot", valueType = ValueType.ARGUMENTS)
default Object getItemSlot(int slot) {
return Reflections.ENUM_ITEM_SLOT.getEnumConstants()[slot];
}
@PacketValue(keyName = "removeTab")
default Object createTabRemovePacket(Object nmsEntity) throws ReflectiveOperationException {
try {
return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.REMOVE_PLAYER_FIELD.get(), Collections.singletonList(nmsEntity));
} catch (Throwable throwable) {
boolean useOldMethod = (Reflections.PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CLASS != null);
if (useOldMethod) return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CONSTRUCTOR.get().newInstance(Collections.singletonList(Reflections.GET_UNIQUE_ID_METHOD.get().invoke(nmsEntity)));
return Reflections.PACKET_PLAY_OUT_PLAYER_INFO_CONSTRUCTOR.get().newInstance(Reflections.REMOVE_PLAYER_FIELD.get(), nmsEntity);
}
}
@PacketValue(keyName = "equipPackets")
ImmutableList<Object> createEquipmentPacket(NPC paramNPC) throws ReflectiveOperationException;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@PacketValue(keyName = "scoreboardPackets") @PacketValue(keyName = "scoreboardPackets")
default ImmutableList<Object> updateScoreboard(NPC npc) throws ReflectiveOperationException { default ImmutableList<Object> updateScoreboard(NPC npc) throws ReflectiveOperationException {

@ -6,7 +6,7 @@ import io.github.znetworkw.znpcservers.utility.Utils;
import java.util.Comparator; import java.util.Comparator;
public final class NMSFactory { public final class NMSFactory {
public static final ImmutableSet<NMS> ALL = ImmutableSet.of(new NMSV8(), new NMSV9(), new NMSV16(), new NMSV17(), new NMSV18(), new NMSV19()); public static final ImmutableSet<NMS> ALL = ImmutableSet.of(new NMSV8(), new NMSV9(), new NMSV17(), new NMSV18(), new NMSV19());
public static final NMS NMS_FOR_CURRENT_VERSION = findPacketForVersion(Utils.BUKKIT_VERSION); public static final NMS NMS_FOR_CURRENT_VERSION = findPacketForVersion(Utils.BUKKIT_VERSION);
@ -14,6 +14,6 @@ public final class NMSFactory {
return ALL.stream() return ALL.stream()
.filter(NMS -> (version >= NMS.version())) .filter(NMS -> (version >= NMS.version()))
.max(Comparator.comparing(NMS::version)) .max(Comparator.comparing(NMS::version))
.orElseThrow(() -> new IllegalArgumentException("No packet instance found for version: " + version)); .orElseThrow(() -> new IllegalArgumentException("No NMS instance found for version: " + version));
} }
} }

@ -1,27 +0,0 @@
package io.github.znetworkw.znpcservers.nms;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.npc.ItemSlot;
import io.github.znetworkw.znpcservers.npc.NPC;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Map;
public class NMSV16 extends NMSV9 {
public int version() {
return 16;
}
public ImmutableList<Object> createEquipmentPacket(NPC npc) throws ReflectiveOperationException {
List<Pair<?, ?>> pairs = Lists.newArrayListWithCapacity((ItemSlot.values()).length);
for (Map.Entry<ItemSlot, ItemStack> entry : npc.getNpcPojo().getNpcEquip().entrySet())
pairs.add(new Pair<>(getItemSlot(entry
.getKey().getSlot()),
createEntityEquipmentPacket(npc.getEntityID(), entry.getKey(), entry.getValue())));
return ImmutableList.of(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1.get().newInstance(npc.getEntityID(), pairs));
}
}

@ -6,7 +6,7 @@ import io.github.znetworkw.znpcservers.npc.NPC;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class NMSV17 extends NMSV16 { public class NMSV17 extends NMSV9 {
public int version() { public int version() {
return 17; return 17;
} }

@ -1,16 +1,12 @@
package io.github.znetworkw.znpcservers.nms; package io.github.znetworkw.znpcservers.nms;
import com.google.common.collect.ImmutableList;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.npc.ItemSlot;
import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.npc.NPC;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.Map;
public class NMSV8 implements NMS { public class NMSV8 implements NMS {
public int version() { public int version() {
@ -23,15 +19,6 @@ public class NMSV8 implements NMS {
.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, constructor.newInstance(nmsWorld)); .get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, constructor.newInstance(nmsWorld));
} }
public Object createSpawnPacket(Object nmsEntity, boolean isPlayer) throws ReflectiveOperationException {
return isPlayer ? Reflections.PACKET_PLAY_OUT_NAMED_ENTITY_CONSTRUCTOR.get().newInstance(nmsEntity) : Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(nmsEntity);
}
public Object createEntityEquipmentPacket(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException {
return Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(entityId,
itemSlot.getSlotOld(), Reflections.AS_NMS_COPY_METHOD.get().invoke(Reflections.CRAFT_ITEM_STACK_CLASS, itemStack));
}
public Object createMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException { public Object createMetadataPacket(int entityId, Object nmsEntity) throws ReflectiveOperationException {
Object dataWatcher = Reflections.GET_DATA_WATCHER_METHOD.get().invoke(nmsEntity); Object dataWatcher = Reflections.GET_DATA_WATCHER_METHOD.get().invoke(nmsEntity);
try { try {
@ -45,15 +32,6 @@ public class NMSV8 implements NMS {
return Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(armorStand); return Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(armorStand);
} }
public ImmutableList<Object> createEquipmentPacket(NPC npc) throws ReflectiveOperationException {
ImmutableList.Builder<Object> builder = ImmutableList.builder();
for (Map.Entry<ItemSlot, ItemStack> stackEntry : npc.getNpcPojo().getNpcEquip().entrySet()) {
builder.add(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD.get().newInstance(npc.getEntityID(), stackEntry.getKey().getSlotOld(),
createEntityEquipmentPacket(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue())));
}
return builder.build();
}
public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException { public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException {
throw new IllegalStateException("Glow color is not supported for 1.8 version."); throw new IllegalStateException("Glow color is not supported for 1.8 version.");
} }

@ -1,33 +1,14 @@
package io.github.znetworkw.znpcservers.nms; package io.github.znetworkw.znpcservers.nms;
import com.google.common.collect.ImmutableList;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.npc.ItemSlot;
import io.github.znetworkw.znpcservers.npc.NPC; import io.github.znetworkw.znpcservers.npc.NPC;
import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.Utils;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
public class NMSV9 extends NMSV8 { public class NMSV9 extends NMSV8 {
public int version() { public int version() {
return 9; return 9;
} }
public Object createEntityEquipmentPacket(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException {
return Reflections.AS_NMS_COPY_METHOD.get().invoke(Reflections.CRAFT_ITEM_STACK_CLASS, itemStack);
}
public ImmutableList<Object> createEquipmentPacket(NPC npc) throws ReflectiveOperationException {
ImmutableList.Builder<Object> builder = ImmutableList.builder();
for (Map.Entry<ItemSlot, ItemStack> stackEntry : npc.getNpcPojo().getNpcEquip().entrySet()) {
builder.add(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_NEWEST_OLD.get().newInstance(npc.getEntityID(),
getItemSlot(stackEntry.getKey().getSlot()),
createEntityEquipmentPacket(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue())));
}
return builder.build();
}
public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException { public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException {
Object enumChatString = Reflections.ENUM_CHAT_TO_STRING_METHOD.get().invoke(npc.getGlowColor()); Object enumChatString = Reflections.ENUM_CHAT_TO_STRING_METHOD.get().invoke(npc.getGlowColor());
if (Utils.BUKKIT_VERSION > 12) { if (Utils.BUKKIT_VERSION > 12) {

@ -1,27 +0,0 @@
package io.github.znetworkw.znpcservers.npc;
public enum ItemSlot {
HELMET(5),
CHESTPLATE(4),
LEGGINGS(3),
BOOTS(2),
OFFHAND(1),
HAND(0);
private final int slot;
private final int slotOld;
ItemSlot(int slot) {
this.slot = slot;
this.slotOld = (slot == 0) ? 0 : (slot - 1);
}
public int getSlot() {
return this.slot;
}
public int getSlotOld() {
return this.slotOld;
}
}

@ -1,9 +1,13 @@
package io.github.znetworkw.znpcservers.npc; package io.github.znetworkw.znpcservers.npc;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.Equipment;
import com.github.retrooper.packetevents.wrapper.play.server.*;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import io.github.znetworkw.znpcservers.UnexpectedCallException; import io.github.znetworkw.znpcservers.UnexpectedCallException;
import io.github.znetworkw.znpcservers.hologram.Hologram; import io.github.znetworkw.znpcservers.hologram.Hologram;
import io.github.znetworkw.znpcservers.nms.PacketCache; import io.github.znetworkw.znpcservers.nms.PacketCache;
@ -14,6 +18,7 @@ import io.github.znetworkw.znpcservers.utility.Utils;
import io.github.znetworkw.znpcservers.utility.location.ZLocation; import io.github.znetworkw.znpcservers.utility.location.ZLocation;
import lol.pyr.znpcsplus.ZNPCsPlus; import lol.pyr.znpcsplus.ZNPCsPlus;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.*; import java.util.*;
@ -211,19 +216,31 @@ public class NPC {
ImmutableList<Object> scoreboardPackets = this.packets.getNms().updateScoreboard(this); ImmutableList<Object> scoreboardPackets = this.packets.getNms().updateScoreboard(this);
scoreboardPackets.forEach(p -> Utils.sendPackets(user, p)); scoreboardPackets.forEach(p -> Utils.sendPackets(user, p));
} }
ZLocation location = npcPojo.getLocation();
Player player = user.toPlayer();
if (npcIsPlayer) { if (npcIsPlayer) {
if (FunctionFactory.isTrue(this, "mirror")) updateProfile(user.getGameProfile().getProperties()); if (FunctionFactory.isTrue(this, "mirror")) updateProfile(user.getGameProfile().getProperties());
Utils.sendPackets(user, this.tabConstructor, this.updateTabConstructor); Utils.sendPackets(user, this.tabConstructor, this.updateTabConstructor);
ZNPCsPlus.SCHEDULER.runTask(() -> {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnPlayer(entityID,
this.gameProfile.getId(), SpigotConversionUtil.fromBukkitLocation(location.toBukkitLocation())));
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityHeadLook(entityID, location.getYaw()));
});
} }
Utils.sendPackets(user, this.packets.getNms().createSpawnPacket(this.nmsEntity, npcIsPlayer)); else PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnEntity(entityID,
Optional.of(uuid), SpigotConversionUtil.fromBukkitEntityType(((Entity) bukkitEntity).getType()),
location.toVector3d(), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.empty()));
if (FunctionFactory.isTrue(this, "holo")) this.hologram.spawn(user); if (FunctionFactory.isTrue(this, "holo")) this.hologram.spawn(user);
updateMetadata(Collections.singleton(user)); updateMetadata(Collections.singleton(user));
sendEquipPackets(user); sendEquipPackets(user);
lookAt(user, getLocation(), true); lookAt(user, getLocation(), true);
if (npcIsPlayer) { if (npcIsPlayer) ZNPCsPlus.SCHEDULER.scheduleSyncDelayedTask(() -> {
Object removeTabPacket = this.packets.getNms().createTabRemovePacket(this.nmsEntity); PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerPlayerInfoRemove(gameProfile.getId()));
ZNPCsPlus.SCHEDULER.scheduleSyncDelayedTask(() -> Utils.sendPackets(user, removeTabPacket, this.updateTabConstructor), 60); Utils.sendPackets(user, this.updateTabConstructor);
} }, 60);
} catch (ReflectiveOperationException operationException) { } catch (ReflectiveOperationException operationException) {
delete(user); delete(user);
throw new UnexpectedCallException(operationException); throw new UnexpectedCallException(operationException);
@ -237,13 +254,10 @@ public class NPC {
} }
private void handleDelete(ZUser user) { private void handleDelete(ZUser user) {
try { Player player = user.toPlayer();
if (this.npcPojo.getNpcType() == NPCType.PLAYER) this.packets.getNms().createTabRemovePacket(this.nmsEntity); this.hologram.delete(user);
this.hologram.delete(user); if (this.npcPojo.getNpcType() == NPCType.PLAYER) PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerPlayerInfoRemove(gameProfile.getId()));
Utils.sendPackets(user, this.packets.getNms().createEntityDestroyPacket(this.entityID)); PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerDestroyEntities(this.entityID));
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException);
}
} }
public void lookAt(ZUser player, Location location, boolean rotation) { public void lookAt(ZUser player, Location location, boolean rotation) {
@ -289,12 +303,12 @@ public class NPC {
public void sendEquipPackets(ZUser zUser) { public void sendEquipPackets(ZUser zUser) {
if (this.npcPojo.getNpcEquip().isEmpty()) return; if (this.npcPojo.getNpcEquip().isEmpty()) return;
try { List<Equipment> equipment = npcPojo.getNpcEquip().entrySet().stream()
ImmutableList<Object> equipPackets = this.packets.getNms().createEquipmentPacket(this); .map(entry -> new Equipment(entry.getKey(), SpigotConversionUtil.fromBukkitItemStack(entry.getValue())))
equipPackets.forEach(o -> Utils.sendPackets(zUser, o)); .toList();
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException.getCause()); if (Utils.versionNewer(16)) PacketEvents.getAPI().getPlayerManager().sendPacket(zUser.toPlayer(), new WrapperPlayServerEntityEquipment(entityID, equipment));
} else for (Equipment e : equipment) PacketEvents.getAPI().getPlayerManager().sendPacket(zUser.toPlayer(), new WrapperPlayServerEntityEquipment(entityID, List.of(e)));
} }
public void setPath(NPCPath.AbstractTypeWriter typeWriter) { public void setPath(NPCPath.AbstractTypeWriter typeWriter) {

@ -1,5 +1,6 @@
package io.github.znetworkw.znpcservers.npc; package io.github.znetworkw.znpcservers.npc;
import com.github.retrooper.packetevents.protocol.player.EquipmentSlot;
import io.github.znetworkw.znpcservers.npc.conversation.ConversationModel; import io.github.znetworkw.znpcservers.npc.conversation.ConversationModel;
import io.github.znetworkw.znpcservers.utility.location.ZLocation; import io.github.znetworkw.znpcservers.utility.location.ZLocation;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -20,7 +21,7 @@ public class NPCModel {
private NPCType npcType; private NPCType npcType;
private List<String> hologramLines; private List<String> hologramLines;
private List<NPCAction> clickActions; private List<NPCAction> clickActions;
private Map<ItemSlot, ItemStack> npcEquip; private Map<EquipmentSlot, ItemStack> npcEquip;
private Map<String, Boolean> npcFunctions; private Map<String, Boolean> npcFunctions;
private Map<String, String[]> customizationMap; private Map<String, String[]> customizationMap;
@ -190,15 +191,15 @@ public class NPCModel {
return this; return this;
} }
public Map<ItemSlot, ItemStack> getNpcEquip() { public Map<EquipmentSlot, ItemStack> getNpcEquip() {
return this.npcEquip; return this.npcEquip;
} }
public void setNpcEquip(Map<ItemSlot, ItemStack> npcEquip) { public void setNpcEquip(Map<EquipmentSlot, ItemStack> npcEquip) {
this.npcEquip = npcEquip; this.npcEquip = npcEquip;
} }
public NPCModel withNpcEquip(Map<ItemSlot, ItemStack> npcEquip) { public NPCModel withNpcEquip(Map<EquipmentSlot, ItemStack> npcEquip) {
setNpcEquip(npcEquip); setNpcEquip(npcEquip);
return this; return this;
} }

@ -8,7 +8,6 @@ import io.github.znetworkw.znpcservers.reflection.types.MethodReflection;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -361,16 +360,9 @@ public final class Reflections {
public static final Class<?> ENUM_CHAT_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT) public static final Class<?> ENUM_CHAT_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.MINECRAFT)
.withClassName("EnumChatFormat")).get(); .withClassName("EnumChatFormat")).get();
public static final Class<?> ENUM_ITEM_SLOT = new ClassReflection(new ReflectionBuilder(ReflectionPackage.ENTITY)
.withClassName("EnumItemSlot")
.setStrict(Utils.versionNewer(9))).get();
public static final Class<?> I_CHAT_BASE_COMPONENT = new ClassReflection(new ReflectionBuilder(ReflectionPackage.CHAT) public static final Class<?> I_CHAT_BASE_COMPONENT = new ClassReflection(new ReflectionBuilder(ReflectionPackage.CHAT)
.withClassName("IChatBaseComponent")).get(); .withClassName("IChatBaseComponent")).get();
public static final Class<?> ITEM_STACK_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.ITEM)
.withClassName("ItemStack")).get();
public static final Class<?> DATA_WATCHER_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.SYNCHER) public static final Class<?> DATA_WATCHER_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.SYNCHER)
.withClassName("DataWatcher") .withClassName("DataWatcher")
.setStrict(Utils.versionNewer(9))).get(); .setStrict(Utils.versionNewer(9))).get();
@ -390,9 +382,6 @@ public final class Reflections {
public static final Class<?> WORLD_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.WORLD_LEVEL) public static final Class<?> WORLD_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.WORLD_LEVEL)
.withClassName("World")).get(); .withClassName("World")).get();
public static final Class<?> CRAFT_ITEM_STACK_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT)
.withClassName("inventory.CraftItemStack")).get();
public static final Class<?> WORLD_SERVER_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.SERVER_LEVEL) public static final Class<?> WORLD_SERVER_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.SERVER_LEVEL)
.withClassName("WorldServer")).get(); .withClassName("WorldServer")).get();
@ -409,16 +398,9 @@ public final class Reflections {
.withClassName("PacketPlayOutPlayerInfo") .withClassName("PacketPlayOutPlayerInfo")
.withClassName("ClientboundPlayerInfoUpdatePacket")).get(); .withClassName("ClientboundPlayerInfoUpdatePacket")).get();
public static final Class<?> PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("ClientboundPlayerInfoRemovePacket")
.setStrict(false)).get();
public static final Class<?> PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.PACKET) public static final Class<?> PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutScoreboardTeam")).get(); .withClassName("PacketPlayOutScoreboardTeam")).get();
public static final Class<?> PACKET_PLAY_OUT_ENTITY_DESTROY_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutEntityDestroy")).get();
public static final Class<?> SCOREBOARD_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.WORLD_SCORES) public static final Class<?> SCOREBOARD_CLASS = new ClassReflection(new ReflectionBuilder(ReflectionPackage.WORLD_SCORES)
.withClassName("Scoreboard")).get(); .withClassName("Scoreboard")).get();
@ -460,10 +442,6 @@ public final class Reflections {
.withClassName(PACKET_PLAY_OUT_PLAYER_INFO_CLASS) .withClassName(PACKET_PLAY_OUT_PLAYER_INFO_CLASS)
.withParameterTypes(ENUM_PLAYER_INFO_CLASS, (Utils.BUKKIT_VERSION > 16) ? Collection.class : Iterable.class).withParameterTypes(ENUM_PLAYER_INFO_CLASS, ENTITY_PLAYER_CLASS)); .withParameterTypes(ENUM_PLAYER_INFO_CLASS, (Utils.BUKKIT_VERSION > 16) ? Collection.class : Iterable.class).withParameterTypes(ENUM_PLAYER_INFO_CLASS, ENTITY_PLAYER_CLASS));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName(PACKET_PLAY_OUT_PLAYER_INFO_REMOVE_CLASS)
.withParameterTypes(List.class));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_ENTITY_LOOK_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_ENTITY_LOOK_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutEntity$PacketPlayOutEntityLook") .withClassName("PacketPlayOutEntity$PacketPlayOutEntityLook")
.withParameterTypes(int.class, byte.class, byte.class, boolean.class)); .withParameterTypes(int.class, byte.class, byte.class, boolean.class));
@ -485,14 +463,6 @@ public final class Reflections {
.withClassName("PacketPlayOutEntityMetadata") .withClassName("PacketPlayOutEntityMetadata")
.withParameterTypes(int.class, List.class)); .withParameterTypes(int.class, List.class));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_NAMED_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutNamedEntitySpawn")
.withParameterTypes(ENTITY_HUMAN_CLASS));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_ENTITY_DESTROY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName(PACKET_PLAY_OUT_ENTITY_DESTROY_CLASS)
.withParameterTypes(int.class).withParameterTypes(int[].class));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutSpawnEntity") .withClassName("PacketPlayOutSpawnEntity")
.withClassName("PacketPlayOutSpawnEntityLiving") .withClassName("PacketPlayOutSpawnEntityLiving")
@ -509,18 +479,6 @@ public final class Reflections {
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET) public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_SCOREBOARD_TEAM_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName(PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS)); .withClassName(PACKET_PLAY_OUT_SCOREBOARD_TEAM_CLASS));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutEntityEquipment")
.withParameterTypes(int.class, int.class, ITEM_STACK_CLASS));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_NEWEST_OLD = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutEntityEquipment")
.withParameterTypes(int.class, ENUM_ITEM_SLOT, ITEM_STACK_CLASS));
public static final ReflectionLazyLoader<Constructor<?>> PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1 = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutEntityEquipment")
.withParameterTypes(int.class, List.class));
public static final ReflectionLazyLoader<Constructor<?>> I_CHAT_BASE_COMPONENT_A_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.CHAT) public static final ReflectionLazyLoader<Constructor<?>> I_CHAT_BASE_COMPONENT_A_CONSTRUCTOR = new ConstructorReflection(new ReflectionBuilder(ReflectionPackage.CHAT)
.withClassName("ChatComponentText") .withClassName("ChatComponentText")
.withParameterTypes(String.class)); .withParameterTypes(String.class));
@ -533,11 +491,6 @@ public final class Reflections {
.withClassName(DATA_WATCHER_OBJECT) .withClassName(DATA_WATCHER_OBJECT)
.withParameterTypes(int.class, DATA_WATCHER_SERIALIZER)); .withParameterTypes(int.class, DATA_WATCHER_SERIALIZER));
public static final ReflectionLazyLoader<Method> AS_NMS_COPY_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.BUKKIT)
.withClassName("inventory.CraftItemStack")
.withMethodName("asNMSCopy")
.withParameterTypes(ItemStack.class));
public static final ReflectionLazyLoader<Method> GET_PROFILE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.ENTITY) public static final ReflectionLazyLoader<Method> GET_PROFILE_METHOD = new MethodReflection(new ReflectionBuilder(ReflectionPackage.ENTITY)
.withClassName(ENTITY_HUMAN_CLASS) .withClassName(ENTITY_HUMAN_CLASS)
.withExpectResult(GameProfile.class)); .withExpectResult(GameProfile.class));
@ -676,11 +629,6 @@ public final class Reflections {
.withClassName("ClientboundPlayerInfoUpdatePacket$a") .withClassName("ClientboundPlayerInfoUpdatePacket$a")
.withFieldName("d")).staticValueLoader(); .withFieldName("d")).staticValueLoader();
public static final ReflectionLazyLoader<Object> REMOVE_PLAYER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName("PacketPlayOutPlayerInfo$EnumPlayerInfoAction")
.withClassName("ClientboundPlayerInfoUpdatePacket$a")
.withFieldName((Utils.BUKKIT_VERSION > 16) ? "e" : "REMOVE_PLAYER")).staticValueLoader();
public static final ReflectionLazyLoader<Object> DATA_WATCHER_REGISTER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET) public static final ReflectionLazyLoader<Object> DATA_WATCHER_REGISTER_FIELD = new FieldReflection(new ReflectionBuilder(ReflectionPackage.PACKET)
.withClassName(DATA_WATCHER_REGISTRY) .withClassName(DATA_WATCHER_REGISTRY)
.withFieldName("a")).staticValueLoader(); .withFieldName("a")).staticValueLoader();

@ -73,10 +73,7 @@ public final class Utils {
public static void sendPackets(ZUser user, Object... packets) { public static void sendPackets(ZUser user, Object... packets) {
try { try {
for (Object packet : packets) { for (Object packet : packets) if (packet != null) Reflections.SEND_PACKET_METHOD.get().invoke(user.getPlayerConnection(), packet);
if (packet != null)
Reflections.SEND_PACKET_METHOD.get().invoke(user.getPlayerConnection(), packet);
}
} catch (IllegalAccessException | java.lang.reflect.InvocationTargetException e) { } catch (IllegalAccessException | java.lang.reflect.InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }

@ -1,5 +1,6 @@
package io.github.znetworkw.znpcservers.utility.location; package io.github.znetworkw.znpcservers.utility.location;
import com.github.retrooper.packetevents.util.Vector3d;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.gson.*; import com.google.gson.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -73,6 +74,10 @@ public class ZLocation {
return new Vector(x, y, z); return new Vector(x, y, z);
} }
public Vector3d toVector3d() {
return new Vector3d(x, y, z);
}
private static final double _2PI = 2 * Math.PI; private static final double _2PI = 2 * Math.PI;
public Location pointingTo(Location loc) { public Location pointingTo(Location loc) {

@ -60,7 +60,10 @@ public class ZNPCsPlus extends JavaPlugin {
NPC find = NPC.find(id); NPC find = NPC.find(id);
if (find != null) if (find != null)
return find; return find;
NPCModel pojo = (new NPCModel(id)).withHologramLines(Collections.singletonList(name)).withHologramHeight(npcType.getHoloHeight()).withLocation(new ZLocation(location)).withNpcType(npcType); NPCModel pojo = new NPCModel(id).withHologramLines(Collections.singletonList(name))
.withHologramHeight(npcType.getHoloHeight())
.withLocation(new ZLocation(location))
.withNpcType(npcType);
ConfigurationConstants.NPC_LIST.add(pojo); ConfigurationConstants.NPC_LIST.add(pojo);
return new NPC(pojo, true); return new NPC(pojo, true);
} }