Archived
0

исключена ссылка на World в Location и ChunkSection

This commit is contained in:
2018-08-26 01:13:21 +03:00
parent 2147c18f81
commit 464a2e7be6
12 changed files with 133 additions and 154 deletions

View File

@@ -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();

View File

@@ -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 {

View File

@@ -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));
}
}
}

View File

@@ -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();
}