1 package com.irtimaled.bbor.client.providers;
3 import com.irtimaled.bbor.client.PlayerCoords;
4 import com.irtimaled.bbor.common.BoundingBoxType;
5 import com.irtimaled.bbor.common.Dimensions;
6 import com.irtimaled.bbor.common.models.BoundingBoxCuboid;
7 import com.irtimaled.bbor.common.models.Coords;
9 import java.util.HashMap;
11 import java.util.concurrent.ConcurrentHashMap;
13 public class CustomBoxProvider implements IBoundingBoxProvider<BoundingBoxCuboid> {
14 private static Map<Integer, Map<Integer, BoundingBoxCuboid>> dimensionCache = buildDimensionCache();
16 private static Map<Integer, Map<Integer, BoundingBoxCuboid>> buildDimensionCache() {
17 Map<Integer, Map<Integer, BoundingBoxCuboid>> map = new HashMap<>();
18 map.put(Dimensions.OVERWORLD, new ConcurrentHashMap<>());
19 map.put(Dimensions.NETHER, new ConcurrentHashMap<>());
20 map.put(Dimensions.THE_END, new ConcurrentHashMap<>());
24 private static int getHashKey(Coords minCoords, Coords maxCoords) {
25 return (31 + minCoords.hashCode()) * 31 + maxCoords.hashCode();
28 private static Map<Integer, BoundingBoxCuboid> getCache(int dimensionId) {
29 return dimensionCache.computeIfAbsent(dimensionId, i -> new ConcurrentHashMap<>());
32 public static void add(Coords minCoords, Coords maxCoords) {
33 int dimensionId = PlayerCoords.getDimensionId();
34 int cacheKey = getHashKey(minCoords, maxCoords);
35 BoundingBoxCuboid cuboid = BoundingBoxCuboid.from(minCoords, maxCoords, BoundingBoxType.Custom);
36 getCache(dimensionId).put(cacheKey, cuboid);
39 public static void remove(Coords minCoords, Coords maxCoords) {
40 int dimensionId = PlayerCoords.getDimensionId();
41 int cacheKey = getHashKey(minCoords, maxCoords);
42 getCache(dimensionId).remove(cacheKey);
45 public static void clear() {
46 dimensionCache.values().forEach(Map::clear);
49 public Iterable<BoundingBoxCuboid> get(int dimensionId) {
50 return getCache(dimensionId).values();