github.com/df-mc/dragonfly@v0.9.13/server/item/flint_and_steel.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  	"math/rand"
     9  	"time"
    10  )
    11  
    12  // FlintAndSteel is an item used to light blocks on fire.
    13  type FlintAndSteel struct{}
    14  
    15  // MaxCount ...
    16  func (f FlintAndSteel) MaxCount() int {
    17  	return 1
    18  }
    19  
    20  // DurabilityInfo ...
    21  func (f FlintAndSteel) DurabilityInfo() DurabilityInfo {
    22  	return DurabilityInfo{
    23  		MaxDurability: 65,
    24  		BrokenItem:    simpleItem(Stack{}),
    25  	}
    26  }
    27  
    28  // ignitable represents a block that can be lit by a fire emitter, such as flint and steel.
    29  type ignitable interface {
    30  	// Ignite is called when the block is lit by flint and steel.
    31  	Ignite(pos cube.Pos, w *world.World) bool
    32  }
    33  
    34  // UseOnBlock ...
    35  func (f FlintAndSteel) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, _ User, ctx *UseContext) bool {
    36  	ctx.DamageItem(1)
    37  	if l, ok := w.Block(pos).(ignitable); ok && l.Ignite(pos, w) {
    38  		return true
    39  	} else if s := pos.Side(face); w.Block(s) == air() {
    40  		w.PlaySound(s.Vec3Centre(), sound.Ignite{})
    41  		w.SetBlock(s, fire(), nil)
    42  		w.ScheduleBlockUpdate(s, time.Duration(30+rand.Intn(10))*time.Second/20)
    43  		return true
    44  	}
    45  	return false
    46  }
    47  
    48  // EncodeItem ...
    49  func (f FlintAndSteel) EncodeItem() (name string, meta int16) {
    50  	return "minecraft:flint_and_steel", 0
    51  }
    52  
    53  // air returns an air block.
    54  func air() world.Block {
    55  	a, ok := world.BlockByName("minecraft:air", nil)
    56  	if !ok {
    57  		panic("could not find air block")
    58  	}
    59  	return a
    60  }
    61  
    62  // fire returns a fire block.
    63  func fire() world.Block {
    64  	f, ok := world.BlockByName("minecraft:fire", map[string]any{"age": int32(0)})
    65  	if !ok {
    66  		panic("could not find fire block")
    67  	}
    68  	return f
    69  }