mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-17 10:24:23 +01:00
Reworking toxics slightly
This commit is contained in:
parent
aa724f56b0
commit
92c50f2676
11 changed files with 161 additions and 152 deletions
|
@ -29,7 +29,7 @@ public class DrinkableItem extends Item {
|
|||
stack.decrement(1);
|
||||
}
|
||||
|
||||
((ToxicHolder)this).getToxic(stack).ifPresent(t -> t.finishUsing(stack, world, user));
|
||||
((ToxicHolder)this).getToxic(stack).finishUsing(stack, world, user);
|
||||
|
||||
return stack.isEmpty() ? new ItemStack(getRecipeRemainder()) : stack;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.item;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
import com.minelittlepony.unicopia.Unicopia;
|
||||
import com.minelittlepony.unicopia.item.toxin.Toxic;
|
||||
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
|
@ -19,15 +20,13 @@ public interface UItemGroups {
|
|||
DefaultedList<ItemStack> defs = DefaultedList.of();
|
||||
UItems.ITEMS.stream()
|
||||
.filter(item -> !(item instanceof ChameleonItem) || ((ChameleonItem)item).isFullyDisguised())
|
||||
.forEach(item -> {
|
||||
item.appendStacks(ItemGroup.SEARCH, defs);
|
||||
});
|
||||
.forEach(item -> item.appendStacks(ItemGroup.SEARCH, defs));
|
||||
list.addAll(defs);
|
||||
}).icon(UItems.EMPTY_JAR::getDefaultStack).build();
|
||||
ItemGroup HORSE_FEED = FabricItemGroupBuilder.create(Unicopia.id("horsefeed")).appendItems(list -> {
|
||||
list.addAll(Registry.ITEM.stream()
|
||||
.map(Item::getDefaultStack)
|
||||
.filter(item -> item.getItem() instanceof ToxicHolder && ((ToxicHolder)item.getItem()).getToxic(item).isPresent())
|
||||
.filter(item -> ((ToxicHolder)item.getItem()).getToxic(item) != Toxic.EMPTY)
|
||||
.collect(Collectors.toList()));
|
||||
}).icon(UItems.ZAP_APPLE::getDefaultStack).build();
|
||||
|
||||
|
|
|
@ -1,17 +1,12 @@
|
|||
package com.minelittlepony.unicopia.item;
|
||||
|
||||
import static com.minelittlepony.unicopia.item.toxin.Toxin.INNERT;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.minelittlepony.unicopia.UTags;
|
||||
import com.minelittlepony.unicopia.Unicopia;
|
||||
import com.minelittlepony.unicopia.advancement.UCriteria;
|
||||
import com.minelittlepony.unicopia.entity.player.Pony;
|
||||
import com.minelittlepony.unicopia.item.toxin.Ailment;
|
||||
import com.minelittlepony.unicopia.item.toxin.Toxic;
|
||||
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
|
||||
import com.minelittlepony.unicopia.item.toxin.Toxicity;
|
||||
import com.minelittlepony.unicopia.item.toxin.*;
|
||||
import com.minelittlepony.unicopia.particle.ParticleUtils;
|
||||
import com.minelittlepony.unicopia.particle.UParticles;
|
||||
import com.minelittlepony.unicopia.util.MagicalDamageSource;
|
||||
|
@ -26,9 +21,7 @@ import net.minecraft.entity.mob.CreeperEntity;
|
|||
import net.minecraft.entity.passive.PigEntity;
|
||||
import net.minecraft.entity.passive.VillagerEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemGroup;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.*;
|
||||
import net.minecraft.predicate.entity.EntityPredicates;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
|
@ -44,9 +37,6 @@ import net.minecraft.world.World;
|
|||
import net.minecraft.world.event.GameEvent;
|
||||
|
||||
public class ZapAppleItem extends Item implements ChameleonItem, ToxicHolder {
|
||||
private static final Optional<Toxic> TOXIC = Optional.of(new Toxic.Builder(Ailment.of(Toxicity.SEVERE, INNERT)).build("zap"));
|
||||
private static final Optional<Toxic> HIDDEN_TOXIC = Optional.of(new Toxic.Builder(Ailment.of(Toxicity.SAFE, INNERT)).build("zap_hidden"));
|
||||
|
||||
public ZapAppleItem(Settings settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
@ -136,8 +126,8 @@ public class ZapAppleItem extends Item implements ChameleonItem, ToxicHolder {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Optional<Toxic> getToxic(ItemStack stack) {
|
||||
return hasAppearance(stack) ? TOXIC : HIDDEN_TOXIC;
|
||||
public Toxic getToxic(ItemStack stack) {
|
||||
return hasAppearance(stack) ? Toxics.SEVERE_INNERT : Toxics.FORAGE_EDIBLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,40 +1,63 @@
|
|||
package com.minelittlepony.unicopia.item.toxin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import com.minelittlepony.unicopia.Race;
|
||||
import com.minelittlepony.unicopia.entity.player.Pony;
|
||||
|
||||
import net.minecraft.client.item.TooltipContext;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.TypedActionResult;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class Ailment implements Affliction {
|
||||
public record Ailment (
|
||||
Toxicity toxicity,
|
||||
Toxin effect
|
||||
) {
|
||||
public static final Ailment INNERT = of(Toxicity.SAFE, Toxin.INNERT);
|
||||
|
||||
private final Toxicity toxicity;
|
||||
private final Toxin effect;
|
||||
|
||||
Ailment(Toxicity toxicity, Toxin effect) {
|
||||
this.toxicity = toxicity;
|
||||
this.effect = effect;
|
||||
}
|
||||
|
||||
public Toxicity getToxicity() {
|
||||
return toxicity;
|
||||
}
|
||||
|
||||
public void appendTooltip(List<Text> tooltip, TooltipContext context) {
|
||||
tooltip.add(getToxicity().getTooltip());
|
||||
if (context.isAdvanced()) {
|
||||
effect.appendTooltip(tooltip);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afflict(PlayerEntity player, ItemStack stack) {
|
||||
effect.afflict(player, stack);
|
||||
}
|
||||
|
||||
public static Ailment of(Toxicity toxicity, Toxin effect) {
|
||||
return new Ailment(toxicity, effect);
|
||||
}
|
||||
|
||||
public void appendTooltip(List<Text> tooltip, TooltipContext context) {
|
||||
tooltip.add(toxicity().getTooltip());
|
||||
if (context.isAdvanced()) {
|
||||
effect().appendTooltip(tooltip);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) {
|
||||
effect().afflict((PlayerEntity)entity, stack);
|
||||
return stack;
|
||||
}
|
||||
|
||||
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
||||
if (!Pony.of(player).getSpecies().hasIronGut()) {
|
||||
return TypedActionResult.fail(player.getStackInHand(hand));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public interface Set {
|
||||
Set EMPTY = e -> Optional.empty();
|
||||
|
||||
Optional<Ailment> get(LivingEntity entity);
|
||||
|
||||
static Ailment.Set of(Ailment def, Map<Race, Ailment> map) {
|
||||
if (map.isEmpty()) {
|
||||
return of(def);
|
||||
}
|
||||
return entity -> Optional.of(entity instanceof PlayerEntity player ? map.getOrDefault(Pony.of(player).getSpecies(), def) : def);
|
||||
}
|
||||
|
||||
static Ailment.Set of(Ailment ailment) {
|
||||
final Optional<Ailment> value = Optional.of(ailment);
|
||||
return entity -> value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,77 +3,39 @@ package com.minelittlepony.unicopia.item.toxin;
|
|||
import java.util.*;
|
||||
|
||||
import com.minelittlepony.unicopia.Race;
|
||||
import com.minelittlepony.unicopia.UTags;
|
||||
import com.minelittlepony.unicopia.entity.player.Pony;
|
||||
|
||||
import net.minecraft.client.item.TooltipContext;
|
||||
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.tag.TagKey;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.TypedActionResult;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.UseAction;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class Toxic {
|
||||
private final UseAction action;
|
||||
public record Toxic (
|
||||
Optional<UseAction> useAction,
|
||||
Optional<FoodComponent> component,
|
||||
Ailment.Set ailment
|
||||
) {
|
||||
public static final Toxic EMPTY = new Toxic(Optional.empty(), Optional.empty(), Ailment.Set.EMPTY);
|
||||
|
||||
private final Optional<FoodComponent> component;
|
||||
|
||||
private final Ailment defaultAilment;
|
||||
private final Map<Race, Ailment> ailments;
|
||||
|
||||
private final TagKey<Item> tag;
|
||||
|
||||
Toxic(UseAction action, Optional<FoodComponent> component, TagKey<Item> tag, Ailment defaultAilment, Map<Race, Ailment> ailments) {
|
||||
this.action = action;
|
||||
this.component = component;
|
||||
this.tag = tag;
|
||||
this.defaultAilment = defaultAilment;
|
||||
this.ailments = ailments;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean matches(Item item) {
|
||||
return item.getRegistryEntry().isIn(tag);
|
||||
}
|
||||
|
||||
public Optional<FoodComponent> getFoodComponent() {
|
||||
return component;
|
||||
}
|
||||
|
||||
public UseAction getUseAction(ItemStack stack) {
|
||||
return action;
|
||||
}
|
||||
|
||||
public Ailment getAilmentFor(PlayerEntity player) {
|
||||
if (player == null) {
|
||||
return defaultAilment;
|
||||
}
|
||||
return ailments.getOrDefault(Pony.of(player).getSpecies(), defaultAilment);
|
||||
public void appendTooltip(PlayerEntity player, List<Text> tooltip, TooltipContext context) {
|
||||
ailment.get(player).ifPresent(ailment -> ailment.appendTooltip(tooltip, context));
|
||||
}
|
||||
|
||||
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) {
|
||||
if (entity instanceof PlayerEntity) {
|
||||
getAilmentFor((PlayerEntity)entity).afflict((PlayerEntity)entity, stack);
|
||||
}
|
||||
|
||||
ailment.get(entity).ifPresent(ailment -> ailment.effect().afflict((PlayerEntity)entity, stack));
|
||||
return stack;
|
||||
}
|
||||
|
||||
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
||||
if (!Pony.of(player).getSpecies().hasIronGut()) {
|
||||
return TypedActionResult.fail(player.getStackInHand(hand));
|
||||
}
|
||||
return null;
|
||||
public static Toxic innert(Toxicity toxicity) {
|
||||
return new Builder(Ailment.of(toxicity, Toxin.INNERT)).build();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private final Ailment def;
|
||||
private final Map<Race, Ailment> ailments = new HashMap<>();
|
||||
private UseAction action = UseAction.EAT;
|
||||
private final Map<Race, Ailment> overrides = new HashMap<>();
|
||||
private Optional<UseAction> action = Optional.of(UseAction.EAT);
|
||||
private Optional<FoodComponent> component = Optional.empty();
|
||||
|
||||
public Builder(Ailment def) {
|
||||
|
@ -81,7 +43,7 @@ public class Toxic {
|
|||
}
|
||||
|
||||
public Builder action(UseAction action) {
|
||||
this.action = action;
|
||||
this.action = Optional.of(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -91,12 +53,16 @@ public class Toxic {
|
|||
}
|
||||
|
||||
public Builder with(Race race, Ailment ailment) {
|
||||
ailments.put(race, ailment);
|
||||
overrides.put(race, ailment);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Toxic build(String name) {
|
||||
return new Toxic(action, component, UTags.item(name), def, ailments);
|
||||
public Toxic build() {
|
||||
return new Toxic(action, component, Ailment.Set.of(def, overrides));
|
||||
}
|
||||
|
||||
public Optional<Toxic> buildOptional() {
|
||||
return Optional.of(build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,32 @@
|
|||
package com.minelittlepony.unicopia.item.toxin;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.*;
|
||||
|
||||
public interface ToxicHolder {
|
||||
default Optional<Toxic> getToxic(ItemStack stack) {
|
||||
return Optional.empty();
|
||||
@Nullable
|
||||
default FoodComponent getOriginalFoodComponent() {
|
||||
return null;
|
||||
}
|
||||
|
||||
default Toxic getDefaultToxic() {
|
||||
return getOriginalFoodComponent() == null ? Toxic.EMPTY : Toxics.FORAGE_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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.minelittlepony.unicopia.item.toxin;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.tag.TagKey;
|
||||
|
||||
public record ToxicRegistryEntry (
|
||||
Toxic value,
|
||||
TagKey<Item> tag
|
||||
) {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean matches(Item item) {
|
||||
return item.getRegistryEntry().isIn(tag);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +1,8 @@
|
|||
package com.minelittlepony.unicopia.item.toxin;
|
||||
|
||||
import com.minelittlepony.unicopia.Race;
|
||||
import com.minelittlepony.unicopia.*;
|
||||
import com.minelittlepony.unicopia.util.Registries;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
import static com.minelittlepony.unicopia.item.toxin.Toxicity.*;
|
||||
|
@ -11,50 +10,53 @@ import static com.minelittlepony.unicopia.item.toxin.Ailment.*;
|
|||
import static com.minelittlepony.unicopia.item.toxin.Toxin.*;
|
||||
|
||||
public interface Toxics {
|
||||
Registry<Toxic> REGISTRY = Registries.createSimple(new Identifier("unicopia:toxic"));
|
||||
Registry<ToxicRegistryEntry> REGISTRY = Registries.createSimple(Unicopia.id("toxic"));
|
||||
|
||||
Toxic EDIBLE = register("forage_edible", new Toxic.Builder(Ailment.INNERT)
|
||||
Toxic SEVERE_INNERT = Toxic.innert(Toxicity.SEVERE);
|
||||
|
||||
Toxic FORAGE_EDIBLE = register("forage_edible", new Toxic.Builder(Ailment.INNERT)
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING))
|
||||
.with(Race.CHANGELING, of(FAIR, LOVE_SICKNESS))
|
||||
);
|
||||
Toxic RISKY = register("forage_risky", new Toxic.Builder(of(FAIR, WEAK_NAUSEA.withChance(20)))
|
||||
|
||||
Toxic FORAGE_RISKY = register("forage_risky", new Toxic.Builder(of(FAIR, WEAK_NAUSEA.withChance(20)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING))
|
||||
);
|
||||
Toxic MODERATE = register("forage_moderate", new Toxic.Builder(of(MILD, POISON.and(WEAK_NAUSEA)))
|
||||
Toxic FORAGE_MODERATE = register("forage_moderate", new Toxic.Builder(of(MILD, POISON.and(WEAK_NAUSEA)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING))
|
||||
);
|
||||
Toxic DANGEROUS = register("forage_dangerous", new Toxic.Builder(of(SEVERE, FOOD_POISONING))
|
||||
Toxic FORAGE_DANGEROUS = register("forage_dangerous", new Toxic.Builder(of(SEVERE, FOOD_POISONING))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING.and(NAUSEA)))
|
||||
);
|
||||
Toxic NAUSEATING = register("forage_nauseating", new Toxic.Builder(of(SAFE, NAUSEA.and(WEAKNESS.withChance(30))))
|
||||
Toxic FORAGE_NAUSEATING = register("forage_nauseating", new Toxic.Builder(of(SAFE, NAUSEA.and(WEAKNESS.withChance(30))))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING.and(NAUSEA)))
|
||||
);
|
||||
Toxic RADIOACTIVE = register("forage_radioactive", new Toxic.Builder(of(SAFE, NAUSEA.and(RADIOACTIVITY.withChance(30))))
|
||||
Toxic FORAGE_RADIOACTIVE = register("forage_radioactive", new Toxic.Builder(of(SAFE, NAUSEA.and(RADIOACTIVITY.withChance(30))))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING.and(NAUSEA)))
|
||||
);
|
||||
Toxic PRICKLY = register("forage_prickly", new Toxic.Builder(of(SAFE, PRICKLING.withChance(30)))
|
||||
Toxic FORAGE_PRICKLY = register("forage_prickly", new Toxic.Builder(of(SAFE, PRICKLING.withChance(30)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING.and(NAUSEA)))
|
||||
);
|
||||
Toxic STRENGHTENING = register("forage_strengthening", new Toxic.Builder(of(SEVERE, STRENGTH.and(WEAK_NAUSEA)))
|
||||
Toxic FORAGE_STRENGHTENING = register("forage_strengthening", new Toxic.Builder(of(SEVERE, STRENGTH.and(WEAK_NAUSEA)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING.and(WEAKNESS)))
|
||||
);
|
||||
Toxic SEVERELY_NAUSEATING = register("forage_severely_nauseating", new Toxic.Builder(of(SEVERE, STRONG_NAUSEA.and(WEAKNESS)))
|
||||
Toxic FORAGE_SEVERELY_NAUSEATING = register("forage_severely_nauseating", new Toxic.Builder(of(SEVERE, STRONG_NAUSEA.and(WEAKNESS)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING.and(WEAKNESS)))
|
||||
);
|
||||
Toxic BLINDING = register("forage_blinding", new Toxic.Builder(of(SEVERE, BLINDNESS.and(WEAK_NAUSEA)))
|
||||
Toxic FORAGE_BLINDING = register("forage_blinding", new Toxic.Builder(of(SEVERE, BLINDNESS.and(WEAK_NAUSEA)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING))
|
||||
);
|
||||
Toxic SEVERELY_PRICKLY = register("forage_severely_prickly", new Toxic.Builder(of(SEVERE, PRICKLING.and(NAUSEA)))
|
||||
Toxic FORAGE_SEVERELY_PRICKLY = register("forage_severely_prickly", new Toxic.Builder(of(SEVERE, PRICKLING.and(NAUSEA)))
|
||||
.food(UFoodComponents.RANDOM_FOLIAGE)
|
||||
.with(Race.HUMAN, of(LETHAL, FOOD_POISONING))
|
||||
);
|
||||
|
@ -98,10 +100,11 @@ public interface Toxics {
|
|||
.with(Race.BAT, Ailment.INNERT)
|
||||
);
|
||||
|
||||
Toxic LOVE = register("love", new Toxic.Builder(Ailment.INNERT));
|
||||
|
||||
static void bootstrap() {}
|
||||
|
||||
static Toxic register(String name, Toxic.Builder builder) {
|
||||
name = "food_types/" + name;
|
||||
return Registry.register(REGISTRY, name, builder.build(name));
|
||||
return Registry.register(REGISTRY, Unicopia.id(name), new ToxicRegistryEntry(builder.build(), UTags.item("food_types/" + name))).value();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.minelittlepony.unicopia.mixin;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
||||
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
|
@ -18,14 +19,12 @@ abstract class MixinBlockItem extends Item implements ToxicHolder {
|
|||
|
||||
@Override
|
||||
public UseAction getUseAction(ItemStack stack) {
|
||||
return getToxic(stack)
|
||||
.map(t -> t.getUseAction(stack))
|
||||
.orElseGet(() -> super.getUseAction(stack));
|
||||
return getToxic(stack).useAction().orElseGet(() -> super.getUseAction(stack));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
||||
return getToxic(player.getStackInHand(hand))
|
||||
return getToxic(player.getStackInHand(hand)).ailment().get(player)
|
||||
.map(t -> t.use(world, player, hand))
|
||||
.orElseGet(() -> super.use(world, player, hand));
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.minelittlepony.unicopia.mixin;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
@ -14,11 +13,9 @@ import org.spongepowered.asm.mixin.injection.At;
|
|||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import com.minelittlepony.unicopia.item.toxin.Toxic;
|
||||
import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
|
||||
import com.minelittlepony.unicopia.item.toxin.Toxics;
|
||||
import com.minelittlepony.unicopia.entity.ItemImpl;
|
||||
import com.minelittlepony.unicopia.entity.ItemImpl.GroundTickCallback;
|
||||
import com.minelittlepony.unicopia.item.toxin.*;
|
||||
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.item.FoodComponent;
|
||||
|
@ -36,41 +33,36 @@ abstract class MixinItem implements ToxicHolder, ItemImpl.TickableItem {
|
|||
@Shadow @Mutable
|
||||
private @Final FoodComponent foodComponent;
|
||||
|
||||
private List<ItemImpl.GroundTickCallback> tickCallbacks;
|
||||
private final List<ItemImpl.GroundTickCallback> tickCallbacks = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public List<GroundTickCallback> getCallbacks() {
|
||||
if (tickCallbacks == null) {
|
||||
tickCallbacks = new ArrayList<>();
|
||||
}
|
||||
return tickCallbacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Toxic> getToxic(ItemStack stack) {
|
||||
public void clearFoodOverride() {
|
||||
foodComponent = getOriginalFoodComponent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFoodOverride(FoodComponent component) {
|
||||
if (getOriginalFoodComponent() == null) {
|
||||
foodComponent = component;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FoodComponent getOriginalFoodComponent() {
|
||||
if (!foodLoaded) {
|
||||
foodLoaded = true;
|
||||
originalFoodComponent = ((Item)(Object)this).getFoodComponent();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (!toxic.isPresent() && ((Item)(Object)this).getFoodComponent() != null) {
|
||||
return Optional.of(Toxics.EDIBLE);
|
||||
}
|
||||
return toxic;
|
||||
return originalFoodComponent;
|
||||
}
|
||||
|
||||
@Inject(method = "finishUsing", at = @At("HEAD"), cancellable = true)
|
||||
private void finishUsing(ItemStack stack, World world, LivingEntity entity, CallbackInfoReturnable<ItemStack> info) {
|
||||
getToxic(stack).ifPresent(t -> t.finishUsing(stack, world, entity));
|
||||
getToxic(stack).finishUsing(stack, world, entity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,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(stack).ifPresent(t -> t.getAilmentFor(MinecraftClient.getInstance().player).appendTooltip(tooltip, context));
|
||||
getToxic(stack).appendTooltip(MinecraftClient.getInstance().player, tooltip, context);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue