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

     1  package block
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/block/model"
     6  	"github.com/df-mc/dragonfly/server/item"
     7  	"github.com/df-mc/dragonfly/server/world"
     8  	"github.com/df-mc/dragonfly/server/world/sound"
     9  	"github.com/go-gl/mathgl/mgl64"
    10  )
    11  
    12  // Anvil is a block that allows players to repair items, rename items, and combine enchantments.
    13  type Anvil struct {
    14  	gravityAffected
    15  	transparent
    16  
    17  	// Type is the type of anvil.
    18  	Type AnvilType
    19  	// Facing is the direction that the anvil is facing.
    20  	Facing cube.Direction
    21  }
    22  
    23  // Model ...
    24  func (a Anvil) Model() world.BlockModel {
    25  	return model.Anvil{Facing: a.Facing}
    26  }
    27  
    28  // BreakInfo ...
    29  func (a Anvil) BreakInfo() BreakInfo {
    30  	return newBreakInfo(5, pickaxeHarvestable, pickaxeEffective, oneOf(a)).withBlastResistance(6000)
    31  }
    32  
    33  // Activate ...
    34  func (Anvil) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _ *item.UseContext) bool {
    35  	if opener, ok := u.(ContainerOpener); ok {
    36  		opener.OpenBlockContainer(pos)
    37  		return true
    38  	}
    39  	return false
    40  }
    41  
    42  // UseOnBlock ...
    43  func (a Anvil) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
    44  	pos, _, used = firstReplaceable(w, pos, face, a)
    45  	if !used {
    46  		return
    47  	}
    48  	a.Facing = user.Rotation().Direction().RotateRight()
    49  	place(w, pos, a, user, ctx)
    50  	return placed(ctx)
    51  }
    52  
    53  // NeighbourUpdateTick ...
    54  func (a Anvil) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
    55  	a.fall(a, pos, w)
    56  }
    57  
    58  // Damage returns the damage per block fallen of the anvil and the maximum damage the anvil can deal.
    59  func (Anvil) Damage() (damagePerBlock, maxDamage float64) {
    60  	return 2, 40
    61  }
    62  
    63  // Break breaks the anvil and moves it to the next damage stage. If the anvil is at the last damage stage, it will be
    64  // destroyed.
    65  func (a Anvil) Break() world.Block {
    66  	switch a.Type {
    67  	case UndamagedAnvil():
    68  		a.Type = SlightlyDamagedAnvil()
    69  	case SlightlyDamagedAnvil():
    70  		a.Type = VeryDamagedAnvil()
    71  	case VeryDamagedAnvil():
    72  		return Air{}
    73  	}
    74  	return a
    75  }
    76  
    77  // Landed is called when a falling anvil hits the ground, used to, for example, play a sound.
    78  func (Anvil) Landed(w *world.World, pos cube.Pos) {
    79  	w.PlaySound(pos.Vec3Centre(), sound.AnvilLand{})
    80  }
    81  
    82  // EncodeItem ...
    83  func (a Anvil) EncodeItem() (name string, meta int16) {
    84  	return "minecraft:anvil", int16(a.Type.Uint8() * 4)
    85  }
    86  
    87  // EncodeBlock ...
    88  func (a Anvil) EncodeBlock() (string, map[string]any) {
    89  	return "minecraft:anvil", map[string]any{
    90  		"damage":                       a.Type.String(),
    91  		"minecraft:cardinal_direction": a.Facing.String(),
    92  	}
    93  }
    94  
    95  // allAnvils ...
    96  func allAnvils() (anvils []world.Block) {
    97  	for _, t := range AnvilTypes() {
    98  		for _, d := range cube.Directions() {
    99  			anvils = append(anvils, Anvil{Type: t, Facing: d})
   100  		}
   101  	}
   102  	return
   103  }