1 package com.irtimaled.bbor.client.providers;
3 import com.irtimaled.bbor.client.Player;
4 import com.irtimaled.bbor.common.BoundingBoxType;
5 import com.irtimaled.bbor.common.models.BoundingBoxCuboid;
6 import com.irtimaled.bbor.common.models.Coords;
7 import com.irtimaled.bbor.common.models.DimensionId;
9 import java.util.HashMap;
11 import java.util.concurrent.ConcurrentHashMap;
13 public class CustomBoxProvider implements IBoundingBoxProvider<BoundingBoxCuboid>, ICachingProvider {
14 private static final Map<DimensionId, Map<Integer, BoundingBoxCuboid>> dimensionCache = new HashMap<>();
16 private static int getHashKey(Coords minCoords, Coords maxCoords) {
17 return (31 + minCoords.hashCode()) * 31 + maxCoords.hashCode();
20 private static Map<Integer, BoundingBoxCuboid> getCache(DimensionId dimensionId) {
21 return dimensionCache.computeIfAbsent(dimensionId, i -> new ConcurrentHashMap<>());
24 public static void add(Coords minCoords, Coords maxCoords) {
25 DimensionId dimensionId = Player.getDimensionId();
26 int cacheKey = getHashKey(minCoords, maxCoords);
27 BoundingBoxCuboid cuboid = BoundingBoxCuboid.from(minCoords, maxCoords, BoundingBoxType.Custom);
28 getCache(dimensionId).put(cacheKey, cuboid);
31 public static boolean remove(Coords minCoords, Coords maxCoords) {
32 DimensionId dimensionId = Player.getDimensionId();
33 int cacheKey = getHashKey(minCoords, maxCoords);
34 return getCache(dimensionId).remove(cacheKey) != null;
37 public static void clear() {
38 dimensionCache.values().forEach(Map::clear);
41 public void clearCache() {
46 public Iterable<BoundingBoxCuboid> get(DimensionId dimensionId) {
47 return getCache(dimensionId).values();