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;
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 net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.tag.Tag;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
@ -31,9 +25,9 @@ public class Toxic {
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.type = type;
this.component = component;
@ -43,7 +37,7 @@ public class Toxic {
}
public boolean matches(Item item) {
return tag.test(item);
return tag.contains(item);
}
public Optional<FoodComponent> getFoodComponent() {
@ -54,31 +48,26 @@ public class Toxic {
return action;
}
@Environment(EnvType.CLIENT)
public Text getTooltip(ItemStack stack) {
Pony pony = Pony.of(MinecraftClient.getInstance().player);
public Ailment getAilmentFor(PlayerEntity player) {
Pony pony = Pony.of(player);
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) {
if (entity instanceof PlayerEntity) {
Race race = Pony.of((PlayerEntity)entity).getSpecies();
(race.canConsume(type) ? lowerBound : upperBound).afflict((PlayerEntity)entity, type, stack);
getAilmentFor((PlayerEntity)entity).afflict((PlayerEntity)entity, type, 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()) {
return TypedActionResult.fail(player.getStackInHand(hand));
}
return sup.get();
return null;
}
}

View file

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

View file

@ -1,14 +1,11 @@
package com.minelittlepony.unicopia.item.toxin;
import java.util.Optional;
import java.util.function.Predicate;
import com.minelittlepony.unicopia.UTags;
import com.minelittlepony.unicopia.util.Registries;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import net.minecraft.util.UseAction;
import net.minecraft.util.registry.Registry;
@ -42,25 +39,22 @@ public interface Toxics {
if (toxin != FOOD) {
toxin = FOOD.and(toxin);
}
name = "forage_" + name;
return register(name, UseAction.EAT, FoodType.FORAGE,
return register("forage_" + name, UseAction.EAT, FoodType.FORAGE,
Optional.of(UFoodComponents.RANDOM_FOLIAGE),
UTags.item(name)::contains,
new Ailment(toxicity, toxin),
new Ailment(Toxicity.LETHAL, toxin));
}
static Toxic meat(FoodType type, Toxicity toxicity) {
String name = type.name().toLowerCase();
return register(name, UseAction.EAT, type,
return register(type.name().toLowerCase(), UseAction.EAT, type,
Optional.empty(),
UTags.item(name)::contains,
Ailment.INNERT,
Ailment.of(toxicity));
}
static Toxic register(String name, UseAction action, FoodType type, Optional<FoodComponent> component, Predicate<Item> tag, Ailment lower, Ailment upper) {
return Registry.register(REGISTRY, name, new Toxic(action, type, component, tag, lower, upper));
static Toxic register(String name, UseAction action, FoodType type, Optional<FoodComponent> component,
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
public UseAction getUseAction(ItemStack stack) {
if (getToxic().isPresent()) {
return getToxic().get().getUseAction(stack);
}
return super.getUseAction(stack);
return getToxic()
.map(t -> t.getUseAction(stack))
.orElseGet(() -> super.getUseAction(stack));
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (getToxic().isPresent()) {
return getToxic().get().use(world, player, hand, () -> super.use(world, player, hand));
}
return super.use(world, player, hand);
return getToxic()
.map(t -> t.use(world, player, hand))
.orElseGet(() -> super.use(world, player, hand));
}
}

View file

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