From bf6c99498154a4abd4824b7037553f7414385e3a Mon Sep 17 00:00:00 2001 From: WenXin2 Date: Sat, 18 Jan 2025 16:46:03 -0600 Subject: [PATCH] Fix leashing part --- .../part_entities/PiranhaPlantPart.java | 28 ++++- .../data/EntityInteractPayload.java | 54 ++++++++++ .../handler/EntityInteractPacket.java | 100 ++++++++++++++++++ 3 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/wenxin2/marioverse/network/server_bound/data/EntityInteractPayload.java create mode 100644 src/main/java/com/wenxin2/marioverse/network/server_bound/handler/EntityInteractPacket.java diff --git a/src/main/java/com/wenxin2/marioverse/entities/part_entities/PiranhaPlantPart.java b/src/main/java/com/wenxin2/marioverse/entities/part_entities/PiranhaPlantPart.java index 52d929f5..075cd6f7 100644 --- a/src/main/java/com/wenxin2/marioverse/entities/part_entities/PiranhaPlantPart.java +++ b/src/main/java/com/wenxin2/marioverse/entities/part_entities/PiranhaPlantPart.java @@ -16,6 +16,7 @@ import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityDimensions; +import net.minecraft.world.entity.Leashable; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.Pose; import net.minecraft.world.entity.player.Player; @@ -24,10 +25,11 @@ import net.neoforged.neoforge.entity.PartEntity; import org.jetbrains.annotations.NotNull; -public class PiranhaPlantPart extends PartEntity { +public class PiranhaPlantPart extends PartEntity implements Leashable { public final PiranhaPlantEntity parentMob; public final String name; private final EntityDimensions size; + private Leashable.LeashData leashData; public PiranhaPlantPart(PiranhaPlantEntity parentMob, String name, float width, float height) { super(parentMob); @@ -41,10 +43,14 @@ public PiranhaPlantPart(PiranhaPlantEntity parentMob, String name, float width, protected void defineSynchedData(SynchedEntityData.Builder builder) {} @Override - public void readAdditionalSaveData(CompoundTag tag) {} + public void readAdditionalSaveData(CompoundTag tag) { + this.leashData = this.readLeashData(tag); + } @Override - public void addAdditionalSaveData(CompoundTag tag) {} + public void addAdditionalSaveData(CompoundTag tag) { + this.writeLeashData(tag, this.leashData); + } @Override public boolean isPickable() { @@ -126,6 +132,22 @@ public boolean canRiderInteract() { return this.getParent().canRiderInteract(); } + @Nullable + @Override + public LeashData getLeashData() { + return this.leashData; + } + + @Override + public void setLeashData(@Nullable Leashable.LeashData leashData) { + this.leashData = leashData; + } + + @Override + public boolean canBeLeashed() { + return true; + } + @Override public boolean canCollideWith(final Entity entity) { return !this.isPassengerOfSameVehicle(entity); diff --git a/src/main/java/com/wenxin2/marioverse/network/server_bound/data/EntityInteractPayload.java b/src/main/java/com/wenxin2/marioverse/network/server_bound/data/EntityInteractPayload.java new file mode 100644 index 00000000..d91ed41c --- /dev/null +++ b/src/main/java/com/wenxin2/marioverse/network/server_bound/data/EntityInteractPayload.java @@ -0,0 +1,54 @@ +//package com.wenxin2.marioverse.network.server_bound.data; +// +//import com.wenxin2.marioverse.Marioverse; +//import net.minecraft.network.FriendlyByteBuf; +//import net.minecraft.network.codec.ByteBufCodecs; +//import net.minecraft.network.codec.StreamCodec; +//import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +//import net.minecraft.resources.ResourceLocation; +//import net.minecraft.world.entity.Entity; +//import net.minecraft.world.level.Level; +//import net.minecraft.world.phys.Vec3; +//import org.jetbrains.annotations.NotNull; +// +//public record EntityInteractPayload(Entity entity, Boolean isSneaking, Vec3 hitVec) implements CustomPacketPayload { +// public static final Type ENTITY_INTERACT_PAYLOAD = new Type<>(ResourceLocation.fromNamespaceAndPath(Marioverse.MOD_ID, "entity_interact_payload")); +// +// @NotNull +// @Override +// public Type type() { +// return ENTITY_INTERACT_PAYLOAD; +// } +// +// public void getLevel() { +// entity.level(); +// } +// +// public static StreamCodec createStreamCodec(Level level) { +// return StreamCodec.composite( +// // Codec for Entity (using level) +// StreamCodec.of( +// (buf, entity) -> buf.writeInt(entity.getId()), // Serialize entity ID +// buf -> { +// int entityId = buf.readInt(); +// return level.getEntity(entityId); // Deserialize entity by ID +// } +// ), EntityInteractPayload::entity, +// +// // Codec for Boolean (isSneaking) +// ByteBufCodecs.BOOL, EntityInteractPayload::isSneaking, +// +// // Codec for Vec3 (hitVec) +// StreamCodec.of( +// (buf, vec3) -> { +// buf.writeDouble(vec3.x); +// buf.writeDouble(vec3.y); +// buf.writeDouble(vec3.z); +// }, +// buf -> new Vec3(buf.readDouble(), buf.readDouble(), buf.readDouble()) +// ), EntityInteractPayload::hitVec, +// +// EntityInteractPayload::new +// ); +// } +//} diff --git a/src/main/java/com/wenxin2/marioverse/network/server_bound/handler/EntityInteractPacket.java b/src/main/java/com/wenxin2/marioverse/network/server_bound/handler/EntityInteractPacket.java new file mode 100644 index 00000000..0b6b5428 --- /dev/null +++ b/src/main/java/com/wenxin2/marioverse/network/server_bound/handler/EntityInteractPacket.java @@ -0,0 +1,100 @@ +//package com.wenxin2.marioverse.network.server_bound.handler; +// +//import com.wenxin2.marioverse.blocks.WarpPipeBlock; +//import com.wenxin2.marioverse.blocks.entities.WarpPipeBlockEntity; +//import com.wenxin2.marioverse.entities.PiranhaPlantEntity; +//import com.wenxin2.marioverse.network.server_bound.data.ClosePipeButtonPayload; +//import com.wenxin2.marioverse.network.server_bound.data.EntityInteractPayload; +//import java.util.Optional; +//import net.minecraft.core.BlockPos; +//import net.minecraft.network.protocol.game.ServerboundInteractPacket; +//import net.minecraft.server.level.ServerLevel; +//import net.minecraft.server.level.ServerPlayer; +//import net.minecraft.world.InteractionHand; +//import net.minecraft.world.InteractionResult; +//import net.minecraft.world.entity.Entity; +//import net.minecraft.world.item.ItemStack; +//import net.minecraft.world.level.Level; +//import net.minecraft.world.level.block.entity.BlockEntity; +//import net.minecraft.world.level.block.state.BlockState; +//import net.neoforged.neoforge.entity.PartEntity; +//import net.neoforged.neoforge.network.handling.IPayloadContext; +//import org.jetbrains.annotations.Nullable; +// +//public class EntityInteractPacket { +// public static final EntityInteractPacket INSTANCE = new EntityInteractPacket(); +// +// public static EntityInteractPacket get() { +// return INSTANCE; +// } +// +// public void handle(final EntityInteractPayload payload, IPayloadContext context) { +// if (context.flow().isServerbound()) { +// final ServerLevel serverWorld = (ServerLevel) context.player().level(); +// +// final Entity entity = payload.entity(); +// +// final double d0 = 36.0D; +// context.enqueueWork(() -> { +// if (entity != null) +// { +// // Convert to the relevant part if found. +// if (entity.isMultipartEntity()) for (final PartEntity p : entity.getParts()) +// if (context.player().distanceToSqr(entity) < d0) +// { +// final InteractionHand hand = context.player().getUsedItemHand(); +// final ItemStack itemstack = context.player().getItemInHand(hand).copy(); +// Optional optional = Optional.empty(); +// +// if (this.getAction() == ServerboundInteractPacket.Handler.INTERACT) +// optional = Optional.of(context.player().interactOn(entity, hand)); +// else if (this.getAction() == ServerboundInteractPacket.ActionType.INTERACT_AT) +// { +// if (net.minecraftforge.common.ForgeHooks.onInteractEntityAt(player, entity, this.getHitVec(), +// hand) != null) +// return; +// optional = Optional.of(entity.interactAt(player, this.getHitVec(), hand)); +// } +// else if (this.getAction() == ServerboundInteractPacket.ActionType.ATTACK) context.player().attack(entity); +// +// if (optional.isPresent() && optional.get().consumesAction()) +// { +// CriteriaTriggers.PLAYER_INTERACTED_WITH_ENTITY.trigger(player, itemstack, entity); +// if (optional.get().shouldSwing()) context.player().swing(hand, true); +// } +// } +// } +// }); +// } +// } +// +// @Nullable +// public Entity getEntityFromWorld(final Level world) { +// return EntityPro.provider.getEntity(world, entityId); +// } +// +// public void changeState(ServerPlayer player, WarpPipeBlockEntity pipeBlockEntity) { +// Level world = pipeBlockEntity.getLevel(); +// if (world == null) +// return; +// BlockPos pos = pipeBlockEntity.getBlockPos(); +// BlockState state = world.getBlockState(pos); +// +// if (!(state.getBlock() instanceof WarpPipeBlock)) +// return; +// +// pipeBlockEntity.closePipe(player); +// } +// +// public static EntityInteractPacket openPipe(BlockPos pos, Boolean closePipe) { +// EntityInteractPacket packet = new EntityInteractPacket(); +// closePipe = false; +// return packet; +// } +// +// public static EntityInteractPacket closePipe(BlockPos pos) { +// EntityInteractPacket packet = new EntityInteractPacket(); +// Boolean closePipe = true; +// return packet; +// } +//}