|
1 | 1 | package com.ferreusveritas.dynamictrees.systems; |
2 | 2 |
|
| 3 | +import com.ferreusveritas.dynamictrees.tree.family.Family; |
3 | 4 | import com.ferreusveritas.dynamictrees.util.function.TetraFunction; |
4 | 5 | import net.minecraft.core.BlockPos; |
5 | 6 | import net.minecraft.core.Direction; |
|
18 | 19 | */ |
19 | 20 | public class BranchConnectables { |
20 | 21 |
|
21 | | - private static final Map<Block, TetraFunction<BlockState, BlockGetter, BlockPos, Direction, Integer>> connectablesMap = new HashMap<>(); |
| 22 | + private static final Map<Block, Map<Family, TetraFunction<BlockState, BlockGetter, BlockPos, Direction, Integer>>> connectablesMap = new HashMap<>(); |
22 | 23 |
|
23 | 24 | //Direction can be null |
| 25 | + public static void makeBlockConnectable(Block block, TetraFunction<BlockState, BlockGetter, BlockPos, Direction, Integer> radiusFunction, Family family) { |
| 26 | + var map = connectablesMap.computeIfAbsent(block, k -> new HashMap<>()); |
| 27 | + map.putIfAbsent(family, radiusFunction); |
| 28 | + } |
24 | 29 | public static void makeBlockConnectable(Block block, TetraFunction<BlockState, BlockGetter, BlockPos, Direction, Integer> radiusFunction) { |
25 | | - connectablesMap.putIfAbsent(block, radiusFunction); |
| 30 | + makeBlockConnectable(block, radiusFunction, Family.NULL_FAMILY); |
| 31 | + } |
| 32 | + public static void replaceBlockConnectable(Block block, TetraFunction<BlockState, BlockGetter, BlockPos, Direction, Integer> radiusFunction, Family family) { |
| 33 | + var map = connectablesMap.computeIfAbsent(block, k -> new HashMap<>()); |
| 34 | + map.remove(family); |
| 35 | + map.put(family, radiusFunction); |
26 | 36 | } |
27 | 37 |
|
28 | 38 | public static boolean isBlockConnectable(Block block) { |
29 | 39 | return connectablesMap.containsKey(block); |
30 | 40 | } |
31 | 41 |
|
32 | | - public static int getConnectionRadiusForBlock(BlockState state, BlockGetter world, BlockPos pos, @Nullable Direction side) { |
| 42 | + public static int getConnectionRadiusForBlock(BlockState state, BlockGetter world, BlockPos pos, @Nullable Direction side, Family family) { |
33 | 43 | final Block block = state.getBlock(); |
34 | | - return isBlockConnectable(block) ? connectablesMap.get(block).apply(state, world, pos, side) : 0; |
| 44 | + if (isBlockConnectable(block)){ |
| 45 | + var function = getFunctionForFamily(block, family); |
| 46 | + if (function == null) return 0; |
| 47 | + return function.apply(state, world, pos, side); |
| 48 | + } else { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + } |
| 52 | + public static int getConnectionRadiusForBlock(BlockState state, BlockGetter world, BlockPos pos, @Nullable Direction side) { |
| 53 | + return getConnectionRadiusForBlock(state, world, pos, side, Family.NULL_FAMILY); |
35 | 54 | } |
36 | 55 |
|
| 56 | + @Nullable |
| 57 | + private static TetraFunction<BlockState, BlockGetter, BlockPos, Direction, Integer> getFunctionForFamily(Block block, Family family){ |
| 58 | + var familyMap = connectablesMap.get(block); |
| 59 | + if (familyMap == null) return null; |
| 60 | + var function = familyMap.get(family); |
| 61 | + if (function == null){ |
| 62 | + function = familyMap.get(Family.NULL_FAMILY); |
| 63 | + } |
| 64 | + return function; |
| 65 | + } |
37 | 66 | } |
0 commit comments