forked from lishid/Orebfuscator
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce memory footprint of proximity block data
- Loading branch information
Showing
9 changed files
with
159 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
orebfuscator-common/src/test/java/net/imprex/orebfuscator/util/BlockPosTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.imprex.orebfuscator.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class BlockPosTest { | ||
|
||
@Test | ||
public void testLongFormat() { | ||
BlockPos positionA = new BlockPos(-52134, BlockPos.MAX_Y, 6243234); | ||
BlockPos positionB = new BlockPos(0, BlockPos.MIN_Y, -4); | ||
BlockPos positionC = new BlockPos(15, 0, -5663423); | ||
BlockPos positionD = new BlockPos(21523, 16, -5663423); | ||
|
||
long valueA = positionA.toLong(); | ||
long valueB = positionB.toLong(); | ||
long valueC = positionC.toLong(); | ||
long valueD = positionD.toLong(); | ||
|
||
assertEquals(positionA, BlockPos.fromLong(valueA)); | ||
assertEquals(positionB, BlockPos.fromLong(valueB)); | ||
assertEquals(positionC, BlockPos.fromLong(valueC)); | ||
assertEquals(positionD, BlockPos.fromLong(valueD)); | ||
} | ||
|
||
@Test | ||
public void testSectionPos() { | ||
final int chunkX = -42 << 4; | ||
final int chunkZ = 6521 << 4; | ||
|
||
BlockPos positionA = new BlockPos(chunkX + 8, BlockPos.MAX_Y, chunkZ); | ||
BlockPos positionB = new BlockPos(chunkX, BlockPos.MIN_Y, chunkZ + 15); | ||
BlockPos positionC = new BlockPos(chunkX + 15, 0, chunkZ + 4); | ||
|
||
int sectionPosA = positionA.toSectionPos(); | ||
int sectionPosB = positionB.toSectionPos(); | ||
int sectionPosC = positionC.toSectionPos(); | ||
|
||
assertEquals(positionA, BlockPos.fromSectionPos(chunkX, chunkZ, sectionPosA)); | ||
assertEquals(positionB, BlockPos.fromSectionPos(chunkX, chunkZ, sectionPosB)); | ||
assertEquals(positionC, BlockPos.fromSectionPos(chunkX, chunkZ, sectionPosC)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...fuscator-plugin/src/main/java/net/imprex/orebfuscator/player/OrebfuscatorPlayerChunk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package net.imprex.orebfuscator.player; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import net.imprex.orebfuscator.util.BlockPos; | ||
|
||
public class OrebfuscatorPlayerChunk { | ||
|
||
private final int chunkX; | ||
private final int chunkZ; | ||
|
||
private int proximitySize; | ||
private final int[] proximityBlocks; | ||
|
||
public OrebfuscatorPlayerChunk(int chunkX, int chunkZ, List<BlockPos> proximityBlocks) { | ||
this.chunkX = chunkX; | ||
this.chunkZ = chunkZ; | ||
|
||
this.proximitySize = proximityBlocks.size(); | ||
this.proximityBlocks = new int[proximityBlocks.size()]; | ||
|
||
for (int i = 0; i < proximityBlocks.size(); i++) { | ||
this.proximityBlocks[i] = proximityBlocks.get(i).toSectionPos(); | ||
} | ||
} | ||
|
||
public boolean isEmpty() { | ||
return proximitySize <= 0; | ||
} | ||
|
||
public Iterator<BlockPos> proximityIterator() { | ||
return new ProximityItr(); | ||
} | ||
|
||
private class ProximityItr implements Iterator<BlockPos> { | ||
|
||
private final int x = chunkX << 4; | ||
private final int z = chunkZ << 4; | ||
|
||
private int cursor; | ||
private int returnCursor = -1; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor < proximitySize; | ||
} | ||
|
||
@Override | ||
public BlockPos next() { | ||
if (cursor >= proximitySize) | ||
throw new NoSuchElementException(); | ||
|
||
int sectionPos = proximityBlocks[returnCursor = cursor]; | ||
cursor++; | ||
|
||
return BlockPos.fromSectionPos(x, z, sectionPos); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
if (returnCursor < 0) | ||
throw new IllegalStateException(); | ||
|
||
// remove entry | ||
final int index = returnCursor; | ||
final int newSize; | ||
if ((newSize = proximitySize - 1) > index) | ||
System.arraycopy(proximityBlocks, index + 1, proximityBlocks, index, newSize - index); | ||
proximityBlocks[proximitySize = newSize] = 0xFFFFFFFF; | ||
|
||
// update cursor positions | ||
cursor = returnCursor; | ||
returnCursor = -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters