diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSPackets.java b/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSPackets.java index 5ae2311..a476db9 100644 --- a/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSPackets.java +++ b/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSPackets.java @@ -83,6 +83,8 @@ public interface NMSPackets { void sendEntityDestroyPacket(IntList entityIds, List sendTo); + void sendEntityScalePacket(int entityId, double scale, List sendTo); + void sendItemDisplayMetadata(int entityId, Vector3f translation, Vector3f scale, diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/packets/PacketInterface.java b/common/src/main/java/me/lojosho/hibiscuscommons/packets/PacketInterface.java index 9326299..7fd0b1b 100644 --- a/common/src/main/java/me/lojosho/hibiscuscommons/packets/PacketInterface.java +++ b/common/src/main/java/me/lojosho/hibiscuscommons/packets/PacketInterface.java @@ -37,6 +37,11 @@ public interface PacketInterface { // Override } + default PacketAction readPlayerScale(@NotNull Player player, @NotNull PlayerScaleWrapper wrapper) { + return PacketAction.NOTHING; + // Override + } + default PacketAction readEntityHandle(@NotNull Player player, @NotNull PlayerInteractWrapper wrapper) { return PacketAction.NOTHING; // Override diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/packets/wrapper/PlayerScaleWrapper.java b/common/src/main/java/me/lojosho/hibiscuscommons/packets/wrapper/PlayerScaleWrapper.java new file mode 100644 index 0000000..19801ca --- /dev/null +++ b/common/src/main/java/me/lojosho/hibiscuscommons/packets/wrapper/PlayerScaleWrapper.java @@ -0,0 +1,16 @@ +package me.lojosho.hibiscuscommons.packets.wrapper; + +import lombok.Getter; + +public class PlayerScaleWrapper { + + @Getter + private final int entityId; + @Getter + private final double scale; + + public PlayerScaleWrapper(int entityId, double scale) { + this.entityId = entityId; + this.scale = scale; + } +} diff --git a/v1_20_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_20_R4/NMSPackets.java b/v1_20_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_20_R4/NMSPackets.java index e134953..df597b2 100644 --- a/v1_20_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_20_R4/NMSPackets.java +++ b/v1_20_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_20_R4/NMSPackets.java @@ -26,6 +26,8 @@ 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.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.level.GameType; @@ -328,6 +330,18 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons. for (Player p : sendTo) sendPacket(p, packet); } + @Override + public void sendEntityScalePacket(int entityId, double scale, List sendTo) { + AttributeInstance attribute = new AttributeInstance( + Attributes.SCALE, + (ignored) -> {} + ); + attribute.setBaseValue(scale); + + ClientboundUpdateAttributesPacket packet = new ClientboundUpdateAttributesPacket(entityId, List.of(attribute)); + for (Player p : sendTo) sendPacket(p, packet); + } + @Override public void sendItemDisplayMetadata(int entityId, Vector3f translation, diff --git a/v1_21_R1/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R1/NMSPackets.java b/v1_21_R1/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R1/NMSPackets.java index d6b843a..a139b28 100644 --- a/v1_21_R1/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R1/NMSPackets.java +++ b/v1_21_R1/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R1/NMSPackets.java @@ -26,6 +26,8 @@ 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.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.level.GameType; @@ -327,6 +329,18 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons. for (Player p : sendTo) sendPacket(p, packet); } + @Override + public void sendEntityScalePacket(int entityId, double scale, List sendTo) { + AttributeInstance attribute = new AttributeInstance( + Attributes.SCALE, + (ignored) -> {} + ); + attribute.setBaseValue(scale); + + ClientboundUpdateAttributesPacket packet = new ClientboundUpdateAttributesPacket(entityId, List.of(attribute)); + for (Player p : sendTo) sendPacket(p, packet); + } + @Override public void sendItemDisplayMetadata(int entityId, Vector3f translation, diff --git a/v1_21_R2/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R2/NMSPackets.java b/v1_21_R2/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R2/NMSPackets.java index 8598794..8192da0 100644 --- a/v1_21_R2/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R2/NMSPackets.java +++ b/v1_21_R2/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R2/NMSPackets.java @@ -27,6 +27,8 @@ 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.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.level.GameType; @@ -330,6 +332,18 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons. for (Player p : sendTo) sendPacket(p, packet); } + @Override + public void sendEntityScalePacket(int entityId, double scale, List sendTo) { + AttributeInstance attribute = new AttributeInstance( + Attributes.SCALE, + (ignored) -> {} + ); + attribute.setBaseValue(scale); + + ClientboundUpdateAttributesPacket packet = new ClientboundUpdateAttributesPacket(entityId, List.of(attribute)); + for (Player p : sendTo) sendPacket(p, packet); + } + @Override public void sendItemDisplayMetadata(int entityId, Vector3f translation, diff --git a/v1_21_R3/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R3/NMSPackets.java b/v1_21_R3/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R3/NMSPackets.java index c281c3c..b414211 100644 --- a/v1_21_R3/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R3/NMSPackets.java +++ b/v1_21_R3/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R3/NMSPackets.java @@ -28,6 +28,8 @@ 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.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.level.GameType; @@ -334,6 +336,18 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons. for (Player p : sendTo) sendPacket(p, packet); } + @Override + public void sendEntityScalePacket(int entityId, double scale, List sendTo) { + AttributeInstance attribute = new AttributeInstance( + Attributes.SCALE, + (ignored) -> {} + ); + attribute.setBaseValue(scale); + + ClientboundUpdateAttributesPacket packet = new ClientboundUpdateAttributesPacket(entityId, List.of(attribute)); + for (Player p : sendTo) sendPacket(p, packet); + } + @Override public void sendItemDisplayMetadata(int entityId, Vector3f translation, diff --git a/v1_21_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R4/NMSPackets.java b/v1_21_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R4/NMSPackets.java index 2afb9e1..c89328a 100644 --- a/v1_21_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R4/NMSPackets.java +++ b/v1_21_R4/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R4/NMSPackets.java @@ -32,6 +32,8 @@ 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.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.level.GameType; @@ -335,6 +337,18 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons. for (Player p : sendTo) sendPacket(p, packet); } + @Override + public void sendEntityScalePacket(int entityId, double scale, List sendTo) { + AttributeInstance attribute = new AttributeInstance( + Attributes.SCALE, + (ignored) -> {} + ); + attribute.setBaseValue(scale); + + ClientboundUpdateAttributesPacket packet = new ClientboundUpdateAttributesPacket(entityId, List.of(attribute)); + for (Player p : sendTo) sendPacket(p, packet); + } + @Override public void sendItemDisplayMetadata(int entityId, Vector3f translation, diff --git a/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPacketChannel.java b/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPacketChannel.java index a675980..c7fbf36 100644 --- a/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPacketChannel.java +++ b/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPacketChannel.java @@ -12,6 +12,7 @@ import me.lojosho.hibiscuscommons.plugins.SubPlugins; import me.lojosho.hibiscuscommons.util.MessagesUtil; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.*; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.inventory.ClickType; import net.minecraft.world.item.ItemStack; import org.bukkit.craftbukkit.CraftEquipmentSlot; @@ -45,6 +46,7 @@ public class NMSPacketChannel extends ChannelDuplexHandler { case ClientboundContainerSetSlotPacket setSlotPacket -> msg = handleSlotChange(setSlotPacket); case ClientboundSetEquipmentPacket equipmentPacket -> msg = handlePlayerEquipment(equipmentPacket); case ClientboundSetPassengersPacket passengerPacket -> msg = handlePassengerSet(passengerPacket); + case ClientboundUpdateAttributesPacket attributesPacket -> msg = handleScaleChange(attributesPacket); default -> {} } @@ -168,6 +170,30 @@ public class NMSPacketChannel extends ChannelDuplexHandler { return (Packet) NMSHandlers.getHandler().getPacketHandler().createMountPacket(ownerId, passengers.stream().mapToInt(Integer::intValue).toArray()); } + private Packet handleScaleChange(@NotNull ClientboundUpdateAttributesPacket packet) { + final List nmsAttributes = packet.getValues(); + final ClientboundUpdateAttributesPacket.AttributeSnapshot nmsScaleAttribute = nmsAttributes.stream() + .filter(attribute -> attribute.attribute().equals(Attributes.SCALE)) + .findFirst() + .orElse(null); + + if (nmsScaleAttribute == null) { + return packet; + } + + AtomicReference action = new AtomicReference<>(PacketAction.NOTHING); + PlayerScaleWrapper wrapper = new PlayerScaleWrapper(packet.getEntityId(), nmsScaleAttribute.base()); + + SubPlugins.getSubPlugins().forEach(plugin -> { + PacketAction pluginAction = plugin.getPacketInterface().readPlayerScale(player, wrapper); + if (pluginAction != PacketAction.NOTHING) action.set(pluginAction); + }); + + if (action.get() == PacketAction.CANCELLED) return null; + if (action.get() == PacketAction.NOTHING) return packet; + return packet; + } + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof Packet packet)) { diff --git a/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPackets.java b/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPackets.java index 6225ad6..15a12ce 100644 --- a/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPackets.java +++ b/v1_21_R5/src/main/java/me/lojosho/hibiscuscommons/nms/v1_21_R5/NMSPackets.java @@ -29,6 +29,8 @@ 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.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.level.GameType; @@ -339,6 +341,18 @@ public class NMSPackets extends NMSCommon implements me.lojosho.hibiscuscommons. for (Player p : sendTo) sendPacket(p, packet); } + @Override + public void sendEntityScalePacket(int entityId, double scale, List sendTo) { + AttributeInstance attribute = new AttributeInstance( + Attributes.SCALE, + (ignored) -> {} + ); + attribute.setBaseValue(scale); + + ClientboundUpdateAttributesPacket packet = new ClientboundUpdateAttributesPacket(entityId, List.of(attribute)); + for (Player p : sendTo) sendPacket(p, packet); + } + @Override public void sendItemDisplayMetadata(int entityId, Vector3f translation,