1 package minecraft 2 3 import ( 4 "testing" 5 6 "vimagination.zapto.org/minecraft/nbt" 7 ) 8 9 func TestNew(t *testing.T) { 10 biomes := make(nbt.ByteArray, 256) 11 biome := int8(-1) 12 blocks := make(nbt.ByteArray, 4096) 13 add := make(nbt.ByteArray, 2048) 14 data := make(nbt.ByteArray, 2048) 15 for i := 0; i < 256; i++ { 16 biomes[i] = biome 17 //if biome++; biome >= 23 { 18 // biome = -1 19 //} 20 } 21 dataTag := nbt.NewTag("", nbt.Compound{ 22 nbt.NewTag("Level", nbt.Compound{ 23 nbt.NewTag("Biomes", biomes), 24 nbt.NewTag("HeightMap", make(nbt.IntArray, 256)), 25 nbt.NewTag("InhabitedTime", nbt.Long(0)), 26 nbt.NewTag("LastUpdate", nbt.Long(0)), 27 nbt.NewTag("Sections", &nbt.ListCompound{ 28 nbt.Compound{ 29 nbt.NewTag("Blocks", blocks), 30 nbt.NewTag("Add", add), 31 nbt.NewTag("Data", data), 32 nbt.NewTag("BlockLight", make(nbt.ByteArray, 2048)), 33 nbt.NewTag("SkyLight", make(nbt.ByteArray, 2048)), 34 nbt.NewTag("Y", nbt.Byte(0)), 35 }, 36 nbt.Compound{ 37 nbt.NewTag("Blocks", blocks), 38 nbt.NewTag("Add", add), 39 nbt.NewTag("Data", data), 40 nbt.NewTag("BlockLight", make(nbt.ByteArray, 2048)), 41 nbt.NewTag("SkyLight", make(nbt.ByteArray, 2048)), 42 nbt.NewTag("Y", nbt.Byte(1)), 43 }, 44 nbt.Compound{ 45 nbt.NewTag("Blocks", blocks), 46 nbt.NewTag("Add", add), 47 nbt.NewTag("Data", data), 48 nbt.NewTag("BlockLight", make(nbt.ByteArray, 2048)), 49 nbt.NewTag("SkyLight", make(nbt.ByteArray, 2048)), 50 nbt.NewTag("Y", nbt.Byte(3)), 51 }, 52 nbt.Compound{ 53 nbt.NewTag("Blocks", blocks), 54 nbt.NewTag("Add", add), 55 nbt.NewTag("Data", data), 56 nbt.NewTag("BlockLight", make(nbt.ByteArray, 2048)), 57 nbt.NewTag("SkyLight", make(nbt.ByteArray, 2048)), 58 nbt.NewTag("Y", nbt.Byte(10)), 59 }, 60 }), 61 nbt.NewTag("TileEntities", &nbt.ListCompound{ 62 nbt.Compound{ 63 nbt.NewTag("id", nbt.String("test1")), 64 nbt.NewTag("x", nbt.Int(-191)), 65 nbt.NewTag("y", nbt.Int(13)), 66 nbt.NewTag("z", nbt.Int(379)), 67 nbt.NewTag("testTag", nbt.Byte(1)), 68 }, 69 nbt.Compound{ 70 nbt.NewTag("id", nbt.String("test2")), 71 nbt.NewTag("x", nbt.Int(-191)), 72 nbt.NewTag("y", nbt.Int(17)), 73 nbt.NewTag("z", nbt.Int(372)), 74 nbt.NewTag("testTag", nbt.Long(8)), 75 }, 76 }), 77 nbt.NewTag("Entities", &nbt.ListCompound{ 78 nbt.Compound{ 79 nbt.NewTag("id", nbt.String("testEntity1")), 80 nbt.NewTag("Pos", &nbt.ListDouble{ 81 nbt.Double(-190), 82 nbt.Double(13), 83 nbt.Double(375), 84 }), 85 nbt.NewTag("Motion", &nbt.ListDouble{ 86 nbt.Double(1), 87 nbt.Double(13), 88 nbt.Double(11), 89 }), 90 nbt.NewTag("Rotation", &nbt.ListFloat{ 91 nbt.Float(13), 92 nbt.Float(11), 93 }), 94 nbt.NewTag("FallDistance", nbt.Float(0)), 95 nbt.NewTag("Fire", nbt.Short(-1)), 96 nbt.NewTag("Air", nbt.Short(300)), 97 nbt.NewTag("OnGround", nbt.Byte(1)), 98 nbt.NewTag("Dimension", nbt.Int(0)), 99 nbt.NewTag("Invulnerable", nbt.Byte(0)), 100 nbt.NewTag("PortalCooldown", nbt.Int(0)), 101 nbt.NewTag("UUIDMost", nbt.Long(0)), 102 nbt.NewTag("UUIDLease", nbt.Long(0)), 103 nbt.NewTag("Riding", nbt.Compound{}), 104 }, 105 nbt.Compound{ 106 nbt.NewTag("id", nbt.String("testEntity2")), 107 nbt.NewTag("Pos", &nbt.ListDouble{ 108 nbt.Double(-186), 109 nbt.Double(2), 110 nbt.Double(378), 111 }), 112 nbt.NewTag("Motion", &nbt.ListDouble{ 113 nbt.Double(17.5), 114 nbt.Double(1000), 115 nbt.Double(54), 116 }), 117 nbt.NewTag("Rotation", &nbt.ListFloat{ 118 nbt.Float(11), 119 nbt.Float(13), 120 }), 121 nbt.NewTag("FallDistance", nbt.Float(30)), 122 nbt.NewTag("Fire", nbt.Short(4)), 123 nbt.NewTag("Air", nbt.Short(30)), 124 nbt.NewTag("OnGround", nbt.Byte(0)), 125 nbt.NewTag("Dimension", nbt.Int(0)), 126 nbt.NewTag("Invulnerable", nbt.Byte(1)), 127 nbt.NewTag("PortalCooldown", nbt.Int(10)), 128 nbt.NewTag("UUIDMost", nbt.Long(1450)), 129 nbt.NewTag("UUIDLease", nbt.Long(6435)), 130 nbt.NewTag("Riding", nbt.Compound{}), 131 }, 132 }), 133 nbt.NewTag("TileTicks", &nbt.ListCompound{ 134 nbt.Compound{ 135 nbt.NewTag("i", nbt.Int(0)), 136 nbt.NewTag("t", nbt.Int(0)), 137 nbt.NewTag("p", nbt.Int(0)), 138 nbt.NewTag("x", nbt.Int(-192)), 139 nbt.NewTag("y", nbt.Int(0)), 140 nbt.NewTag("z", nbt.Int(368)), 141 }, 142 nbt.Compound{ 143 nbt.NewTag("i", nbt.Int(1)), 144 nbt.NewTag("t", nbt.Int(34)), 145 nbt.NewTag("p", nbt.Int(12)), 146 nbt.NewTag("x", nbt.Int(-186)), 147 nbt.NewTag("y", nbt.Int(11)), 148 nbt.NewTag("z", nbt.Int(381)), 149 }, 150 }), 151 nbt.NewTag("TerrainPopulated", nbt.Byte(1)), 152 nbt.NewTag("xPos", nbt.Int(-12)), 153 nbt.NewTag("zPos", nbt.Int(23)), 154 }), 155 }) 156 if _, err := newChunk(-12, 23, dataTag); err != nil { 157 t.Fatalf("reveived unexpected error during testing, %q", err.Error()) 158 } 159 } 160 161 func TestBiomes(t *testing.T) { 162 chunk, _ := newChunk(0, 0, nbt.Tag{}) 163 for b := Biome(0); b < 23; b++ { 164 biome := b 165 for x := int32(0); x < 16; x++ { 166 for z := int32(0); z < 16; z++ { 167 chunk.SetBiome(x, z, biome) 168 if newB := chunk.GetBiome(x, z); newB != biome { 169 t.Errorf("error setting biome at co-ordinates, expecting %q, got %q", biome.String(), newB.String()) 170 } 171 } 172 } 173 } 174 } 175 176 func TestBlock(t *testing.T) { 177 chunk, _ := newChunk(0, 0, nbt.Tag{}) 178 testBlocks := []struct { 179 Block 180 x, y, z int32 181 recheck bool 182 }{ 183 //Test simple set 184 { 185 Block{ 186 ID: 12, 187 }, 188 0, 0, 0, 189 true, 190 }, 191 //Test higher ids 192 { 193 Block{ 194 ID: 853, 195 }, 196 1, 0, 0, 197 true, 198 }, 199 { 200 Block{ 201 ID: 463, 202 }, 203 2, 0, 0, 204 true, 205 }, 206 { 207 Block{ 208 ID: 1001, 209 }, 210 3, 0, 0, 211 true, 212 }, 213 //Test data set 214 { 215 Block{ 216 ID: 143, 217 Data: 12, 218 }, 219 0, 1, 0, 220 true, 221 }, 222 { 223 Block{ 224 ID: 153, 225 Data: 4, 226 }, 227 1, 1, 0, 228 true, 229 }, 230 { 231 Block{ 232 ID: 163, 233 Data: 5, 234 }, 235 2, 1, 0, 236 true, 237 }, 238 //Test metadata [un]set 239 { 240 Block{ 241 metadata: nbt.Compound{ 242 nbt.NewTag("testInt2", nbt.Int(1743)), 243 nbt.NewTag("testString2", nbt.String("world")), 244 }, 245 }, 246 0, 0, 1, 247 true, 248 }, 249 { 250 Block{ 251 metadata: nbt.Compound{ 252 nbt.NewTag("testInt", nbt.Int(15)), 253 nbt.NewTag("testString", nbt.String("hello")), 254 }, 255 }, 256 1, 0, 1, 257 false, 258 }, 259 { 260 Block{}, 261 1, 0, 1, 262 true, 263 }, 264 //Test tick [un]set 265 { 266 Block{ 267 ticks: []Tick{{123, 1, 4}, {123, 7, -1}}, 268 }, 269 0, 1, 1, 270 true, 271 }, 272 { 273 Block{ 274 ticks: []Tick{{654, 4, 6}, {4, 63, 5}, {4, 5, 9}}, 275 }, 276 1, 1, 1, 277 false, 278 }, 279 { 280 Block{}, 281 1, 1, 1, 282 true, 283 }, 284 } 285 for _, tB := range testBlocks { 286 chunk.SetBlock(tB.x, tB.y, tB.z, tB.Block) 287 if block := chunk.GetBlock(tB.x, tB.y, tB.z); !tB.Block.EqualBlock(block) { 288 t.Errorf("blocks do not match, expecting %s, got %s", tB.Block.String(), block.String()) 289 } 290 } 291 for _, tB := range testBlocks { 292 if tB.recheck { 293 if block := chunk.GetBlock(tB.x, tB.y, tB.z); !tB.Block.EqualBlock(block) { 294 t.Errorf("blocks do not match, expecting:-\n%s\ngot:-\n%s", tB.Block.String(), block.String()) 295 } 296 } 297 } 298 } 299 300 func TestHeightMap(t *testing.T) { 301 tests := []struct { 302 x, y, z int32 303 Block 304 height int32 305 }{ 306 {0, 0, 0, Block{}, 0}, 307 {1, 0, 0, Block{ID: 1}, 1}, 308 {1, 1, 0, Block{ID: 1}, 2}, 309 {1, 0, 0, Block{}, 2}, 310 {1, 1, 0, Block{}, 0}, 311 {2, 10, 0, Block{ID: 1}, 11}, 312 {2, 12, 0, Block{ID: 1}, 13}, 313 {2, 12, 0, Block{}, 11}, 314 {2, 10, 0, Block{}, 0}, 315 {3, 15, 0, Block{ID: 1}, 16}, 316 {3, 16, 0, Block{ID: 1}, 17}, 317 {3, 16, 0, Block{}, 16}, 318 {3, 15, 0, Block{}, 0}, 319 {4, 31, 0, Block{ID: 1}, 32}, 320 {4, 32, 0, Block{ID: 1}, 33}, 321 {4, 32, 0, Block{}, 32}, 322 {4, 31, 0, Block{}, 0}, 323 {5, 16, 0, Block{ID: 1}, 17}, 324 {5, 32, 0, Block{ID: 1}, 33}, 325 {5, 32, 0, Block{}, 17}, 326 {5, 16, 0, Block{}, 0}, 327 } 328 chunk, _ := newChunk(0, 0, nbt.Tag{}) 329 for n, test := range tests { 330 chunk.SetBlock(test.x, test.y, test.z, test.Block) 331 if h := chunk.GetHeight(test.x, test.z); h != test.height { 332 t.Errorf("test %d: expecting height %d, got %d", n+1, test.height, h) 333 } 334 } 335 } 336