summaryrefslogtreecommitdiff
path: root/nbt
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-07 22:06:33 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-07 22:06:33 +0200
commite7b9d45bf2ed53fee66ad589161dd3e534e98c7f (patch)
treeb1ba590fa4878b91e2e329fdd90e26cfe4187eda /nbt
parentb990c3d2db0280f0c4b3dccc2520caf5f2b6b6e6 (diff)
downloadgonbt-e7b9d45bf2ed53fee66ad589161dd3e534e98c7f.tar.gz
gonbt-e7b9d45bf2ed53fee66ad589161dd3e534e98c7f.tar.bz2
gonbt-e7b9d45bf2ed53fee66ad589161dd3e534e98c7f.zip
More tag creation functions.
Diffstat (limited to 'nbt')
-rw-r--r--nbt/bigtest_test.go8
-rw-r--r--nbt/create.go32
2 files changed, 32 insertions, 8 deletions
diff --git a/nbt/bigtest_test.go b/nbt/bigtest_test.go
index af6c573..a0f6e7a 100644
--- a/nbt/bigtest_test.go
+++ b/nbt/bigtest_test.go
@@ -253,11 +253,7 @@ func TestCreateBigtest(t *testing.T) {
comp["egg"] = makeNested("Eggbert", 0.5)
rootcomp["nested compound test"] = Tag{TAG_Compound, comp}
- listlong := make([]interface{}, 5)
- for i := 0; i < 5; i++ {
- listlong[i] = int64(i + 11)
- }
- rootcomp["listTest (long)"] = Tag{TAG_List, TagList{TAG_Long, listlong}}
+ rootcomp["listTest (long)"] = NewListTag(TAG_Long, []int64{11, 12, 13, 14, 15})
listcomp := make([]interface{}, 2)
for i := 0; i < 2; i++ {
@@ -266,7 +262,7 @@ func TestCreateBigtest(t *testing.T) {
comp["created-on"] = NewLongTag(1264099775885)
listcomp[i] = comp
}
- rootcomp["listTest (compound)"] = Tag{TAG_List, TagList{TAG_Compound, listcomp}}
+ rootcomp["listTest (compound)"] = NewListTag(TAG_Compound, listcomp)
data := make([]byte, 1000)
for n := 0; n < 1000; n++ {
diff --git a/nbt/create.go b/nbt/create.go
index d515c1b..b3ff3e8 100644
--- a/nbt/create.go
+++ b/nbt/create.go
@@ -1,5 +1,9 @@
package nbt
+import (
+ "reflect"
+)
+
func NewByteTag(v byte) Tag { return Tag{TAG_Byte, v} }
func NewShortTag(v int16) Tag { return Tag{TAG_Short, v} }
func NewIntTag(v int32) Tag { return Tag{TAG_Int, v} }
@@ -10,5 +14,29 @@ func NewByteArrayTag(v []byte) Tag { return Tag{TAG_Byte_Array, v} }
func NewStringTag(v string) Tag { return Tag{TAG_String, v} }
func NewIntArrayTag(v []int32) Tag { return Tag{TAG_Int_Array, v} }
-// func NewCompoundTag() Tag {return Tag{TAG_Compound, make(TagCompound)}}
-// func NewListTag
+// NewCompoundTag creates a new Tag with type TAG_Compound. Usually it is more convenient to make the TagCompound payload and then manually construct the Tag value, though.
+func NewCompoundTag() Tag { return Tag{TAG_Compound, make(TagCompound)} }
+
+// NewListTag creates a new Tag of type TAG_List with tag elems of type ltt.
+//
+// l must either be of type []interface{}, where the elements are payloads for ltt tags OR of type []T, where T is the payload type for ltt tags.
+//
+// YOU are responsible for this, this function will not check the correctness.
+// When given wrong data, this function will either panic, or writing the NBT data will later fail.
+func NewListTag(ltt TagType, l interface{}) Tag {
+ var elems []interface{}
+ if is, ok := l.([]interface{}); ok {
+ elems = is
+ } else {
+ val := reflect.ValueOf(l)
+
+ n := val.Len()
+ elems = make([]interface{}, n)
+
+ for i := 0; i < n; i++ {
+ elems[i] = val.Index(i).Interface()
+ }
+ }
+
+ return Tag{TAG_List, TagList{ltt, elems}}
+}