mirror of
https://github.com/evennia/evennia.git
synced 2026-03-24 16:56:32 +01:00
Merge pull request #2891 from Henddher/issue_2703
fix: Add NonExistentRecipe for gracefully craft an unknown recipe.
This commit is contained in:
commit
b7d96fa0b2
2 changed files with 29 additions and 3 deletions
|
|
@ -119,6 +119,7 @@ a full example of the components for creating a sword from base components.
|
|||
|
||||
"""
|
||||
|
||||
import functools
|
||||
from copy import copy
|
||||
from evennia.utils.utils import iter_to_str, callables_from_module, inherits_from, make_iter
|
||||
from evennia.commands.cmdset import CmdSet
|
||||
|
|
@ -340,6 +341,21 @@ class CraftingRecipeBase:
|
|||
return craft_result
|
||||
|
||||
|
||||
class NonExistentRecipe(CraftingRecipeBase):
|
||||
"""A recipe that does not exist and never produces anything."""
|
||||
allow_craft = True
|
||||
allow_reuse = True
|
||||
|
||||
def __init__(self, crafter, *inputs, name="", **kwargs):
|
||||
super().__init__(crafter, *inputs, **kwargs)
|
||||
self.name = name
|
||||
|
||||
def pre_craft(self, **kwargs):
|
||||
msg = f"Unknown recipe '{self.name}'"
|
||||
self.msg(msg)
|
||||
raise CraftingError(msg)
|
||||
|
||||
|
||||
class CraftingRecipe(CraftingRecipeBase):
|
||||
"""
|
||||
The CraftRecipe implements the most common form of crafting: Combining (and
|
||||
|
|
@ -927,9 +943,12 @@ def craft(crafter, recipe_name, *inputs, raise_exception=False, **kwargs):
|
|||
RecipeClass = matches[0]
|
||||
|
||||
if not RecipeClass:
|
||||
raise KeyError(
|
||||
f"No recipe in settings.CRAFT_RECIPE_MODULES has a name matching {recipe_name}"
|
||||
)
|
||||
if raise_exception:
|
||||
raise KeyError(
|
||||
f"No recipe in settings.CRAFT_RECIPE_MODULES has a name matching {recipe_name}"
|
||||
)
|
||||
else:
|
||||
RecipeClass = functools.partial(NonExistentRecipe, name=recipe_name)
|
||||
recipe = RecipeClass(crafter, *inputs, **kwargs)
|
||||
return recipe.craft(raise_exception=raise_exception)
|
||||
|
||||
|
|
|
|||
|
|
@ -690,3 +690,10 @@ class TestCraftCommand(BaseEvenniaCommandTest):
|
|||
"testrecipe using tool1, tool2",
|
||||
_MockRecipe.error_consumable_missing_message.format(outputs="Result1", missing="cons1"),
|
||||
)
|
||||
|
||||
def test_craft__unknown_recipe__failure(self):
|
||||
self.call(
|
||||
crafting.CmdCraft(),
|
||||
"nonexistent from cons1, cons2, cons3 using tool1, tool2",
|
||||
"Unknown recipe 'nonexistent'",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue