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;
|
|
|
|
|
2021-06-21 22:05:40 +02:00
|
|
|
import org.spongepowered.asm.mixin.Final;
|
2020-04-25 22:03:36 +02:00
|
|
|
import org.spongepowered.asm.mixin.Mixin;
|
2021-06-21 22:05:40 +02:00
|
|
|
import org.spongepowered.asm.mixin.Shadow;
|
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-21 22:05:40 +02:00
|
|
|
private boolean foodLoaded;
|
2021-06-12 17:16:42 +02:00
|
|
|
@Nullable
|
|
|
|
private FoodComponent originalFoodComponent;
|
2020-10-08 13:16:16 +02:00
|
|
|
|
2021-06-21 22:05:40 +02:00
|
|
|
@Shadow
|
|
|
|
private @Final FoodComponent foodComponent;
|
2020-10-08 13:16:16 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Optional<Toxic> getToxic() {
|
2021-06-21 22:05:40 +02:00
|
|
|
if (!foodLoaded) {
|
|
|
|
foodLoaded = true;
|
2021-06-12 17:16:42 +02:00
|
|
|
originalFoodComponent = ((Item)(Object)this).getFoodComponent();
|
|
|
|
}
|
|
|
|
|
2021-06-21 22:05:40 +02:00
|
|
|
foodComponent = originalFoodComponent;
|
|
|
|
Optional<Toxic> toxic = Toxics.REGISTRY.stream()
|
|
|
|
.filter(i -> i.matches((Item)(Object)this))
|
|
|
|
.map(t -> {
|
|
|
|
if (originalFoodComponent == null) {
|
|
|
|
t.getFoodComponent().ifPresent(s -> foodComponent = s);
|
|
|
|
}
|
2021-06-12 17:16:42 +02:00
|
|
|
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) {
|
2021-06-21 22:05:40 +02:00
|
|
|
getToxic().ifPresent(t -> t.finishUsing(stack, world, entity));
|
2020-10-08 13:16:16 +02:00
|
|
|
}
|
2020-04-25 22:03:36 +02:00
|
|
|
}
|