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

     1  package entity
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/internal/nbtconv"
     6  	"github.com/df-mc/dragonfly/server/item"
     7  	"github.com/df-mc/dragonfly/server/world"
     8  	"github.com/go-gl/mathgl/mgl64"
     9  )
    10  
    11  // NewFirework creates a firework entity. Firework is an item (and entity) used
    12  // for creating decorative explosions, boosting when flying with elytra, and
    13  // loading into a crossbow as ammunition.
    14  func NewFirework(pos mgl64.Vec3, rot cube.Rotation, firework item.Firework) *Ent {
    15  	return NewFireworkAttached(pos, rot, firework, nil, false)
    16  }
    17  
    18  // NewFireworkAttached creates a firework entity with an owner that the firework
    19  // may be attached to.
    20  func NewFireworkAttached(pos mgl64.Vec3, rot cube.Rotation, firework item.Firework, owner world.Entity, attached bool) *Ent {
    21  	e := Config{Behaviour: FireworkBehaviourConfig{
    22  		ExistenceDuration:          firework.RandomisedDuration(),
    23  		SidewaysVelocityMultiplier: 1.15,
    24  		UpwardsAcceleration:        0.04,
    25  		Attached:                   attached,
    26  	}.New(firework, owner)}.New(FireworkType{}, pos)
    27  	e.rot = rot
    28  	return e
    29  }
    30  
    31  // FireworkType is a world.EntityType implementation for Firework.
    32  type FireworkType struct{}
    33  
    34  func (FireworkType) EncodeEntity() string        { return "minecraft:fireworks_rocket" }
    35  func (FireworkType) BBox(world.Entity) cube.BBox { return cube.BBox{} }
    36  
    37  func (FireworkType) DecodeNBT(m map[string]any) world.Entity {
    38  	f := NewFirework(
    39  		nbtconv.Vec3(m, "Pos"),
    40  		nbtconv.Rotation(m),
    41  		nbtconv.MapItem(m, "Item").Item().(item.Firework),
    42  	)
    43  	f.vel = nbtconv.Vec3(m, "Motion")
    44  	return f
    45  }
    46  
    47  func (FireworkType) EncodeNBT(e world.Entity) map[string]any {
    48  	f := e.(*Ent)
    49  	yaw, pitch := f.Rotation().Elem()
    50  	return map[string]any{
    51  		"Item":   nbtconv.WriteItem(item.NewStack(f.Behaviour().(*FireworkBehaviour).Firework(), 1), true),
    52  		"Pos":    nbtconv.Vec3ToFloat32Slice(f.Position()),
    53  		"Motion": nbtconv.Vec3ToFloat32Slice(f.Velocity()),
    54  		"Yaw":    float32(yaw),
    55  		"Pitch":  float32(pitch),
    56  	}
    57  }