Unicopia/src/main/java/com/minelittlepony/unicopia/mixin/MixinItem.java

69 lines
2.1 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.mixin;
import java.util.ArrayList;
import java.util.List;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
2021-06-21 22:05:40 +02:00
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
2021-06-21 22:05:40 +02:00
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;
import com.minelittlepony.unicopia.entity.ItemImpl;
import com.minelittlepony.unicopia.entity.ItemImpl.GroundTickCallback;
2022-09-30 12:43:07 +02:00
import com.minelittlepony.unicopia.item.toxin.*;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
@Mixin(Item.class)
abstract class MixinItem implements ToxicHolder, ItemImpl.TickableItem {
2021-06-21 22:05:40 +02:00
private boolean foodLoaded;
@Nullable
private FoodComponent originalFoodComponent;
@Shadow @Mutable
2021-06-21 22:05:40 +02:00
private @Final FoodComponent foodComponent;
2022-09-30 12:43:07 +02:00
private final List<ItemImpl.GroundTickCallback> tickCallbacks = new ArrayList<>();
@Override
public List<GroundTickCallback> getCallbacks() {
return tickCallbacks;
}
@Override
2022-09-30 12:43:07 +02:00
public void clearFoodOverride() {
foodComponent = getOriginalFoodComponent();
}
@Override
public void setFoodOverride(FoodComponent component) {
if (getOriginalFoodComponent() == null) {
foodComponent = component;
}
}
@Override
public FoodComponent getOriginalFoodComponent() {
2021-06-21 22:05:40 +02:00
if (!foodLoaded) {
foodLoaded = true;
originalFoodComponent = ((Item)(Object)this).getFoodComponent();
}
2022-09-30 12:43:07 +02:00
return originalFoodComponent;
}
@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)
private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) {
2022-09-30 12:43:07 +02:00
getToxic(stack).finishUsing(stack, world, entity);
}
}