Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement recovery compasses #627

Merged
merged 5 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/item/recovery_compass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package item

// RecoveryCompass is an item used to point to the location of the player's last death.
type RecoveryCompass struct{}

// EncodeItem ...
func (RecoveryCompass) EncodeItem() (name string, meta int16) {
return "minecraft:recovery_compass", 0
}
1 change: 1 addition & 0 deletions server/item/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func init() {
world.RegisterItem(RawCopper{})
world.RegisterItem(RawGold{})
world.RegisterItem(RawIron{})
world.RegisterItem(RecoveryCompass{})
world.RegisterItem(RottenFlesh{})
world.RegisterItem(Salmon{Cooked: true})
world.RegisterItem(Salmon{})
Expand Down
19 changes: 17 additions & 2 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type Player struct {
lastXPPickup atomic.Value[time.Time]
immunity atomic.Value[time.Time]

deathMu sync.Mutex
deathPos *mgl64.Vec3
deathDimension world.Dimension

enchantSeed atomic.Int64

mc *entity.MovementComputer
Expand Down Expand Up @@ -853,6 +857,14 @@ func (p *Player) Dead() bool {
return p.Health() <= 0
}

// DeathPosition returns the last position the player was at when they died. If the player has never died, the third
// return value will be false.
func (p *Player) DeathPosition() (mgl64.Vec3, world.Dimension, bool) {
p.deathMu.Lock()
defer p.deathMu.Unlock()
return *p.deathPos, p.deathDimension, p.deathPos != nil
}

// kill kills the player, clearing its inventories and resetting it to its base state.
func (p *Player) kill(src damage.Source) {
for _, viewer := range p.viewers() {
Expand All @@ -865,13 +877,16 @@ func (p *Player) kill(src damage.Source) {
p.StopSneaking()
p.StopSprinting()

w := p.World()
w, pos := p.World(), p.Position()
p.dropContents()

for _, e := range p.Effects() {
p.RemoveEffect(e.Type())
}

p.deathMu.Lock()
defer p.deathMu.Unlock()
p.deathPos, p.deathDimension = &pos, w.Dimension()

// Wait a little before removing the entity. The client displays a death animation while the player is dying.
time.AfterFunc(time.Millisecond*1100, func() {
if p.session() == session.Nop {
Expand Down
16 changes: 16 additions & 0 deletions server/session/entity_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/item/potion"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
"time"
)

Expand Down Expand Up @@ -105,6 +106,14 @@ func (s *Session) parseEntityMetadata(e world.Entity) entityMetadata {
m[dataKeyPotionAmbient] = byte(0)
}
}
if l, ok := e.(living); ok && s.c == e {
deathPos, deathDimension, died := l.DeathPosition()
if died {
m[dataKeyPlayerLastDeathPos] = vec64To32(deathPos)
m[dataKeyPlayerLastDeathDimension] = int32(deathDimension.EncodeDimension())
}
m[dataKeyPlayerHasDied] = boolByte(died)
}
if p, ok := e.(splash); ok {
m[dataKeyPotionAuxValue] = int16(p.Type().Uint8())
}
Expand Down Expand Up @@ -179,6 +188,9 @@ const (
dataKeyAreaEffectCloudRadiusPerTick = 97
dataKeyAreaEffectCloudRadiusChangeOnPickup = 98
dataKeyAreaEffectCloudPickupCount = 99
dataKeyPlayerLastDeathPos = 128
dataKeyPlayerLastDeathDimension = 129
dataKeyPlayerHasDied = 130
)

//noinspection GoUnusedConst
Expand Down Expand Up @@ -304,3 +316,7 @@ type gameMode interface {
type tnt interface {
Fuse() time.Duration
}

type living interface {
DeathPosition() (mgl64.Vec3, world.Dimension, bool)
}
8 changes: 8 additions & 0 deletions server/session/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,14 @@ func vec64To32(vec3 mgl64.Vec3) mgl32.Vec3 {
return mgl32.Vec3{float32(vec3[0]), float32(vec3[1]), float32(vec3[2])}
}

// boolByte returns 1 if the bool passed is true, or 0 if it is false.
func boolByte(b bool) uint8 {
if b {
return 1
}
return 0
}

// abs ...
func abs(a int) int {
if a < 0 {
Expand Down