Update to 1.16.3 + add a few new event callbacks.

This commit is contained in:
Tom
2020-09-11 17:58:15 -07:00
parent 5bff8bcb2b
commit 0ec82d90a7
6 changed files with 122 additions and 23 deletions

View File

@ -8,7 +8,30 @@ import (
pk "github.com/Tnze/go-mc/net/packet"
)
type seenPacketFlags uint8
// Valid seenPacketFlags values.
const (
seenJoinGame seenPacketFlags = 1 << iota
seenServerDifficulty
seenPlayerAbilities
seenPlayerInventory
seenUpdateLight
seenChunkData
seenPlayerPositionAndLook
seenSpawnPos
// gameReadyMinPackets are the minimum set of packets that must be seen, for
// the GameReady callback to be invoked.
gameReadyMinPackets = seenJoinGame | seenChunkData | seenUpdateLight |
seenPlayerAbilities | seenPlayerInventory | seenServerDifficulty |
seenPlayerPositionAndLook | seenSpawnPos
)
type eventBroker struct {
seenPackets seenPacketFlags
isReady bool
GameStart func() error
ChatMsg func(msg chat.Message, pos byte, sender uuid.UUID) error
Disconnect func(reason chat.Message) error
@ -28,7 +51,29 @@ type eventBroker struct {
WindowsItem func(id byte, slots []entity.Slot) error
WindowsItemChange func(id byte, slotID int, slot entity.Slot) error
// ReceivePacket will be called when new packet arrive.
// Default handler will run only if pass == false.
// ServerDifficultyChange is called whenever the gamemode of the server changes.
// At time of writing (1.16.3), difficulty values of 0, 1, 2, and 3 correspond
// to peaceful, easy, normal, and hard respectively.
ServerDifficultyChange func(difficulty int) error
// GameReady is called after the client has joined the server and successfully
// received player state. Additionally, the server has begun sending world
// state (such as lighting and chunk information).
//
// Use this callback as a signal as to when your bot should start 'doing'
// things.
GameReady func() error
// ReceivePacket will be called when new packets arrive.
// The default handler will run only if pass == false.
ReceivePacket func(p pk.Packet) (pass bool, err error)
}
func (b *eventBroker) updateSeenPackets(f seenPacketFlags) error {
b.seenPackets |= f
if (^b.seenPackets)&gameReadyMinPackets == 0 && b.GameReady != nil && !b.isReady {
b.isReady = true
return b.GameReady()
}
return nil
}