9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-19 15:09:26 +00:00

refactor: use fake entities instead of reflection for packets where possible

This commit is contained in:
Boy
2025-05-01 11:47:44 +02:00
committed by Boy0000
parent eaa1228708
commit b2c1dd2d4e
6 changed files with 154 additions and 345 deletions

View File

@@ -18,9 +18,12 @@ import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.PositionMoveRotation;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.portal.TeleportTransition;
import net.minecraft.world.phys.Vec3;
@@ -54,9 +57,6 @@ import java.util.*;
public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.nms.NMSPackets {
static Constructor<ClientboundSetPassengersPacket> passengerConstructor;
static Constructor<ClientboundSetEntityLinkPacket> linkConstructor;
static Constructor<ClientboundSetCameraPacket> cameraConstructor;
static Constructor<ClientboundRotateHeadPacket> rotateHeadConstructor;
static {
try {
@@ -65,24 +65,6 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.
} catch (Exception e) {
e.printStackTrace();
}
try {
linkConstructor = ClientboundSetEntityLinkPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
linkConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
try {
cameraConstructor = ClientboundSetCameraPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
cameraConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
try {
rotateHeadConstructor = ClientboundRotateHeadPacket.class.getDeclaredConstructor(FriendlyByteBuf.class);
rotateHeadConstructor.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
@@ -96,16 +78,13 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.
@Override
public void sendRotateHeadPacket(int entityId, Location location, List<Player> sendTo) {
ServerLevel level = MinecraftServer.getServer().overworld();
Entity entity = new ArmorStand(net.minecraft.world.entity.EntityType.ARMOR_STAND, level);
entity.setId(entityId);
byte headRot = (byte) (location.getYaw() * 256.0F / 360.0F);
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entityId);
byteBuf.writeByte(headRot);
try {
ClientboundRotateHeadPacket packet = rotateHeadConstructor.newInstance(byteBuf);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
ClientboundRotateHeadPacket packet = new ClientboundRotateHeadPacket(entity, headRot);
for (Player p : sendTo) sendPacket(p, packet);
}
@Override
@@ -223,15 +202,15 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.
@Override
public void sendLeashPacket(int leashEntity, int entityId, List<Player> sendTo) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeInt(leashEntity);
byteBuf.writeInt(entityId);
try {
ClientboundSetEntityLinkPacket packet = linkConstructor.newInstance(byteBuf);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
// Fake entities just to avoid reflection
ServerLevel level = MinecraftServer.getServer().overworld();
Entity entity1 = new ArmorStand(net.minecraft.world.entity.EntityType.ARMOR_STAND, level);
Entity entity2 = new ArmorStand(net.minecraft.world.entity.EntityType.ARMOR_STAND, level);
entity1.setId(leashEntity);
entity2.setId(entityId);
ClientboundSetEntityLinkPacket packet = new ClientboundSetEntityLinkPacket(entity1, entity2);
for (Player p : sendTo) sendPacket(p, packet);
}
@Override
@@ -255,14 +234,10 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons.
@Override
public void sendCameraPacket(int entityId, List<Player> sendTo) {
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer());
byteBuf.writeVarInt(entityId);
try {
ClientboundSetCameraPacket packet = cameraConstructor.newInstance(byteBuf);
for (Player p : sendTo) sendPacket(p, packet);
} catch (Exception e) {
e.printStackTrace();
}
// Fake entity just to avoid reflection
Entity entity = new ArmorStand(net.minecraft.world.entity.EntityType.ARMOR_STAND, MinecraftServer.getServer().overworld());
ClientboundSetCameraPacket packet = new ClientboundSetCameraPacket(entity);
for (Player p : sendTo) sendPacket(p, packet);
}