Mrbysco / Spoiled

Spoiling the end of your food supply since 2020
https://www.curseforge.com/minecraft/mc-mods/spoiled
MIT License
7 stars 5 forks source link

Add Priority feature to resolve cases with multiple spoil recipes #42

Closed blueridger closed 1 week ago

blueridger commented 8 months ago

Summary

There compatibility tags such as forge:crops and it would be very useful to be able to set a default SpoilRecipe for a category like that, while also being able to override the category with a higher-priority recipe (for example make potatoes spoil slower than other crops).

Creating two recipes like this is already possible, however I have noticed that the order that runtime stores and returns the recipes is arbitrary and not affected by ordering in scripts, so it is likely that the category recipe will always be returned even when there is a lower-level recipe that you would prefer to be used.

This PR adds an optional priority attribute to the SpoilRecipe, and changes the lookup to retrieve all relevant recipes and and select the highest-priority recipe.

Test plan

After creating a new world, under the datapacks folder create test/pack.meta

{
  "pack": {
    "pack_format": 15,
    "description": "Tutorial Data Pack"
  }
}

then create test/data/testpack/recipes/test_recipe.json

{
  "type": "spoiled:spoil_recipe",
  "ingredient": {
    "item": "minecraft:bread"
  },
  "result": {
    "item": "minecraft:stone"
  },
  "spoilTime": 1,
  "priority": 1
}

then in the scripts folder create test_script.zs

import mods.spoiled.SpoilingManager;

var mngr = <recipetype:spoiled:spoil_recipe>;

mngr.addSpoiling("minecraft_bread", <item:minecraft:bread>, <item:minecraft:gold_ore>, 2);
mngr.addSpoiling("minecraft_breadlow", <item:minecraft:bread>, <item:minecraft:iron_ore>, 2, 2);

Then, play with the priority values and verify that the highest-priority recipe is always used.