From c7cdca3ff4c354c7f6fb172baa38a4d5b90c8ead Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 11 Sep 2023 21:20:03 +0100 Subject: [PATCH] Simplify our item mixin --- .../unicopia/item/toxin/ItemDuck.java | 9 +++++ .../unicopia/item/toxin/Toxic.java | 2 - .../unicopia/item/toxin/ToxicHolder.java | 29 +------------- .../unicopia/item/toxin/Toxics.java | 19 +++++++++ .../unicopia/item/toxin/Toxin.java | 1 + .../unicopia/mixin/MixinItem.java | 40 +++++-------------- 6 files changed, 42 insertions(+), 58 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/item/toxin/ItemDuck.java diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/ItemDuck.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/ItemDuck.java new file mode 100644 index 00000000..4ff27425 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/ItemDuck.java @@ -0,0 +1,9 @@ +package com.minelittlepony.unicopia.item.toxin; + +import com.minelittlepony.unicopia.entity.ItemImpl; + +import net.minecraft.item.*; + +public interface ItemDuck extends ItemConvertible, ToxicHolder, ItemImpl.TickableItem { + void setFoodComponent(FoodComponent food); +} diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java index f5a30dad..2395e8ba 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxic.java @@ -17,8 +17,6 @@ public record Toxic ( Optional component, Ailment.Set ailment ) { - public static final Toxic EMPTY = new Toxic(Optional.empty(), Optional.empty(), Ailment.Set.EMPTY); - public void appendTooltip(PlayerEntity player, List tooltip, TooltipContext context) { ailment.get(player).ifPresent(ailment -> ailment.appendTooltip(tooltip, context)); } diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java index 73690016..466b7b15 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/ToxicHolder.java @@ -1,32 +1,7 @@ package com.minelittlepony.unicopia.item.toxin; -import org.jetbrains.annotations.Nullable; - import net.minecraft.item.*; -public interface ToxicHolder extends ItemConvertible { - @Nullable - default FoodComponent getOriginalFoodComponent() { - return null; - } - - default Toxic getDefaultToxic() { - return getOriginalFoodComponent() == null ? Toxic.EMPTY : Toxics.EDIBLE; - } - - default void clearFoodOverride() {} - - default void setFoodOverride(FoodComponent component) {} - - default Toxic getToxic(ItemStack stack) { - clearFoodOverride(); - return Toxics.REGISTRY.stream() - .filter(i -> i.matches((Item)this)) - .map(ToxicRegistryEntry::value) - .map(t -> { - t.component().ifPresent(this::setFoodOverride); - return t; - }).findFirst().orElseGet(this::getDefaultToxic); - } - +public interface ToxicHolder { + Toxic getToxic(ItemStack stack); } diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java index 5d445f84..f930268b 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java @@ -3,15 +3,21 @@ package com.minelittlepony.unicopia.item.toxin; import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.util.RegistryUtils; +import net.minecraft.item.FoodComponent; import net.minecraft.registry.Registry; import static com.minelittlepony.unicopia.item.toxin.Toxicity.*; import static com.minelittlepony.unicopia.item.toxin.Ailment.*; import static com.minelittlepony.unicopia.item.toxin.Toxin.*; +import java.util.Optional; +import org.jetbrains.annotations.Nullable; + public interface Toxics { Registry REGISTRY = RegistryUtils.createSimple(Unicopia.id("toxic")); + Toxic EMPTY = new Toxic(Optional.empty(), Optional.empty(), Ailment.Set.EMPTY); + Toxic SEVERE_INNERT = Toxic.innert(Toxicity.SEVERE); Toxic EDIBLE = register("edible", new Toxic.Builder(Ailment.INNERT) @@ -127,4 +133,17 @@ public interface Toxics { static Toxic register(String name, Toxic.Builder builder) { return Registry.register(REGISTRY, Unicopia.id(name), new ToxicRegistryEntry(builder.build(), UTags.item("food_types/" + name))).value(); } + + static Toxic lookup(ItemDuck item) { + @Nullable FoodComponent food = item.asItem().getFoodComponent(); + return REGISTRY.stream() + .filter(i -> i.matches(item.asItem())) + .map(ToxicRegistryEntry::value) + .map(t -> { + if (food == null) { + t.component().ifPresent(item::setFoodComponent); + } + return t; + }).findFirst().orElse(food == null ? Toxics.EMPTY : Toxics.EDIBLE); + } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxin.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxin.java index 660116b7..041dbe0d 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxin.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxin.java @@ -44,6 +44,7 @@ public interface Toxin extends Affliction { player.removeStatusEffect(StatusEffects.NAUSEA); player.removeStatusEffect(UEffects.FOOD_POISONING); }); + Toxin BAT_PONY_INTOXICATION = Toxin.of(StatusEffects.HEALTH_BOOST, 30, 60, 2, 6) .and(Toxin.of(StatusEffects.JUMP_BOOST, 30, 60, 1, 6)) .and(Toxin.of(StatusEffects.SPEED, 30, 30, 1, 6)) diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinItem.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinItem.java index 4ee6570b..d4924f25 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinItem.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/MixinItem.java @@ -2,17 +2,16 @@ package com.minelittlepony.unicopia.mixin; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; -import org.jetbrains.annotations.Nullable; - -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import com.google.common.base.Suppliers; import com.minelittlepony.unicopia.entity.ItemImpl; import com.minelittlepony.unicopia.entity.ItemImpl.GroundTickCallback; import com.minelittlepony.unicopia.entity.effect.FoodPoisoningStatusEffect; @@ -29,16 +28,9 @@ import net.minecraft.util.TypedActionResult; import net.minecraft.world.World; @Mixin(Item.class) -abstract class MixinItem implements ToxicHolder, ItemImpl.TickableItem { - - private boolean foodLoaded; - @Nullable - private FoodComponent originalFoodComponent; - - @Shadow @Mutable - private @Final FoodComponent foodComponent; - +abstract class MixinItem implements ItemDuck { private final List tickCallbacks = new ArrayList<>(); + private final Supplier originalFoodComponent = Suppliers.memoize(((Item)(Object)this)::getFoodComponent); @Override public List getCallbacks() { @@ -46,24 +38,14 @@ abstract class MixinItem implements ToxicHolder, ItemImpl.TickableItem { } @Override - public void clearFoodOverride() { - foodComponent = getOriginalFoodComponent(); - } + @Mutable + @Accessor("foodComponent") + public abstract void setFoodComponent(FoodComponent food); @Override - public void setFoodOverride(FoodComponent component) { - if (getOriginalFoodComponent() == null) { - foodComponent = component; - } - } - - @Override - public FoodComponent getOriginalFoodComponent() { - if (!foodLoaded) { - foodLoaded = true; - originalFoodComponent = asItem().getFoodComponent(); - } - return originalFoodComponent; + public Toxic getToxic(ItemStack stack) { + setFoodComponent(originalFoodComponent.get()); + return Toxics.lookup(this); } @Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)