Implement toxicity for ingredients (I may remove this in favour of flattening)

This commit is contained in:
Sollace 2020-05-02 00:38:37 +02:00
parent c2967cfe3c
commit 070aeaefe3
12 changed files with 151 additions and 102 deletions

View file

@ -35,6 +35,10 @@ public interface UTags {
Tag<Item> SCOOTALOO_SPIRIT = register("scootaloo_spirit"); Tag<Item> SCOOTALOO_SPIRIT = register("scootaloo_spirit");
Tag<Item> SWEETIE_BELLE_SPIRIT = register("sweetie_belle_spirit"); Tag<Item> SWEETIE_BELLE_SPIRIT = register("sweetie_belle_spirit");
Tag<Item> NON_TOXIC = register("non_toxic");
Tag<Item> FAIRLY_TOXIC = register("fairly_toxic");
Tag<Item> SEVERELY_TOXIC = register("severely_toxic");
static Tag<Item> register(String name) { static Tag<Item> register(String name) {
return TagRegistry.item(new Identifier("unicopia", name)); return TagRegistry.item(new Identifier("unicopia", name));
} }

View file

@ -7,7 +7,7 @@ import java.util.stream.Stream;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import com.google.common.collect.Streams; import com.google.common.collect.Lists;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -19,29 +19,20 @@ import net.minecraft.util.PacketByteBuf;
@Immutable @Immutable
public class Ingredient { public class Ingredient {
public static final Ingredient EMPTY = new Ingredient(Predicate.EMPTY, Predicate.EMPTY, Predicate.EMPTY, Predicate.EMPTY); public static final Ingredient EMPTY = new Ingredient(Predicate.EMPTY, Predicate.EMPTY, Predicate.EMPTY, Predicate.EMPTY, Predicate.EMPTY);
private final Predicate stack; private final List<Predicate> predicates;
private final Predicate tag;
private final Predicate spell;
private final Predicate enchantment;
private final Lazy<List<ItemStack>> matchingStacks; private final Lazy<List<ItemStack>> matchingStacks;
private final Lazy<net.minecraft.recipe.Ingredient> preview; private final Lazy<net.minecraft.recipe.Ingredient> preview;
Ingredient(Predicate stack, Predicate tag, Predicate spell, Predicate enchantment) { Ingredient(Predicate... predicates) {
this.stack = stack; this.predicates = Lists.newArrayList(predicates);
this.tag = tag;
this.spell = spell;
this.enchantment = enchantment;
this.matchingStacks = new Lazy<>(() -> { this.matchingStacks = new Lazy<>(() -> {
return Streams.concat( return this.predicates.stream()
stack.getMatchingStacks(), .flatMap(Predicate::getMatchingStacks)
tag.getMatchingStacks(), .filter(s -> matches(s, 1))
spell.getMatchingStacks(), .collect(Collectors.toList());
enchantment.getMatchingStacks()
).filter(s -> matches(s, 1))
.collect(Collectors.toList());
}); });
this.preview = new Lazy<>(() -> { this.preview = new Lazy<>(() -> {
return net.minecraft.recipe.Ingredient.ofStacks(getMatchingStacks().toArray(ItemStack[]::new)); return net.minecraft.recipe.Ingredient.ofStacks(getMatchingStacks().toArray(ItemStack[]::new));
@ -57,26 +48,19 @@ public class Ingredient {
} }
public ItemStack getStack(Random random) { public ItemStack getStack(Random random) {
ItemStack output = ItemStack.EMPTY.copy(); ItemStack[] output = new ItemStack[] { ItemStack.EMPTY.copy() };
stack.applyModifiers(output, random);
tag.applyModifiers(output, random); predicates.forEach(p -> output[0] = p.applyModifiers(output[0], random));
spell.applyModifiers(output, random);
enchantment.applyModifiers(output, random); return output[0];
return output;
} }
public boolean matches(ItemStack other, int materialMult) { public boolean matches(ItemStack other, int materialMult) {
return stack.matches(other, materialMult) return predicates.stream().allMatch(p -> p.matches(other, materialMult));
&& tag.matches(other, materialMult)
&& spell.matches(other, materialMult)
&& enchantment.matches(other, materialMult);
} }
public void write(PacketByteBuf buf) { public void write(PacketByteBuf buf) {
stack.write(buf); predicates.forEach(p -> p.write(buf));
tag.write(buf);
spell.write(buf);
enchantment.write(buf);
} }
public static Ingredient read(PacketByteBuf buf) { public static Ingredient read(PacketByteBuf buf) {
@ -84,7 +68,8 @@ public class Ingredient {
StackPredicate.read(buf), StackPredicate.read(buf),
TagPredicate.read(buf), TagPredicate.read(buf),
SpellPredicate.read(buf), SpellPredicate.read(buf),
EnchantmentPredicate.read(buf)); EnchantmentPredicate.read(buf),
ToxicityPredicate.read(buf));
} }
public static Ingredient one(JsonElement json) { public static Ingredient one(JsonElement json) {
@ -94,6 +79,7 @@ public class Ingredient {
ChoicePredicate.read(json.getAsJsonArray()), ChoicePredicate.read(json.getAsJsonArray()),
Predicate.EMPTY, Predicate.EMPTY,
Predicate.EMPTY, Predicate.EMPTY,
Predicate.EMPTY,
Predicate.EMPTY); Predicate.EMPTY);
} }
@ -102,7 +88,8 @@ public class Ingredient {
StackPredicate.read(obj), StackPredicate.read(obj),
TagPredicate.read(obj), TagPredicate.read(obj),
SpellPredicate.read(obj), SpellPredicate.read(obj),
EnchantmentPredicate.read(obj)); EnchantmentPredicate.read(obj),
ToxicityPredicate.read(obj));
} }
public static DefaultedList<Ingredient> many(JsonArray arr) { public static DefaultedList<Ingredient> many(JsonArray arr) {

View file

@ -0,0 +1,58 @@
package com.minelittlepony.unicopia.recipe.ingredient;
import java.util.Random;
import java.util.stream.Stream;
import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.toxin.Toxicity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.PacketByteBuf;
/**
* A predicate that tests for a specific spell on an input when matching.
* Appends that spell to the output when crafting.
*/
class ToxicityPredicate implements Ingredient.Predicate {
static Ingredient.Predicate read(PacketByteBuf buf) {
int ordinal = buf.readInt();
if (ordinal == 0) {
return EMPTY;
}
return new ToxicityPredicate(Toxicity.values()[ordinal - 1]);
}
static Ingredient.Predicate read(JsonObject json) {
if (!json.has("toxicity")) {
return EMPTY;
}
return new ToxicityPredicate(Toxicity.byName(json.get("toxicity").getAsString()));
}
private final Toxicity toxicity;
ToxicityPredicate(Toxicity toxicity) {
this.toxicity = toxicity;
}
@Override
public ItemStack applyModifiers(ItemStack output, Random random) {
return toxicity.ontoStack(output);
}
@Override
public boolean matches(ItemStack stack, int materialMult) {
return Toxicity.fromStack(stack) == toxicity;
}
@Override
public void write(PacketByteBuf buf) {
buf.writeInt(toxicity.ordinal() + 1);
}
@Override
public Stream<ItemStack> getMatchingStacks() {
return Stream.empty();
}
}

View file

@ -11,7 +11,7 @@ import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
@ -59,6 +59,11 @@ public enum Toxicity implements Toxin {
return text; return text;
} }
public ItemStack ontoStack(ItemStack stack) {
stack.getOrCreateTag().putString("toxicity", name());
return stack;
}
@Override @Override
public void afflict(PlayerEntity player, Toxicity toxicity, ItemStack stack) { public void afflict(PlayerEntity player, Toxicity toxicity, ItemStack stack) {
if (toxicWhenRaw()) { if (toxicWhenRaw()) {
@ -74,11 +79,15 @@ public enum Toxicity implements Toxin {
public static Toxicity fromStack(ItemStack stack) { public static Toxicity fromStack(ItemStack stack) {
if (stack.hasTag()) { if (stack.hasTag()) {
CompoundTag tag = stack.getSubTag("toxicity"); Tag tag = stack.getTag().get("toxicity");
if (tag != null) { if (tag != null) {
return REGISTRY.getOrDefault(tag.asString(), SAFE); return byName(tag.asString());
} }
} }
return SAFE; return SAFE;
} }
public static Toxicity byName(String name) {
return REGISTRY.getOrDefault(name.toUpperCase(), SAFE);
}
} }

View file

@ -1,6 +1,5 @@
{ {
"type": "minecraft:crafting_shaped", "type": "unicopia:crafting_shaped",
"pattern": [ "pattern": [
"A B", "A B",
" J ", " J ",
@ -20,5 +19,5 @@
{ "item": "minecraft:apple" } { "item": "minecraft:apple" }
] ]
}, },
"result": { "item": "unicopia:apple_cider" } "result": { "item": "unicopia:apple_cider", "toxicity": "fair" }
} }

View file

@ -1,5 +1,5 @@
{ {
"type": "minecraft:crafting_shaped", "type": "unicopia:crafting_shaped",
"pattern": [ "pattern": [
"FFF", "FFF",
"FFF", "FFF",
@ -10,20 +10,10 @@
{ "item": "minecraft:bowl" } { "item": "minecraft:bowl" }
], ],
"F": [ "F": [
{ "item": "minecraft:double_plant", "data": 0 }, { "tag": "#unicopia:non_toxic" },
{ "item": "minecraft:double_plant", "data": 1 }, { "tag": "#unicopia:fairly_toxic" },
{ "item": "minecraft:tallgrass", "data": 1 }, { "item": "unicopia:alfalfa_leaves" }
{ "item": "minecraft:double_plant", "data": 5 },
{ "item": "unicopia:alfalfa_leaves" },
{ "item": "minecraft:red_flower", "data": 1 },
{ "item": "minecraft:red_flower", "data": 2 },
{ "item": "minecraft:red_flower", "data": 3 },
{ "item": "minecraft:red_flower", "data": 4 },
{ "item": "minecraft:red_flower", "data": 5 },
{ "item": "minecraft:red_flower", "data": 6 },
{ "item": "minecraft:red_flower", "data": 7 },
{ "item": "minecraft:yellow_flower", "data": 0 }
] ]
}, },
"result": { "item": "unicopia:salad", "data": 2, "count": 1 } "result": { "item": "unicopia:salad", "toxicity": "fair" }
} }

View file

@ -1,5 +1,5 @@
{ {
"type": "minecraft:crafting_shaped", "type": "unicopia:crafting_shaped",
"pattern": [ "pattern": [
"FFF", "FFF",
"FFF", "FFF",
@ -13,24 +13,12 @@
{ "item": "minecraft:golden_apple", "data": 0 }, { "item": "minecraft:golden_apple", "data": 0 },
{ "item": "unicopia:zap_apple", "data": 0 }, { "item": "unicopia:zap_apple", "data": 0 },
{ "item": "unicopia:corrupted_gem" }, { "item": "unicopia:corrupted_gem" },
{ "item": "minecraft:double_plant", "data": 0 }, { "tag": "#unicopia:non_toxic" },
{ "item": "minecraft:double_plant", "data": 1 }, { "tag": "#unicopia:fairly_toxic" },
{ "item": "minecraft:tallgrass", "data": 1 }, { "tag": "#unicopia:severely_toxic" },
{ "item": "minecraft:tallgrass", "data": 2 },
{ "item": "minecraft:double_plant", "data": 4 },
{ "item": "minecraft:double_plant", "data": 5 },
{ "item": "unicopia:alfalfa_leaves" }, { "item": "unicopia:alfalfa_leaves" },
{ "item": "minecraft:red_flower", "data": 0 }, { "item": "unicopia:wheat_worms" }
{ "item": "minecraft:red_flower", "data": 1 },
{ "item": "minecraft:red_flower", "data": 2 },
{ "item": "minecraft:red_flower", "data": 3 },
{ "item": "minecraft:red_flower", "data": 4 },
{ "item": "minecraft:red_flower", "data": 5 },
{ "item": "minecraft:red_flower", "data": 6 },
{ "item": "minecraft:red_flower", "data": 7 },
{ "item": "minecraft:red_flower", "data": 8 },
{ "item": "minecraft:yellow_flower", "data": 0 }
] ]
}, },
"result": { "item": "unicopia:salad", "data": 4, "count": 1 } "result": { "item": "unicopia:salad", "toxicity": "lethal" }
} }

View file

@ -1,5 +1,5 @@
{ {
"type": "minecraft:crafting_shaped", "type": "unicopia:crafting_shaped",
"pattern": [ "pattern": [
"FFF", "FFF",
"FFF", "FFF",
@ -10,15 +10,9 @@
{ "item": "minecraft:bowl" } { "item": "minecraft:bowl" }
], ],
"F": [ "F": [
{ "item": "minecraft:double_plant", "data": 0 }, { "tag": "#unicopia:non_toxic" },
{ "item": "minecraft:tallgrass", "data": 1 }, { "item": "unicopia:alfalfa_leaves" }
{ "item": "unicopia:alfalfa_leaves" },
{ "item": "minecraft:red_flower", "data": 3 },
{ "item": "minecraft:red_flower", "data": 4 },
{ "item": "minecraft:red_flower", "data": 5 },
{ "item": "minecraft:red_flower", "data": 7 },
{ "item": "minecraft:yellow_flower", "data": 0 }
] ]
}, },
"result": { "item": "unicopia:salad", "data": 0, "count": 1 } "result": { "item": "unicopia:salad", "toxicity": "safe" }
} }

