Skip to content

Commit bd7cdf2

Browse files
committed
refactor: Move multiblock scanning out of KitchenImpl #675
1 parent ebf1ef4 commit bd7cdf2

3 files changed

Lines changed: 94 additions & 57 deletions

File tree

common/src/main/java/net/blay09/mods/cookingforblockheads/block/entity/CookingTableBlockEntity.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.blay09.mods.balm.world.level.block.entity.BalmBlockEntityUtils;
66
import net.blay09.mods.cookingforblockheads.block.entity.util.TransferableBlockEntity;
77
import net.blay09.mods.cookingforblockheads.crafting.KitchenImpl;
8+
import net.blay09.mods.cookingforblockheads.crafting.KitchenMultiblockScanner;
89
import net.blay09.mods.cookingforblockheads.menu.KitchenMenu;
910
import net.blay09.mods.cookingforblockheads.menu.KitchenMenuAccess;
1011
import net.blay09.mods.cookingforblockheads.menu.ModMenus;
@@ -76,9 +77,11 @@ public Component getDisplayName() {
7677
@Nullable
7778
@Override
7879
public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
79-
final var kitchen = new KitchenImpl(level, true);
80-
kitchen.findNeighbourCraftingBlocks(level, worldPosition);
81-
final var kitchenMenuAccess = KitchenMenuAccess.create(kitchen, new ServerKitchenMenuState(kitchen.createCraftingContext(player), hasNoFilterBook()));
80+
final var kitchenScanner = new KitchenMultiblockScanner();
81+
kitchenScanner.findNeighbourCraftingBlocks(level, worldPosition);
82+
final var kitchen = kitchenScanner.createKitchen(level, true);
83+
final var craftingContext = kitchen.createCraftingContext(player);
84+
final var kitchenMenuAccess = KitchenMenuAccess.create(kitchen, new ServerKitchenMenuState(craftingContext, hasNoFilterBook()));
8285
return new KitchenMenu(ModMenus.cookingTable.value(), i, player, kitchenMenuAccess);
8386
}
8487

common/src/main/java/net/blay09/mods/cookingforblockheads/crafting/KitchenImpl.java

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@
55
import net.blay09.mods.cookingforblockheads.api.KitchenItemProcessor;
66
import net.blay09.mods.cookingforblockheads.api.KitchenItemProvider;
77
import net.blay09.mods.cookingforblockheads.api.KitchenRecipeProvider;
8-
import net.blay09.mods.cookingforblockheads.capability.ModCapabilities;
98
import net.blay09.mods.cookingforblockheads.kitchen.ContainerKitchenItemProvider;
109
import net.blay09.mods.cookingforblockheads.mixin.RecipeManagerAccessor;
1110
import net.blay09.mods.cookingforblockheads.network.message.KitchenFeedbackMessage;
1211
import net.blay09.mods.cookingforblockheads.recipe.KitchenProvidedRecipe;
1312
import net.blay09.mods.cookingforblockheads.recipe.ModRecipes;
1413
import net.blay09.mods.cookingforblockheads.registry.CookingForBlockheadsRegistry;
15-
import net.blay09.mods.cookingforblockheads.tag.ModBlockTags;
16-
import net.minecraft.core.BlockPos;
17-
import net.minecraft.core.Direction;
1814
import net.minecraft.resources.Identifier;
1915
import net.minecraft.server.level.ServerLevel;
2016
import net.minecraft.world.entity.player.Player;
2117
import net.minecraft.world.item.ItemStack;
2218
import net.minecraft.world.item.crafting.RecipeHolder;
2319
import net.minecraft.world.level.Level;
24-
import net.minecraft.world.level.block.entity.BlockEntity;
25-
import net.minecraft.world.level.block.state.BlockState;
2620
import org.jspecify.annotations.Nullable;
2721

