mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-23 21:38:00 +01:00
Simplify our item mixin
This commit is contained in:
parent
9e16faaccd
commit
c7cdca3ff4
6 changed files with 42 additions and 58 deletions
|
@ -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);
|
||||
}
|
|
@ -17,8 +17,6 @@ public record Toxic (
|
|||
Optional<FoodComponent> component,
|
||||
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) {
|
||||
ailment.get(player).ifPresent(ailment -> ailment.appendTooltip(tooltip, context));
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<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 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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<ItemImpl.GroundTickCallback> tickCallbacks = new ArrayList<>();
|
||||
private final Supplier<FoodComponent> originalFoodComponent = Suppliers.memoize(((Item)(Object)this)::getFoodComponent);
|
||||
|
||||
@Override
|
||||
public List<GroundTickCallback> 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)
|
||||
|
|
Loading…
Reference in a new issue