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

     1  package item
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/world"
     5  	"image/color"
     6  )
     7  
     8  // Helmet is a defensive item that may be worn in the head slot. It comes in several tiers, each with
     9  // different defence points and armour toughness.
    10  type Helmet struct {
    11  	// Tier is the tier of the armour.
    12  	Tier ArmourTier
    13  }
    14  
    15  // Use handles the using of a helmet to auto-equip it in an armour slot.
    16  func (h Helmet) Use(_ *world.World, _ User, ctx *UseContext) bool {
    17  	ctx.SwapHeldWithArmour(0)
    18  	return false
    19  }
    20  
    21  // MaxCount always returns 1.
    22  func (h Helmet) MaxCount() int {
    23  	return 1
    24  }
    25  
    26  // DefencePoints ...
    27  func (h Helmet) DefencePoints() float64 {
    28  	switch h.Tier.Name() {
    29  	case "leather":
    30  		return 1
    31  	case "golden", "chainmail", "iron":
    32  		return 2
    33  	case "diamond", "netherite":
    34  		return 3
    35  	}
    36  	panic("invalid helmet tier")
    37  }
    38  
    39  // KnockBackResistance ...
    40  func (h Helmet) KnockBackResistance() float64 {
    41  	return h.Tier.KnockBackResistance()
    42  }
    43  
    44  // Toughness ...
    45  func (h Helmet) Toughness() float64 {
    46  	return h.Tier.Toughness()
    47  }
    48  
    49  // EnchantmentValue ...
    50  func (h Helmet) EnchantmentValue() int {
    51  	return h.Tier.EnchantmentValue()
    52  }
    53  
    54  // DurabilityInfo ...
    55  func (h Helmet) DurabilityInfo() DurabilityInfo {
    56  	return DurabilityInfo{
    57  		MaxDurability: int(h.Tier.BaseDurability()),
    58  		BrokenItem:    simpleItem(Stack{}),
    59  	}
    60  }
    61  
    62  // SmeltInfo ...
    63  func (h Helmet) SmeltInfo() SmeltInfo {
    64  	switch h.Tier.(type) {
    65  	case ArmourTierIron, ArmourTierChain:
    66  		return newOreSmeltInfo(NewStack(IronNugget{}, 1), 0.1)
    67  	case ArmourTierGold:
    68  		return newOreSmeltInfo(NewStack(GoldNugget{}, 1), 0.1)
    69  	}
    70  	return SmeltInfo{}
    71  }
    72  
    73  // RepairableBy ...
    74  func (h Helmet) RepairableBy(i Stack) bool {
    75  	return armourTierRepairable(h.Tier)(i)
    76  }
    77  
    78  // Helmet ...
    79  func (h Helmet) Helmet() bool {
    80  	return true
    81  }
    82  
    83  // EncodeItem ...
    84  func (h Helmet) EncodeItem() (name string, meta int16) {
    85  	return "minecraft:" + h.Tier.Name() + "_helmet", 0
    86  }
    87  
    88  // DecodeNBT ...
    89  func (h Helmet) DecodeNBT(data map[string]any) any {
    90  	if t, ok := h.Tier.(ArmourTierLeather); ok {
    91  		if v, ok := data["customColor"].(int32); ok {
    92  			t.Colour = rgbaFromInt32(v)
    93  			h.Tier = t
    94  		}
    95  	}
    96  	return h
    97  }
    98  
    99  // EncodeNBT ...
   100  func (h Helmet) EncodeNBT() map[string]any {
   101  	if t, ok := h.Tier.(ArmourTierLeather); ok && t.Colour != (color.RGBA{}) {
   102  		return map[string]any{"customColor": int32FromRGBA(t.Colour)}
   103  	}
   104  	return nil
   105  }