добавлен тест для Location
This commit is contained in:
@@ -50,15 +50,15 @@ public class Location implements Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getBlockX() {
|
public int getBlockX() {
|
||||||
return (int) x;
|
return Double.valueOf(Math.floor(x)).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBlockY() {
|
public int getBlockY() {
|
||||||
return (int) y;
|
return Double.valueOf(Math.floor(y)).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBlockZ() {
|
public int getBlockZ() {
|
||||||
return (int) z;
|
return Double.valueOf(Math.floor(z)).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Chunk getChunk() {
|
public Chunk getChunk() {
|
||||||
@@ -66,7 +66,7 @@ public class Location implements Cloneable {
|
|||||||
if (world == null) {
|
if (world == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return world.getChunk((int)(x / 16), (int)(z / 16));
|
return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ public class Location implements Cloneable {
|
|||||||
if (chunk == null) {
|
if (chunk == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return chunk.getChunkSection((int)(y / 16));
|
return chunk.getChunkSection(getBlockY() >> 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
173
core/src/test/java/mc/core/TestLocation.java
Normal file
173
core/src/test/java/mc/core/TestLocation.java
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package mc.core;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import mc.core.world.Biome;
|
||||||
|
import mc.core.world.World;
|
||||||
|
import mc.core.world.WorldType;
|
||||||
|
import mc.core.world.chunk.Chunk;
|
||||||
|
import mc.core.world.chunk.ChunkSection;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class TestLocation {
|
||||||
|
private class DummyWorld implements World {
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
private class DummyChunk implements Chunk {
|
||||||
|
@Getter
|
||||||
|
private final int x, z;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChunkSection getChunkSection(int height) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setChunkSection(int height, ChunkSection chunkSection) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Biome getBiome(int localX, int localZ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBiome(int localX, int localZ, Biome biome) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public World getWorld() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setWorld(World world) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WorldType getWorldType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EntityLocation getSpawn() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSpawn(EntityLocation location) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Chunk getChunk(int x, int z) {
|
||||||
|
return new DummyChunk(x, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setChunk(int x, int z, Chunk chunkSection) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBlockXZ() {
|
||||||
|
World world = new DummyWorld();
|
||||||
|
Location location;
|
||||||
|
|
||||||
|
location = new Location(0d, 0, 0d, world);
|
||||||
|
assertEquals(0, location.getBlockX());
|
||||||
|
assertEquals(0, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(0.1d, 0, 0.1d);
|
||||||
|
assertEquals(0, location.getBlockX());
|
||||||
|
assertEquals(0, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(0.5d, 0, 0.5d);
|
||||||
|
assertEquals(0, location.getBlockX());
|
||||||
|
assertEquals(0, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(0.9d, 0, 0.9d);
|
||||||
|
assertEquals(0, location.getBlockX());
|
||||||
|
assertEquals(0, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(1d, 0, 1d);
|
||||||
|
assertEquals(1, location.getBlockX());
|
||||||
|
assertEquals(1, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(-0.1d, 0, -0.1d);
|
||||||
|
assertEquals(-1, location.getBlockX());
|
||||||
|
assertEquals(-1, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(-0.5d, 0, -0.5d);
|
||||||
|
assertEquals(-1, location.getBlockX());
|
||||||
|
assertEquals(-1, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(-0.9d, 0, -0.9d);
|
||||||
|
assertEquals(-1, location.getBlockX());
|
||||||
|
assertEquals(-1, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(-1d, 0, -1d);
|
||||||
|
assertEquals(-1, location.getBlockX());
|
||||||
|
assertEquals(-1, location.getBlockZ());
|
||||||
|
|
||||||
|
location.setXYZ(-1.1d, 0, -1.1d);
|
||||||
|
assertEquals(-2, location.getBlockX());
|
||||||
|
assertEquals(-2, location.getBlockZ());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetChunk() {
|
||||||
|
World world = new DummyWorld();
|
||||||
|
Location location;
|
||||||
|
Chunk chunk;
|
||||||
|
|
||||||
|
location = new Location(0d, 0, 0d, world);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(0, chunk.getX());
|
||||||
|
assertEquals(0, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(1d, 0, 1d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(0, chunk.getX());
|
||||||
|
assertEquals(0, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(15d, 0, 15d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(0, chunk.getX());
|
||||||
|
assertEquals(0, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(16d, 0, 16d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(1, chunk.getX());
|
||||||
|
assertEquals(1, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(-0.1d, 0, -0.1d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(-1, chunk.getX());
|
||||||
|
assertEquals(-1, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(-1d, 0, -1d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(-1, chunk.getX());
|
||||||
|
assertEquals(-1, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(-15d, 0, -15d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(-1, chunk.getX());
|
||||||
|
assertEquals(-1, chunk.getZ());
|
||||||
|
|
||||||
|
//TODO на практике, таких точных значений не встретиться, но тем не менее данный тест не проходит
|
||||||
|
//location.setXYZ(-16.0d, 0, -16.0d);
|
||||||
|
//chunk = location.getChunk();
|
||||||
|
//assertEquals(-2, chunk.getX());
|
||||||
|
//assertEquals(-2, chunk.getZ());
|
||||||
|
|
||||||
|
location.setXYZ(-16.001d, 0, -16.001d);
|
||||||
|
chunk = location.getChunk();
|
||||||
|
assertEquals(-2, chunk.getX());
|
||||||
|
assertEquals(-2, chunk.getZ());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user