From 4866f1360529d4d77dbd85c5e23d77a032043667 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Tue, 2 Apr 2024 03:26:21 +0200 Subject: Make carrots tradeable --- .../mixin/ShillCarrotsToVillagerPatch.java | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/moe/nea/potatocrime/mixin/ShillCarrotsToVillagerPatch.java (limited to 'src/main/java/moe/nea/potatocrime/mixin/ShillCarrotsToVillagerPatch.java') diff --git a/src/main/java/moe/nea/potatocrime/mixin/ShillCarrotsToVillagerPatch.java b/src/main/java/moe/nea/potatocrime/mixin/ShillCarrotsToVillagerPatch.java new file mode 100644 index 0000000..e091069 --- /dev/null +++ b/src/main/java/moe/nea/potatocrime/mixin/ShillCarrotsToVillagerPatch.java @@ -0,0 +1,51 @@ +package moe.nea.potatocrime.mixin; + +import moe.nea.potatocrime.registry.PotatoTranslations; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.passive.MerchantEntity; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(VillagerEntity.class) +public abstract class ShillCarrotsToVillagerPatch extends MerchantEntity { + @Shadow + public abstract boolean isClient(); + + public ShillCarrotsToVillagerPatch(EntityType entityType, World world) { + super(entityType, world); + throw new UnsupportedOperationException(); + } + + @Inject(method = "interactMob", at = @At("HEAD"), cancellable = true) + private void onInteractMob(PlayerEntity player, Hand hand, CallbackInfoReturnable cir) { + var itemInHand = player.getStackInHand(hand); + if (this.isAlive() && !this.hasCustomer() && !this.isSleeping()) { + if (itemInHand.isOf(Items.CARROT)) { + if (!isClient()) { + itemInHand.setCount(itemInHand.getCount() - 1); + player.giveItemStack(new ItemStack(Items.EMERALD)); + player.sendMessage(PotatoTranslations.INSTANCE.getVillagerTrade().format()); + } + cir.setReturnValue(ActionResult.success(isClient())); + } + if (itemInHand.isOf(Items.GOLDEN_CARROT)) { + if (!isClient()) { + itemInHand.setCount(itemInHand.getCount() - 1); + player.giveItemStack(new ItemStack(Items.EMERALD_BLOCK)); + player.sendMessage(PotatoTranslations.INSTANCE.getVillagerTrade().format()); + } + cir.setReturnValue(ActionResult.success(isClient())); + } + } + } +} -- cgit