3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

fixed encodeblockpos, use map<long, short[]>

Dieser Commit ist enthalten in:
creeper123123321 2019-01-15 15:43:10 -02:00
Ursprung 193084927b
Commit 2285c453da
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 0AC57D54786721D1

Datei anzeigen

@ -7,26 +7,21 @@ import us.myles.ViaVersion.api.minecraft.Position;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class BlockConnectionStorage extends StoredObject { public class BlockConnectionStorage extends StoredObject {
private Map<Long, Map<Short, Short>> blockStorage = createLongObjectMap(); private Map<Long, short[]> blockStorage = createLongObjectMap();
private static short[] short4096 = new short[4096];
private static Constructor<?> fastUtilLongObjectHashMap; private static Constructor<?> fastUtilLongObjectHashMap;
private static Constructor<?> fastUtilShortShortHashMap;
static { static {
try { try {
fastUtilLongObjectHashMap = Class.forName("it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap").getConstructor(); fastUtilLongObjectHashMap = Class.forName("it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap").getConstructor();
Via.getPlatform().getLogger().info("Using FastUtil Long2ObjectOpenHashMap for block connections"); Via.getPlatform().getLogger().info("Using FastUtil Long2ObjectOpenHashMap for block connections");
} catch (ClassNotFoundException | NoSuchMethodException ignored) { } catch (ClassNotFoundException | NoSuchMethodException ignored) {
}
try {
fastUtilShortShortHashMap = Class.forName("it.unimi.dsi.fastutil.shorts.Short2ShortOpenHashMap").getConstructor();
Via.getPlatform().getLogger().info("Using FastUtil Short2ShortOpenHashMap for block connections");
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
} }
} }
@ -35,23 +30,24 @@ public class BlockConnectionStorage extends StoredObject {
} }
public void store(Position position, int blockState) { public void store(Position position, int blockState) {
long pair = getChunkIndex(position); long pair = getChunkSectionIndex(position);
Map<Short, Short> map = getChunkMap(pair); short[] map = getChunkSection(pair);
map.put(encodeBlockPos(position), (short) blockState); map[encodeBlockPos(position)] = (short) blockState;
} }
public int get(Position position) { public int get(Position position) {
long pair = getChunkIndex(position); long pair = getChunkSectionIndex(position);
Map<Short, Short> map = getChunkMap(pair); short[] map = blockStorage.get(pair);
if (map == null) return 0;
short blockPosition = encodeBlockPos(position); short blockPosition = encodeBlockPos(position);
return map.containsKey(blockPosition) ? map.get(blockPosition) : 0; return map[blockPosition];
} }
public void remove(Position position) { public void remove(Position position) {
long pair = getChunkIndex(position); long pair = getChunkSectionIndex(position);
Map<Short, Short> map = getChunkMap(pair); short[] map = getChunkSection(pair);
map.remove(encodeBlockPos(position)); map[encodeBlockPos(position)] = 0;
if (map.isEmpty()) { if (Arrays.equals(short4096, map)) {
blockStorage.remove(pair); blockStorage.remove(pair);
} }
} }
@ -61,28 +57,30 @@ public class BlockConnectionStorage extends StoredObject {
} }
public void unloadChunk(int x, int z) { public void unloadChunk(int x, int z) {
blockStorage.remove(getChunkIndex(x, z)); for (int y = 0; y < 256; y += 16) {
blockStorage.remove(getChunkSectionIndex(x, y, z));
}
} }
private Map<Short, Short> getChunkMap(long index) { private short[] getChunkSection(long index) {
Map<Short, Short> map = blockStorage.get(index); short[] map = blockStorage.get(index);
if (map == null) { if (map == null) {
map = createShortShortMap(); map = new short[4096];
blockStorage.put(index, map); blockStorage.put(index, map);
} }
return map; return map;
} }
private long getChunkIndex(int x, int z) { private long getChunkSectionIndex(int x, int y, int z) {
return (long) x << 32 | (z & 0xFFFFFFFFL); return (((x >> 4) & 0x3FFFFFFL) << 38) | (((y >> 4) & 0xFFFL) << 26) | ((z >> 4) & 0x3FFFFFFL);
} }
private long getChunkIndex(Position position) { private long getChunkSectionIndex(Position position) {
return getChunkIndex(position.getX().intValue(), position.getZ().intValue()); return getChunkSectionIndex(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue());
} }
private short encodeBlockPos(int x, int y, int z) { private short encodeBlockPos(int x, int y, int z) {
return (short) (y << 8 | x & 0xF << 4 | z & 0xF); return (short) (((y & 0xF) << 8) | ((x & 0xF) << 4) | (z & 0xF));
} }
private short encodeBlockPos(Position pos) { private short encodeBlockPos(Position pos) {
@ -99,15 +97,4 @@ public class BlockConnectionStorage extends StoredObject {
} }
return new HashMap<>(); return new HashMap<>();
} }
private Map<Short, Short> createShortShortMap() {
if (fastUtilShortShortHashMap != null) {
try {
return (Map<Short, Short>) fastUtilShortShortHashMap.newInstance();
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
}
return new HashMap<>();
}
} }