Skip to content

Commit 2367afe

Browse files
committed
improvements to branch connectables
1 parent 0865250 commit 2367afe

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/main/java/com/ferreusveritas/dynamictrees/block/NullTreePart.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ferreusveritas.dynamictrees.block;
22

3+
import com.ferreusveritas.dynamictrees.api.TreeHelper;
34
import com.ferreusveritas.dynamictrees.api.cell.Cell;
45
import com.ferreusveritas.dynamictrees.api.cell.CellNull;
56
import com.ferreusveritas.dynamictrees.api.network.MapSignal;
@@ -35,7 +36,7 @@ public GrowSignal growSignal(Level level, BlockPos pos, GrowSignal signal) {
3536
public int getRadiusForConnection(BlockState state, BlockGetter level, BlockPos pos, BranchBlock from, Direction side, int fromRadius) {
3637
// Connectable blocks such as bee nests and shroomlight will be handled here.
3738
if (BranchConnectables.isBlockConnectable(state.getBlock())) {
38-
int rad = BranchConnectables.getConnectionRadiusForBlock(state, level, pos, side);
39+
int rad = BranchConnectables.getConnectionRadiusForBlock(state, level, pos, side, from.getFamily());
3940
if (rad > 0) {
4041
return rad;
4142
}

src/main/java/com/ferreusveritas/dynamictrees/systems/BranchConnectables.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ferreusveritas.dynamictrees.systems;
22

3+
import com.ferreusveritas.dynamictrees.tree.family.Family;
34
import com.ferreusveritas.dynamictrees.util.function.TetraFunction;
45
import net.minecraft.core.BlockPos;
56
import net.minecraft.core.Direction;
@@ -18,20 +19,48 @@
1819
*/
1920
public class BranchConnectables {
2021

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<>();
2223

2324
//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+
}
2429
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);
2636
}
2737

2838
public static boolean isBlockConnectable(Block block) {
2939
return connectablesMap.containsKey(block);
3040
}
3141

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) {
3343
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);
3554
}
3655

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+
}
3766
}

src/main/java/com/ferreusveritas/dynamictrees/systems/nodemapper/DestroyerNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public DestroyerNode setPlayer(Player player) {
3838
}
3939
@Override
4040
public boolean run(BlockState state, LevelAccessor level, BlockPos pos, @Nullable Direction fromDir) {
41-
if (BranchConnectables.getConnectionRadiusForBlock(state, level, pos, fromDir == null ? null : fromDir.getOpposite()) > 0) {
41+
if (BranchConnectables.getConnectionRadiusForBlock(state, level, pos, fromDir == null ? null : fromDir.getOpposite(), species.getFamily()) > 0) {
4242
if (player != null && level instanceof Level) {
4343
BlockEntity te = level.getBlockEntity(pos);
4444
state.getBlock().onDestroyedByPlayer(state, (Level) level, pos, player, false, level.getFluidState(pos));

0 commit comments

Comments
 (0)