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

     1  package block
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/world"
     6  	"math/rand"
     7  	"time"
     8  )
     9  
    10  // CoralBlock is a solid block that comes in 5 variants.
    11  type CoralBlock struct {
    12  	solid
    13  	bassDrum
    14  
    15  	// Type is the type of coral of the block.
    16  	Type CoralType
    17  	// Dead is whether the coral block is dead.
    18  	Dead bool
    19  }
    20  
    21  // NeighbourUpdateTick ...
    22  func (c CoralBlock) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
    23  	if c.Dead {
    24  		return
    25  	}
    26  	w.ScheduleBlockUpdate(pos, time.Second*5/2)
    27  }
    28  
    29  // ScheduledTick ...
    30  func (c CoralBlock) ScheduledTick(pos cube.Pos, w *world.World, _ *rand.Rand) {
    31  	if c.Dead {
    32  		return
    33  	}
    34  
    35  	adjacentWater := false
    36  	pos.Neighbours(func(neighbour cube.Pos) {
    37  		if liquid, ok := w.Liquid(neighbour); ok {
    38  			if _, ok := liquid.(Water); ok {
    39  				adjacentWater = true
    40  			}
    41  		}
    42  	}, w.Range())
    43  	if !adjacentWater {
    44  		c.Dead = true
    45  		w.SetBlock(pos, c, nil)
    46  	}
    47  }
    48  
    49  // BreakInfo ...
    50  func (c CoralBlock) BreakInfo() BreakInfo {
    51  	return newBreakInfo(7, pickaxeHarvestable, pickaxeEffective, silkTouchOneOf(CoralBlock{Type: c.Type, Dead: true}, c)).withBlastResistance(4.5)
    52  }
    53  
    54  // EncodeBlock ...
    55  func (c CoralBlock) EncodeBlock() (name string, properties map[string]any) {
    56  	return "minecraft:coral_block", map[string]any{"coral_color": c.Type.Colour().SilverString(), "dead_bit": c.Dead}
    57  }
    58  
    59  // EncodeItem ...
    60  func (c CoralBlock) EncodeItem() (name string, meta int16) {
    61  	if c.Dead {
    62  		return "minecraft:coral_block", int16(c.Type.Uint8() | 8)
    63  	}
    64  	return "minecraft:coral_block", int16(c.Type.Uint8())
    65  }
    66  
    67  // allCoralBlocks returns a list of all coral block variants
    68  func allCoralBlocks() (c []world.Block) {
    69  	f := func(dead bool) {
    70  		for _, t := range CoralTypes() {
    71  			c = append(c, CoralBlock{Type: t, Dead: dead})
    72  		}
    73  	}
    74  	f(true)
    75  	f(false)
    76  	return
    77  }