Move toxics to item tags and add a default for when a food isn't registered

This commit is contained in:
Sollace 2021-06-12 17:16:42 +02:00
parent 241daec138
commit 0a06dee124
23 changed files with 172 additions and 71 deletions

View file

@ -51,7 +51,7 @@ public enum Race implements Affine {
public boolean canConsume(FoodType type) { public boolean canConsume(FoodType type) {
if (!isUsable()) { if (!isUsable()) {
return type != FoodType.VEGAN; return type != FoodType.FORAGE;
} }
if (type.isMeat()) { if (type.isMeat()) {
return this == BAT || this == CHANGELING || this == HUMAN; return this == BAT || this == CHANGELING || this == HUMAN;

View file

@ -1,5 +1,7 @@
package com.minelittlepony.unicopia; package com.minelittlepony.unicopia;
import com.minelittlepony.unicopia.item.toxin.Toxics;
import net.fabricmc.fabric.api.tag.TagRegistry; import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -10,10 +12,6 @@ public interface UTags {
Tag<Item> APPLES = item("apples"); Tag<Item> APPLES = item("apples");
Tag<Item> FESH_APPLES = item("fresh_apples"); Tag<Item> FESH_APPLES = item("fresh_apples");
Tag<Item> NON_TOXIC = item("non_toxic");
Tag<Item> FAIRLY_TOXIC = item("fairly_toxic");
Tag<Item> SEVERELY_TOXIC = item("severely_toxic");
Tag<Item> FALLS_SLOWLY = item("falls_slowly"); Tag<Item> FALLS_SLOWLY = item("falls_slowly");
Tag<Block> FRAGILE = block("fragile"); Tag<Block> FRAGILE = block("fragile");
@ -30,5 +28,7 @@ public interface UTags {
return TagRegistry.block(new Identifier("unicopia", name)); return TagRegistry.block(new Identifier("unicopia", name));
} }
static void bootstrap() { } static void bootstrap() {
Toxics.bootstrap();
}
} }

View file

@ -87,7 +87,6 @@ public interface UItems {
} }
static void bootstrap() { static void bootstrap() {
Toxics.bootstrap();
UEnchantments.bootstrap(); UEnchantments.bootstrap();
URecipes.bootstrap(); URecipes.bootstrap();
UItemGroups.bootstrap(); UItemGroups.bootstrap();

View file

@ -3,10 +3,10 @@ package com.minelittlepony.unicopia.item.toxin;
public enum FoodType { public enum FoodType {
RAW_MEAT, COOKED_MEAT, RAW_MEAT, COOKED_MEAT,
RAW_FISH, COOKED_FISH, RAW_FISH, COOKED_FISH,
VEGAN; FORAGE;
public boolean isRaw() { public boolean isRaw() {
return this == RAW_MEAT || this == RAW_FISH || this == VEGAN; return this == RAW_MEAT || this == RAW_FISH || this == FORAGE;
} }
public boolean isMeat() { public boolean isMeat() {

View file

@ -1,5 +1,7 @@
package com.minelittlepony.unicopia.item.toxin; package com.minelittlepony.unicopia.item.toxin;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Race;
@ -10,6 +12,8 @@ import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient; 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.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
@ -25,13 +29,27 @@ public class Toxic {
private final FoodType type; private final FoodType type;
Toxic(UseAction action, FoodType type, Ailment lowerBound, Ailment upperBound) { private final Optional<FoodComponent> component;
private final Predicate<Item> tag;
Toxic(UseAction action, FoodType type, Optional<FoodComponent> component, Predicate<Item> tag, Ailment lowerBound, Ailment upperBound) {
this.action = action; this.action = action;
this.type = type; this.type = type;
this.component = component;
this.tag = tag;
this.lowerBound = lowerBound; this.lowerBound = lowerBound;
this.upperBound = upperBound; this.upperBound = upperBound;
} }
public boolean matches(Item item) {
return tag.test(item);
}
public Optional<FoodComponent> getFoodComponent() {
return component;
}
public UseAction getUseAction(ItemStack stack) { public UseAction getUseAction(ItemStack stack) {
return action; return action;
} }

View file

@ -7,8 +7,6 @@ import net.minecraft.item.FoodComponent;
public interface ToxicHolder { public interface ToxicHolder {
void setFood(FoodComponent food); void setFood(FoodComponent food);
void setToxic(Toxic toxic);
default Optional<Toxic> getToxic() { default Optional<Toxic> getToxic() {
return Optional.empty(); return Optional.empty();
} }

View file

@ -1,62 +1,66 @@
package com.minelittlepony.unicopia.item.toxin; package com.minelittlepony.unicopia.item.toxin;
import java.util.Objects; 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.FoodComponent;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import net.minecraft.util.UseAction; import net.minecraft.util.UseAction;
import net.minecraft.util.registry.Registry;
import static com.minelittlepony.unicopia.item.toxin.Toxicity.*;
import static com.minelittlepony.unicopia.item.toxin.Toxin.*;
public interface Toxics { public interface Toxics {
Toxic FORAGED = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Items.DANDELION, Items.BLUE_ORCHID, Items.RED_TULIP, Items.ORANGE_TULIP, Items.PINK_TULIP, Items.CORNFLOWER, Items.PEONY, Items.TALL_GRASS); Registry<Toxic> REGISTRY = Registries.createSimple(new Identifier("unicopia:toxic"));
Toxic RISKY_FORAGED = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.FAIR, Items.ALLIUM, Items.WHITE_TULIP);
Toxic DANGER_FORAGED = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Items.POPPY);
Toxic GRASS = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Toxin.NAUSEA, Items.GRASS); Toxic EDIBLE = forage("edible", SAFE, FOOD);
Toxic AZUER_BLUET = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Toxin.RADIOACTIVITY, Items.AZURE_BLUET); Toxic RISKY = forage("risky", FAIR, FOOD);
Toxic ROSE_BUSH = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Toxin.DAMAGE, Items.ROSE_BUSH); Toxic DANGEROUS = forage("dangerous", SEVERE, FOOD);
Toxic FERN = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.STRENGTH, Items.FERN); Toxic NAUSEATING = forage("nauseating", SAFE, NAUSEA);
Toxic DEAD_BUSH = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.NAUSEA, Items.DEAD_BUSH); Toxic RADIOACTIVE = forage("radioactive", SAFE, RADIOACTIVITY);
Toxic OXEYE_DAISY = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.BLINDNESS, Items.OXEYE_DAISY); Toxic PRICKLY = forage("prickly", SAFE, DAMAGE);
Toxic LARGE_FERN = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.DAMAGE, Items.LARGE_FERN); Toxic STRENGHTENING = forage("strengthening", SEVERE, STRENGTH);
Toxic SEVERELY_NAUSEATING = forage("severely_nauseating", SEVERE, NAUSEA);
Toxic BLINDING = forage("blinding", SEVERE, BLINDNESS);
Toxic SEVERELY_PRICKLY = forage("severely_prickly", SEVERE, DAMAGE);
Toxic RAW_MEAT = register(new Toxic(UseAction.EAT, FoodType.RAW_MEAT, Ailment.INNERT, Ailment.of(Toxicity.MILD)), Items.PORKCHOP, Items.BEEF, Items.MUTTON, Items.RABBIT, Items.CHICKEN); Toxic RAW_MEAT = meat(FoodType.RAW_MEAT, MILD);
Toxic COOKED_MEAT = register(new Toxic(UseAction.EAT, FoodType.COOKED_MEAT, Ailment.INNERT, Ailment.of(Toxicity.MILD)), Items.COOKED_PORKCHOP, Items.COOKED_BEEF, Items.COOKED_MUTTON, Items.COOKED_RABBIT, Items.COOKED_CHICKEN); Toxic COOKED_MEAT = meat(FoodType.COOKED_MEAT, MILD);
Toxic RAW_FISH = register(new Toxic(UseAction.EAT, FoodType.RAW_FISH, Ailment.INNERT, Ailment.of(Toxicity.FAIR)), Items.PUFFERFISH, Items.COD, Items.SALMON, Items.TROPICAL_FISH); Toxic RAW_FISH = meat(FoodType.RAW_FISH, FAIR);
Toxic COOKED_FISH = register(new Toxic(UseAction.EAT, FoodType.COOKED_FISH, Ailment.INNERT, Ailment.of(Toxicity.FAIR)), Items.COOKED_COD, Items.COOKED_SALMON); Toxic COOKED_FISH = meat(FoodType.COOKED_FISH, FAIR);
static void bootstrap() {} static void bootstrap() {}
static Toxic register(FoodComponent food, FoodType type, UseAction action, Toxicity toxicity, Item... items) { static Toxic forage(String name, Toxicity toxicity, Toxin toxin) {
Toxic toxic = new Toxic(action, type, Ailment.of(toxicity), Ailment.of(Toxicity.LETHAL)); if (toxin != FOOD) {
for (Item i : items) { toxin = FOOD.and(toxin);
ToxicHolder holder = (ToxicHolder)i; }
holder.setToxic(toxic); name = "forage_" + name;
holder.setFood(Objects.requireNonNull(food, i.getTranslationKey() + " food"));
return register(name, UseAction.EAT, FoodType.FORAGE,
Optional.of(UFoodComponents.RANDOM_FOLIAGE),
UTags.item(name)::contains,
new Ailment(toxicity, toxin),
new Ailment(Toxicity.LETHAL, toxin));
} }
return toxic; static Toxic meat(FoodType type, Toxicity toxicity) {
String name = type.name().toLowerCase();
return register(name, UseAction.EAT, type,
Optional.empty(),
UTags.item(name)::contains,
Ailment.INNERT,
Ailment.of(toxicity));
} }
static Toxic register(FoodComponent food, FoodType type, UseAction action, Toxicity toxicity, Toxin toxin, Item target) { static Toxic register(String name, UseAction action, FoodType type, Optional<FoodComponent> component, Predicate<Item> tag, Ailment lower, Ailment upper) {
toxin = Toxin.FOOD.and(toxin); return Registry.register(REGISTRY, name, new Toxic(action, type, component, tag, lower, upper));
return register(target, food, type, action, new Ailment(toxicity, toxin), new Ailment(Toxicity.LETHAL, toxin));
}
static Toxic register(Item target, FoodComponent food, FoodType type, UseAction action, Ailment lowerBound, Ailment upperbound) {
Toxic toxic = new Toxic(action, type, lowerBound, upperbound);
ToxicHolder holder = (ToxicHolder)target;
holder.setToxic(toxic);
holder.setFood(Objects.requireNonNull(food, target.getTranslationKey() + " food"));
return toxic;
}
static Toxic register(Toxic toxic, Item... items) {
for (Item i : items) {
((ToxicHolder)i).setToxic(toxic);
}
return toxic;
} }
} }

View file

@ -2,6 +2,8 @@ package com.minelittlepony.unicopia.mixin;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -10,6 +12,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.item.toxin.Toxic; import com.minelittlepony.unicopia.item.toxin.Toxic;
import com.minelittlepony.unicopia.item.toxin.ToxicHolder; import com.minelittlepony.unicopia.item.toxin.ToxicHolder;
import com.minelittlepony.unicopia.item.toxin.Toxics;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.item.FoodComponent; import net.minecraft.item.FoodComponent;
@ -20,19 +23,28 @@ import net.minecraft.world.World;
@Mixin(Item.class) @Mixin(Item.class)
abstract class MixinItem implements ToxicHolder { abstract class MixinItem implements ToxicHolder {
private Optional<Toxic> toxic = Optional.empty(); @Nullable
private FoodComponent originalFoodComponent;
@Override @Override
@Accessor("foodComponent") @Accessor("foodComponent")
public abstract void setFood(FoodComponent food); public abstract void setFood(FoodComponent food);
@Override
public void setToxic(Toxic toxic) {
this.toxic = Optional.of(toxic);
}
@Override @Override
public Optional<Toxic> getToxic() { public Optional<Toxic> getToxic() {
if (originalFoodComponent == null) {
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);
return t;
}).findFirst();
if (!toxic.isPresent() && ((Item)(Object)this).getFoodComponent() != null) {
return Optional.of(Toxics.EDIBLE);
}
return toxic; return toxic;
} }

View file

@ -0,0 +1,7 @@
{
"replace": false,
"values": [
"minecraft:cooked_cod",
"minecraft:cooked_salmon"
]
}

View file

@ -0,0 +1,10 @@
{
"replace": false,
"values": [
"minecraft:cooked_porkchop",
"minecraft:cooked_beef",
"minecraft:cooked_mutton",
"minecraft:cooked_rabbit",
"minecraft:cooked_chicken"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:oxey_daisy"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:poppy"
]
}

View file

@ -1,15 +1,11 @@
{ {
"replace": false, "replace": false,
"values": [ "values": [
"minecraft:grass",
"minecraft:dandelion",
"minecraft:blue_orchid", "minecraft:blue_orchid",
"minecraft:azure_bluet",
"minecraft:red_tulip", "minecraft:red_tulip",
"minecraft:orange_tulip", "minecraft:orange_tulip",
"minecraft:pink_tulip", "minecraft:pink_tulip",
"minecraft:cornflower", "minecraft:cornflower",
"minecraft:rose_bush",
"minecraft:peony", "minecraft:peony",
"minecraft:tall_grass" "minecraft:tall_grass"
] ]

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:grass"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:rose_bush"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:azure_bluet"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:dead_bush"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:large_fern"
]
}

View file

@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:fern"
]
}

View file

@ -0,0 +1,9 @@
{
"replace": false,
"values": [
"minecraft:pufferfish",
"minecraft:cod",
"minecraft:salmon",
"minecraft:tropical_fish"
]
}

View file

@ -0,0 +1,10 @@
{
"replace": false,
"values": [
"minecraft:porkchop",
"minecraft:beef",
"minecraft:mutton",
"minecraft:rabbit",
"minecraft:chicken"
]
}

View file

@ -1,10 +0,0 @@
{
"replace": false,
"values": [
"minecraft:fern",
"minecraft:dead_bush",
"minecraft:poppy",
"minecraft:oxeye_daisy",
"minecraft:large_fern"
]
}