Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve overall memory footprint per player #359

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public static BlockPos fromLong(long value) {
return new BlockPos(x, y, z);
}

public int toSectionPos() {
return (this.x & 0xF) << 12 | (this.y & 0xFFF) << 0 | (this.z & 0xF) << 16;
}

public static BlockPos fromSectionPos(int x, int z, int sectionPos) {
x += (sectionPos >> 12) & 0xF;
int y = (sectionPos << 20 >> 20);
z += (sectionPos >> 16) & 0xF;
return new BlockPos(x, y, z);
}

@Override
public int compareTo(BlockPos other) {
if (this.y == other.y) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.imprex.orebfuscator.util;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import com.google.common.collect.ImmutableList;

Expand All @@ -12,12 +14,12 @@ public static Builder builder(NamespacedKey key) {

private final NamespacedKey key;
private final BlockStateProperties defaultBlockState;
private final ImmutableList<BlockStateProperties> possibleBlockStates;
private final ImmutableList<BlockStateProperties> blockStates;

private BlockProperties(Builder builder) {
this.key = builder.key;
this.defaultBlockState = builder.defaultBlockState;
this.possibleBlockStates = builder.possibleBlockStates;
this.blockStates = ImmutableList.copyOf(builder.blockStates);
}

public NamespacedKey getKey() {
Expand All @@ -28,8 +30,8 @@ public BlockStateProperties getDefaultBlockState() {
return defaultBlockState;
}

public ImmutableList<BlockStateProperties> getPossibleBlockStates() {
return possibleBlockStates;
public ImmutableList<BlockStateProperties> getBlockStates() {
return blockStates;
}

@Override
Expand All @@ -51,34 +53,44 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "BlockProperties [key=" + key + ", defaultBlockState=" + defaultBlockState + ", possibleBlockStates="
+ possibleBlockStates + "]";
return "BlockProperties [key=" + key + ", defaultBlockState=" + defaultBlockState + ", blockStates="
+ blockStates + "]";
}

public static class Builder {

private final NamespacedKey key;

private BlockStateProperties defaultBlockState;
private ImmutableList<BlockStateProperties> possibleBlockStates;
private final Set<BlockStateProperties> blockStates = new HashSet<>();

private Builder(NamespacedKey key) {
this.key = key;
}

public Builder withDefaultBlockState(BlockStateProperties defaultBlockState) {
this.defaultBlockState = defaultBlockState;
return this;
}
public Builder withBlockState(BlockStateProperties blockState) {
if (!blockStates.add(blockState)) {
throw new IllegalStateException(String.format("duplicate block state id (%s) for block: %s", blockState.getId(), key));
}

if (blockState.isDefaultState()) {
// check for multiple default blocks
if (this.defaultBlockState != null) {
throw new IllegalStateException(String.format("multiple default block states for block: %s", blockState.getId(), key));
}

this.defaultBlockState = blockState;
}

public Builder withPossibleBlockStates(ImmutableList<BlockStateProperties> possibleBlockStates) {
this.possibleBlockStates = possibleBlockStates;
return this;
}

public BlockProperties build() {
Objects.requireNonNull(this.defaultBlockState, "missing default block state for " + this.key);
Objects.requireNonNull(this.possibleBlockStates, "missing possible block states for " + this.key);
Objects.requireNonNull(this.defaultBlockState, "missing default block state for block: " + this.key);

if (this.blockStates.size() == 0) {
throw new IllegalStateException("missing block states for block: " + this.key);
}

return new BlockProperties(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public static Builder builder(int id) {
private final boolean isAir;
private final boolean isOccluding;
private final boolean isBlockEntity;
private final boolean isDefaultState;

private BlockStateProperties(Builder builder) {
this.id = builder.id;
this.isAir = builder.isAir;
this.isOccluding = builder.isOccluding;
this.isBlockEntity = builder.isBlockEntity;
this.isDefaultState = builder.isDefaultState;
}

public int getId() {
Expand All @@ -35,6 +37,10 @@ public boolean isBlockEntity() {
return isBlockEntity;
}

public boolean isDefaultState() {
return isDefaultState;
}

@Override
public int hashCode() {
return id;
Expand All @@ -54,8 +60,8 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "BlockStateProperties [id=" + id + ", isAir=" + isAir + ", isOccluding=" + isOccluding
+ ", isBlockEntity=" + isBlockEntity + "]";
return "BlockStateProperties [id=" + id + ", isDefaultState=" + isDefaultState + ", isAir=" + isAir
+ ", isOccluding=" + isOccluding + ", isBlockEntity=" + isBlockEntity + "]";
}

public static class Builder {
Expand All @@ -65,6 +71,7 @@ public static class Builder {
private boolean isAir;
private boolean isOccluding;
private boolean isBlockEntity;
private boolean isDefaultState;

private Builder(int id) {
this.id = id;
Expand All @@ -85,6 +92,11 @@ public Builder withIsBlockEntity(boolean isBlockEntity) {
return this;
}

public Builder withIsDefaultState(boolean isDefaultState) {
this.isDefaultState = isDefaultState;
return this;
}

public BlockStateProperties build() {
return new BlockStateProperties(this);
}
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ public AbstractNmsManager(int uniqueBlockStateCount, AbstractRegionFileCache<?>
this.blockStates = new BlockStateProperties[uniqueBlockStateCount];
}

protected final void registerBlockStateProperties(BlockStateProperties properties) {
this.blockStates[properties.getId()] = properties;
}

protected final void registerBlockProperties(BlockProperties properties) {
this.blocks.put(properties.getKey(), properties);
}
protected final void registerBlockProperties(BlockProperties block) {
this.blocks.put(block.getKey(), block);

protected final BlockStateProperties getBlockStateProperties(int id) {
return this.blockStates[id];
for (BlockStateProperties blockState : block.getBlockStates()) {
this.blockStates[blockState.getId()] = blockState;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.bukkit.World;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
Expand Down Expand Up @@ -82,33 +83,25 @@ public NmsManager(Config config) {
Block block = Block.REGISTRY.get(key);

ImmutableList<IBlockData> possibleBlockStates = block.t().a();
List<BlockStateProperties> possibleBlockStateProperties = new ArrayList<>();
BlockProperties.Builder builder = BlockProperties.builder(namespacedKey);

for (IBlockData blockState : possibleBlockStates) {

BlockStateProperties properties = BlockStateProperties.builder(getBlockId(blockState))
.withIsAir(block instanceof BlockAir)
/**
* p -> for barrier/slime_block/spawner
* j -> for every other block
*/
* p -> for barrier/slime_block/spawner
* j -> for every other block
*/
.withIsOccluding(blockState.p() && block.j()/*canOcclude*/)
.withIsBlockEntity(block.isTileEntity())
.withIsDefaultState(Objects.equals(block.getBlockData(), blockState))
.build();

possibleBlockStateProperties.add(properties);
this.registerBlockStateProperties(properties);
builder.withBlockState(properties);
}

int defaultBlockStateId = getBlockId(block.getBlockData());
BlockStateProperties defaultBlockState = getBlockStateProperties(defaultBlockStateId);

BlockProperties blockProperties = BlockProperties.builder(namespacedKey)
.withDefaultBlockState(defaultBlockState)
.withPossibleBlockStates(ImmutableList.copyOf(possibleBlockStateProperties))
.build();

this.registerBlockProperties(blockProperties);
this.registerBlockProperties(builder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.bukkit.World;
import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
Expand Down Expand Up @@ -82,7 +83,7 @@ public NmsManager(Config config) {
Block block = Block.REGISTRY.get(key);

ImmutableList<IBlockData> possibleBlockStates = block.s().a();
List<BlockStateProperties> possibleBlockStateProperties = new ArrayList<>();
BlockProperties.Builder builder = BlockProperties.builder(namespacedKey);

for (IBlockData blockState : possibleBlockStates) {

Expand All @@ -94,21 +95,13 @@ public NmsManager(Config config) {
*/
.withIsOccluding(blockState.q() && blockState.s()/*canOcclude*/)
.withIsBlockEntity(block.isTileEntity())
.withIsDefaultState(Objects.equals(block.getBlockData(), blockState))
.build();

possibleBlockStateProperties.add(properties);
this.registerBlockStateProperties(properties);
builder.withBlockState(properties);
}

int defaultBlockStateId = getBlockId(block.getBlockData());
BlockStateProperties defaultBlockState = getBlockStateProperties(defaultBlockStateId);

BlockProperties blockProperties = BlockProperties.builder(namespacedKey)
.withDefaultBlockState(defaultBlockState)
.withPossibleBlockStates(ImmutableList.copyOf(possibleBlockStateProperties))
.build();

this.registerBlockProperties(blockProperties);
this.registerBlockProperties(builder.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.bukkit.World;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
Expand Down Expand Up @@ -82,32 +83,25 @@ public NmsManager(Config config) {
Block block = Block.REGISTRY.get(key);

ImmutableList<IBlockData> possibleBlockStates = block.s().a();
List<BlockStateProperties> possibleBlockStateProperties = new ArrayList<>();
BlockProperties.Builder builder = BlockProperties.builder(namespacedKey);

for (IBlockData blockState : possibleBlockStates) {

BlockStateProperties properties = BlockStateProperties.builder(getBlockId(blockState))
.withIsAir(block instanceof BlockAir)
/**
* p -> for barrier/slime_block/spawner
* r -> for every other block
*/
* p -> for barrier/slime_block/spawner
* r -> for every other block
*/
.withIsOccluding(blockState.p() && blockState.r()/*canOcclude*/)
.withIsBlockEntity(block.isTileEntity())
.withIsDefaultState(Objects.equals(block.getBlockData(), blockState))
.build();

possibleBlockStateProperties.add(properties);
this.registerBlockStateProperties(properties);
builder.withBlockState(properties);
}

int defaultBlockStateId = getBlockId(block.getBlockData());
BlockStateProperties defaultBlockState = getBlockStateProperties(defaultBlockStateId);

BlockProperties blockProperties = BlockProperties.builder(namespacedKey)
.withDefaultBlockState(defaultBlockState)
.withPossibleBlockStates(ImmutableList.copyOf(possibleBlockStateProperties))
.build();

this.registerBlockProperties(blockProperties);
this.registerBlockProperties(builder.build());
}
}

Expand Down
Loading
Loading