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

     1  package recipe
     2  
     3  import (
     4  	_ "embed"
     5  	// Ensure all blocks and items are registered before trying to load vanilla recipes.
     6  	_ "github.com/df-mc/dragonfly/server/block"
     7  	_ "github.com/df-mc/dragonfly/server/item"
     8  	"github.com/sandertv/gophertunnel/minecraft/nbt"
     9  )
    10  
    11  var (
    12  	//go:embed crafting_data.nbt
    13  	vanillaCraftingData []byte
    14  	//go:embed smithing_data.nbt
    15  	vanillaSmithingData []byte
    16  	//go:embed stonecutter_data.nbt
    17  	vanillaStonecutterData []byte
    18  )
    19  
    20  // shapedRecipe is a recipe that must be crafted in a specific shape.
    21  type shapedRecipe struct {
    22  	Input    inputItems  `nbt:"input"`
    23  	Output   outputItems `nbt:"output"`
    24  	Block    string      `nbt:"block"`
    25  	Width    int32       `nbt:"width"`
    26  	Height   int32       `nbt:"height"`
    27  	Priority int32       `nbt:"priority"`
    28  }
    29  
    30  // shapelessRecipe is a recipe that may be crafted without a strict shape.
    31  type shapelessRecipe struct {
    32  	Input    inputItems  `nbt:"input"`
    33  	Output   outputItems `nbt:"output"`
    34  	Block    string      `nbt:"block"`
    35  	Priority int32       `nbt:"priority"`
    36  }
    37  
    38  func init() {
    39  	var craftingRecipes struct {
    40  		Shaped    []shapedRecipe    `nbt:"shaped"`
    41  		Shapeless []shapelessRecipe `nbt:"shapeless"`
    42  	}
    43  	if err := nbt.Unmarshal(vanillaCraftingData, &craftingRecipes); err != nil {
    44  		panic(err)
    45  	}
    46  
    47  	var stonecutterRecipes []shapelessRecipe
    48  	if err := nbt.Unmarshal(vanillaStonecutterData, &stonecutterRecipes); err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	for _, s := range append(craftingRecipes.Shapeless, stonecutterRecipes...) {
    53  		input, ok := s.Input.Stacks()
    54  		output, okTwo := s.Output.Stacks()
    55  		if !ok || !okTwo {
    56  			// This can be expected to happen, as some recipes contain blocks or items that aren't currently implemented.
    57  			continue
    58  		}
    59  		Register(Shapeless{recipe{
    60  			input:    input,
    61  			output:   output,
    62  			block:    s.Block,
    63  			priority: uint32(s.Priority),
    64  		}})
    65  	}
    66  
    67  	for _, s := range craftingRecipes.Shaped {
    68  		input, ok := s.Input.Stacks()
    69  		output, okTwo := s.Output.Stacks()
    70  		if !ok || !okTwo {
    71  			// This can be expected to happen - refer to the comment above.
    72  			continue
    73  		}
    74  		Register(Shaped{
    75  			shape: Shape{int(s.Width), int(s.Height)},
    76  			recipe: recipe{
    77  				input:    input,
    78  				output:   output,
    79  				block:    s.Block,
    80  				priority: uint32(s.Priority),
    81  			},
    82  		})
    83  	}
    84  
    85  	var smithingRecipes []shapelessRecipe
    86  	if err := nbt.Unmarshal(vanillaSmithingData, &smithingRecipes); err != nil {
    87  		panic(err)
    88  	}
    89  
    90  	for _, s := range smithingRecipes {
    91  		input, ok := s.Input.Stacks()
    92  		output, okTwo := s.Output.Stacks()
    93  		if !ok || !okTwo {
    94  			// This can be expected to happen - refer to the comment above.
    95  			continue
    96  		}
    97  		Register(Smithing{recipe{
    98  			input:    input,
    99  			output:   output,
   100  			block:    s.Block,
   101  			priority: uint32(s.Priority),
   102  		}})
   103  	}
   104  }