Skip to content

Commit

Permalink
Respawn
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Oct 13, 2023
1 parent 5c125ca commit dff56b2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ whitelist.json
*.bin
log
/cpu.out
/ram.out
/ram.out
/permissions
4 changes: 4 additions & 0 deletions server/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func (p *PlayerController) BroadcastMovement(id int32, x1, y1, z1 float64, yaw,
p.player.SetPosition(x1, y1, z1, yaw, pitch, ong)
inArea, notInArea := p.PlayersInArea(x1, y1, z1)

if distance > 8 {
id = 0
}

for _, pl := range notInArea {
if pl.IsSpawned(p.player.EntityId()) {
pl.DespawnPlayer(p)
Expand Down
3 changes: 3 additions & 0 deletions server/network/handlers/PlayerMovement.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func PlayerMovement(
state *player.Player,
p packet.Packet,
) {
if state.IsDead() {
return
}
x, y, z := state.Position()
yaw, pitch := state.Rotation()
switch pk := p.(type) {
Expand Down
13 changes: 13 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Player struct {
isHardCore bool
gameMode byte

dead bool
health float32
food int32
foodSaturation float32
Expand Down Expand Up @@ -64,6 +65,18 @@ func (p *Player) Dimension() *world.Dimension {
return p.dimension
}

func (p *Player) IsDead() bool {
p.mu.RLock()
defer p.mu.RUnlock()
return p.dead
}

func (p *Player) SetDead(a bool) {
p.mu.Lock()
defer p.mu.Unlock()
p.dead = a
}

func (p *Player) SetDimension(d *world.Dimension) {
p.mu.Lock()
defer p.mu.Unlock()
Expand Down
3 changes: 3 additions & 0 deletions server/player_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (p *PlayerController) Name() string {
}

func (p *PlayerController) Respawn(d *world.Dimension) {
p.player.SetDead(false)
p.session.SendPacket(&packet.Respawn{
GameMode: p.player.GameMode(),
PreviousGameMode: -1,
Expand Down Expand Up @@ -150,6 +151,7 @@ func (p *PlayerController) SetHealth(health float32) {
}

func (p *PlayerController) Kill(message string) {
p.player.SetDead(true)
p.BroadcastHealth()
if f, _ := world.GameRule(p.Server.World.Gamerules()["doImmediateRespawn"]).Bool(); !f {
p.session.SendPacket(&packet.GameEvent{
Expand All @@ -161,6 +163,7 @@ func (p *PlayerController) Kill(message string) {
EntityID: p.player.EntityId(),
SourceTypeID: 0,
})
p.Despawn()
p.session.SendPacket(&packet.CombatDeath{
Message: message,
PlayerID: p.player.EntityId(),
Expand Down
2 changes: 2 additions & 0 deletions server/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var DefaultConfig = Config{
ReloadComplete: "§aReload complete.",
ServerClosed: "Server closed.",
OnlineMode: "The server is in online mode.",
Slain: "%player% was slain by %killer%",
},
Web: Web{
ServerIP: "0.0.0.0",
Expand Down Expand Up @@ -135,6 +136,7 @@ type Messages struct {
ReloadComplete string `toml:"reload_complete"`
ServerClosed string `toml:"server_closed"`
OnlineMode string `toml:"online_mode"`
Slain string `toml:"slain"`
}

type Chat struct {
Expand Down

0 comments on commit dff56b2

Please sign in to comment.