All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.level.util.SimpleTickCachedBlockStore Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.level.util;

import cn.nukkit.api.PowerNukkitXOnly;
import cn.nukkit.api.Since;
import cn.nukkit.block.Block;
import cn.nukkit.level.Level;

import java.util.concurrent.ConcurrentHashMap;

@PowerNukkitXOnly
@Since("1.6.0.0-PNX")
public final class SimpleTickCachedBlockStore implements TickCachedBlockStore {
    private final ConcurrentHashMap tickCachedBlockStore;
    private final Level level;

    public SimpleTickCachedBlockStore(Level level) {
        this.tickCachedBlockStore = new ConcurrentHashMap<>(32, 0.75f);
        this.level = level;
    }

    @Override
    public void clearCachedStore() {
        this.tickCachedBlockStore.clear();
    }

    @Override
    public void saveIntoCachedStore(Block block, int x, int y, int z, int layer) {
        tickCachedBlockStore.put(Level.localBlockHash(x, y, z, layer, level), block);
    }

    @Override
    public Block getFromCachedStore(int x, int y, int z, int layer) {
        return tickCachedBlockStore.get(Level.localBlockHash(x, y, z, layer, level));
    }

    @Override
    public Block computeFromCachedStore(int x, int y, int z, int layer, CachedBlockComputer cachedBlockComputer) {
        return tickCachedBlockStore.computeIfAbsent(Level.localBlockHash(x, y, z, layer, level), ignore -> cachedBlockComputer.compute());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy