]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/providers/CustomLineProvider.java
Move client side only models to client side
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / providers / CustomLineProvider.java
1 package com.irtimaled.bbor.client.providers;
2
3 import com.irtimaled.bbor.client.Player;
4 import com.irtimaled.bbor.client.models.BoundingBoxLine;
5 import com.irtimaled.bbor.client.models.Point;
6 import com.irtimaled.bbor.common.BoundingBoxType;
7
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap;
11
12 public class CustomLineProvider implements IBoundingBoxProvider<BoundingBoxLine> {
13     private static final Map<Integer, Map<Integer, BoundingBoxLine>> dimensionCache = new HashMap<>();
14
15     private static int getHashKey(Point minPoint, Point maxPoint) {
16         return (31 + minPoint.hashCode()) * 31 + maxPoint.hashCode();
17     }
18
19     private static Map<Integer, BoundingBoxLine> getCache(int dimensionId) {
20         return dimensionCache.computeIfAbsent(dimensionId, i -> new ConcurrentHashMap<>());
21     }
22
23     public static void add(Point minPoint, Point maxPoint, Double width) {
24         int dimensionId = Player.getDimensionId();
25         int cacheKey = getHashKey(minPoint, maxPoint);
26         BoundingBoxLine line = BoundingBoxLine.from(minPoint, maxPoint, width, BoundingBoxType.Custom);
27         getCache(dimensionId).put(cacheKey, line);
28     }
29
30     public static boolean remove(Point min, Point max) {
31         int dimensionId = Player.getDimensionId();
32         int cacheKey = getHashKey(min, max);
33         return getCache(dimensionId).remove(cacheKey) != null;
34     }
35
36     public static void clear() {
37         dimensionCache.values().forEach(Map::clear);
38     }
39
40     public Iterable<BoundingBoxLine> get(int dimensionId) {
41         return getCache(dimensionId).values();
42     }
43 }