add player package with key client-side player functionality, update protocol codecs, and refactor metadata definitions and slot usage
This commit is contained in:
29
pkg/bot/client.go
Normal file
29
pkg/bot/client.go
Normal file
@ -0,0 +1,29 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.konjactw.dev/patyhank/minego/pkg/auth"
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/server"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
Connect(ctx context.Context, addr string, options *ConnectOptions) error
|
||||
Close(ctx context.Context) error
|
||||
IsConnected() bool
|
||||
WritePacket(ctx context.Context, packet server.ServerboundPacket) error
|
||||
|
||||
PacketHandler() PacketHandler
|
||||
EventHandler() EventHandler
|
||||
World() World
|
||||
Inventory() InventoryHandler
|
||||
Player() Player
|
||||
}
|
||||
|
||||
type ClientOptions struct {
|
||||
AuthProvider auth.Provider
|
||||
}
|
||||
|
||||
type ConnectOptions struct {
|
||||
FakeHost string
|
||||
}
|
20
pkg/bot/event.go
Normal file
20
pkg/bot/event.go
Normal file
@ -0,0 +1,20 @@
|
||||
package bot
|
||||
|
||||
type EventHandler interface {
|
||||
PublishEvent(event string, data any) error
|
||||
SubscribeEvent(event string, handler func(data any) error)
|
||||
}
|
||||
|
||||
type Event interface {
|
||||
EventID() string
|
||||
}
|
||||
|
||||
func PublishEvent(client Client, event Event) error {
|
||||
return client.EventHandler().PublishEvent(event.EventID(), event)
|
||||
}
|
||||
|
||||
func SubscribeEvent(client Client, event string, handler func(event Event) error) {
|
||||
client.EventHandler().SubscribeEvent(event, func(data any) error {
|
||||
return handler(data.(Event))
|
||||
})
|
||||
}
|
24
pkg/bot/handler.go
Normal file
24
pkg/bot/handler.go
Normal file
@ -0,0 +1,24 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/client"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
)
|
||||
|
||||
type PacketHandler interface {
|
||||
AddPacketHandler(id packetid.ClientboundPacketID, handler func(ctx context.Context, p client.ClientboundPacket))
|
||||
AddGenericPacketHandler(handler func(ctx context.Context, p client.ClientboundPacket))
|
||||
HandlePacket(ctx context.Context, p client.ClientboundPacket)
|
||||
}
|
||||
|
||||
type HandlerFunc[T client.ClientboundPacket] func(ctx context.Context, p T)
|
||||
|
||||
func AddHandler[T client.ClientboundPacket](c Client, f HandlerFunc[T]) {
|
||||
var t T
|
||||
handler := c.PacketHandler()
|
||||
handler.AddPacketHandler(t.PacketID(), func(ctx context.Context, p client.ClientboundPacket) {
|
||||
f(ctx, p.(T))
|
||||
})
|
||||
}
|
23
pkg/bot/inventory.go
Normal file
23
pkg/bot/inventory.go
Normal file
@ -0,0 +1,23 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol/slot"
|
||||
"github.com/Tnze/go-mc/level/item"
|
||||
)
|
||||
|
||||
type Container interface {
|
||||
GetSlot(index int) slot.Slot
|
||||
Slots() []slot.Slot
|
||||
SlotCount() int
|
||||
FindEmpty() int16
|
||||
FindItem(itemID item.ID) int16
|
||||
Click(slot int16, mode int32, button int32) error
|
||||
}
|
||||
|
||||
type InventoryHandler interface {
|
||||
Inventory() Container
|
||||
Container() Container
|
||||
CurrentContainerID() int32
|
||||
Click(container int32, slot int16, mode int32, button int32) error
|
||||
Close()
|
||||
}
|
25
pkg/bot/player.go
Normal file
25
pkg/bot/player.go
Normal file
@ -0,0 +1,25 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol"
|
||||
"github.com/go-gl/mathgl/mgl64"
|
||||
)
|
||||
|
||||
type Player interface {
|
||||
StateID() int32
|
||||
UpdateStateID(id int32)
|
||||
Entity() Entity
|
||||
|
||||
FlyTo(pos mgl64.Vec3) error
|
||||
WalkTo(pos mgl64.Vec3) error
|
||||
LookAt(vec3 mgl64.Vec3) error
|
||||
|
||||
BreakBlock(pos protocol.Position) error
|
||||
PlaceBlock(pos protocol.Position) error
|
||||
PlaceBlockWithArgs(pos protocol.Position, face int32, cursor mgl64.Vec3) error
|
||||
OpenContainer(pos protocol.Position) (Container, error)
|
||||
|
||||
UseItem(hand int8) error
|
||||
|
||||
OpenMenu(command string) (Container, error)
|
||||
}
|
35
pkg/bot/world.go
Normal file
35
pkg/bot/world.go
Normal file
@ -0,0 +1,35 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol"
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol/metadata"
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol/slot"
|
||||
"github.com/Tnze/go-mc/data/entity"
|
||||
"github.com/Tnze/go-mc/level/block"
|
||||
"github.com/go-gl/mathgl/mgl64"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type World interface {
|
||||
GetBlock(pos protocol.Position) (block.Block, error)
|
||||
SetBlock(pos protocol.Position, b block.Block) error
|
||||
|
||||
GetNearbyBlocks(pos protocol.Position, radius int32) ([]block.Block, error)
|
||||
FindNearbyBlock(pos protocol.Position, radius int32, blk block.Block) (protocol.Position, error)
|
||||
|
||||
Entities() []Entity
|
||||
GetEntity(id int32) Entity
|
||||
GetNearbyEntities(radius int32) []Entity
|
||||
GetEntitiesByType(entityType entity.ID) []Entity
|
||||
}
|
||||
|
||||
type Entity interface {
|
||||
ID() int32
|
||||
UUID() uuid.UUID
|
||||
Type() entity.ID
|
||||
Position() mgl64.Vec3
|
||||
Rotation() mgl64.Vec2
|
||||
|
||||
Metadata() map[uint8]metadata.Metadata
|
||||
Equipment() map[int8]slot.Slot
|
||||
}
|
Reference in New Issue
Block a user