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

     1  package block
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/item"
     6  	"github.com/df-mc/dragonfly/server/world"
     7  	"github.com/df-mc/dragonfly/server/world/particle"
     8  	"github.com/go-gl/mathgl/mgl64"
     9  	"math/rand"
    10  )
    11  
    12  // BeetrootSeeds are a crop that can be harvested to craft soup or red dye.
    13  type BeetrootSeeds struct {
    14  	crop
    15  }
    16  
    17  // SameCrop ...
    18  func (BeetrootSeeds) SameCrop(c Crop) bool {
    19  	_, ok := c.(BeetrootSeeds)
    20  	return ok
    21  }
    22  
    23  // BoneMeal ...
    24  func (b BeetrootSeeds) BoneMeal(pos cube.Pos, w *world.World) bool {
    25  	if b.Growth == 7 {
    26  		return false
    27  	}
    28  	if rand.Float64() < 0.75 {
    29  		b.Growth++
    30  		w.SetBlock(pos, b, nil)
    31  		return true
    32  	}
    33  	return false
    34  }
    35  
    36  // UseOnBlock ...
    37  func (b BeetrootSeeds) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
    38  	pos, _, used := firstReplaceable(w, pos, face, b)
    39  	if !used {
    40  		return false
    41  	}
    42  
    43  	if _, ok := w.Block(pos.Side(cube.FaceDown)).(Farmland); !ok {
    44  		return false
    45  	}
    46  
    47  	place(w, pos, b, user, ctx)
    48  	return placed(ctx)
    49  }
    50  
    51  // BreakInfo ...
    52  func (b BeetrootSeeds) BreakInfo() BreakInfo {
    53  	return newBreakInfo(0, alwaysHarvestable, nothingEffective, func(item.Tool, []item.Enchantment) []item.Stack {
    54  		if b.Growth < 7 {
    55  			return []item.Stack{item.NewStack(b, 1)}
    56  		}
    57  		return []item.Stack{item.NewStack(item.Beetroot{}, 1), item.NewStack(b, rand.Intn(4)+1)}
    58  	})
    59  }
    60  
    61  // CompostChance ...
    62  func (BeetrootSeeds) CompostChance() float64 {
    63  	return 0.3
    64  }
    65  
    66  // EncodeItem ...
    67  func (b BeetrootSeeds) EncodeItem() (name string, meta int16) {
    68  	return "minecraft:beetroot_seeds", 0
    69  }
    70  
    71  // RandomTick ...
    72  func (b BeetrootSeeds) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {
    73  	if w.Light(pos) < 8 {
    74  		w.SetBlock(pos, nil, nil)
    75  		w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: b})
    76  	} else if b.Growth < 7 && r.Intn(3) > 0 && r.Float64() <= b.CalculateGrowthChance(pos, w) {
    77  		b.Growth++
    78  		w.SetBlock(pos, b, nil)
    79  	}
    80  }
    81  
    82  // EncodeBlock ...
    83  func (b BeetrootSeeds) EncodeBlock() (name string, properties map[string]any) {
    84  	return "minecraft:beetroot", map[string]any{"growth": int32(b.Growth)}
    85  }
    86  
    87  // allBeetroot ...
    88  func allBeetroot() (beetroot []world.Block) {
    89  	for i := 0; i <= 7; i++ {
    90  		beetroot = append(beetroot, BeetrootSeeds{crop: crop{Growth: i}})
    91  	}
    92  	return
    93  }