Skip to content

Commit 94557a9

Browse files
committed
More warning cleanups, minor performance tweaks
1 parent e602ecc commit 94557a9

37 files changed

+417
-304
lines changed

DynmapCore/src/main/java/org/dynmap/common/chunk/GenericChunk.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.util.Arrays;
44

5-
import org.dynmap.renderer.DynmapBlockState;
65
import org.dynmap.common.BiomeMap;
6+
import org.dynmap.renderer.DynmapBlockState;
77

88
// Generic chunk representation
99
public class GenericChunk {
@@ -74,6 +74,7 @@ public final boolean isSectionEmpty(int cy) {
7474
public final long getInhabitedTicks() {
7575
return inhabitedTicks;
7676
}
77+
@Override
7778
public String toString() {
7879
return String.format("chunk(%d,%d:%s,off=%d", cx, cz, Arrays.deepToString((sections)), cy_min);
7980
}
@@ -91,7 +92,7 @@ public static class Builder {
9192
public Builder(int world_ymin, int world_ymax) {
9293
reset(world_ymin, world_ymax);
9394
}
94-
public void reset(int world_ymin, int world_ymax) {
95+
public final void reset(int world_ymin, int world_ymax) {
9596
x = 0; z = 0;
9697
y_min = world_ymin >> 4;
9798
dataversion = 0;
@@ -140,11 +141,11 @@ public Builder generateSky() {
140141
int totalval = 0; // Use for allzero or allfull
141142
int yidx = y << 7;
142143
// Light next layer down
143-
for (int x = 0; x < 16; x++) {
144-
for (int z = 0; z < 16; z++) {
145-
int idx = (z << 4) + x;
144+
for (int sx = 0; sx < 16; sx++) {
145+
for (int sz = 0; sz < 16; sz++) {
146+
int idx = (sz << 4) + sx;
146147
int val = sky[idx];
147-
DynmapBlockState bs = sect.blocks.getBlock(x, y, z); // Get block
148+
DynmapBlockState bs = sect.blocks.getBlock(sx, y, sz); // Get block
148149
int atten = bs.getLightAttenuation();
149150
if ((atten > 0) && (val > 0)) {
150151
val = (val >= atten) ? (val - atten) : 0;
@@ -163,12 +164,12 @@ public Builder generateSky() {
163164
boolean changed;
164165
do {
165166
changed = false;
166-
for (int x = 0; x < 16; x++) {
167-
for (int z = 0; z < 16; z++) {
168-
int idx = (z << 4) + x;
167+
for (int sx = 0; sx < 16; sx++) {
168+
for (int sz = 0; sz < 16; sz++) {
169+
int idx = (sz << 4) + sx;
169170
int cur = sky[idx];
170171
boolean curnonopaq = nonOpaque[idx];
171-
if (x < 15) { // If not right edge, check X spread
172+
if (sx < 15) { // If not right edge, check X spread
172173
int right = sky[idx+1];
173174
boolean rightnonopaq = nonOpaque[idx+1];
174175
// If spread right
@@ -180,7 +181,7 @@ else if (curnonopaq && (cur < (right - 1))) {
180181
sky[idx] = cur = right - 1; changed = true;
181182
}
182183
}
183-
if (z < 15) { // If not bottom edge, check Z spread
184+
if (sz < 15) { // If not bottom edge, check Z spread
184185
int down = sky[idx+16];
185186
boolean downnonopaq = nonOpaque[idx+16];
186187
// If spread down

DynmapCore/src/main/java/org/dynmap/common/chunk/GenericChunkCache.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.lang.ref.Reference;
44
import java.lang.ref.ReferenceQueue;
5-
import java.lang.ref.WeakReference;
65
import java.lang.ref.SoftReference;
6+
import java.lang.ref.WeakReference;
77
import java.util.HashMap;
88
import java.util.IdentityHashMap;
99
import java.util.LinkedHashMap;
@@ -20,13 +20,13 @@ public static class ChunkCacheRec {
2020

2121
private CacheHashMap snapcache;
2222
private final Object snapcachelock;
23-
private ReferenceQueue<ChunkCacheRec> refqueue;
23+
private final ReferenceQueue<ChunkCacheRec> refqueue;
2424
private long cache_attempts;
2525
private long cache_success;
26-
private boolean softref;
26+
private final boolean softref;
2727
// World name -> small integer ID, used to build long cache keys without String concatenation.
2828
// Accessed only while holding snapcachelock.
29-
private final HashMap<String, Integer> worldIds = new HashMap<String, Integer>();
29+
private final HashMap<String, Integer> worldIds = new HashMap<>();
3030
private int nextWorldId = 0;
3131

3232
private static class CacheRec {
@@ -35,14 +35,15 @@ private static class CacheRec {
3535

3636
@SuppressWarnings("serial")
3737
public class CacheHashMap extends LinkedHashMap<Long, CacheRec> {
38-
private int limit;
38+
private final int limit;
3939
private IdentityHashMap<Reference<ChunkCacheRec>, Long> reverselookup;
4040

4141
public CacheHashMap(int lim) {
4242
super(16, (float)0.75, true);
4343
limit = lim;
44-
reverselookup = new IdentityHashMap<Reference<ChunkCacheRec>, Long>();
44+
reverselookup = new IdentityHashMap<>();
4545
}
46+
@Override
4647
protected boolean removeEldestEntry(Map.Entry<Long, CacheRec> last) {
4748
boolean remove = (size() >= limit);
4849
if(remove && (last != null) && (last.getValue() != null)) {
@@ -58,7 +59,7 @@ protected boolean removeEldestEntry(Map.Entry<Long, CacheRec> last) {
5859
public GenericChunkCache(int max_size, boolean softref) {
5960
snapcachelock = new Object();
6061
snapcache = new CacheHashMap(max_size);
61-
refqueue = new ReferenceQueue<ChunkCacheRec>();
62+
refqueue = new ReferenceQueue<>();
6263
this.softref = softref;
6364
}
6465
/**
@@ -137,9 +138,9 @@ public void putSnapshot(String w, int chunkx, int chunkz, ChunkCacheRec ss) {
137138
processRefQueue();
138139
CacheRec rec = new CacheRec();
139140
if (softref)
140-
rec.ref = new SoftReference<ChunkCacheRec>(ss, refqueue);
141+
rec.ref = new SoftReference<>(ss, refqueue);
141142
else
142-
rec.ref = new WeakReference<ChunkCacheRec>(ss, refqueue);
143+
rec.ref = new WeakReference<>(ss, refqueue);
143144
synchronized(snapcachelock) {
144145
long key = getKey(w, chunkx, chunkz);
145146
CacheRec prevrec = (snapcache != null) ? snapcache.put(key, rec) : null;

DynmapCore/src/main/java/org/dynmap/common/chunk/GenericChunkSection.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ private static class BlockStateAccess3D implements BlockStateAccess {
2424
BlockStateAccess3D(DynmapBlockState bs[]) {
2525
blocks = bs;
2626
}
27+
@Override
2728
public final DynmapBlockState getBlock(int x, int y, int z) {
2829
return blocks[((y & 0xF) << 8) | ((z & 0xF) << 4) | (x & 0xF)];
2930
}
31+
@Override
3032
public final DynmapBlockState getBlock(GenericChunkPos pos) {
3133
return blocks[pos.soffset];
3234
}
@@ -39,9 +41,11 @@ private static class BlockStateAccess3DPalette implements BlockStateAccess {
3941
blocks = blks;
4042
palette = pal;
4143
}
44+
@Override
4245
public final DynmapBlockState getBlock(int x, int y, int z) {
4346
return palette[blocks[((y & 0xF) << 8) | ((z & 0xF) << 4) | (x & 0xF)]];
4447
}
48+
@Override
4549
public final DynmapBlockState getBlock(GenericChunkPos pos) {
4650
return palette[blocks[pos.soffset]];
4751
}
@@ -51,9 +55,11 @@ private static class BlockStateAccessSingle implements BlockStateAccess {
5155
BlockStateAccessSingle(DynmapBlockState bs) {
5256
block = bs;
5357
}
58+
@Override
5459
public final DynmapBlockState getBlock(int x, int y, int z) {
5560
return block;
5661
}
62+
@Override
5763
public final DynmapBlockState getBlock(GenericChunkPos pos) {
5864
return block;
5965
}
@@ -70,12 +76,15 @@ private static class BiomeAccess2D implements BiomeAccess {
7076
BiomeAccess2D(BiomeMap b[]) {
7177
biomes = b;
7278
}
79+
@Override
7380
public final BiomeMap getBiome(int x, int y, int z) {
7481
return biomes[((z & 0xF) << 4) + (x & 0xF)];
7582
}
83+
@Override
7684
public final BiomeMap getBiome(GenericChunkPos pos) {
7785
return biomes[pos.soffset & 0xFF]; // Just ZX portion
7886
}
87+
@Override
7988
public String toString() {
8089
return String.format("Biome2D(%s)", Arrays.deepToString(biomes));
8190
}
@@ -87,12 +96,15 @@ private static class BiomeAccess3D implements BiomeAccess {
8796
BiomeAccess3D(BiomeMap[] b) {
8897
biomes = b;
8998
}
99+
@Override
90100
public final BiomeMap getBiome(int x, int y, int z) {
91101
return biomes[ ((y & 0xC) << 2) | (z & 0xC) | ((x & 0xC) >> 2) ];
92102
}
103+
@Override
93104
public final BiomeMap getBiome(GenericChunkPos pos) {
94105
return biomes[pos.sdiv4offset];
95106
}
107+
@Override
96108
public String toString() {
97109
return String.format("Biome3D(%s)", Arrays.deepToString(biomes));
98110
}
@@ -103,12 +115,15 @@ private static class BiomeAccessSingle implements BiomeAccess {
103115
BiomeAccessSingle(BiomeMap b) {
104116
biome = b;
105117
}
118+
@Override
106119
public final BiomeMap getBiome(int x, int y, int z) {
107120
return biome;
108121
}
122+
@Override
109123
public final BiomeMap getBiome(GenericChunkPos pos) {
110124
return biome;
111125
}
126+
@Override
112127
public String toString() {
113128
return String.format("Biome1(%s)", biome);
114129
}
@@ -130,9 +145,11 @@ private static class LightingAccess3D implements LightingAccess {
130145
}
131146
}
132147
}
148+
@Override
133149
public final int getLight(int x, int y, int z) {
134150
return 0xF & (int)(light[((y & 0xF) << 4) | (z & 0xF)] >> ((x & 0xF) << 2));
135151
}
152+
@Override
136153
public final int getLight(GenericChunkPos pos) {
137154
return 0xF & (int)(light[pos.soffset >> 4] >> (4 * pos.sx));
138155
}
@@ -142,9 +159,11 @@ private static class LightingAccessSingle implements LightingAccess {
142159
LightingAccessSingle(int lig) {
143160
light = lig & 0xF;
144161
}
162+
@Override
145163
public final int getLight(int x, int y, int z) {
146164
return light;
147165
}
166+
@Override
148167
public final int getLight(GenericChunkPos pos) {
149168
return light;
150169
}
@@ -156,12 +175,13 @@ private GenericChunkSection(BlockStateAccess blks, BiomeAccess bio, LightingAcce
156175
emitted = emitac;
157176
isEmpty = empty;
158177
}
178+
@Override
159179
public String toString() {
160180
return String.format("sect(bip:%s)", biomes);
161181
}
162-
private static BiomeAccess defaultBiome = new BiomeAccessSingle(BiomeMap.NULL);
163-
private static BlockStateAccess defaultBlockState = new BlockStateAccessSingle(DynmapBlockState.AIR);
164-
private static LightingAccess defaultLight = new LightingAccessSingle(0);
182+
private static final BiomeAccess defaultBiome = new BiomeAccessSingle(BiomeMap.NULL);
183+
private static final BlockStateAccess defaultBlockState = new BlockStateAccessSingle(DynmapBlockState.AIR);
184+
private static final LightingAccess defaultLight = new LightingAccessSingle(0);
165185

166186
// Shared default empty section
167187
public static final GenericChunkSection EMPTY = new GenericChunkSection(defaultBlockState, defaultBiome, new LightingAccessSingle(15), defaultLight, true);
@@ -182,7 +202,7 @@ public Builder() {
182202
reset();
183203
}
184204
// Reset builder to default state
185-
public void reset() {
205+
public final void reset() {
186206
bsaccumsing = DynmapBlockState.AIR;
187207
bsaccum = null;
188208
baaccumsingle = BiomeMap.NULL;
@@ -250,12 +270,16 @@ public Builder singleBlockState(DynmapBlockState block) {
250270
}
251271
// Set block state
252272
public Builder xyzBlockState(int x, int y, int z, DynmapBlockState block) {
273+
return xyzBlockState(((y & 0xF) << 8) + ((z & 0xF) << 4) + (x & 0xF), block);
274+
}
275+
// Set block state (yzx=nibble order Y<<8 | Z << 4 | X)
276+
public Builder xyzBlockState(int yzx, DynmapBlockState block) {
253277
if (bsaccum == null) {
254278
bsaccum = new DynmapBlockState[4096];
255279
Arrays.fill(bsaccum, DynmapBlockState.AIR);
256280
bsaccumsing = DynmapBlockState.AIR;
257281
}
258-
bsaccum[((y & 0xF) << 8) + ((z & 0xF) << 4) + (x & 0xF)] = block;
282+
bsaccum[yzx] = block;
259283
empty = false;
260284
return this;
261285
}
@@ -269,10 +293,14 @@ public Builder xyzBlockStatePalette(DynmapBlockState[] bspalette) {
269293
}
270294
// Set block state using palette
271295
public Builder xyzBlockStateInPalette(int x, int y, int z, short palidx) {
296+
return xyzBlockStateInPalette(((y & 0xF) << 8) + ((z & 0xF) << 4) + (x & 0xF), palidx);
297+
}
298+
// Set block state using palette (yzx=nibble order Y<<8 | Z << 4 | X)
299+
public Builder xyzBlockStateInPalette(int yzx, short palidx) {
272300
if (bsblks == null) {
273301
bsblks = new short[4096];
274302
}
275-
bsblks[((y & 0xF) << 8) + ((z & 0xF) << 4) + (x & 0xF)] = palidx;
303+
bsblks[yzx] = palidx;
276304
empty = false;
277305
return this;
278306
}
@@ -293,8 +321,8 @@ public GenericChunkSection build() {
293321
if (bsaccum != null) {
294322
DynmapBlockState v = bsaccum[0]; // Get first
295323
boolean mismatch = false;
296-
for (int i = 0; i < bsaccum.length; i++) {
297-
if (bsaccum[i] != v) {
324+
for (DynmapBlockState bsaccum1 : bsaccum) {
325+
if (bsaccum1 != v) {
298326
mismatch = true;
299327
break;
300328
}
@@ -333,8 +361,8 @@ else if (bsaccumsing == DynmapBlockState.AIR) { // Just air?
333361
if (baaccum != null) {
334362
BiomeMap v = baaccum[0]; // Get first
335363
boolean mismatch = false;
336-
for (int i = 0; i < baaccum.length; i++) {
337-
if (baaccum[i] != v) {
364+
for (BiomeMap baaccum1 : baaccum) {
365+
if (baaccum1 != v) {
338366
mismatch = true;
339367
break;
340368
}

0 commit comments

Comments
 (0)