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