Slightly simplify the code

This commit is contained in:
Sollace 2021-06-21 22:05:40 +02:00
parent b14b1e44dd
commit 6a02f83a43
6 changed files with 40 additions and 58 deletions

View file

@ -1,21 +1,15 @@
package com.minelittlepony.unicopia.item.toxin; package com.minelittlepony.unicopia.item.toxin;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent; import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.Text; import net.minecraft.tag.Tag;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult; import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction; import net.minecraft.util.UseAction;
@ -31,9 +25,9 @@ public class Toxic {
private final Optional<FoodComponent> component; private final Optional<FoodComponent> component;
private final Predicate<Item> tag; private final Tag<Item> tag;
Toxic(UseAction action, FoodType type, Optional<FoodComponent> component, Predicate<Item> tag, Ailment lowerBound, Ailment upperBound) { Toxic(UseAction action, FoodType type, Optional<FoodComponent> component, Tag<Item> tag, Ailment lowerBound, Ailment upperBound) {
this.action = action; this.action = action;
this.type = type; this.type = type;
this.component = component; this.component = component;
@ -43,7 +37,7 @@ public class Toxic {
} }
public boolean matches(Item item) { public boolean matches(Item item) {
return tag.test(item); return tag.contains(item);
} }
public Optional<FoodComponent> getFoodComponent() { public Optional<FoodComponent> getFoodComponent() {
@ -54,31 +48,26 @@ public class Toxic {
return action; return action;
} }
@Environment(EnvType.CLIENT) public Ailment getAilmentFor(PlayerEntity player) {
public Text getTooltip(ItemStack stack) { Pony pony = Pony.of(player);
Pony pony = Pony.of(MinecraftClient.getInstance().player);
if (pony != null && !pony.getSpecies().canConsume(type)) { if (pony != null && !pony.getSpecies().canConsume(type)) {
return upperBound.getToxicity().getTooltip(); return upperBound;
} }
return lowerBound.getToxicity().getTooltip(); return lowerBound;
} }
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) { public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) {
if (entity instanceof PlayerEntity) { if (entity instanceof PlayerEntity) {
Race race = Pony.of((PlayerEntity)entity).getSpecies(); getAilmentFor((PlayerEntity)entity).afflict((PlayerEntity)entity, type, stack);
(race.canConsume(type) ? lowerBound : upperBound).afflict((PlayerEntity)entity, type, stack);
} }
return stack; return stack;
} }
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand, Supplier<TypedActionResult<ItemStack>> sup) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!Pony.of(player).getSpecies().hasIronGut()) { if (!Pony.of(player).getSpecies().hasIronGut()) {
return TypedActionResult.fail(player.getStackInHand(hand)); return TypedActionResult.fail(player.getStackInHand(hand));
} }
return null;
return sup.get();
} }
} }

View file

@ -2,11 +2,7 @@ package com.minelittlepony.unicopia.item.toxin;
import java.util.Optional; import java.util.Optional;
import net.minecraft.item.FoodComponent;
public interface ToxicHolder { public interface ToxicHolder {
void setFood(FoodComponent food);
default Optional<Toxic> getToxic() { default Optional<Toxic> getToxic() {
return Optional.empty(); return Optional.empty();
} }

View file

@ -1,14 +1,11 @@
package com.minelittlepony.unicopia.item.toxin; package com.minelittlepony.unicopia.item.toxin;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate;
import com.minelittlepony.unicopia.UTags; import com.minelittlepony.unicopia.UTags;
import com.minelittlepony.unicopia.util.Registries; import com.minelittlepony.unicopia.util.Registries;
import net.minecraft.item.FoodComponent; import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.UseAction; import net.minecraft.util.UseAction;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@ -42,25 +39,22 @@ public interface Toxics {
if (toxin != FOOD) { if (toxin != FOOD) {
toxin = FOOD.and(toxin); toxin = FOOD.and(toxin);
} }
name = "forage_" + name; return register("forage_" + name, UseAction.EAT, FoodType.FORAGE,
return register(name, UseAction.EAT, FoodType.FORAGE,
Optional.of(UFoodComponents.RANDOM_FOLIAGE), Optional.of(UFoodComponents.RANDOM_FOLIAGE),
UTags.item(name)::contains,
new Ailment(toxicity, toxin), new Ailment(toxicity, toxin),
new Ailment(Toxicity.LETHAL, toxin)); new Ailment(Toxicity.LETHAL, toxin));
} }
static Toxic meat(FoodType type, Toxicity toxicity) { static Toxic meat(FoodType type, Toxicity toxicity) {
String name = type.name().toLowerCase(); return register(type.name().toLowerCase(), UseAction.EAT, type,
return register(name, UseAction.EAT, type,
Optional.empty(), Optional.empty(),
UTags.item(name)::contains,
Ailment.INNERT, Ailment.INNERT,
Ailment.of(toxicity)); Ailment.of(toxicity));
} }
static Toxic register(String name, UseAction action, FoodType type, Optional<FoodComponent> component, Predicate<Item> tag, Ailment lower, Ailment upper) { static Toxic register(String name, UseAction action, FoodType type, Optional<FoodComponent> component,
return Registry.register(REGISTRY, name, new Toxic(action, type, component, tag, lower, upper)); Ailment lower,
Ailment upper) {
return Registry.register(REGISTRY, name, new Toxic(action, type, component, UTags.item(name), lower, upper));
} }
} }

