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) {
if (!isUsable()) {
return type != FoodType.VEGAN;
return type != FoodType.FORAGE;
}
if (type.isMeat()) {
return this == BAT || this == CHANGELING || this == HUMAN;

View file

@ -1,5 +1,7 @@
package com.minelittlepony.unicopia;
import com.minelittlepony.unicopia.item.toxin.Toxics;
import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
@ -10,10 +12,6 @@ public interface UTags {
Tag<Item> APPLES = item("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<Block> FRAGILE = block("fragile");
@ -30,5 +28,7 @@ public interface UTags {
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() {
Toxics.bootstrap();
UEnchantments.bootstrap();
URecipes.bootstrap();
UItemGroups.bootstrap();

View file

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

View file

@ -1,5 +1,7 @@
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;
@ -10,6 +12,8 @@ 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.util.Hand;
@ -25,13 +29,27 @@ public class Toxic {
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.type = type;
this.component = component;
this.tag = tag;
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
public boolean matches(Item item) {
return tag.test(item);
}
public Optional<FoodComponent> getFoodComponent() {
return component;
}
public UseAction getUseAction(ItemStack stack) {
return action;
}

View file

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

View file

@ -1,62 +1,66 @@
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.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
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 {
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);
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);
Registry<Toxic> REGISTRY = Registries.createSimple(new Identifier("unicopia:toxic"));
Toxic GRASS = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Toxin.NAUSEA, Items.GRASS);
Toxic AZUER_BLUET = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Toxin.RADIOACTIVITY, Items.AZURE_BLUET);
Toxic ROSE_BUSH = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SAFE, Toxin.DAMAGE, Items.ROSE_BUSH);
Toxic FERN = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.STRENGTH, Items.FERN);
Toxic DEAD_BUSH = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.NAUSEA, Items.DEAD_BUSH);
Toxic OXEYE_DAISY = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.BLINDNESS, Items.OXEYE_DAISY);
Toxic LARGE_FERN = register(UFoodComponents.RANDOM_FOLIAGE, FoodType.VEGAN, UseAction.EAT, Toxicity.SEVERE, Toxin.DAMAGE, Items.LARGE_FERN);
Toxic EDIBLE = forage("edible", SAFE, FOOD);
Toxic RISKY = forage("risky", FAIR, FOOD);
Toxic DANGEROUS = forage("dangerous", SEVERE, FOOD);
Toxic NAUSEATING = forage("nauseating", SAFE, NAUSEA);
Toxic RADIOACTIVE = forage("radioactive", SAFE, RADIOACTIVITY);
Toxic PRICKLY = forage("prickly", SAFE, DAMAGE);
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 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 RAW_MEAT = meat(FoodType.RAW_MEAT, MILD);
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 COOKED_FISH = register(new Toxic(UseAction.EAT, FoodType.COOKED_FISH, Ailment.INNERT, Ailment.of(Toxicity.FAIR)), Items.COOKED_COD, Items.COOKED_SALMON);
Toxic RAW_FISH = meat(FoodType.RAW_FISH, FAIR);
Toxic COOKED_FISH = meat(FoodType.COOKED_FISH, FAIR);
static void bootstrap() {}
static Toxic register(FoodComponent food, FoodType type, UseAction action, Toxicity toxicity, Item... items) {
Toxic toxic = new Toxic(action, type, Ailment.of(toxicity), Ailment.of(Toxicity.LETHAL));
for (Item i : items) {
ToxicHolder holder = (ToxicHolder)i;
holder.setToxic(toxic);
holder.setFood(Objects.requireNonNull(food, i.getTranslationKey() + " food"));
static Toxic forage(String name, Toxicity toxicity, Toxin toxin) {
if (toxin != FOOD) {
toxin = FOOD.and(toxin);
}
name = "forage_" + name;
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) {
toxin = Toxin.FOOD.and(toxin);
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;
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));
}
}

View file

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

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,
"values": [
"minecraft:grass",
"minecraft:dandelion",
"minecraft:blue_orchid",
"minecraft:azure_bluet",
"minecraft:red_tulip",
"minecraft:orange_tulip",
"minecraft:pink_tulip",
"minecraft:cornflower",
"minecraft:rose_bush",
"minecraft:peony",
"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"
]
}