View file

@ -1,5 +1,5 @@
{ {
"type": "minecraft:crafting_shaped", "type": "unicopia:crafting_shaped",
"pattern": [ "pattern": [
"FFF", "FFF",
"FFF", "FFF",
@ -10,25 +10,12 @@
{ "item": "minecraft:bowl" } { "item": "minecraft:bowl" }
], ],
"F": [ "F": [
{ "item": "minecraft:double_plant", "data": 0 }, { "tag": "#unicopia:non_toxic" },
{ "item": "minecraft:double_plant", "data": 1 }, { "tag": "#unicopia:fairly_toxic" },
{ "item": "minecraft:tallgrass", "data": 1 }, { "tag": "#unicopia:severely_toxic" },
{ "item": "minecraft:double_plant", "data": 3 },
{ "item": "minecraft:double_plant", "data": 4 },
{ "item": "minecraft:double_plant", "data": 5 },
{ "item": "unicopia:alfalfa_leaves" }, { "item": "unicopia:alfalfa_leaves" },
{ "item": "unicopia:wheat_worms" }, { "item": "unicopia:wheat_worms" }
{ "item": "minecraft:red_flower", "data": 0 },
{ "item": "minecraft:red_flower", "data": 1 },
{ "item": "minecraft:red_flower", "data": 2 },
{ "item": "minecraft:red_flower", "data": 3 },
{ "item": "minecraft:red_flower", "data": 4 },
{ "item": "minecraft:red_flower", "data": 5 },
{ "item": "minecraft:red_flower", "data": 6 },
{ "item": "minecraft:red_flower", "data": 7 },
{ "item": "minecraft:red_flower", "data": 8 },
{ "item": "minecraft:yellow_flower", "data": 0 }
] ]
}, },
"result": { "item": "unicopia:salad", "data": 3, "count": 1 } "result": { "item": "unicopia:salad", "toxicity": "severe" }
} }

View file

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

View file

@ -0,0 +1,16 @@
{
"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",
"tall_grass"
]
}

View file

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