Skip to content

Commit

Permalink
Make sub-chunk offsets signed bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JustTalDevelops committed Mar 9, 2022
1 parent 9144e62 commit 2a44b7f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions minecraft/protocol/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type IO interface {
Int64(x *int64)
Float32(x *float32)
Uint8(x *uint8)
Int8(x *int8)
Bool(x *bool)
Varint64(x *int64)
Varuint64(x *uint64)
Expand Down
12 changes: 6 additions & 6 deletions minecraft/protocol/packet/sub_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func (pk *SubChunk) Marshal(w *protocol.Writer) {
count := uint32(len(pk.SubChunkEntries))
w.Uint32(&count)
for _, entry := range pk.SubChunkEntries {
w.Uint8(&entry.Offset[0])
w.Uint8(&entry.Offset[1])
w.Uint8(&entry.Offset[2])
w.Int8(&entry.Offset[0])
w.Int8(&entry.Offset[1])
w.Int8(&entry.Offset[2])

w.Uint8(&entry.Result)
if !pk.CacheEnabled || entry.Result != protocol.SubChunkResultSuccessAllAir {
Expand Down Expand Up @@ -65,9 +65,9 @@ func (pk *SubChunk) Unmarshal(r *protocol.Reader) {
for i := uint32(0); i < count; i++ {
var entry protocol.SubChunkEntry

r.Uint8(&entry.Offset[0])
r.Uint8(&entry.Offset[1])
r.Uint8(&entry.Offset[2])
r.Int8(&entry.Offset[0])
r.Int8(&entry.Offset[1])
r.Int8(&entry.Offset[2])

r.Uint8(&entry.Result)
if !pk.CacheEnabled || entry.Result != protocol.SubChunkResultSuccessAllAir {
Expand Down
18 changes: 9 additions & 9 deletions minecraft/protocol/packet/sub_chunk_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type SubChunkRequest struct {
// coordinates represent the chunk coordinates, while the Y coordinate is the absolute sub-chunk index.
Position protocol.SubChunkPos
// Offsets contains all requested offsets around the center point.
Offsets [][3]byte
Offsets [][3]int8
}

// ID ...
Expand All @@ -28,9 +28,9 @@ func (pk *SubChunkRequest) Marshal(w *protocol.Writer) {
count := uint32(len(pk.Offsets))
w.Uint32(&count)
for _, offset := range pk.Offsets {
w.Uint8(&offset[0])
w.Uint8(&offset[1])
w.Uint8(&offset[2])
w.Int8(&offset[0])
w.Int8(&offset[1])
w.Int8(&offset[2])
}
}

Expand All @@ -42,12 +42,12 @@ func (pk *SubChunkRequest) Unmarshal(r *protocol.Reader) {
var count uint32
r.Uint32(&count)

pk.Offsets = make([][3]byte, count)
pk.Offsets = make([][3]int8, count)
for i := uint32(0); i < count; i++ {
var offset [3]byte
r.Uint8(&offset[0])
r.Uint8(&offset[1])
r.Uint8(&offset[2])
var offset [3]int8
r.Int8(&offset[0])
r.Int8(&offset[1])
r.Int8(&offset[2])

pk.Offsets[i] = offset
}
Expand Down
7 changes: 7 additions & 0 deletions minecraft/protocol/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func (r *Reader) Uint8(x *uint8) {
}
}

// Int8 reads an int8 from the underlying buffer.
func (r *Reader) Int8(x *int8) {
var b uint8
r.Uint8(&b)
*x = int8(b)
}

// Bool reads a bool from the underlying buffer.
func (r *Reader) Bool(x *bool) {
u, err := r.r.ReadByte()
Expand Down
2 changes: 1 addition & 1 deletion minecraft/protocol/sub_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// requesting system introduced in v1.18.10.
type SubChunkEntry struct {
// Offset contains the offset between the sub-chunk position and the center position.
Offset [3]byte
Offset [3]int8
// Result is always one of the constants defined in the SubChunkResult constants.
Result byte
// RawPayload contains the serialized sub-chunk data.
Expand Down
5 changes: 5 additions & 0 deletions minecraft/protocol/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (w *Writer) Uint8(x *uint8) {
_ = w.w.WriteByte(*x)
}

// Int8 writes an int8 to the underlying buffer.
func (w *Writer) Int8(x *int8) {
_ = w.w.WriteByte(byte(*x) & 0xff)
}

// Bool writes a bool as either 0 or 1 to the underlying buffer.
func (w *Writer) Bool(x *bool) {
_ = w.w.WriteByte(*(*byte)(unsafe.Pointer(x)))
Expand Down

0 comments on commit 2a44b7f

Please sign in to comment.