refactor NMS

This commit is contained in:
Pyrbu 2023-04-22 20:27:08 +01:00
parent cdc78e9fe1
commit f6940df7d2
10 changed files with 44 additions and 54 deletions

@ -176,7 +176,7 @@ public class NPC {
try {
Object nmsWorld = Reflections.GET_HANDLE_WORLD_METHOD.get().invoke(getLocation().getWorld());
boolean isPlayer = (npcType == NPCType.PLAYER);
this.nmsEntity = isPlayer ? this.packets.getNms().getPlayerPacket(nmsWorld, this.gameProfile) : (Utils.versionNewer(14) ? npcType.getConstructor().newInstance(npcType.getNmsEntityType(), nmsWorld) : npcType.getConstructor().newInstance(nmsWorld));
this.nmsEntity = isPlayer ? this.packets.getNms().createPlayer(nmsWorld, this.gameProfile) : (Utils.versionNewer(14) ? npcType.getConstructor().newInstance(npcType.getNmsEntityType(), nmsWorld) : npcType.getConstructor().newInstance(nmsWorld));
this.bukkitEntity = Reflections.GET_BUKKIT_ENTITY_METHOD.get().invoke(this.nmsEntity);
this.uuid = (UUID) Reflections.GET_UNIQUE_ID_METHOD.get().invoke(this.nmsEntity, new Object[0]);
if (isPlayer) {
@ -215,13 +215,13 @@ public class NPC {
if (FunctionFactory.isTrue(this, "mirror")) updateProfile(user.getGameProfile().getProperties());
Utils.sendPackets(user, this.tabConstructor, this.updateTabConstructor);
}
Utils.sendPackets(user, this.packets.getNms().getSpawnPacket(this.nmsEntity, npcIsPlayer));
Utils.sendPackets(user, this.packets.getNms().createSpawnPacket(this.nmsEntity, npcIsPlayer));
if (FunctionFactory.isTrue(this, "holo")) this.hologram.spawn(user);
updateMetadata(Collections.singleton(user));
sendEquipPackets(user);
lookAt(user, getLocation(), true);
if (npcIsPlayer) {
Object removeTabPacket = this.packets.getNms().getTabRemovePacket(this.nmsEntity);
Object removeTabPacket = this.packets.getNms().createTabRemovePacket(this.nmsEntity);
ZNPCsPlus.SCHEDULER.scheduleSyncDelayedTask(() -> Utils.sendPackets(user, removeTabPacket, this.updateTabConstructor), 60);
}
} catch (ReflectiveOperationException operationException) {
@ -238,9 +238,9 @@ public class NPC {
private void handleDelete(ZUser user) {
try {
if (this.npcPojo.getNpcType() == NPCType.PLAYER) this.packets.getNms().getTabRemovePacket(this.nmsEntity);
if (this.npcPojo.getNpcType() == NPCType.PLAYER) this.packets.getNms().createTabRemovePacket(this.nmsEntity);
this.hologram.delete(user);
Utils.sendPackets(user, this.packets.getNms().getDestroyPacket(this.entityID));
Utils.sendPackets(user, this.packets.getNms().createEntityDestroyPacket(this.entityID));
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException);
}
@ -267,7 +267,7 @@ public class NPC {
protected void updateMetadata(Iterable<ZUser> users) {
try {
Object metaData = this.packets.getNms().getMetadataPacket(this.entityID, this.nmsEntity);
Object metaData = this.packets.getNms().createMetadataPacket(this.entityID, this.nmsEntity);
for (ZUser user : users) Utils.sendPackets(user, metaData);
} catch (ReflectiveOperationException operationException) {
operationException.getCause().printStackTrace();
@ -290,7 +290,7 @@ public class NPC {
public void sendEquipPackets(ZUser zUser) {
if (this.npcPojo.getNpcEquip().isEmpty()) return;
try {
ImmutableList<Object> equipPackets = this.packets.getNms().getEquipPackets(this);
ImmutableList<Object> equipPackets = this.packets.getNms().createEquipmentPacket(this);
equipPackets.forEach(o -> Utils.sendPackets(zUser, o));
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException.getCause());

@ -35,7 +35,7 @@ public class GlowFunction extends NPCFunction {
}
protected boolean allow(NPC npc) {
return npc.getPackets().getNms().allowGlowColor();
return npc.getPackets().getNms().allowsGlowColor();
}
public NPCFunction.ResultType resolve(NPC npc) {

@ -46,7 +46,7 @@ public class Hologram {
y += LINE_SPACING;
}
setLocation(location, 0.0D);
this.npc.getPackets().flushCache("getHologramSpawnPacket");
this.npc.getPackets().flushCache("createArmorStandSpawnPacket");
this.npc.getViewers().forEach(this::spawn);
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException);
@ -56,7 +56,7 @@ public class Hologram {
public void spawn(ZUser user) {
this.hologramLines.forEach(hologramLine -> {
try {
Object entityPlayerPacketSpawn = this.npc.getPackets().getNms().getHologramSpawnPacket(hologramLine.armorStand);
Object entityPlayerPacketSpawn = this.npc.getPackets().getNms().createArmorStandSpawnPacket(hologramLine.armorStand);
Utils.sendPackets(user, entityPlayerPacketSpawn);
} catch (ReflectiveOperationException operationException) {
delete(user);
@ -67,7 +67,7 @@ public class Hologram {
public void delete(ZUser user) {
this.hologramLines.forEach(hologramLine -> {
try {
Utils.sendPackets(user, this.npc.getPackets().getNms().getDestroyPacket(hologramLine.id));
Utils.sendPackets(user, this.npc.getPackets().getNms().createEntityDestroyPacket(hologramLine.id));
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException);
}
@ -78,7 +78,7 @@ public class Hologram {
for (HologramLine hologramLine : this.hologramLines) {
try {
updateLine(hologramLine.line, hologramLine.armorStand, user);
Object metaData = this.npc.getPackets().getNms().getMetadataPacket(hologramLine.id, hologramLine.armorStand);
Object metaData = this.npc.getPackets().getNms().createMetadataPacket(hologramLine.id, hologramLine.armorStand);
Utils.sendPackets(user, metaData);
} catch (ReflectiveOperationException operationException) {
throw new UnexpectedCallException(operationException);

@ -18,23 +18,21 @@ public interface NMS {
int version();
@PacketValue(keyName = "playerPacket")
Object getPlayerPacket(Object paramObject, GameProfile paramGameProfile) throws ReflectiveOperationException;
Object createPlayer(Object paramObject, GameProfile paramGameProfile) throws ReflectiveOperationException;
@PacketValue(keyName = "spawnPacket")
Object getSpawnPacket(Object paramObject, boolean paramBoolean) throws ReflectiveOperationException;
Object createSpawnPacket(Object paramObject, boolean paramBoolean) throws ReflectiveOperationException;
Object convertItemStack(int paramInt, ItemSlot paramItemSlot, ItemStack paramItemStack) throws ReflectiveOperationException;
Object createEntityEquipmentPacket(int paramInt, ItemSlot paramItemSlot, ItemStack paramItemStack) throws ReflectiveOperationException;
Object getClickType(Object paramObject) throws ReflectiveOperationException;
Object getMetadataPacket(int paramInt, Object paramObject) throws ReflectiveOperationException;
Object createMetadataPacket(int paramInt, Object paramObject) throws ReflectiveOperationException;
@PacketValue(keyName = "hologramSpawnPacket", valueType = ValueType.ARGUMENTS)
Object getHologramSpawnPacket(Object paramObject) throws ReflectiveOperationException;
Object createArmorStandSpawnPacket(Object paramObject) throws ReflectiveOperationException;
@SuppressWarnings("SuspiciousTernaryOperatorInVarargsCall")
@PacketValue(keyName = "destroyPacket", valueType = ValueType.ARGUMENTS)
default Object getDestroyPacket(int entityId) throws ReflectiveOperationException {
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);
}
@ -44,7 +42,7 @@ public interface NMS {
}
@PacketValue(keyName = "removeTab")
default Object getTabRemovePacket(Object nmsEntity) throws ReflectiveOperationException {
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) {
@ -55,7 +53,7 @@ public interface NMS {
}
@PacketValue(keyName = "equipPackets")
ImmutableList<Object> getEquipPackets(NPC paramNPC) throws ReflectiveOperationException;
ImmutableList<Object> createEquipmentPacket(NPC paramNPC) throws ReflectiveOperationException;
@SuppressWarnings("unchecked")
@PacketValue(keyName = "scoreboardPackets")
@ -91,15 +89,15 @@ public interface NMS {
} else {
collection.add(npc.getUUID().toString());
}
if (allowGlowColor() && FunctionFactory.isTrue(npc, "glow"))
updateGlowPacket(npc, scoreboardTeamPacket);
if (allowsGlowColor() && FunctionFactory.isTrue(npc, "glow"))
updateGlow(npc, scoreboardTeamPacket);
builder.add(isVersion17 ? Reflections.PACKET_PLAY_OUT_SCOREBOARD_TEAM_CREATE.get().invoke(null, scoreboardTeamPacket, Boolean.TRUE) : scoreboardTeamPacket);
return builder.build();
}
void updateGlowPacket(NPC paramNPC, Object paramObject) throws ReflectiveOperationException;
void updateGlow(NPC paramNPC, Object paramObject) throws ReflectiveOperationException;
boolean allowGlowColor();
boolean allowsGlowColor();
default void update(PacketCache packetCache) throws ReflectiveOperationException {
packetCache.flushCache("scoreboardPackets");

@ -16,12 +16,12 @@ public class NMSV16 extends NMSV9 {
return 16;
}
public ImmutableList<Object> getEquipPackets(NPC npc) throws ReflectiveOperationException {
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()),
convertItemStack(npc.getEntityID(), entry.getKey(), entry.getValue())));
createEntityEquipmentPacket(npc.getEntityID(), entry.getKey(), entry.getValue())));
return ImmutableList.of(Reflections.PACKET_PLAY_OUT_ENTITY_EQUIPMENT_CONSTRUCTOR_V1.get().newInstance(npc.getEntityID(), pairs));
}
}

@ -11,15 +11,11 @@ public class NMSV17 extends NMSV16 {
return 17;
}
public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
public Object createPlayer(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
return Reflections.PLAYER_CONSTRUCTOR_NEW.get().newInstance(Reflections.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile);
}
public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException {
public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException {
Utils.setValue(packet, "n", Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName()));
}
public Object getClickType(Object interactPacket) {
return "INTERACT";
}
}

@ -9,7 +9,7 @@ public class NMSV18 extends NMSV17 {
return 18;
}
public void updateGlowPacket(NPC npc, Object packet) throws ReflectiveOperationException {
public void updateGlow(NPC npc, Object packet) throws ReflectiveOperationException {
Utils.setValue(packet, "m", Reflections.ENUM_CHAT_FORMAT_FIND.get().invoke(null, npc.getNpcPojo().getGlowName()));
}
}

@ -9,7 +9,7 @@ public class NMSV19 extends NMSV18 {
return 19;
}
public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
public Object createPlayer(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
try {
return Reflections.PLAYER_CONSTRUCTOR_NEW_1.get().newInstance(Reflections.GET_SERVER_METHOD.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, null);
} catch (Throwable e) {

@ -17,26 +17,22 @@ public class NMSV8 implements NMS {
return 8;
}
public Object getPlayerPacket(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
public Object createPlayer(Object nmsWorld, GameProfile gameProfile) throws ReflectiveOperationException {
Constructor<?> constructor = (Utils.BUKKIT_VERSION > 13) ? Reflections.PLAYER_INTERACT_MANAGER_NEW_CONSTRUCTOR.get() : Reflections.PLAYER_INTERACT_MANAGER_OLD_CONSTRUCTOR.get();
return Reflections.PLAYER_CONSTRUCTOR_OLD.get().newInstance(Reflections.GET_SERVER_METHOD
.get().invoke(Bukkit.getServer()), nmsWorld, gameProfile, constructor.newInstance(nmsWorld));
}
public Object getSpawnPacket(Object nmsEntity, boolean isPlayer) throws ReflectiveOperationException {
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 convertItemStack(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException {
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 getClickType(Object interactPacket) throws ReflectiveOperationException {
return Utils.getValue(interactPacket, "action");
}
public Object getMetadataPacket(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);
try {
return Reflections.PACKET_PLAY_OUT_ENTITY_META_DATA_CONSTRUCTOR.get().newInstance(entityId, dataWatcher, true);
@ -45,24 +41,24 @@ public class NMSV8 implements NMS {
}
}
public Object getHologramSpawnPacket(Object armorStand) throws ReflectiveOperationException {
public Object createArmorStandSpawnPacket(Object armorStand) throws ReflectiveOperationException {
return Reflections.PACKET_PLAY_OUT_SPAWN_ENTITY_CONSTRUCTOR.get().newInstance(armorStand);
}
public ImmutableList<Object> getEquipPackets(NPC npc) throws ReflectiveOperationException {
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(),
convertItemStack(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue())));
createEntityEquipmentPacket(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue())));
}
return builder.build();
}
public void updateGlowPacket(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.");
}
public boolean allowGlowColor() {
public boolean allowsGlowColor() {
return false;
}
}

@ -14,21 +14,21 @@ public class NMSV9 extends NMSV8 {
return 9;
}
public Object convertItemStack(int entityId, ItemSlot itemSlot, ItemStack itemStack) throws ReflectiveOperationException {
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> getEquipPackets(NPC npc) throws ReflectiveOperationException {
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()),
convertItemStack(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue())));
createEntityEquipmentPacket(npc.getEntityID(), stackEntry.getKey(), stackEntry.getValue())));
}
return builder.build();
}
public void updateGlowPacket(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());
if (Utils.BUKKIT_VERSION > 12) {
Utils.setValue(packet, npc.getGlowColor(), Reflections.ENUM_CHAT_CLASS);
@ -39,7 +39,7 @@ public class NMSV9 extends NMSV8 {
}
}
public boolean allowGlowColor() {
public boolean allowsGlowColor() {
return true;
}
}