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

     1  package item
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/world"
     6  	"github.com/df-mc/dragonfly/server/world/sound"
     7  	"github.com/go-gl/mathgl/mgl64"
     8  	"time"
     9  )
    10  
    11  // Shovel is a tool generally used for mining ground-like blocks, such as sand, gravel and dirt. Additionally,
    12  // shovels may be used to turn grass into dirt paths.
    13  type Shovel struct {
    14  	// Tier is the tier of the shovel.
    15  	Tier ToolTier
    16  }
    17  
    18  // UseOnBlock handles the creation of dirt path blocks from dirt or grass blocks.
    19  func (s Shovel) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, _ User, ctx *UseContext) bool {
    20  	if b, ok := w.Block(pos).(shovellable); ok {
    21  		if res, ok := b.Shovel(); ok {
    22  			if face == cube.FaceDown {
    23  				// Dirt paths are not created when the bottom face is clicked.
    24  				return false
    25  			}
    26  			if w.Block(pos.Side(cube.FaceUp)) != air() {
    27  				// Dirt paths can only be created if air is above the grass block.
    28  				return false
    29  			}
    30  			w.SetBlock(pos, res, nil)
    31  			w.PlaySound(pos.Vec3(), sound.ItemUseOn{Block: res})
    32  
    33  			ctx.DamageItem(1)
    34  			return true
    35  		}
    36  	}
    37  	return false
    38  }
    39  
    40  // shovellable represents a block that can be changed by using a shovel on it.
    41  type shovellable interface {
    42  	// Shovel returns a block that results from using a shovel on it, or false if it could not be changed using
    43  	// a shovel.
    44  	Shovel() (world.Block, bool)
    45  }
    46  
    47  // MaxCount always returns 1.
    48  func (s Shovel) MaxCount() int {
    49  	return 1
    50  }
    51  
    52  // AttackDamage returns the attack damage to the shovel.
    53  func (s Shovel) AttackDamage() float64 {
    54  	return s.Tier.BaseAttackDamage
    55  }
    56  
    57  // ToolType returns the tool type for shovels.
    58  func (s Shovel) ToolType() ToolType {
    59  	return TypeShovel
    60  }
    61  
    62  // HarvestLevel ...
    63  func (s Shovel) HarvestLevel() int {
    64  	return s.Tier.HarvestLevel
    65  }
    66  
    67  // BaseMiningEfficiency ...
    68  func (s Shovel) BaseMiningEfficiency(world.Block) float64 {
    69  	return s.Tier.BaseMiningEfficiency
    70  }
    71  
    72  // EnchantmentValue ...
    73  func (s Shovel) EnchantmentValue() int {
    74  	return s.Tier.EnchantmentValue
    75  }
    76  
    77  // DurabilityInfo ...
    78  func (s Shovel) DurabilityInfo() DurabilityInfo {
    79  	return DurabilityInfo{
    80  		MaxDurability:    s.Tier.Durability,
    81  		BrokenItem:       simpleItem(Stack{}),
    82  		AttackDurability: 2,
    83  		BreakDurability:  1,
    84  	}
    85  }
    86  
    87  // SmeltInfo ...
    88  func (s Shovel) SmeltInfo() SmeltInfo {
    89  	switch s.Tier {
    90  	case ToolTierIron:
    91  		return newOreSmeltInfo(NewStack(IronNugget{}, 1), 0.1)
    92  	case ToolTierGold:
    93  		return newOreSmeltInfo(NewStack(GoldNugget{}, 1), 0.1)
    94  	}
    95  	return SmeltInfo{}
    96  }
    97  
    98  // FuelInfo ...
    99  func (s Shovel) FuelInfo() FuelInfo {
   100  	if s.Tier == ToolTierWood {
   101  		return newFuelInfo(time.Second * 10)
   102  	}
   103  	return FuelInfo{}
   104  }
   105  
   106  // RepairableBy ...
   107  func (s Shovel) RepairableBy(i Stack) bool {
   108  	return toolTierRepairable(s.Tier)(i)
   109  }
   110  
   111  // EncodeItem ...
   112  func (s Shovel) EncodeItem() (name string, meta int16) {
   113  	return "minecraft:" + s.Tier.Name + "_shovel", 0
   114  }