From 8b235bd2a4781cad7b000dc286c27326572bcbc2 Mon Sep 17 00:00:00 2001 From: Sam Kim <21956685+sam-k@users.noreply.github.com> Date: Mon, 19 Aug 2024 04:43:42 -0700 Subject: [PATCH] Implement --- .../mixin/entity/LivingEntityMixin.java | 53 +++++++++++++++++++ src/main/resources/vshorses.mixins.json | 1 + 2 files changed, 54 insertions(+) create mode 100644 src/main/java/com/vanillastar/vshorses/mixin/entity/LivingEntityMixin.java diff --git a/src/main/java/com/vanillastar/vshorses/mixin/entity/LivingEntityMixin.java b/src/main/java/com/vanillastar/vshorses/mixin/entity/LivingEntityMixin.java new file mode 100644 index 0000000..ea9362d --- /dev/null +++ b/src/main/java/com/vanillastar/vshorses/mixin/entity/LivingEntityMixin.java @@ -0,0 +1,53 @@ +package com.vanillastar.vshorses.mixin.entity; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.Leashable; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.registry.tag.FluidTags; +import net.minecraft.world.World; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(LivingEntity.class) +public abstract class LivingEntityMixin extends Entity { + private LivingEntityMixin(EntityType type, World world) { + super(type, world); + } + + @Inject(method = "onDismounted", at = @At("TAIL")) + private void leashUponUnderwaterDismount( + @NotNull Entity vehicle, CallbackInfo ci + ) { + if (!((LivingEntity) (Object) this instanceof PlayerEntity player) || + !player.isSubmergedIn(FluidTags.WATER)) { + return; + } + if (!vehicle.shouldDismountUnderwater() || + !(vehicle instanceof Leashable leashable) || + !leashable.canLeashAttachTo()) { + return; + } + + PlayerInventory playerInventory = player.getInventory(); + int leadSlot = playerInventory.getSlotWithStack(new ItemStack(Items.LEAD)); + if (leadSlot < 0) { + // No leads in inventory. + return; + } + ItemStack itemStack = playerInventory.getStack(leadSlot); + if (!this.getWorld().isClient()) { + leashable.attachLeash(player, true); + } + if (!player.isInCreativeMode()) { + itemStack.decrement(1); + } + } +} diff --git a/src/main/resources/vshorses.mixins.json b/src/main/resources/vshorses.mixins.json index 31e18f0..794e8f7 100644 --- a/src/main/resources/vshorses.mixins.json +++ b/src/main/resources/vshorses.mixins.json @@ -5,6 +5,7 @@ "mixins": [ "block.DispenserBehaviorMixin", "entity.AbstractHorseEntityMixin", + "entity.LivingEntityMixin", "item.AnimalArmorItemMixin", "item.ItemsMixin", "item.SmithingTemplateItemMixin",