]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/CustomSphereProvider.java
Add support for custom sphere
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / providers / CustomSphereProvider.java
1 package com.irtimaled.bbor.client.providers;
2
3 import com.irtimaled.bbor.client.Player;
4 import com.irtimaled.bbor.common.BoundingBoxType;
5 import com.irtimaled.bbor.common.models.BoundingBoxSphere;
6 import com.irtimaled.bbor.common.models.Point;
7
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap;
11
12 public class CustomSphereProvider implements IBoundingBoxProvider<BoundingBoxSphere> {
13     private static final Map<Integer, Map<Integer, BoundingBoxSphere>> dimensionCache = new HashMap<>();
14
15     private static Map<Integer, BoundingBoxSphere> getCache(int dimensionId) {
16         return dimensionCache.computeIfAbsent(dimensionId, i -> new ConcurrentHashMap<>());
17     }
18
19     public static void add(Point center, double radius) {
20         int dimensionId = Player.getDimensionId();
21         int cacheKey = center.hashCode();
22         BoundingBoxSphere sphere = new BoundingBoxSphere(center, radius, BoundingBoxType.Custom);
23         getCache(dimensionId).put(cacheKey, sphere);
24     }
25
26     public static boolean remove(Point center) {
27         int dimensionId = Player.getDimensionId();
28         int cacheKey = center.hashCode();
29         return getCache(dimensionId).remove(cacheKey) != null;
30     }
31
32     public static void clear() {
33         dimensionCache.values().forEach(Map::clear);
34     }
35
36     public Iterable<BoundingBoxSphere> get(int dimensionId) {
37         return getCache(dimensionId).values();
38     }
39 }