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

Added scale change listener

This commit is contained in:
OakLoaf
2025-07-05 18:43:33 +01:00
parent 2b113ee336
commit 9f824031c5
3 changed files with 44 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,13 @@
package me.lojosho.hibiscuscommons.packets.wrapper;
import lombok.Getter;
public class PlayerScaleWrapper {
@Getter
private final double scale;
public PlayerScaleWrapper(double scale) {
this.scale = scale;
}
}

View File

@@ -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<ClientboundUpdateAttributesPacket.AttributeSnapshot> 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<PacketAction> action = new AtomicReference<>(PacketAction.NOTHING);
PlayerScaleWrapper wrapper = new PlayerScaleWrapper(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)) {