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

     1  package recipe
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/item"
     5  	"github.com/df-mc/dragonfly/server/world"
     6  	"math"
     7  )
     8  
     9  // inputItems is a type representing a list of input items, with helper functions to convert them to
    10  type inputItems []struct {
    11  	// Name is the name of the item being inputted.
    12  	Name string `nbt:"name"`
    13  	// Meta is the meta of the item. This can change the item almost completely, or act as durability.
    14  	Meta int32 `nbt:"meta"`
    15  	// Count is the amount of the item.
    16  	Count int32 `nbt:"count"`
    17  	// State is included if the output is a block. If it's not included, the meta can be discarded and the output item can be incorrect.
    18  	State struct {
    19  		Name       string                 `nbt:"name"`
    20  		Properties map[string]interface{} `nbt:"states"`
    21  		Version    int32                  `nbt:"version"`
    22  	} `nbt:"block"`
    23  }
    24  
    25  // Stacks converts input items to item stacks.
    26  func (d inputItems) Stacks() ([]item.Stack, bool) {
    27  	s := make([]item.Stack, 0, len(d))
    28  	for _, i := range d {
    29  		it, ok := world.ItemByName(i.Name, int16(i.Meta))
    30  		if !ok {
    31  			return nil, false
    32  		}
    33  		if b, ok := world.BlockByName(i.State.Name, i.State.Properties); ok {
    34  			if it, ok = b.(world.Item); !ok {
    35  				return nil, false
    36  			}
    37  		}
    38  		st := item.NewStack(it, int(i.Count))
    39  		if i.Meta == math.MaxInt16 {
    40  			st = st.WithValue("variants", true)
    41  		}
    42  		s = append(s, st)
    43  	}
    44  	return s, true
    45  }
    46  
    47  // outputItems is an array of output items.
    48  type outputItems []struct {
    49  	// Name is the name of the item being output.
    50  	Name string `nbt:"name"`
    51  	// Meta is the meta of the item. This can change the item almost completely, or act as durability.
    52  	Meta int32 `nbt:"meta"`
    53  	// Count is the amount of the item.
    54  	Count int16 `nbt:"count"`
    55  	// State is included if the output is a block. If it's not included, the meta can be discarded and the output item can be incorrect.
    56  	State struct {
    57  		Name       string                 `nbt:"name"`
    58  		Properties map[string]interface{} `nbt:"states"`
    59  		Version    int32                  `nbt:"version"`
    60  	} `nbt:"block"`
    61  	// NBTData contains extra NBTData which may modify the item in other, more discreet ways.
    62  	NBTData map[string]interface{} `nbt:"data"`
    63  }
    64  
    65  // Stacks converts output items to item stacks.
    66  func (d outputItems) Stacks() ([]item.Stack, bool) {
    67  	s := make([]item.Stack, 0, len(d))
    68  	for _, o := range d {
    69  		it, ok := world.ItemByName(o.Name, int16(o.Meta))
    70  		if !ok {
    71  			return nil, false
    72  		}
    73  		if b, ok := world.BlockByName(o.State.Name, o.State.Properties); ok {
    74  			if it, ok = b.(world.Item); !ok {
    75  				return nil, false
    76  			}
    77  		}
    78  		if n, ok := it.(world.NBTer); ok {
    79  			it = n.DecodeNBT(o.NBTData).(world.Item)
    80  		}
    81  		s = append(s, item.NewStack(it, int(o.Count)))
    82  	}
    83  	return s, true
    84  }