View file

@ -18,17 +18,15 @@ abstract class MixinBlockItem extends Item implements ToxicHolder {
@Override @Override
public UseAction getUseAction(ItemStack stack) { public UseAction getUseAction(ItemStack stack) {
if (getToxic().isPresent()) { return getToxic()
return getToxic().get().getUseAction(stack); .map(t -> t.getUseAction(stack))
} .orElseGet(() -> super.getUseAction(stack));
return super.getUseAction(stack);
} }
@Override @Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (getToxic().isPresent()) { return getToxic()
return getToxic().get().use(world, player, hand, () -> super.use(world, player, hand)); .map(t -> t.use(world, player, hand))
} .orElseGet(() -> super.use(world, player, hand));
return super.use(world, player, hand);
} }
} }

View file

@ -4,8 +4,9 @@ import java.util.Optional;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.Shadow;
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;
@ -23,22 +24,27 @@ import net.minecraft.world.World;
@Mixin(Item.class) @Mixin(Item.class)
abstract class MixinItem implements ToxicHolder { abstract class MixinItem implements ToxicHolder {
private boolean foodLoaded;
@Nullable @Nullable
private FoodComponent originalFoodComponent; private FoodComponent originalFoodComponent;
@Override @Shadow
@Accessor("foodComponent") private @Final FoodComponent foodComponent;
public abstract void setFood(FoodComponent food);
@Override @Override
public Optional<Toxic> getToxic() { public Optional<Toxic> getToxic() {
if (originalFoodComponent == null) { if (!foodLoaded) {
foodLoaded = true;
originalFoodComponent = ((Item)(Object)this).getFoodComponent(); originalFoodComponent = ((Item)(Object)this).getFoodComponent();
} }
setFood(originalFoodComponent);
Optional<Toxic> toxic = Toxics.REGISTRY.stream().filter(i -> i.matches((Item)(Object)this)).map(t -> { foodComponent = originalFoodComponent;
t.getFoodComponent().ifPresent(this::setFood); Optional<Toxic> toxic = Toxics.REGISTRY.stream()
.filter(i -> i.matches((Item)(Object)this))
.map(t -> {
if (originalFoodComponent == null) {
t.getFoodComponent().ifPresent(s -> foodComponent = s);
}
return t; return t;
}).findFirst(); }).findFirst();
@ -50,8 +56,6 @@ abstract class MixinItem implements ToxicHolder {
@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true) @Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)
private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) { private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) {
if (getToxic().isPresent()) { getToxic().ifPresent(t -> t.finishUsing(stack, world, entity));
getToxic().get().finishUsing(stack, world, entity);
}
} }
} }

View file

@ -9,6 +9,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.minelittlepony.unicopia.item.toxin.ToxicHolder; import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.item.TooltipContext; import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -19,6 +20,6 @@ import net.minecraft.world.World;
abstract class MixinItem implements ToxicHolder { abstract class MixinItem implements ToxicHolder {
@Inject(method = "appendTooltip", at = @At("RETURN")) @Inject(method = "appendTooltip", at = @At("RETURN"))
private void onAppendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context, CallbackInfo into) { private void onAppendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context, CallbackInfo into) {
getToxic().ifPresent(t -> tooltip.add(t.getTooltip(stack))); getToxic().ifPresent(t -> tooltip.add(t.getAilmentFor(MinecraftClient.getInstance().player).getToxicity().getTooltip()));
} }
} }