def use(self, item: Optional[Union[Block, Loot, Item]] = None) -> List[ItemStack]: """Use the tool on a given Item. Args: item: Item to use the tool on. Returns: List of ItemStack gathered by using the tool on the item. """ if self._durability > 0 and isinstance(item, Block): stack_size = min(self.durability, 1 + int(self.speed / item.hardness)) self._durability -= stack_size findings = [ ItemStack(item_dropped, stack_size) for item_dropped in item.items_dropped ] return findings if self._durability > 0 and isinstance(item, Loot): self._durability -= 1 return [ItemStack(item, item.stack_size)] return []
def recipe_wooden_pickaxe(plank, stick, wooden_pickaxe): return Recipe( recipe_id=3, inputs=[ItemStack(plank, 3), ItemStack(stick, 2)], outputs=[ItemStack(wooden_pickaxe)], needed_properties={"has_crafting": True}, added_properties=None, )
def test_inventory_add_stacks(self, items): """should be able to add a list of ItemStack.""" inv = Inventory(items) stacks = [ItemStack(items[0], 15), ItemStack(items[2], 33)] inv.add_stacks(stacks) check.is_true(np.all(inv.content == [15, 0, 33]))
def test_inventory_remove_stacks(self, items): """should be able to remove a list of ItemStack.""" inv = Inventory(items) inv.content = np.array([15, 0, 33], dtype=np.int32) stacks = [ItemStack(items[0], 8), ItemStack(items[2], 21)] inv.remove_stacks(stacks) check.is_true(np.all(inv.content == [15 - 8, 0, 33 - 21]))
def recipe_crafting_table(plank): return Recipe( recipe_id=3, inputs=[ItemStack(plank, 4)], outputs=[], needed_properties=None, added_properties={"has_crafting": True}, )
def test_recipe_craft_inv(forest, all_items, plank, stick, recipe_wooden_pickaxe): inv = Inventory(all_items) inv.add_stacks([ItemStack(stick, 10), ItemStack(plank, 7)]) forest.properties["has_crafting"] = True success = recipe_wooden_pickaxe.craft(inv, forest) if not success or not np.all(inv.content == [0, 4, 0, 8, 1, 0]): raise ValueError("First craft failed unexpectedly") success = recipe_wooden_pickaxe.craft(inv, forest) if not success or not np.all(inv.content == [0, 1, 0, 6, 2, 0]): raise ValueError("Second craft failed unexpectedly") success = recipe_wooden_pickaxe.craft(inv, forest) if success or not np.all(inv.content == [0, 1, 0, 6, 2, 0]): raise ValueError("Third craft succeded unexpectedly")
def search_for(self, item: Item, tool: Tool = None) -> List[ItemStack]: """Searches for the given item using a tool Args: item: The item to look for. tool: The tool to use. Return: The found item stacks. """ if item in self.items: required_tools = item.required_tools # If no tool is usable, just gather item if required_tools is None: return [ItemStack(item)] # If a tool is needed, gather items relative to used tool if tool is not None and tool in required_tools: return tool.use(item) # If no tool is needed anyway, just gather item if None in required_tools: return [ItemStack(item)] return []
def test_recipe_craft_zone(forest, all_items, plank, recipe_crafting_table): inv = Inventory(all_items) inv.add_stacks([ItemStack(plank, 9)]) assert "has_crafting" not in forest.properties success = recipe_crafting_table.craft(inv, forest) print(inv, success, forest.properties["has_crafting"]) if not success or not forest.properties["has_crafting"]: raise ValueError("First craft failed unexpectedly") success = recipe_crafting_table.craft(inv, forest) print(inv, success, forest.properties["has_crafting"]) if not success or not forest.properties["has_crafting"]: raise ValueError("Second craft failed unexpectedly") success = recipe_crafting_table.craft(inv, forest) print(inv, success, forest.properties["has_crafting"]) if success: raise ValueError("Third craft succeded unexpectedly")
def search_for(self, item: Item, tool: McTool) -> int: n_items_found = super().search_for(item, tool) if tool is not None and tool.is_broken: self.inventory.remove_stacks([ItemStack(tool)]) tool.reset() return n_items_found
def test_init(self): """should instanciate correctly.""" stack = ItemStack(self.item, 8) check.equal(stack.item_id, self.item.item_id) check.equal(stack.size, 8)
from crafting.world.zones import Zone from crafting.player.inventory import Inventory from crafting.player.player import Player WOODEN_PICKAXE = Tool(18, "wooden_pickaxe") AIR = Tool(0, "air") DIRT = Item(3, "dirt", required_tools=[AIR]) WOOD = Item(17, "wood", required_tools=[AIR]) STICK = Item(280, "stick") WOOD_PLANK = Item(5, "plank") STONE = Item(1, "stone", required_tools=[WOODEN_PICKAXE]) R_WOOD_PLANK = Recipe( recipe_id=5, inputs=[ItemStack(WOOD, 1)], outputs=[ItemStack(WOOD_PLANK, 4)], needed_properties=None, added_properties=None, ) R_STICK = Recipe( recipe_id=8, inputs=[ItemStack(WOOD_PLANK, 2)], outputs=[ItemStack(STICK, 4)], needed_properties=None, added_properties=None, ) R_CRAFTING_TABLE = Recipe( recipe_id=28,
def _build_recipes( self, items: List[Item], findables: List[Item], n_inputs_per_craft: List[float], items_per_tool: Dict[int, List[Item]], ) -> List[Recipe]: """Build random recipes to make every item accessible. Args: items: List of items. findables: List of findable items. n_inputs_per_craft: List of probabilities of having x+1 inputs where x is the index. items_per_tool: Dictionary mapping tool item_id to all findable items that requires it. Returns: List of random recipes. """ recipes = [] accessible_items = set(findable for findable in findables if findable.required_tools is None) unaccessible_items = [item for item in items if item not in findables] self.np_random.shuffle(unaccessible_items) while len(accessible_items) < len(items): new_accessible_item = unaccessible_items.pop() new_is_tool = isinstance(new_accessible_item, Tool) # Don't build recipes from tools or unaccesible items accessible_notool_items = [ item for item in items if item in accessible_items and not isinstance(item, Tool) ] outputs = [ItemStack(new_accessible_item)] # Chooses randomly the number of input items (>=1) n_inputs_probs = np.array(n_inputs_per_craft) / np.sum( n_inputs_per_craft) n_inputs = 1 + self.np_random.choice(range(len(n_inputs_probs)), p=n_inputs_probs) n_inputs = min(n_inputs, len(accessible_notool_items)) # Chooses randomly accessible items and build ItemStacks of size 1 (default). input_items = list( self.np_random.choice(accessible_notool_items, size=n_inputs, replace=False)) inputs = [ItemStack(item) for item in input_items] # Build recipe new_recipe = Recipe(len(recipes), inputs=inputs, outputs=outputs) recipes.append(new_recipe) # If new accessible item is a tool, # add findables that can be gathered with it in accessible items if new_is_tool: for new_accessible_item_by_tool in items_per_tool[ new_accessible_item.item_id]: accessible_items.add(new_accessible_item_by_tool) accessible_items.add(new_accessible_item) return recipes
""" Minecraft Recipes All used Minecraft recipies. """ from crafting.world.items import ItemStack from crafting.world.recipes import Recipe from crafting.examples.minecraft.items import * from crafting.examples.minecraft.tools import * # Hand-Crafting items #: Recipe of WOOD_PLANK R_WOOD_PLANK = Recipe(5, inputs=[ItemStack(WOOD, 1)], outputs=[ItemStack(WOOD_PLANK, 4)]) #: Recipe of STICK R_STICK = Recipe(280, inputs=[ItemStack(WOOD_PLANK, 2)], outputs=[ItemStack(STICK, 4)]) R_HAND = [R_WOOD_PLANK, R_STICK] # Zone modifiers #: Recipe of CRAFTING_TABLE (enable 'has_crafting' zone property) R_CRAFTING_TABLE = Recipe( 58, inputs=[ItemStack(WOOD_PLANK, 4)], added_properties={"has_crafting": True}, ) #: Recipe of FURNACE (enable 'has_furnace' zone property)