update comments

This commit is contained in:
Tnze
2023-02-12 00:57:17 +08:00
parent 807e94e360
commit e7984776c8
7 changed files with 119 additions and 61 deletions

View File

@ -7,14 +7,13 @@
// There is 4 kinds of clientbound packet is handled by this package.
// - LoginPacket, for cache player info. The player info will be stored in [Player.PlayerInfo].
// - KeepAlivePacket, for avoid the client to be kicked by the server.
// - PlayerPosition, is only received when server teleporting the player. And the confirm packet is automatically sent.
// - PlayerPosition, is only received when server teleporting the player.
// - Respawn, for updating player info, which may change when player respawned.
//
// # [EventsListener]
//
// Handles some basic event you probably need.
// - GameStart
// - ChatMsg
// - Disconnect
// - HealthChange
// - Death
@ -32,21 +31,22 @@ type Player struct {
PlayerInfo
WorldInfo
isSpawn bool
}
// NewPlayer create a new Player manager.
func NewPlayer(c *bot.Client, settings Settings, events EventsListener) *Player {
p := &Player{c: c, Settings: settings}
c.Events.AddListener(
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLogin, F: p.handleLoginPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: p.handleKeepAlivePacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundPlayerPosition, F: p.handlePlayerPosition},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundRespawn, F: p.handleRespawnPacket},
)
events.attach(p)
return p
}
// Respawn is used to send a respawn packet to the server.
// Typically, you should call this method when the player is dead (in the [Death] event handler).
func (p *Player) Respawn() error {
const PerformRespawn = 0
@ -61,6 +61,19 @@ func (p *Player) Respawn() error {
return nil
}
// AcceptTeleportation is used to send a teleport confirmation packet to the server.
// Typically, you should call this method when received a ClientboundPlayerPosition packet (in the [Teleported] event handler).
func (p *Player) AcceptTeleportation(teleportID pk.VarInt) error {
err := p.c.Conn.WritePacket(pk.Marshal(
packetid.ServerboundAcceptTeleportation,
teleportID,
))
if err != nil {
return Error{err}
}
return nil
}
type Error struct {
Err error
}