bot can receive chunks now

This commit is contained in:
Tnze
2022-03-13 11:57:02 +08:00
parent d8695636b6
commit 2aace6b51a
17 changed files with 452 additions and 78 deletions

View File

@ -11,7 +11,7 @@ import (
// WorldInfo content player info in server.
type WorldInfo struct {
DimensionCodec nbt.StringifiedMessage
Dimension nbt.StringifiedMessage
Dimension Dimension
WorldNames []string // Identifiers for all worlds on the server.
WorldName string // Name of the world being spawned into.
HashedSeed int64 // First 8 bytes of the SHA-256 hash of the world's seed. Used client side for biome noise
@ -24,6 +24,25 @@ type WorldInfo struct {
IsFlat bool // True if the world is a superflat world; flat worlds have different void fog and a horizon at y=0 instead of y=63.
}
type Dimension struct {
PiglinSafe int8 `nbt:"piglin_safe"`
Natural int8 `nbt:"natural"`
AmbientLight float32 `nbt:"ambient_light"`
FixedTime *int64 `nbt:"fixed_time"`
Infiniburn string `nbt:"infiniburn"`
RespawnAnchorWorks int8 `nbt:"respawn_anchor_works"`
HasSkylight int8 `nbt:"has_skylight"`
BedWorks int8 `nbt:"bed_works"`
Effects string `nbt:"effects"`
HasRaids int8 `nbt:"has_raids"`
LogicalHeight int32 `nbt:"logical_height"`
CoordinateScale float64 `nbt:"coordinate_scale"`
MinY int32 `nbt:"min_y"`
HasCeiling int8 `nbt:"has_ceiling"`
Ultrawarm int8 `nbt:"ultrawarm"`
Height int32 `nbt:"height"`
}
type PlayerInfo struct {
EID int32 // The player's Entity ID (EID).
Hardcore bool // Is hardcore

View File

@ -28,10 +28,7 @@ func (c *Client) JoinServer(addr string) (err error) {
// JoinServerWithDialer is similar to JoinServer but using a Dialer.
func (c *Client) JoinServerWithDialer(d *net.Dialer, addr string) (err error) {
var dialer *mcnet.Dialer
if d != nil {
dialer = &mcnet.Dialer{Dialer: *d}
}
dialer := (*mcnet.Dialer)(d)
return c.join(context.Background(), dialer, addr)
}

66
bot/world/chunks.go Normal file
View File

@ -0,0 +1,66 @@
package world
import (
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/level"
pk "github.com/Tnze/go-mc/net/packet"
)
type World struct {
c *bot.Client
p *basic.Player
events EventsListener
Columns map[level.ChunkPos]*level.Chunk
}
func NewWorld(c *bot.Client, p *basic.Player, events EventsListener) (w *World) {
w = &World{
c: c, p: p,
events: events,
Columns: make(map[level.ChunkPos]*level.Chunk),
}
c.Events.AddListener(
bot.PacketHandler{Priority: 64, ID: packetid.ClientboundLogin, F: w.onPlayerSpawn},
bot.PacketHandler{Priority: 64, ID: packetid.ClientboundRespawn, F: w.onPlayerSpawn},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLevelChunkWithLight, F: w.handleLevelChunkWithLightPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundForgetLevelChunk, F: w.handleForgetLevelChunkPacket},
)
return
}
func (w *World) onPlayerSpawn(pk.Packet) error {
// unload all chunks
w.Columns = make(map[level.ChunkPos]*level.Chunk)
return nil
}
func (w *World) handleLevelChunkWithLightPacket(packet pk.Packet) error {
var pos level.ChunkPos
chunk := level.EmptyChunk(int(w.p.WorldInfo.Dimension.Height / 16))
if err := packet.Scan(&pos, chunk); err != nil {
return err
}
w.Columns[pos] = chunk
if w.events.LoadChunk != nil {
if err := w.events.LoadChunk(pos); err != nil {
return err
}
}
return nil
}
func (w *World) handleForgetLevelChunkPacket(packet pk.Packet) error {
var pos level.ChunkPos
if err := packet.Scan(&pos); err != nil {
return err
}
var err error
if w.events.UnloadChunk != nil {
err = w.events.UnloadChunk(pos)
}
delete(w.Columns, pos)
return err
}

8
bot/world/events.go Normal file
View File

@ -0,0 +1,8 @@
package world
import "github.com/Tnze/go-mc/level"
type EventsListener struct {
LoadChunk func(pos level.ChunkPos) error
UnloadChunk func(pos level.ChunkPos) error
}