/* * Decompiled with CFR 0.152. * * Could not load the following classes: * org.jspecify.annotations.Nullable */ package net.minecraft.network.protocol.game; import java.util.function.Function; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.PacketType; import net.minecraft.network.protocol.game.GamePacketTypes; import net.minecraft.network.protocol.game.ServerGamePacketListener; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.Entity; import net.minecraft.world.phys.Vec3; import org.jspecify.annotations.Nullable; public class ServerboundInteractPacket implements Packet { public static final StreamCodec STREAM_CODEC = Packet.codec(ServerboundInteractPacket::write, ServerboundInteractPacket::new); private final int entityId; private final Action action; private final boolean usingSecondaryAction; private static final Action ATTACK_ACTION = new Action(){ @Override public ActionType getType() { return ActionType.ATTACK; } @Override public void dispatch(Handler handler) { handler.onAttack(); } @Override public void write(FriendlyByteBuf output) { } }; private ServerboundInteractPacket(int entityId, boolean usingSecondaryAction, Action action) { this.entityId = entityId; this.action = action; this.usingSecondaryAction = usingSecondaryAction; } public static ServerboundInteractPacket createAttackPacket(Entity entity, boolean usingSecondaryAction) { return new ServerboundInteractPacket(entity.getId(), usingSecondaryAction, ATTACK_ACTION); } public static ServerboundInteractPacket createInteractionPacket(Entity entity, boolean usingSecondaryAction, InteractionHand hand) { return new ServerboundInteractPacket(entity.getId(), usingSecondaryAction, new InteractionAction(hand)); } public static ServerboundInteractPacket createInteractionPacket(Entity entity, boolean usingSecondaryAction, InteractionHand hand, Vec3 location) { return new ServerboundInteractPacket(entity.getId(), usingSecondaryAction, new InteractionAtLocationAction(hand, location)); } private ServerboundInteractPacket(FriendlyByteBuf input) { this.entityId = input.readVarInt(); ActionType type = input.readEnum(ActionType.class); this.action = type.reader.apply(input); this.usingSecondaryAction = input.readBoolean(); } private void write(FriendlyByteBuf output) { output.writeVarInt(this.entityId); output.writeEnum(this.action.getType()); this.action.write(output); output.writeBoolean(this.usingSecondaryAction); } @Override public PacketType type() { return GamePacketTypes.SERVERBOUND_INTERACT; } @Override public void handle(ServerGamePacketListener listener) { listener.handleInteract(this); } public @Nullable Entity getTarget(ServerLevel level) { return level.getEntityOrPart(this.entityId); } public boolean isUsingSecondaryAction() { return this.usingSecondaryAction; } public void dispatch(Handler handler) { this.action.dispatch(handler); } private static interface Action { public ActionType getType(); public void dispatch(Handler var1); public void write(FriendlyByteBuf var1); } private static class InteractionAction implements Action { private final InteractionHand hand; private InteractionAction(InteractionHand hand) { this.hand = hand; } private InteractionAction(FriendlyByteBuf input) { this.hand = input.readEnum(InteractionHand.class); } @Override public ActionType getType() { return ActionType.INTERACT; } @Override public void dispatch(Handler handler) { handler.onInteraction(this.hand); } @Override public void write(FriendlyByteBuf output) { output.writeEnum(this.hand); } } private static class InteractionAtLocationAction implements Action { private final InteractionHand hand; private final Vec3 location; private InteractionAtLocationAction(InteractionHand hand, Vec3 location) { this.hand = hand; this.location = location; } private InteractionAtLocationAction(FriendlyByteBuf input) { this.location = new Vec3(input.readFloat(), input.readFloat(), input.readFloat()); this.hand = input.readEnum(InteractionHand.class); } @Override public ActionType getType() { return ActionType.INTERACT_AT; } @Override public void dispatch(Handler handler) { handler.onInteraction(this.hand, this.location); } @Override public void write(FriendlyByteBuf output) { output.writeFloat((float)this.location.x); output.writeFloat((float)this.location.y); output.writeFloat((float)this.location.z); output.writeEnum(this.hand); } } private static enum ActionType { INTERACT(InteractionAction::new), ATTACK(input -> ATTACK_ACTION), INTERACT_AT(InteractionAtLocationAction::new); private final Function reader; private ActionType(Function reader) { this.reader = reader; } } public static interface Handler { public void onInteraction(InteractionHand var1); public void onInteraction(InteractionHand var1, Vec3 var2); public void onAttack(); } }