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

     1  package entity
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/entity/effect"
     6  	"github.com/df-mc/dragonfly/server/internal/nbtconv"
     7  	"github.com/df-mc/dragonfly/server/item/potion"
     8  	"github.com/df-mc/dragonfly/server/world"
     9  	"github.com/df-mc/dragonfly/server/world/particle"
    10  	"github.com/df-mc/dragonfly/server/world/sound"
    11  	"github.com/go-gl/mathgl/mgl64"
    12  )
    13  
    14  // NewSplashPotion creates a splash potion. SplashPotion is an item that grants
    15  // effects when thrown.
    16  func NewSplashPotion(pos mgl64.Vec3, owner world.Entity, t potion.Potion) *Ent {
    17  	colour, _ := effect.ResultingColour(t.Effects())
    18  
    19  	conf := splashPotionConf
    20  	conf.Potion = t
    21  	conf.Particle = particle.Splash{Colour: colour}
    22  	conf.Hit = potionSplash(0.75, t, false)
    23  	return Config{Behaviour: conf.New(owner)}.New(SplashPotionType{}, pos)
    24  }
    25  
    26  var splashPotionConf = ProjectileBehaviourConfig{
    27  	Gravity: 0.05,
    28  	Drag:    0.01,
    29  	Damage:  -1,
    30  	Sound:   sound.GlassBreak{},
    31  }
    32  
    33  // SplashPotionType is a world.EntityType implementation for SplashPotion.
    34  type SplashPotionType struct{}
    35  
    36  func (SplashPotionType) EncodeEntity() string { return "minecraft:splash_potion" }
    37  func (SplashPotionType) Glint() bool          { return true }
    38  func (SplashPotionType) BBox(world.Entity) cube.BBox {
    39  	return cube.Box(-0.125, 0, -0.125, 0.125, 0.25, 0.125)
    40  }
    41  
    42  func (SplashPotionType) DecodeNBT(m map[string]any) world.Entity {
    43  	pot := NewSplashPotion(nbtconv.Vec3(m, "Pos"), nil, potion.From(nbtconv.Int32(m, "PotionId")))
    44  	pot.vel = nbtconv.Vec3(m, "Motion")
    45  	return pot
    46  }
    47  
    48  func (SplashPotionType) EncodeNBT(e world.Entity) map[string]any {
    49  	pot := e.(*Ent)
    50  	return map[string]any{
    51  		"Pos":      nbtconv.Vec3ToFloat32Slice(pot.Position()),
    52  		"Motion":   nbtconv.Vec3ToFloat32Slice(pot.Velocity()),
    53  		"PotionId": int32(pot.conf.Behaviour.(*ProjectileBehaviour).conf.Potion.Uint8()),
    54  	}
    55  }