Skip to content

Commit 6640e7d

Browse files
Merge pull request #5 from secmc/chore/cleanup2
chore: add utility method to execute player methods
2 parents c46b1e0 + c265e4f commit 6640e7d

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

plugin/adapters/plugin/emitter.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -462,47 +462,35 @@ func (m *Emitter) handleSendChat(act *pb.SendChatAction) {
462462
if err != nil {
463463
return
464464
}
465-
if handle, ok := m.srv.Player(id); ok {
466-
handle.ExecWorld(func(tx *world.Tx, e world.Entity) {
467-
if pl, ok := e.(*player.Player); ok {
468-
pl.Message(act.Message)
469-
}
470-
})
471-
}
465+
466+
m.execMethod(id, func(pl *player.Player) {
467+
pl.Message(act.Message)
468+
})
472469
}
473470

474471
func (m *Emitter) handleTeleport(act *pb.TeleportAction) {
475472
id, err := uuid.Parse(act.PlayerUuid)
476473
if err != nil {
477474
return
478475
}
479-
if handle, ok := m.srv.Player(id); ok {
480-
handle.ExecWorld(func(tx *world.Tx, e world.Entity) {
481-
pl, ok := e.(*player.Player)
482-
if !ok {
483-
return
484-
}
485-
pl.Teleport(mgl64.Vec3{act.X, act.Y, act.Z})
486-
rot := pl.Rotation()
487-
deltaYaw := float64(act.Yaw) - rot.Yaw()
488-
deltaPitch := float64(act.Pitch) - rot.Pitch()
489-
pl.Move(mgl64.Vec3{}, deltaYaw, deltaPitch)
490-
})
491-
}
476+
477+
m.execMethod(id, func(pl *player.Player) {
478+
pl.Teleport(mgl64.Vec3{act.X, act.Y, act.Z})
479+
rot := pl.Rotation()
480+
deltaYaw := float64(act.Yaw) - rot.Yaw()
481+
deltaPitch := float64(act.Pitch) - rot.Pitch()
482+
pl.Move(mgl64.Vec3{}, deltaYaw, deltaPitch)
483+
})
492484
}
493485

494486
func (m *Emitter) handleKick(act *pb.KickAction) {
495487
id, err := uuid.Parse(act.PlayerUuid)
496488
if err != nil {
497489
return
498490
}
499-
if handle, ok := m.srv.Player(id); ok {
500-
handle.ExecWorld(func(tx *world.Tx, e world.Entity) {
501-
if pl, ok := e.(*player.Player); ok {
502-
pl.Disconnect(act.Reason)
503-
}
504-
})
505-
}
491+
m.execMethod(id, func(pl *player.Player) {
492+
pl.Disconnect(act.Reason)
493+
})
506494
}
507495

508496
func (m *Emitter) handleSetGameMode(act *pb.SetGameModeAction) {
@@ -514,10 +502,16 @@ func (m *Emitter) handleSetGameMode(act *pb.SetGameModeAction) {
514502
if !ok {
515503
return
516504
}
505+
m.execMethod(id, func(pl *player.Player) {
506+
pl.SetGameMode(gameMode)
507+
})
508+
}
509+
510+
func (m *Emitter) execMethod(id uuid.UUID, method func(pl *player.Player)) {
517511
if handle, ok := m.srv.Player(id); ok {
518512
handle.ExecWorld(func(tx *world.Tx, e world.Entity) {
519513
if pl, ok := e.(*player.Player); ok {
520-
pl.SetGameMode(gameMode)
514+
method(pl)
521515
}
522516
})
523517
}

0 commit comments

Comments
 (0)