mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +01:00
Move nbt cache into block implementation
This commit is contained in:
parent
d365373775
commit
a255abf0dd
@ -1,8 +1,6 @@
|
|||||||
package net.minestom.server.instance;
|
package net.minestom.server.instance;
|
||||||
|
|
||||||
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
|
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
import net.minestom.server.entity.pathfinding.PFBlockDescription;
|
import net.minestom.server.entity.pathfinding.PFBlockDescription;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
@ -19,7 +17,6 @@ import java.lang.ref.SoftReference;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a {@link Chunk} which store each individual block in memory.
|
* Represents a {@link Chunk} which store each individual block in memory.
|
||||||
@ -28,11 +25,6 @@ import java.util.concurrent.TimeUnit;
|
|||||||
*/
|
*/
|
||||||
public class DynamicChunk extends Chunk {
|
public class DynamicChunk extends Chunk {
|
||||||
|
|
||||||
private static final Cache<NBTCompound, NBTCompound> NBT_CACHE = Caffeine.newBuilder()
|
|
||||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
|
||||||
.weakValues()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
protected final TreeMap<Integer, Section> sectionMap = new TreeMap<>();
|
protected final TreeMap<Integer, Section> sectionMap = new TreeMap<>();
|
||||||
|
|
||||||
// Key = ChunkUtils#getBlockIndex
|
// Key = ChunkUtils#getBlockIndex
|
||||||
@ -72,8 +64,7 @@ public class DynamicChunk extends Chunk {
|
|||||||
// Nbt
|
// Nbt
|
||||||
final NBTCompound nbt = block.nbt();
|
final NBTCompound nbt = block.nbt();
|
||||||
if (nbt != null) {
|
if (nbt != null) {
|
||||||
final var cachedNbt = NBT_CACHE.get(nbt, compound -> nbt);
|
this.nbtMap.put(index, nbt);
|
||||||
this.nbtMap.put(index, cachedNbt);
|
|
||||||
} else {
|
} else {
|
||||||
this.nbtMap.remove(index);
|
this.nbtMap.remove(index);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package net.minestom.server.instance.block;
|
package net.minestom.server.instance.block;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import net.minestom.server.registry.Registry;
|
import net.minestom.server.registry.Registry;
|
||||||
import net.minestom.server.tag.Tag;
|
import net.minestom.server.tag.Tag;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -10,21 +12,27 @@ import java.util.Collections;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
class BlockTest implements Block {
|
class BlockTest implements Block {
|
||||||
|
|
||||||
|
private static final Cache<NBTCompound, NBTCompound> NBT_CACHE = Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
|
.weakValues()
|
||||||
|
.build();
|
||||||
|
|
||||||
private final Registry.BlockEntry registry;
|
private final Registry.BlockEntry registry;
|
||||||
private final Map<String, String> properties;
|
private final Map<String, String> properties;
|
||||||
private final NBTCompound compound;
|
private final NBTCompound nbt;
|
||||||
private final BlockHandler handler;
|
private final BlockHandler handler;
|
||||||
|
|
||||||
BlockTest(@NotNull Registry.BlockEntry registry,
|
BlockTest(@NotNull Registry.BlockEntry registry,
|
||||||
@NotNull Map<String, String> properties,
|
@NotNull Map<String, String> properties,
|
||||||
@Nullable NBTCompound compound,
|
@Nullable NBTCompound nbt,
|
||||||
@Nullable BlockHandler handler) {
|
@Nullable BlockHandler handler) {
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.properties = Collections.unmodifiableMap(properties);
|
this.properties = Collections.unmodifiableMap(properties);
|
||||||
this.compound = compound;
|
this.nbt = nbt;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,17 +51,19 @@ class BlockTest implements Block {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
||||||
return new BlockTest(registry, properties, compound, handler);
|
final var cachedNbt = NBT_CACHE.get(compound, c -> compound);
|
||||||
|
return new BlockTest(registry, properties, cachedNbt, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
||||||
return new BlockTest(registry, properties, compound, handler);
|
return new BlockTest(registry, properties, nbt, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable NBTCompound nbt() {
|
public @Nullable NBTCompound nbt() {
|
||||||
return compound != null ? compound.deepClone() : null;
|
// TODO return immutable compound without clone
|
||||||
|
return nbt != null ? nbt.deepClone() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -73,8 +83,8 @@ class BlockTest implements Block {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||||
if (compound == null)
|
if (nbt == null)
|
||||||
return null;
|
return null;
|
||||||
return tag.read(compound);
|
return tag.read(nbt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user