Skip to content

Commit

Permalink
add more common types
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jun 21, 2024
1 parent 1f7858b commit 9a0245a
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
20 changes: 20 additions & 0 deletions inventory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types

type Inventory struct {
Size int `json:"size"`
Items []*Item `json:"items"`
}

func (inv *Inventory) IsEmpty() bool {
if len(inv.Items) == 0 {
return true
}

for _, item := range inv.Items {
if item.Name != "" && item.Count > 0 {
return false
}
}

return true
}
12 changes: 12 additions & 0 deletions item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package types

type Item struct {
Name string `json:"name"`
Count int `json:"count"`
Wear int `json:"wear"`
//TODO: metadata
}

func (i *Item) IsEmpty() bool {
return i.Name == "" && i.Count == 0
}
36 changes: 36 additions & 0 deletions mapblock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package types

type MapBlock struct {
Size int `json:"size"`
Version byte `json:"version"`
Underground bool `json:"underground"`
Timestamp uint32 `json:"timestamp"`
Mapdata *MapData `json:"mapdata"`
Metadata *Metadata `json:"metadata"`
BlockMapping map[int]string `json:"blockmapping"`
}

func NewMapblock() *MapBlock {
mb := MapBlock{}
mb.Metadata = NewMetadata()
mb.BlockMapping = make(map[int]string)
return &mb
}

// returns true if the mapblock is empty (air-only)
func (mb *MapBlock) IsEmpty() bool {
return len(mb.BlockMapping) == 0
}

func (mb *MapBlock) GetNodeId(p *Pos) int {
return mb.Mapdata.ContentId[p.Index()]
}

func (mb *MapBlock) GetParam2(p *Pos) int {
return mb.Mapdata.Param2[p.Index()]
}

func (mb *MapBlock) GetNodeName(p *Pos) string {
id := mb.GetNodeId(p)
return mb.BlockMapping[id]
}
7 changes: 7 additions & 0 deletions mapdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

type MapData struct {
ContentId []int `json:"contentid"`
Param1 []int `json:"param1"`
Param2 []int `json:"param2"`
}
52 changes: 52 additions & 0 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package types

type Metadata struct {
Inventories map[int]map[string]*Inventory `json:"inventories"`
Pairs map[int]map[string]string `json:"pairs"`
}

func NewMetadata() *Metadata {
md := Metadata{}
md.Inventories = make(map[int]map[string]*Inventory)
md.Pairs = make(map[int]map[string]string)
return &md
}

func (md *Metadata) GetMetadata(p *Pos) map[string]string {
return md.GetPairsMap(p.Index())
}

func (md *Metadata) GetPairsMap(pos int) map[string]string {
pairsMap := md.Pairs[pos]
if pairsMap == nil {
pairsMap = make(map[string]string)
md.Pairs[pos] = pairsMap
}

return pairsMap
}

func (md *Metadata) GetInventoryMap(index int) map[string]*Inventory {
invMap := md.Inventories[index]
if invMap == nil {
invMap = make(map[string]*Inventory)
md.Inventories[index] = invMap
}

return invMap
}

func (md *Metadata) GetInventoryMapAtPos(p *Pos) map[string]*Inventory {
return md.GetInventoryMap(p.Index())
}

func (md *Metadata) GetInventory(index int, name string) *Inventory {
m := md.GetInventoryMap(index)
inv := m[name]
if inv == nil {
inv = &Inventory{}
m[name] = inv
}

return inv
}
13 changes: 13 additions & 0 deletions pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ func NewPos(x, y, z int) *Pos {
return &Pos{x, y, z}
}

func NewPosFromIndex(i int) *Pos {
x := i % 16
i /= 16
y := i % 16
i /= 16
z := i % 16
return NewPos(x, y, z)
}

func SortPos(p1, p2 *Pos) (*Pos, *Pos) {
return &Pos{
min(p1[0], p2[0]),
Expand Down Expand Up @@ -68,3 +77,7 @@ func (p *Pos) IsWithin(min, max *Pos) bool {
p[1] >= min[1] && p[1] <= max[1] &&
p[2] >= min[2] && p[2] <= max[2]
}

func (p *Pos) Index() int {
return p[0] + (p[1] * 16) + (p[2] * 256)
}
11 changes: 11 additions & 0 deletions pos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func TestPos(t *testing.T) {
assert.Equal(t, 30, p2.Z())
}

func TestPosIndex(t *testing.T) {
pos := types.NewPos(5, 8, 12)

i := pos.Index()
pos = types.NewPosFromIndex(i)

assert.Equal(t, pos.X(), 5)
assert.Equal(t, pos.Y(), 8)
assert.Equal(t, pos.Z(), 12)
}

func TestSortPos(t *testing.T) {
p1 := &types.Pos{1, 2, 3}
p2 := &types.Pos{3, 2, 1}
Expand Down

0 comments on commit 9a0245a

Please sign in to comment.