Skip to content

Commit

Permalink
Handle player movement packets
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Sep 26, 2023
1 parent 048c484 commit 69bb602
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package packets
package handlers

import "fmt"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package packets
package handlers

func ChatMessagePacket(content string) {
}
31 changes: 31 additions & 0 deletions server/network/handlers/PlayerMovement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package handlers

import (
"github.com/aimjel/minecraft/packet"
"github.com/dynamitemc/dynamite/server/player"
)

func PlayerMovement(
state *player.Player,
pk packet.Packet,
) {
switch pk := pk.(type) {
case *packet.PlayerPosition:
{
state.X, state.Y, state.Z, state.OnGround = pk.X, pk.FeetY, pk.Z, pk.OnGround
}
case *packet.PlayerPositionRotation:
{
state.X, state.Y, state.Z, state.Yaw, state.Pitch, state.OnGround = pk.X, pk.FeetY, pk.Z, pk.Yaw, pk.Pitch, pk.OnGround
}
case *packet.PlayerRotation:
{
state.Yaw, state.Pitch, state.OnGround = pk.Yaw, pk.Pitch, pk.OnGround
}
case *packet.PlayerMovement:
{
state.OnGround = pk.OnGround
}
}

}
4 changes: 4 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type Player struct {

viewDistance int32
simulationDistance int32

X, Y, Z float64
Yaw, Pitch float32
OnGround bool
}

func New() *Player {
Expand Down
15 changes: 13 additions & 2 deletions server/player_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package server

import (
"github.com/aimjel/minecraft/packet"
"github.com/dynamitemc/dynamite/server/network"
"github.com/dynamitemc/dynamite/server/player"
"github.com/dynamitemc/dynamite/server/world"
)

type PlayerController struct {
player *player.Player
session *network.Session
session *Session

UUID string
}
Expand Down Expand Up @@ -40,3 +39,15 @@ func (p *PlayerController) SendAvailableCommands(commands *packet.DeclareCommand
func (p *PlayerController) SystemChatMessage(s string) error {
return p.session.SendPacket(&packet.SystemChatMessage{Content: s})
}

func (p *PlayerController) Position() (x float64, y float64, z float64) {
return p.player.X, p.player.Y, p.player.Z
}

func (p *PlayerController) Rotation() (yaw float32, pitch float32) {
return p.player.Yaw, p.player.Pitch
}

func (p *PlayerController) OnGround() bool {
return p.player.OnGround
}
3 changes: 1 addition & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
//"github.com/dynamitemc/dynamite/web"
"github.com/dynamitemc/dynamite/logger"
"github.com/dynamitemc/dynamite/server/commands"
"github.com/dynamitemc/dynamite/server/network"
"github.com/dynamitemc/dynamite/server/player"
"github.com/dynamitemc/dynamite/server/world"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ func (srv *Server) handleNewConn(conn *minecraft.Conn) {
}

plyr := player.New()
sesh := network.New(conn, plyr)
sesh := New(conn, plyr)
cntrl := &PlayerController{player: plyr, session: sesh}
cntrl.UUID = util.AddDashesToUUID(hex.EncodeToString(conn.Info.UUID[:]))
if err := cntrl.JoinDimension(srv.world.DefaultDimension()); err != nil {
Expand Down
17 changes: 12 additions & 5 deletions server/network/session.go → server/session.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package network
package server

import (
"net"

"github.com/aimjel/minecraft"
"github.com/aimjel/minecraft/packet"
player2 "github.com/aimjel/minecraft/player"
"github.com/dynamitemc/dynamite/server/network/packets"
"github.com/dynamitemc/dynamite/server/network/handlers"
"github.com/dynamitemc/dynamite/server/player"
"net"
)

type Session struct {
Expand All @@ -28,9 +29,15 @@ func (s *Session) HandlePackets() error {

switch pk := p.(type) {
case *packet.ChatMessageServer:
packets.ChatMessagePacket(pk.Message)
handlers.ChatMessagePacket(pk.Message)
case *packet.ChatCommandServer:
packets.ChatCommandPacket(pk.Command)
handlers.ChatCommandPacket(pk.Command)
}
switch p.ID() {
case 0x14, 0x15, 0x16, 0x17:
{
handlers.PlayerMovement(s.state, p)
}
}
}
}
Expand Down

0 comments on commit 69bb602

Please sign in to comment.