-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
CachingChunkBlockSource.java
111 lines (92 loc) · 3.2 KB
/
CachingChunkBlockSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.citizensnpcs.api.astar.pathfinder;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import com.google.common.collect.Maps;
public abstract class CachingChunkBlockSource<T> extends BlockSource {
private final Map<ChunkCoord, ChunkCache> chunkCache = Maps.newHashMap();
private final Object[][] chunks;
private final int chunkX;
private final int chunkZ;
protected final World world;
protected CachingChunkBlockSource(Location location, float radius) {
this(location.getWorld(), location.getBlockX(), location.getBlockZ(), radius);
}
protected CachingChunkBlockSource(World world, int x, int z, float radius) {
this(world, (int) (x - radius), (int) (z - radius), (int) (x + radius), (int) (z + radius));
}
protected CachingChunkBlockSource(World world, int minX, int minZ, int maxX, int maxZ) {
this.world = world;
this.chunkX = minX >> 4;
this.chunkZ = minZ >> 4;
int maxChunkX = maxX >> 4, maxChunkZ = maxZ >> 4;
chunks = new Object[maxChunkX - chunkX + 1][maxChunkZ - chunkZ + 1];
for (int x = chunkX; x < maxChunkX; x++) {
for (int z = chunkZ; z < maxChunkZ; z++) {
chunks[x - chunkX][z - chunkZ] = getChunkObject(x, z);
}
}
}
@Override
public int getBlockTypeIdAt(int x, int y, int z) {
T chunk = getSpecific(x, z);
if (chunk != null)
return getId(chunk, x & 15, y, z & 15);
return world.getBlockTypeIdAt(x, y, z);
}
protected abstract T getChunkObject(int x, int z);
protected abstract int getId(T chunk, int x, int y, int z);
protected abstract int getLightLevel(T chunk, int x, int y, int z);
@SuppressWarnings("unchecked")
private T getSpecific(int x, int z) {
int xx = (x >> 4) - chunkX;
int zz = (z >> 4) - chunkZ;
if (xx >= 0 && xx < chunks.length) {
Object[] inner = chunks[xx];
if (zz >= 0 && zz < inner.length) {
return (T) inner[zz];
}
}
ChunkCoord key = new ChunkCoord(x >> 4, z >> 4);
ChunkCache prev = chunkCache.get(key);
if (prev == null) {
chunkCache.put(key, prev = new ChunkCache());
} else if (prev.obj != null) {
return prev.obj;
} else if (++prev.hitCount >= 2) {
return prev.obj = getChunkObject(x >> 4, z >> 4);
}
return null;
}
@Override
public World getWorld() {
return world;
}
private class ChunkCache {
int hitCount;
T obj;
}
private static class ChunkCoord {
int x, z;
public ChunkCoord(int xx, int zz) {
this.x = xx;
this.z = zz;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
ChunkCoord other = (ChunkCoord) obj;
return x == other.x && z == other.z;
}
@Override
public int hashCode() {
int result = 31 * x;
return 31 * result + z;
}
}
}