исключена ссылка на World в Location и ChunkSection
This commit is contained in:
@@ -6,16 +6,24 @@ package mc.core;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.exception.ResourceUnloadedException;
|
||||
import mc.core.world.World;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import mc.core.world.chunk.ChunkSection;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class EntityLocation extends Location implements Cloneable {
|
||||
@Getter
|
||||
@Setter
|
||||
private float yaw, pitch;
|
||||
private Reference<World> refWorld;
|
||||
|
||||
public EntityLocation(double x, double y, double z, float yaw, float pitch, World world) {
|
||||
super(x, y, z, world);
|
||||
super(x, y, z);
|
||||
setYawPitch(yaw, pitch);
|
||||
setWorld(world);
|
||||
}
|
||||
|
||||
public void setYawPitch(float yaw, float pitch) {
|
||||
@@ -27,6 +35,38 @@ public class EntityLocation extends Location implements Cloneable {
|
||||
setYawPitch(entityLocation.yaw, entityLocation.pitch);
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
if (refWorld == null) {
|
||||
return null;
|
||||
} else if (refWorld.get() == null) {
|
||||
throw new ResourceUnloadedException("World unloaded");
|
||||
} else {
|
||||
return refWorld.get();
|
||||
}
|
||||
}
|
||||
|
||||
public void setWorld (World world) {
|
||||
this.refWorld = new WeakReference<>(world);
|
||||
}
|
||||
|
||||
public Chunk getChunk() {
|
||||
World world = getWorld();
|
||||
if (world == null) {
|
||||
return null;
|
||||
} else {
|
||||
return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
public ChunkSection getChunkSection() {
|
||||
Chunk chunk = getChunk();
|
||||
if (chunk == null) {
|
||||
return null;
|
||||
} else {
|
||||
return chunk.getChunkSection(getBlockY() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityLocation clone() {
|
||||
return (EntityLocation) super.clone();
|
||||
|
||||
@@ -6,23 +6,14 @@ package mc.core;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.exception.ResourceUnloadedException;
|
||||
import mc.core.world.World;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import mc.core.world.chunk.ChunkSection;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class Location implements Cloneable {
|
||||
@Getter
|
||||
@Setter
|
||||
private double x, y, z;
|
||||
private Reference<World> refWorld;
|
||||
|
||||
public Location (double x, double y, double z, World world) {
|
||||
public Location (double x, double y, double z) {
|
||||
setXYZ(x, y, z);
|
||||
setWorld(world);
|
||||
}
|
||||
|
||||
public void setXYZ(double x, double y, double z) {
|
||||
@@ -35,20 +26,6 @@ public class Location implements Cloneable {
|
||||
setXYZ(location.x, location.y, location.z);
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
if (refWorld == null) {
|
||||
return null;
|
||||
} else if (refWorld.get() == null) {
|
||||
throw new ResourceUnloadedException("World unloaded");
|
||||
} else {
|
||||
return refWorld.get();
|
||||
}
|
||||
}
|
||||
|
||||
public void setWorld (World world) {
|
||||
this.refWorld = new WeakReference<>(world);
|
||||
}
|
||||
|
||||
public int getBlockX() {
|
||||
return Double.valueOf(Math.floor(x)).intValue();
|
||||
}
|
||||
@@ -61,24 +38,6 @@ public class Location implements Cloneable {
|
||||
return Double.valueOf(Math.floor(z)).intValue();
|
||||
}
|
||||
|
||||
public Chunk getChunk() {
|
||||
World world = getWorld();
|
||||
if (world == null) {
|
||||
return null;
|
||||
} else {
|
||||
return world.getChunk(getBlockX() >> 4, getBlockZ() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
public ChunkSection getChunkSection() {
|
||||
Chunk chunk = getChunk();
|
||||
if (chunk == null) {
|
||||
return null;
|
||||
} else {
|
||||
return chunk.getChunkSection(getBlockY() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location clone() {
|
||||
try {
|
||||
|
||||
@@ -5,15 +5,15 @@ import mc.core.world.World;
|
||||
|
||||
public class BlockFactory {
|
||||
|
||||
public Block create(BlockType blockType, int x, int y, int z, World world) {
|
||||
return new EmbeddedBlock(blockType, x, y, z, world);
|
||||
public Block create(BlockType blockType, int x, int y, int z) {
|
||||
return new EmbeddedBlock(blockType, x, y, z);
|
||||
}
|
||||
|
||||
/** For first-time generation */
|
||||
private class EmbeddedBlock extends AbstractBlock {
|
||||
EmbeddedBlock(BlockType type, int x, int y, int z, World world) {
|
||||
EmbeddedBlock(BlockType type, int x, int y, int z) {
|
||||
super(type);
|
||||
setLocation(new Location(x,y,z, world));
|
||||
setLocation(new Location(x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,4 @@ public interface ChunkSection {
|
||||
void setAddition(int x, int y, int z, int value);
|
||||
|
||||
Biome getBiome(int localX, int localZ);
|
||||
|
||||
World getWorld();
|
||||
}
|
||||
|
||||
@@ -1,19 +1,37 @@
|
||||
package mc.core;
|
||||
|
||||
import mc.core.world.World;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class TestEntityLocation {
|
||||
private World world;
|
||||
|
||||
@Before
|
||||
public void prepareWorld() {
|
||||
this.world = mock(World.class);
|
||||
when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> {
|
||||
Object[] args = invocation.getArguments();
|
||||
|
||||
Chunk chunk = mock(Chunk.class);
|
||||
when(chunk.getX()).thenReturn((int) args[0]);
|
||||
when(chunk.getZ()).thenReturn((int) args[1]);
|
||||
|
||||
return chunk;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloneTest() {
|
||||
World dummyWorld = mock(World.class);
|
||||
|
||||
EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, dummyWorld);
|
||||
assertSame("Lost world reference before cloning", dummyWorld, firstLocation.getWorld());
|
||||
EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, world);
|
||||
assertSame("Lost world reference before cloning", world, firstLocation.getWorld());
|
||||
EntityLocation locationClone = firstLocation.clone();
|
||||
|
||||
assertEquals("X mismatch", firstLocation.getX(), locationClone.getX(), 0);
|
||||
@@ -23,4 +41,56 @@ public class TestEntityLocation {
|
||||
assertEquals("Yaw mismatch", firstLocation.getYaw(), locationClone.getYaw(), 0);
|
||||
assertSame("World mismatch (accidental clone of the World object?)", firstLocation.getWorld(), locationClone.getWorld());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetChunk() {
|
||||
EntityLocation location;
|
||||
Chunk chunk;
|
||||
|
||||
location = new EntityLocation(0d, 0, 0d, 0f, 0f, 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,15 @@
|
||||
package mc.core;
|
||||
|
||||
import mc.core.world.World;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class TestLocation {
|
||||
private World world;
|
||||
|
||||
@Before
|
||||
public void prepareWorld() {
|
||||
this.world = mock(World.class);
|
||||
when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> {
|
||||
Object[] args = invocation.getArguments();
|
||||
|
||||
Chunk chunk = mock(Chunk.class);
|
||||
when(chunk.getX()).thenReturn((int) args[0]);
|
||||
when(chunk.getZ()).thenReturn((int) args[1]);
|
||||
|
||||
return chunk;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBlockXZ() {
|
||||
Location location;
|
||||
|
||||
location = new Location(0d, 0, 0d, world);
|
||||
location = new Location(0d, 0, 0d);
|
||||
assertEquals(0, location.getBlockX());
|
||||
assertEquals(0, location.getBlockZ());
|
||||
|
||||
@@ -69,56 +49,4 @@ public class TestLocation {
|
||||
assertEquals(-2, location.getBlockX());
|
||||
assertEquals(-2, location.getBlockZ());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetChunk() {
|
||||
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