Fix some warnings and typos

This commit is contained in:
Tnze
2021-02-18 14:07:30 +08:00
parent 750f87e780
commit bc9cd93066
23 changed files with 102 additions and 86 deletions

View File

@ -4,21 +4,21 @@ package world
// values. If the next value does not fit into remaining space, the remaining
// space of a uint64 is unused.
type bitArray struct {
width uint // bit width of each value
valsPerElement uint // number of values which fit into a single uint64.
width uint // bit width of each value
valuesPerElement uint // number of values which fit into a single uint64.
data []uint64
}
// Size returns the number of elements that can fit into the bit array.
func (b *bitArray) Size() int {
return int(b.valsPerElement) * len(b.data)
return int(b.valuesPerElement) * len(b.data)
}
func (b *bitArray) Set(idx, val uint) {
var (
arrayIdx = idx / b.valsPerElement
startBit = (idx % b.valsPerElement) * b.width
arrayIdx = idx / b.valuesPerElement
startBit = (idx % b.valuesPerElement) * b.width
mask = ^uint64((1<<b.width - 1) << startBit) // set for all bits except target
)
b.data[arrayIdx] = (b.data[arrayIdx] & mask) | uint64(val<<startBit)
@ -26,13 +26,13 @@ func (b *bitArray) Set(idx, val uint) {
func (b *bitArray) Get(idx uint) uint {
var (
arrayIdx = idx / b.valsPerElement
offset = (idx % b.valsPerElement) * b.width
arrayIdx = idx / b.valuesPerElement
offset = (idx % b.valuesPerElement) * b.width
mask = uint64((1<<b.width - 1) << offset) // set for just the target
)
return uint(b.data[arrayIdx]&mask) >> offset
}
func valsPerBitArrayElement(bitsPerValue uint) uint {
func valuesPerBitArrayElement(bitsPerValue uint) uint {
return uint(64 / bitsPerValue)
}

View File

@ -8,9 +8,9 @@ import (
func TestBitArrayBasic(t *testing.T) {
a := bitArray{
width: 5,
valsPerElement: valsPerBitArrayElement(5),
data: make([]uint64, 5),
width: 5,
valuesPerElement: valuesPerBitArrayElement(5),
data: make([]uint64, 5),
}
if got, want := a.Size(), 12*5; got != want {
@ -32,9 +32,9 @@ func TestBitArrayHardcoded(t *testing.T) {
d2, _ := hex.DecodeString("01018A7260F68C87")
a := bitArray{
width: 5,
valsPerElement: valsPerBitArrayElement(5),
data: []uint64{binary.BigEndian.Uint64(d1), binary.BigEndian.Uint64(d2)},
width: 5,
valuesPerElement: valuesPerBitArrayElement(5),
data: []uint64{binary.BigEndian.Uint64(d1), binary.BigEndian.Uint64(d2)},
}
if got, want := a.Size(), 12*2; got != want {

View File

@ -92,9 +92,9 @@ func readSection(data pk.DecodeReader) (s Section, err error) {
width := perBits(byte(bpb))
sec := directSection{
bitArray{
width: width,
valsPerElement: valsPerBitArrayElement(width),
data: dataArray,
width: width,
valuesPerElement: valuesPerBitArrayElement(width),
data: dataArray,
},
}
if bpb <= maxPaletteBits {
@ -133,12 +133,12 @@ func (d *directSection) clone(bpb uint) *directSection {
}
func newSectionWithSize(bpb uint) *directSection {
valsPerElement := valsPerBitArrayElement(bpb)
valuesPerElement := valuesPerBitArrayElement(bpb)
return &directSection{
bitArray{
width: bpb,
valsPerElement: valsPerElement,
data: make([]uint64, int(math.Ceil(16*16*16/float64(valsPerElement)))),
width: bpb,
valuesPerElement: valuesPerElement,
data: make([]uint64, int(math.Ceil(16*16*16/float64(valuesPerElement)))),
},
}
}

View File

@ -84,9 +84,13 @@ func (s Slot) Encode() []byte {
b.Write(pk.Byte(s.Count).Encode())
if s.NBT != nil {
nbt.NewEncoder(&b).Encode(s.NBT)
if err := nbt.NewEncoder(&b).Encode(s.NBT); err != nil {
panic(err)
}
} else {
b.Write([]byte{nbt.TagEnd})
if _, err := b.Write([]byte{nbt.TagEnd}); err != nil {
panic(err)
}
}
return b.Bytes()

View File

@ -24,7 +24,7 @@ func (w *World) PlayerEntities() []entity.Entity {
}
// OnSpawnEntity should be called when a SpawnEntity packet
// is recieved.
// is received.
func (w *World) OnSpawnEntity(pkt ptypes.SpawnEntity) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()
@ -53,7 +53,7 @@ func (w *World) OnSpawnEntity(pkt ptypes.SpawnEntity) error {
}
// OnSpawnLivingEntity should be called when a SpawnLivingEntity packet
// is recieved.
// is received.
func (w *World) OnSpawnLivingEntity(pkt ptypes.SpawnLivingEntity) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()
@ -81,7 +81,7 @@ func (w *World) OnSpawnLivingEntity(pkt ptypes.SpawnLivingEntity) error {
}
// OnSpawnPlayer should be called when a SpawnPlayer packet
// is recieved.
// is received.
func (w *World) OnSpawnPlayer(pkt ptypes.SpawnPlayer) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()
@ -100,7 +100,7 @@ func (w *World) OnSpawnPlayer(pkt ptypes.SpawnPlayer) error {
}
// OnEntityPosUpdate should be called when an EntityPosition packet
// is recieved.
// is received.
func (w *World) OnEntityPosUpdate(pkt ptypes.EntityPosition) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()
@ -118,7 +118,7 @@ func (w *World) OnEntityPosUpdate(pkt ptypes.EntityPosition) error {
}
// OnEntityPosLookUpdate should be called when an EntityPositionLook packet
// is recieved.
// is received.
func (w *World) OnEntityPosLookUpdate(pkt ptypes.EntityPositionLook) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()
@ -137,7 +137,7 @@ func (w *World) OnEntityPosLookUpdate(pkt ptypes.EntityPositionLook) error {
}
// OnEntityLookUpdate should be called when an EntityRotation packet
// is recieved.
// is received.
func (w *World) OnEntityLookUpdate(pkt ptypes.EntityRotation) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()
@ -153,7 +153,7 @@ func (w *World) OnEntityLookUpdate(pkt ptypes.EntityRotation) error {
}
// OnEntityDestroy should be called when a DestroyEntities packet
// is recieved.
// is received.
func (w *World) OnEntityDestroy(eIDs []pk.VarInt) error {
w.entityLock.Lock()
defer w.entityLock.Unlock()