|
1 | 1 | package net.blay09.mods.cookingforblockheads.crafting; |
2 | 2 |
|
3 | 3 | import net.blay09.mods.balm.Balm; |
| 4 | +import net.blay09.mods.cookingforblockheads.CookingForBlockheads; |
4 | 5 | import net.blay09.mods.cookingforblockheads.api.Kitchen; |
5 | 6 | import net.blay09.mods.cookingforblockheads.api.KitchenItemProcessor; |
6 | 7 | import net.blay09.mods.cookingforblockheads.api.KitchenItemProvider; |
|
10 | 11 | import net.minecraft.core.BlockPos; |
11 | 12 | import net.minecraft.core.Direction; |
12 | 13 | import net.minecraft.world.level.Level; |
13 | | -import net.minecraft.world.level.block.entity.BlockEntity; |
14 | | -import net.minecraft.world.level.block.state.BlockState; |
15 | 14 |
|
| 15 | +import java.util.ArrayDeque; |
16 | 16 | import java.util.ArrayList; |
17 | 17 | import java.util.HashSet; |
18 | 18 | import java.util.List; |
19 | 19 | import java.util.Set; |
20 | 20 |
|
21 | 21 | public class KitchenMultiblockScanner { |
22 | | - private final Set<BlockPos> checkedPos = new HashSet<>(); |
| 22 | + static final int MAX_SCANNED_POSITIONS = 4096; |
| 23 | + |
| 24 | + private final Set<BlockPos> closedList = new HashSet<>(); |
23 | 25 | private final List<KitchenItemProvider> itemProviders = new ArrayList<>(); |
24 | 26 | private final List<KitchenRecipeProvider> recipeProviders = new ArrayList<>(); |
25 | 27 | private final List<KitchenItemProcessor> itemProcessors = new ArrayList<>(); |
26 | 28 |
|
27 | 29 | public void findNeighbourCraftingBlocks(Level level, BlockPos pos) { |
28 | | - findNeighbourCraftingBlocks(level, pos, true); |
29 | | - } |
| 30 | + final var openList = new ArrayDeque<BlockPos>(); |
| 31 | + gatherNeighboursInto(openList, closedList, pos, true); |
| 32 | + |
| 33 | + while (!openList.isEmpty()) { |
| 34 | + final var current = openList.removeLast(); |
| 35 | + final var state = level.getBlockState(current); |
| 36 | + final var blockEntity = level.getBlockEntity(current); |
| 37 | + |
| 38 | + boolean continueSearch = false; |
| 39 | + boolean extendUpwards = false; |
| 40 | + if (blockEntity != null) { |
| 41 | + var itemProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROVIDER); |
| 42 | + if (itemProvider != null) { |
| 43 | + itemProviders.add(itemProvider); |
| 44 | + } |
| 45 | + |
| 46 | + final var recipeProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_RECIPE_PROVIDER); |
| 47 | + if (recipeProvider != null) { |
| 48 | + recipeProviders.add(recipeProvider); |
| 49 | + } |
30 | 50 |
|
31 | | - public void findNeighbourCraftingBlocks(Level level, BlockPos pos, boolean extendedUpSearch) { |
32 | | - for (Direction direction : Direction.values()) { |
33 | | - int upSearch = (extendedUpSearch && direction == Direction.UP) ? 2 : 1; |
34 | | - for (int n = 1; n <= upSearch; n++) { |
35 | | - BlockPos position = pos.relative(direction, n); |
36 | | - if (!checkedPos.contains(position)) { |
37 | | - checkedPos.add(position); |
| 51 | + final var itemProcessor = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROCESSOR); |
| 52 | + if (itemProcessor != null) { |
| 53 | + itemProcessors.add(itemProcessor); |
| 54 | + } |
38 | 55 |
|
39 | | - BlockState state = level.getBlockState(position); |
40 | | - BlockEntity blockEntity = level.getBlockEntity(position); |
41 | | - if (blockEntity != null) { |
42 | | - var itemProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROVIDER); |
43 | | - if (itemProvider != null) { |
44 | | - itemProviders.add(itemProvider); |
45 | | - } |
| 56 | + if (itemProvider != null || recipeProvider != null || itemProcessor != null || state.is(ModBlockTags.KITCHEN_CONNECTORS)) { |
| 57 | + continueSearch = true; |
| 58 | + extendUpwards = true; |
| 59 | + } |
| 60 | + } else if (state.is(ModBlockTags.KITCHEN_CONNECTORS)) { |
| 61 | + continueSearch = true; |
| 62 | + } |
46 | 63 |
|
47 | | - final var recipeProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_RECIPE_PROVIDER); |
48 | | - if (recipeProvider != null) { |
49 | | - recipeProviders.add(recipeProvider); |
50 | | - } |
| 64 | + if (closedList.size() >= MAX_SCANNED_POSITIONS) { |
| 65 | + CookingForBlockheads.logger.warn("Stopping kitchen multiblock search at {}", closedList.size()); |
| 66 | + break; |
| 67 | + } |
51 | 68 |
|
52 | | - final var itemProcessor = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROCESSOR); |
53 | | - if (itemProcessor != null) { |
54 | | - itemProcessors.add(itemProcessor); |
55 | | - } |
| 69 | + if (continueSearch) { |
| 70 | + gatherNeighboursInto(openList, closedList, current, extendUpwards); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
56 | 74 |
|
57 | | - if (itemProvider != null || recipeProvider != null || itemProcessor != null || state.is(ModBlockTags.KITCHEN_CONNECTORS)) { |
58 | | - findNeighbourCraftingBlocks(level, position, true); |
59 | | - } |
60 | | - } else if (state.is(ModBlockTags.KITCHEN_CONNECTORS)) { |
61 | | - findNeighbourCraftingBlocks(level, position, false); |
62 | | - } |
| 75 | + private void gatherNeighboursInto(ArrayDeque<BlockPos> openList, Set<BlockPos> closedList, BlockPos pos, boolean extendedUp) { |
| 76 | + for (final var direction : Direction.values()) { |
| 77 | + if (extendedUp && direction == Direction.UP) { |
| 78 | + final var above = pos.above(); |
| 79 | + if (closedList.add(above)) { |
| 80 | + openList.addLast(above); |
| 81 | + } |
| 82 | + final var aboveTwo = pos.above(2); |
| 83 | + if (closedList.add(aboveTwo)) { |
| 84 | + openList.addLast(aboveTwo); |
| 85 | + } |
| 86 | + } else { |
| 87 | + final var neighbour = pos.relative(direction); |
| 88 | + if (closedList.add(neighbour)) { |
| 89 | + openList.addLast(neighbour); |
63 | 90 | } |
64 | 91 | } |
65 | 92 | } |
|
0 commit comments