2822
import java.util.*;
@@ -38,64 +32,33 @@ public class KitchenImpl implements Kitchen {
3832
*/
3933
@Deprecated
4034
private final boolean allowCrafting;
41-
private final Set<BlockPos> checkedPos = new HashSet<>();
42-
private final List<KitchenItemProvider> itemProviderList = new ArrayList<>();
43-
private final List<KitchenRecipeProvider> recipeProviderList = new ArrayList<>();
44-
private final List<KitchenItemProcessor> itemProcessorList = new ArrayList<>();
35+
private final List<KitchenItemProvider> itemProviders;
36+
private final List<KitchenRecipeProvider> recipeProviders;
37+
private final List<KitchenItemProcessor> itemProcessors;
4538

4639
public KitchenImpl(Level level, boolean allowCrafting) {
47-
this.level = level;
48-
this.allowCrafting = allowCrafting;
49-
}
50-
51-
public void findNeighbourCraftingBlocks(Level level, BlockPos pos) {
52-
findNeighbourCraftingBlocks(level, pos, true);
40+
this(level, allowCrafting, List.of(), List.of(), List.of());
5341
}
5442

55-
public void findNeighbourCraftingBlocks(Level level, BlockPos pos, boolean extendedUpSearch) {
56-
for (Direction direction : Direction.values()) {
57-
int upSearch = (extendedUpSearch && direction == Direction.UP) ? 2 : 1;
58-
for (int n = 1; n <= upSearch; n++) {
59-
BlockPos position = pos.relative(direction, n);
60-
if (!checkedPos.contains(position)) {
61-
checkedPos.add(position);
62-
63-
BlockState state = level.getBlockState(position);
64-
BlockEntity blockEntity = level.getBlockEntity(position);
65-
if (blockEntity != null) {
66-
var itemProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROVIDER);
67-
if (itemProvider != null) {
68-
itemProviderList.add(itemProvider);
69-
}
70-
71-
final var recipeProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_RECIPE_PROVIDER);
72-
if (recipeProvider != null) {
73-
recipeProviderList.add(recipeProvider);
74-
}
75-
76-
final var itemProcessor = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROCESSOR);
77-
if (itemProcessor != null) {
78-
itemProcessorList.add(itemProcessor);
79-
}
80-
81-
if (itemProvider != null || recipeProvider != null || itemProcessor != null || state.is(ModBlockTags.KITCHEN_CONNECTORS)) {
82-
findNeighbourCraftingBlocks(level, position, true);
83-
}
84-
} else if (state.is(ModBlockTags.KITCHEN_CONNECTORS)) {
85-
findNeighbourCraftingBlocks(level, position, false);
86-
}
87-
}
88-
}
89-
}
43+
public KitchenImpl(Level level,
44+
boolean allowCrafting,
45+
List<KitchenItemProvider> itemProviders,
46+
List<KitchenRecipeProvider> recipeProviders,
47+
List<KitchenItemProcessor> itemProcessors) {
48+
this.level = level;
49+
this.allowCrafting = allowCrafting;
50+
this.itemProviders = itemProviders;
51+
this.recipeProviders = recipeProviders;
52+
this.itemProcessors = itemProcessors;
9053
}
9154

9255
@Override
9356
public CraftingContext createCraftingContext(@Nullable Player player) {
94-
final var itemProviders = new ArrayList<>(itemProviderList);
57+
final var itemProviders = new ArrayList<>(this.itemProviders);
9558
if (player != null) {
9659
itemProviders.addFirst(new ContainerKitchenItemProvider(player.getInventory()));
9760
}
98-
return new CraftingContext(itemProviders, itemProcessorList, allowCrafting).addListener(operation -> {
61+
return new CraftingContext(itemProviders, itemProcessors, allowCrafting).addListener(operation -> {
9962
if (player != null) {
10063
final var feedback = operation.getFeedback();
10164
feedback.ifPresent(component -> Balm.networking().sendTo(player, new KitchenFeedbackMessage(component)));
@@ -141,7 +104,7 @@ private Collection<RecipeHolder<KitchenProvidedRecipe>> getProvidedRecipes(Level
141104

142105
private Set<Identifier> getAvailableRecipeSources() {
143106
final var result = new HashSet<Identifier>();
144-
for (final var craftableProvider : recipeProviderList) {
107+
for (final var craftableProvider : recipeProviders) {
145108
result.addAll(craftableProvider.getKitchenRecipeSources());
146109
}
147110
return result;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package net.blay09.mods.cookingforblockheads.crafting;
2+
3+
import net.blay09.mods.balm.Balm;
4+
import net.blay09.mods.cookingforblockheads.api.Kitchen;
5+
import net.blay09.mods.cookingforblockheads.api.KitchenItemProcessor;
6+
import net.blay09.mods.cookingforblockheads.api.KitchenItemProvider;
7+
import net.blay09.mods.cookingforblockheads.api.KitchenRecipeProvider;
8+
import net.blay09.mods.cookingforblockheads.capability.ModCapabilities;
9+
import net.blay09.mods.cookingforblockheads.tag.ModBlockTags;
10+
import net.minecraft.core.BlockPos;
11+
import net.minecraft.core.Direction;
12+
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+
16+
import java.util.ArrayList;
17+
import java.util.HashSet;
18+
import java.util.List;
19+
import java.util.Set;
20+
21+
public class KitchenMultiblockScanner {
22+
private final Set<BlockPos> checkedPos = new HashSet<>();
23+
private final List<KitchenItemProvider> itemProviders = new ArrayList<>();
24+
private final List<KitchenRecipeProvider> recipeProviders = new ArrayList<>();
25+
private final List<KitchenItemProcessor> itemProcessors = new ArrayList<>();
26+
27+
public void findNeighbourCraftingBlocks(Level level, BlockPos pos) {
28+
findNeighbourCraftingBlocks(level, pos, true);
29+
}
30+
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);
38+
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+
}
46+
47+
final var recipeProvider = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_RECIPE_PROVIDER);
48+
if (recipeProvider != null) {
49+
recipeProviders.add(recipeProvider);
50+
}
51+
52+
final var itemProcessor = Balm.capabilities().getCapability(blockEntity, ModCapabilities.KITCHEN_ITEM_PROCESSOR);
53+
if (itemProcessor != null) {
54+
itemProcessors.add(itemProcessor);
55+
}
56+
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+
}
63+
}
64+
}
65+
}
66+
}
67+
68+
public Kitchen createKitchen(Level level, boolean allowCrafting) {
69+
return new KitchenImpl(level, allowCrafting, itemProviders, recipeProviders, itemProcessors);
70+
}
71+
}

0 commit comments

Comments
 (0)