github.com/df-mc/dragonfly@v0.9.13/server/item/recipe/recipe.go (about)

     1  package recipe
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/item"
     5  )
     6  
     7  // Recipe is implemented by all recipe types.
     8  type Recipe interface {
     9  	// Input returns the items required to craft the recipe.
    10  	Input() []item.Stack
    11  	// Output returns the items that are produced when the recipe is crafted.
    12  	Output() []item.Stack
    13  	// Block returns the block that is used to craft the recipe.
    14  	Block() string
    15  	// Priority returns the priority of the recipe. Recipes with lower priority are preferred compared to recipes with
    16  	// higher priority.
    17  	Priority() uint32
    18  }
    19  
    20  // Shapeless is a recipe that has no particular shape.
    21  type Shapeless struct {
    22  	recipe
    23  }
    24  
    25  // NewShapeless creates a new shapeless recipe and returns it. The recipe can only be crafted on the block passed in the
    26  // parameters. If the block given a crafting table, the recipe can also be crafted in the 2x2 crafting grid in the
    27  // player's inventory.
    28  func NewShapeless(input []item.Stack, output item.Stack, block string) Shapeless {
    29  	return Shapeless{recipe: recipe{
    30  		input:  input,
    31  		output: []item.Stack{output},
    32  		block:  block,
    33  	}}
    34  }
    35  
    36  // Smithing represents a recipe only craftable on a smithing table.
    37  type Smithing struct {
    38  	recipe
    39  }
    40  
    41  // NewSmithing creates a new smithing recipe and returns it. The recipe can only be crafted on the block passed in the
    42  // parameters. If the block given a crafting table, the recipe can also be crafted in the 2x2 crafting grid in the
    43  // player's inventory.
    44  func NewSmithing(base, addition, template, output item.Stack, block string) Smithing {
    45  	return Smithing{recipe: recipe{
    46  		input:  []item.Stack{base, addition, template},
    47  		output: []item.Stack{output},
    48  		block:  block,
    49  	}}
    50  }
    51  
    52  // Shaped is a recipe that has a specific shape that must be used to craft the output of the recipe.
    53  type Shaped struct {
    54  	recipe
    55  	// shape contains the width and height of the shaped recipe.
    56  	shape Shape
    57  }
    58  
    59  // NewShaped creates a new shaped recipe and returns it. The recipe can only be crafted on the block passed in the
    60  // parameters. If the block given a crafting table, the recipe can also be crafted in the 2x2 crafting grid in the
    61  // player's inventory. If nil is passed, the block will be autofilled as a crafting table. The inputs must always match
    62  // the width*height of the shape.
    63  func NewShaped(input []item.Stack, output item.Stack, shape Shape, block string) Shaped {
    64  	return Shaped{
    65  		shape: shape,
    66  		recipe: recipe{
    67  			input:  input,
    68  			output: []item.Stack{output},
    69  			block:  block,
    70  		},
    71  	}
    72  }
    73  
    74  // Shape returns the shape of the recipe.
    75  func (r Shaped) Shape() Shape {
    76  	return r.shape
    77  }
    78  
    79  // recipe implements the Recipe interface. Structs in this package may embed it to gets its functionality
    80  // out of the box.
    81  type recipe struct {
    82  	// input is a list of items that serve as the input of the shaped recipe. These items are the items
    83  	// required to craft the output. The amount of input items must be exactly equal to Width * Height.
    84  	input []item.Stack
    85  	// output contains items that are created as a result of crafting the recipe.
    86  	output []item.Stack
    87  	// block is the block that is used to craft the recipe.
    88  	block string
    89  	// priority is the priority of the recipe versus others.
    90  	priority uint32
    91  }
    92  
    93  // Input ...
    94  func (r recipe) Input() []item.Stack {
    95  	return r.input
    96  }
    97  
    98  // Output ...
    99  func (r recipe) Output() []item.Stack {
   100  	return r.output
   101  }
   102  
   103  // Block ...
   104  func (r recipe) Block() string {
   105  	return r.block
   106  }
   107  
   108  // Priority ...
   109  func (r recipe) Priority() uint32 {
   110  	return r.priority
   111  }