com.magistuarmory.network.PacketLongReachAttack Maven / Gradle / Ivy
package com.magistuarmory.network;
import com.magistuarmory.EpicKnights;
import com.magistuarmory.item.MedievalWeaponItem;
import dev.architectury.networking.NetworkManager;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
public class PacketLongReachAttack
{
public static final ResourceLocation ID = new ResourceLocation(EpicKnights.ID, "packet_long_reach_attack");
public static void sendToServer(int entityid)
{
NetworkManager.sendToServer(ID, PacketLongReachAttack.encode(entityid));
}
static FriendlyByteBuf encode(int entityId)
{
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeInt(entityId);
return buf;
}
public static void apply(FriendlyByteBuf buf, NetworkManager.PacketContext context)
{
if (!(context.getPlayer() instanceof ServerPlayer player))
return;
Entity victim = player.f_19853_.m_6815_(buf.readInt());
if (victim == null)
return;
context.queue(() -> execute(victim, player));
}
static void execute(Entity victim, ServerPlayer player)
{
ItemStack stack = player.m_6844_(EquipmentSlot.MAINHAND);
if (!(stack.m_41720_() instanceof MedievalWeaponItem weapon) || !weapon.isLong())
return;
player.m_5706_(victim);
player.m_21011_(InteractionHand.MAIN_HAND, true);
player.m_36334_();
}
}