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

     1  package entity
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/block/cube/trace"
     6  	"github.com/df-mc/dragonfly/server/internal/nbtconv"
     7  	"github.com/df-mc/dragonfly/server/world"
     8  	"github.com/df-mc/dragonfly/server/world/particle"
     9  	"github.com/df-mc/dragonfly/server/world/sound"
    10  	"github.com/go-gl/mathgl/mgl64"
    11  	"math/rand"
    12  )
    13  
    14  // NewBottleOfEnchanting ...
    15  func NewBottleOfEnchanting(pos mgl64.Vec3, owner world.Entity) *Ent {
    16  	return Config{Behaviour: bottleOfEnchantingConf.New(owner)}.New(BottleOfEnchantingType{}, pos)
    17  }
    18  
    19  var bottleOfEnchantingConf = ProjectileBehaviourConfig{
    20  	Gravity:  0.07,
    21  	Drag:     0.01,
    22  	Particle: particle.Splash{},
    23  	Sound:    sound.GlassBreak{},
    24  	Hit:      spawnExperience,
    25  	Damage:   -1,
    26  }
    27  
    28  // spawnExperience spawns experience orbs with a value of 3-11 at the target of
    29  // a trace.Result.
    30  func spawnExperience(e *Ent, target trace.Result) {
    31  	for _, orb := range NewExperienceOrbs(target.Position(), rand.Intn(9)+3) {
    32  		orb.SetVelocity(mgl64.Vec3{(rand.Float64()*0.2 - 0.1) * 2, rand.Float64() * 0.4, (rand.Float64()*0.2 - 0.1) * 2})
    33  		e.World().AddEntity(orb)
    34  	}
    35  }
    36  
    37  // BottleOfEnchantingType is a world.EntityType for BottleOfEnchanting.
    38  type BottleOfEnchantingType struct{}
    39  
    40  // Glint returns true if the bottle should render with glint. It always returns
    41  // true for bottles of enchanting.
    42  func (BottleOfEnchantingType) Glint() bool {
    43  	return true
    44  }
    45  func (BottleOfEnchantingType) EncodeEntity() string {
    46  	return "minecraft:xp_bottle"
    47  }
    48  func (BottleOfEnchantingType) BBox(world.Entity) cube.BBox {
    49  	return cube.Box(-0.125, 0, -0.125, 0.125, 0.25, 0.125)
    50  }
    51  
    52  func (BottleOfEnchantingType) DecodeNBT(m map[string]any) world.Entity {
    53  	b := NewBottleOfEnchanting(nbtconv.Vec3(m, "Pos"), nil)
    54  	b.vel = nbtconv.Vec3(m, "Motion")
    55  	return b
    56  }
    57  
    58  func (BottleOfEnchantingType) EncodeNBT(e world.Entity) map[string]any {
    59  	b := e.(*Ent)
    60  	return map[string]any{
    61  		"Pos":    nbtconv.Vec3ToFloat32Slice(b.Position()),
    62  		"Motion": nbtconv.Vec3ToFloat32Slice(b.Velocity()),
    63  	}
    64  }