Simplify our item mixin

This commit is contained in:
Sollace 2023-09-11 21:20:03 +01:00
parent 9e16faaccd
commit c7cdca3ff4
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
6 changed files with 42 additions and 58 deletions

View file

@ -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);
}

View file

@ -17,8 +17,6 @@ public record Toxic (
Optional<FoodComponent> component, Optional<FoodComponent> component,
Ailment.Set ailment Ailment.Set ailment
) { ) {
public static final Toxic EMPTY = new Toxic(Optional.empty(), Optional.empty(), Ailment.Set.EMPTY);
public void appendTooltip(PlayerEntity player, List<Text> tooltip, TooltipContext context) { public void appendTooltip(PlayerEntity player, List<Text> tooltip, TooltipContext context) {
ailment.get(player).ifPresent(ailment -> ailment.appendTooltip(tooltip, context)); ailment.get(player).ifPresent(ailment -> ailment.appendTooltip(tooltip, context));
} }

View file

@ -1,32 +1,7 @@
package com.minelittlepony.unicopia.item.toxin; package com.minelittlepony.unicopia.item.toxin;
import org.jetbrains.annotations.Nullable;
import net.minecraft.item.*; import net.minecraft.item.*;
public interface ToxicHolder extends ItemConvertible { public interface ToxicHolder {
@Nullable Toxic getToxic(ItemStack stack);
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);
}
} }

View file

@ -3,15 +3,21 @@ package com.minelittlepony.unicopia.item.toxin;
import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.*;
import com.minelittlepony.unicopia.util.RegistryUtils; import com.minelittlepony.unicopia.util.RegistryUtils;
import net.minecraft.item.FoodComponent;
import net.minecraft.registry.Registry; import net.minecraft.registry.Registry;
import static com.minelittlepony.unicopia.item.toxin.Toxicity.*; import static com.minelittlepony.unicopia.item.toxin.Toxicity.*;
import static com.minelittlepony.unicopia.item.toxin.Ailment.*; import static com.minelittlepony.unicopia.item.toxin.Ailment.*;
import static com.minelittlepony.unicopia.item.toxin.Toxin.*; import static com.minelittlepony.unicopia.item.toxin.Toxin.*;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
public interface Toxics { public interface Toxics {
Registry<ToxicRegistryEntry> REGISTRY = RegistryUtils.createSimple(Unicopia.id("toxic")); Registry<ToxicRegistryEntry> 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 SEVERE_INNERT = Toxic.innert(Toxicity.SEVERE);
Toxic EDIBLE = register("edible", new Toxic.Builder(Ailment.INNERT) Toxic EDIBLE = register("edible", new Toxic.Builder(Ailment.INNERT)
@ -127,4 +133,17 @@ public interface Toxics {
static Toxic register(String name, Toxic.Builder builder) { 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(); 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);
}
} }

View file

@ -44,6 +44,7 @@ public interface Toxin extends Affliction {
player.removeStatusEffect(StatusEffects.NAUSEA); player.removeStatusEffect(StatusEffects.NAUSEA);
player.removeStatusEffect(UEffects.FOOD_POISONING); player.removeStatusEffect(UEffects.FOOD_POISONING);
}); });
Toxin BAT_PONY_INTOXICATION = Toxin.of(StatusEffects.HEALTH_BOOST, 30, 60, 2, 6) 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.JUMP_BOOST, 30, 60, 1, 6))
.and(Toxin.of(StatusEffects.SPEED, 30, 30, 1, 6)) .and(Toxin.of(StatusEffects.SPEED, 30, 30, 1, 6))

View file

@ -2,17 +2,16 @@ package com.minelittlepony.unicopia.mixin;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.Mixin;
import org.spongepowered.asm.mixin.Mutable; 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.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; 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;
import com.minelittlepony.unicopia.entity.ItemImpl.GroundTickCallback; import com.minelittlepony.unicopia.entity.ItemImpl.GroundTickCallback;
import com.minelittlepony.unicopia.entity.effect.FoodPoisoningStatusEffect; import com.minelittlepony.unicopia.entity.effect.FoodPoisoningStatusEffect;
@ -29,16 +28,9 @@ import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World; import net.minecraft.world.World;
@Mixin(Item.class) @Mixin(Item.class)
abstract class MixinItem implements ToxicHolder, ItemImpl.TickableItem { abstract class MixinItem implements ItemDuck {
private boolean foodLoaded;
@Nullable
private FoodComponent originalFoodComponent;
@Shadow @Mutable
private @Final FoodComponent foodComponent;
private final List<ItemImpl.GroundTickCallback> tickCallbacks = new ArrayList<>(); private final List<ItemImpl.GroundTickCallback> tickCallbacks = new ArrayList<>();
private final Supplier<FoodComponent> originalFoodComponent = Suppliers.memoize(((Item)(Object)this)::getFoodComponent);
@Override @Override
public List<GroundTickCallback> getCallbacks() { public List<GroundTickCallback> getCallbacks() {
@ -46,24 +38,14 @@ abstract class MixinItem implements ToxicHolder, ItemImpl.TickableItem {
} }
@Override @Override
public void clearFoodOverride() { @Mutable
foodComponent = getOriginalFoodComponent(); @Accessor("foodComponent")
} public abstract void setFoodComponent(FoodComponent food);
@Override @Override
public void setFoodOverride(FoodComponent component) { public Toxic getToxic(ItemStack stack) {
if (getOriginalFoodComponent() == null) { setFoodComponent(originalFoodComponent.get());
foodComponent = component; return Toxics.lookup(this);
}
}
@Override
public FoodComponent getOriginalFoodComponent() {
if (!foodLoaded) {
foodLoaded = true;
originalFoodComponent = asItem().getFoodComponent();
}
return originalFoodComponent;
} }
@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true) @Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)