2020-04-25 22:03:36 +02:00
|
|
|
package com.minelittlepony.unicopia.mixin;
|
|
|
|
|
2020-10-08 13:16:16 +02:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2021-06-12 17:16:42 +02:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2020-04-25 22:03:36 +02:00
|
|
|
import org.spongepowered.asm.mixin.Mixin;
|
|
|
|
import org.spongepowered.asm.mixin.gen.Accessor;
|
2020-10-08 13:16:16 +02:00
|
|
|
import org.spongepowered.asm.mixin.injection.At;
|
|
|
|
import org.spongepowered.asm.mixin.injection.Inject;
|
|
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
2020-04-25 22:03:36 +02:00
|
|
|
|
2020-10-08 13:16:16 +02:00
|
|
|
import com.minelittlepony.unicopia.item.toxin.Toxic;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
|
2021-06-12 17:16:42 +02:00
|
|
|
import com.minelittlepony.unicopia.item.toxin.Toxics;
|
2020-04-25 22:03:36 +02:00
|
|
|
|
2020-10-08 13:16:16 +02:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2020-04-25 22:03:36 +02:00
|
|
|
import net.minecraft.item.FoodComponent;
|
|
|
|
import net.minecraft.item.Item;
|
2020-10-08 13:16:16 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.world.World;
|
2020-04-25 22:03:36 +02:00
|
|
|
|
|
|
|
@Mixin(Item.class)
|
|
|
|
abstract class MixinItem implements ToxicHolder {
|
2020-10-08 13:16:16 +02:00
|
|
|
|
2021-06-12 17:16:42 +02:00
|
|
|
@Nullable
|
|
|
|
private FoodComponent originalFoodComponent;
|
2020-10-08 13:16:16 +02:00
|
|
|
|
2020-04-25 22:03:36 +02:00
|
|
|
@Override
|
|
|
|
@Accessor("foodComponent")
|
|
|
|
public abstract void setFood(FoodComponent food);
|
2020-10-08 13:16:16 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Optional<Toxic> getToxic() {
|
2021-06-12 17:16:42 +02:00
|
|
|
if (originalFoodComponent == null) {
|
|
|
|
originalFoodComponent = ((Item)(Object)this).getFoodComponent();
|
|
|
|
}
|
|
|
|
setFood(originalFoodComponent);
|
|
|
|
|
|
|
|
Optional<Toxic> toxic = Toxics.REGISTRY.stream().filter(i -> i.matches((Item)(Object)this)).map(t -> {
|
|
|
|
t.getFoodComponent().ifPresent(this::setFood);
|
|
|
|
return t;
|
|
|
|
}).findFirst();
|
|
|
|
|
|
|
|
if (!toxic.isPresent() && ((Item)(Object)this).getFoodComponent() != null) {
|
|
|
|
return Optional.of(Toxics.EDIBLE);
|
|
|
|
}
|
2020-10-08 13:16:16 +02:00
|
|
|
return toxic;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)
|
|
|
|
private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) {
|
|
|
|
if (getToxic().isPresent()) {
|
|
|
|
getToxic().get().finishUsing(stack, world, entity);
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 22:03:36 +02:00
|
|
|
}
|