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

Add flight speed #917

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
19 changes: 18 additions & 1 deletion server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ type Player struct {
// lastTickedWorld holds the world that the player was in, in the last tick.
lastTickedWorld *world.World

speed atomic.Uint64
speed atomic.Uint64
flightSpeed atomic.Uint32

health *entity.HealthManager
experience *entity.ExperienceManager
effects *entity.EffectManager
Expand Down Expand Up @@ -138,6 +140,7 @@ func New(name string, skin skin.Skin, pos mgl64.Vec3) *Player {
p.Handle(nil)
p.skin.Store(&skin)
p.speed.Store(math.Float64bits(0.1))
p.flightSpeed.Store(math.Float32bits(0.05))
p.nameTag.Store(&name)
p.scoreTag.Store(&scoreTag)
p.airSupplyTicks.Store(300)
Expand Down Expand Up @@ -486,6 +489,20 @@ func (p *Player) Speed() float64 {
return math.Float64frombits(p.speed.Load())
}

// SetFlightSpeed sets the flight speed of the player. The value passed represents the base speed, which is
// multiplied by 10 to obtain the actual blocks/tick speed that the player will then obtain while flying.
func (p *Player) SetFlightSpeed(flightSpeed float32) {
p.flightSpeed.Store(math.Float32bits(flightSpeed))
p.session().SendAbilities()
}

// FlightSpeed returns the flight speed of the player, with the value representing the base speed. The actual
// blocks/tick speed is this value multiplied by 10. The default flight speed of a player is 0.05, which
// corresponds to 0.5 blocks/tick.
func (p *Player) FlightSpeed() float32 {
return math.Float32frombits(p.flightSpeed.Load())
}

// Health returns the current health of the player. It will always be lower than Player.MaxHealth().
func (p *Player) Health() float64 {
return p.health.Health()
Expand Down
2 changes: 2 additions & 0 deletions server/session/controllable.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ type Controllable interface {
SetHeldItems(right, left item.Stack)

Move(deltaPos mgl64.Vec3, deltaYaw, deltaPitch float64)

Speed() float64
FlightSpeed() float32
Sergittos marked this conversation as resolved.
Show resolved Hide resolved

Chat(msg ...any)
ExecuteCommand(commandLine string)
Expand Down
2 changes: 1 addition & 1 deletion server/session/handler_request_ability.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (a RequestAbilityHandler) Handle(p packet.Packet, s *Session) error {
if pk.Ability == packet.AbilityFlying {
if !s.c.GameMode().AllowsFlying() {
s.log.Debugf("failed processing packet from %v (%v): RequestAbility: flying flag enabled while not being able to fly\n", s.conn.RemoteAddr(), s.c.Name())
s.sendAbilities()
s.SendAbilities()
return nil
}
s.c.StartFlying()
Expand Down
10 changes: 5 additions & 5 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,11 @@ func (s *Session) SendGameMode(mode world.GameMode) {
return
}
s.writePacket(&packet.SetPlayerGameType{GameType: gameTypeFromMode(mode)})
s.sendAbilities()
s.SendAbilities()
}

// sendAbilities sends the abilities of the Controllable entity of the session to the client.
func (s *Session) sendAbilities() {
// SendAbilities sends the abilities of the Controllable entity of the session to the client.
func (s *Session) SendAbilities() {
mode, abilities := s.c.GameMode(), uint32(0)
if mode.AllowsFlying() {
abilities |= protocol.AbilityMayFly
Expand Down Expand Up @@ -447,12 +447,12 @@ func (s *Session) sendAbilities() {
EntityUniqueID: selfEntityRuntimeID,
PlayerPermissions: packet.PermissionLevelMember,
CommandPermissions: packet.CommandPermissionLevelNormal,
Layers: []protocol.AbilityLayer{ // TODO: Support customization of fly and walk speeds.
Layers: []protocol.AbilityLayer{ // TODO: Support customization of walk speed.
{
Type: protocol.AbilityLayerTypeBase,
Abilities: protocol.AbilityCount - 1,
Values: abilities,
FlySpeed: protocol.AbilityBaseFlySpeed,
FlySpeed: s.c.FlightSpeed(),
WalkSpeed: protocol.AbilityBaseWalkSpeed,
},
},
Expand Down
Loading