aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/potatocrime/mixin/ShillCarrotsToVillagerPatch.java
blob: 205f51dbc2900caaddd5fecd2ee16da32aa12757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.sound.SoundEvents;
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<? extends MerchantEntity> 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<ActionResult> cir) {
        var itemInHand = player.getStackInHand(hand);
        if (this.isAlive() && !this.hasCustomer() && !this.isSleeping() && this.method_58842()) { // method_58842 = isPotato
            if (itemInHand.isOf(Items.CARROT)) {
                if (!isClient()) {
                    playSound(SoundEvents.ENTITY_VILLAGER_TRADE);
                    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()) {
                    playSound(SoundEvents.ENTITY_VILLAGER_TRADE);
                    itemInHand.setCount(itemInHand.getCount() - 1);
                    player.giveItemStack(new ItemStack(Items.EMERALD_BLOCK));
                    player.sendMessage(PotatoTranslations.INSTANCE.getVillagerTrade().format());
                }
                cir.setReturnValue(ActionResult.success(isClient()));
            }
        }
    }
}