fixup codes

This commit is contained in:
2025-08-26 12:06:00 +08:00
committed by 蒟蒻
parent 093fdaaecf
commit 9ae126c648
252 changed files with 627 additions and 369 deletions

View File

@ -1,6 +1,6 @@
package inventory
import "github.com/Tnze/go-mc/chat"
import "git.konjactw.dev/falloutBot/go-mc/chat"
type ContainerOpenEvent struct {
WindowID int32

View File

@ -3,10 +3,11 @@ package inventory
import (
"context"
"git.konjactw.dev/falloutBot/go-mc/level/item"
"git.konjactw.dev/patyhank/minego/pkg/bot"
"git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/server"
"git.konjactw.dev/patyhank/minego/pkg/protocol/slot"
"github.com/Tnze/go-mc/level/item"
)
// Container 代表一個容器

View File

@ -80,8 +80,10 @@ func (m *Manager) CurrentContainerID() int32 {
func (m *Manager) Close() {
if m.currentContainerID != -1 {
_ = m.c.WritePacket(context.Background(), &server.ContainerClose{WindowID: m.currentContainerID})
m.currentContainerID = -1
} else {
_ = m.c.WritePacket(context.Background(), &server.ContainerClose{WindowID: 0})
m.currentContainerID = -1
}
}

View File

@ -1,6 +1,6 @@
package player
import "github.com/Tnze/go-mc/chat"
import "git.konjactw.dev/falloutBot/go-mc/chat"
type MessageEvent struct {
Message chat.Message

View File

@ -4,10 +4,12 @@ import (
"container/heap"
"math"
"github.com/go-gl/mathgl/mgl64"
"git.konjactw.dev/falloutBot/go-mc/level/block"
"git.konjactw.dev/patyhank/minego/pkg/bot"
"git.konjactw.dev/patyhank/minego/pkg/protocol"
"github.com/Tnze/go-mc/level/block"
"github.com/go-gl/mathgl/mgl64"
)
// Node 表示 A* 演算法中的節點

View File

@ -6,13 +6,15 @@ import (
"math"
"time"
"github.com/go-gl/mathgl/mgl64"
pk "git.konjactw.dev/falloutBot/go-mc/net/packet"
"git.konjactw.dev/patyhank/minego/pkg/bot"
"git.konjactw.dev/patyhank/minego/pkg/game/world"
"git.konjactw.dev/patyhank/minego/pkg/protocol"
"git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/client"
"git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/server"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/go-gl/mathgl/mgl64"
)
type Player struct {
@ -27,8 +29,9 @@ type Player struct {
// New 創建新的 Player 實例
func New(c bot.Client) *Player {
pl := &Player{
c: c,
entity: &world.Entity{},
c: c,
entity: &world.Entity{},
stateID: 1,
}
c.PacketHandler().AddGenericPacketHandler(func(ctx context.Context, pk client.ClientboundPacket) {
@ -36,7 +39,7 @@ func New(c bot.Client) *Player {
})
bot.AddHandler(c, func(ctx context.Context, p *client.KeepAlive) {
c.WritePacket(ctx, &server.KeepAlive{
_ = c.WritePacket(ctx, &server.KeepAlive{
ID: p.ID,
})
})
@ -49,10 +52,51 @@ func New(c bot.Client) *Player {
}
})
bot.AddHandler(c, func(ctx context.Context, p *client.PlayerPosition) {
pl.entity.SetPosition(mgl64.Vec3{p.X, p.Y, p.Z})
pl.entity.SetRotation(mgl64.Vec2{float64(p.XRot), float64(p.YRot)})
fmt.Println(p)
position := pl.entity.Position()
if p.Flags&0x01 != 0 {
position[0] += p.X
} else {
position[0] = p.X
}
if p.Flags&0x02 != 0 {
position[1] += p.Y
} else {
position[1] = p.Y
}
if p.Flags&0x04 != 0 {
position[2] += p.Z
} else {
position[2] = p.Z
}
pl.entity.SetPosition(position)
rot := pl.entity.Rotation()
if p.Flags&0x08 != 0 {
rot[0] += float64(p.XRot)
} else {
rot[0] = float64(p.XRot)
}
if p.Flags&0x10 != 0 {
rot[1] += float64(p.YRot)
} else {
rot[1] = float64(p.YRot)
}
pl.entity.SetRotation(rot)
c.WritePacket(context.Background(), &server.AcceptTeleportation{TeleportID: p.ID})
c.WritePacket(context.Background(), &server.MovePlayerPosRot{
X: p.X,
FeetY: p.Y,
Z: p.Z,
Yaw: p.XRot,
Pitch: p.YRot,
Flags: 0x00,
})
})
bot.AddHandler(c, func(ctx context.Context, p *client.PlayerRotation) {
pl.entity.SetRotation(mgl64.Vec2{float64(p.Yaw), float64(p.Pitch)})
@ -173,6 +217,17 @@ func (p *Player) WalkTo(pos mgl64.Vec3) error {
return nil
}
func (p *Player) UpdateLocation() {
_ = p.c.WritePacket(context.Background(), &server.MovePlayerPosRot{
X: p.entity.Position().X(),
FeetY: p.entity.Position().Y(),
Z: p.entity.Position().Z(),
Yaw: float32(p.entity.Rotation().X()),
Pitch: float32(p.entity.Rotation().Y()),
Flags: 0x00,
})
}
// LookAt 看向指定位置
func (p *Player) LookAt(target mgl64.Vec3) error {
if p.c == nil {
@ -191,6 +246,8 @@ func (p *Player) LookAt(target mgl64.Vec3) error {
yaw := float32(math.Atan2(-direction.X(), direction.Z()) * 180 / math.Pi)
pitch := float32(math.Asin(-direction.Y()) * 180 / math.Pi)
p.entity.SetRotation(mgl64.Vec2{float64(yaw), float64(pitch)})
return p.c.WritePacket(context.Background(), &server.MovePlayerRot{
Yaw: yaw,
Pitch: pitch,
@ -290,13 +347,24 @@ func (p *Player) OpenContainer(pos protocol.Position) (bot.Container, error) {
return nil, fmt.Errorf("failed to open container: %w", err)
}
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10)
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFunc()
for p.c.Inventory().Container() == nil && ctx.Err() == nil {
for ctx.Err() == nil && p.c.Inventory().CurrentContainerID() <= 0 {
time.Sleep(time.Millisecond * 50)
}
for ctx.Err() == nil && p.c.Inventory().Container().SlotCount() == 0 {
time.Sleep(time.Millisecond * 50)
}
if ctx.Err() != nil {
return nil, fmt.Errorf("failed to open container: %w", ctx.Err())
}
if p.c.Inventory().CurrentContainerID() <= 0 {
return nil, fmt.Errorf("failed to open container: no container opened")
}
return p.c.Inventory().Container(), nil
}
@ -329,3 +397,15 @@ func (p *Player) OpenMenu(command string) (bot.Container, error) {
// 返回客戶端的容器處理器
return p.c.Inventory().Container(), nil
}
func (p *Player) Command(msg string) error {
return p.c.WritePacket(context.Background(), &server.ChatCommand{
Command: msg,
})
}
func (p *Player) Chat(msg string) error {
return p.c.WritePacket(context.Background(), &server.Chat{
Message: msg,
})
}

View File

@ -1,11 +1,13 @@
package world
import (
"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/go-gl/mathgl/mgl64"
"github.com/google/uuid"
"git.konjactw.dev/falloutBot/go-mc/data/entity"
"git.konjactw.dev/patyhank/minego/pkg/protocol/metadata"
"git.konjactw.dev/patyhank/minego/pkg/protocol/slot"
)
type Entity struct {

View File

@ -6,17 +6,19 @@ import (
"errors"
"sync"
"github.com/go-gl/mathgl/mgl64"
"golang.org/x/exp/constraints"
"git.konjactw.dev/falloutBot/go-mc/data/entity"
"git.konjactw.dev/falloutBot/go-mc/level"
"git.konjactw.dev/falloutBot/go-mc/level/block"
pk "git.konjactw.dev/falloutBot/go-mc/net/packet"
"git.konjactw.dev/patyhank/minego/pkg/bot"
"git.konjactw.dev/patyhank/minego/pkg/protocol"
"git.konjactw.dev/patyhank/minego/pkg/protocol/metadata"
cp "git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/client"
"git.konjactw.dev/patyhank/minego/pkg/protocol/slot"
"github.com/Tnze/go-mc/data/entity"
"github.com/Tnze/go-mc/level"
"github.com/Tnze/go-mc/level/block"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/go-gl/mathgl/mgl64"
"golang.org/x/exp/constraints"
)
type World struct {
@ -42,6 +44,7 @@ func NewWorld(c bot.Client) *World {
w.columns[p.Pos] = p.Data
})
bot.AddHandler(c, func(ctx context.Context, p *cp.ForgetLevelChunk) {
w.chunkLock.Lock()
defer w.chunkLock.Unlock()
@ -147,7 +150,8 @@ func (w *World) GetBlock(pos protocol.Position) (block.Block, error) {
blockX := pos[0] & 15
blockZ := pos[2] & 15
blockIdx := (pos[1] << 8) | (blockZ << 4) | blockX
blockY := pos[1] & 15
blockIdx := (blockY << 8) | (blockZ << 4) | blockX
sectionY := pos[1] >> 4
if sectionY < 0 || int(sectionY) >= len(chunk.Sections) {
return nil, errors.New("invalid section Y coordinate")
@ -186,9 +190,6 @@ func (w *World) SetBlock(pos protocol.Position, blk block.Block) error {
}
func (w *World) GetNearbyBlocks(pos protocol.Position, radius int32) ([]block.Block, error) {
w.chunkLock.Lock()
defer w.chunkLock.Unlock()
var blocks []block.Block
for dx := -radius; dx <= radius; dx++ {
@ -207,8 +208,6 @@ func (w *World) GetNearbyBlocks(pos protocol.Position, radius int32) ([]block.Bl
}
func (w *World) FindNearbyBlock(pos protocol.Position, radius int32, blk block.Block) (protocol.Position, error) {
w.chunkLock.Lock()
defer w.chunkLock.Unlock()
visited := make(map[protocol.Position]bool)
queue := list.New()
start := pos