Skip to content

Commit a9e2d85

Browse files
Merge pull request #32 from CodeMonkeysMods/coderabbitai/docstrings/5f162d4
📝 Add docstrings to `feat/add-workbench`
2 parents 5f162d4 + 13c40cb commit a9e2d85

18 files changed

Lines changed: 259 additions & 62 deletions

src/client/java/com/tcm/MineTale/MineTaleClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
public class MineTaleClient implements ClientModInitializer {
1212
/**
13-
* Registers client-side screen factories for custom workbench menu types.
13+
* Register client-side screen factories for custom workbench menu types.
1414
*
15-
* Binds the furnace and campfire workbench menu types to their corresponding screen constructors
16-
* so the client can create the appropriate GUI when those menus are opened.
15+
* Binds ModMenuTypes.FURNACE_WORKBENCH_MENU to FurnaceWorkbenchScreen,
16+
* ModMenuTypes.CAMPFIRE_WORKBENCH_MENU to CampfireWorkbenchScreen, and
17+
* ModMenuTypes.WORKBENCH_WORKBENCH_MENU to WorkbenchWorkbenchScreen so the client
18+
* can create the appropriate GUI when those menus open.
1719
*/
1820
@Override
1921
public void onInitializeClient() {

src/client/java/com/tcm/MineTale/block/workbenches/screen/WorkbenchWorkbenchScreen.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public WorkbenchWorkbenchScreen(WorkbenchWorkbenchMenu menu, Inventory inventory
4949
this(menu, inventory, title, createRecipeBookComponent(menu));
5050
}
5151

52+
/**
53+
* Creates a WorkbenchWorkbenchScreen bound to the given menu, player inventory, title, and recipe book component.
54+
*
55+
* @param menu the menu backing this screen
56+
* @param inventory the player's inventory shown in the screen
57+
* @param title the screen title component
58+
* @param recipeBook the MineTaleRecipeBookComponent used to display and manage recipes in this screen
59+
*/
5260
private WorkbenchWorkbenchScreen(WorkbenchWorkbenchMenu menu, Inventory inventory, Component title, MineTaleRecipeBookComponent recipeBook) {
5361
super(menu, recipeBook, inventory, title);
5462
this.mineTaleRecipeBook = recipeBook;
@@ -71,11 +79,12 @@ private static MineTaleRecipeBookComponent createRecipeBookComponent(WorkbenchWo
7179
}
7280

7381
/**
74-
* Sets the screen's GUI size and initializes layout so the title is centered.
75-
*
76-
* Sets imageWidth to 176 and imageHeight to 166 before delegating to the superclass
77-
* init method to complete widget and layout initialization (including horizontal title centering).
78-
*/
82+
* Configure the screen's GUI dimensions and initialize widgets.
83+
*
84+
* Sets the layout size (imageWidth = 176, imageHeight = 166), delegates remaining
85+
* layout initialization to the superclass, and creates the three craft buttons
86+
* ("1", "30", "All") wired to their respective handlers.
87+
*/
7988
@Override
8089
protected void init() {
8190
// Important: Set your GUI size before super.init()
@@ -156,7 +165,15 @@ protected void init() {
156165
// }
157166
// }
158167

159-
// RecipeBookComponent
168+
/**
169+
* Sends a crafting request for the currently selected recipe in the integrated recipe book.
170+
*
171+
* Locates the last recipe collection and last selected recipe ID from the recipe book component,
172+
* resolves the recipe's result item, and sends a CraftRequestPayload to the server containing that
173+
* item and the requested amount.
174+
*
175+
* @param amount the quantity to craft; use -1 to request crafting of the full available stack ("All")
176+
*/
160177

161178
private void handleCraftRequest(int amount) {
162179
// 1. Cast the book component to the Accessor to get the selected data
@@ -189,12 +206,12 @@ private void handleCraftRequest(int amount) {
189206
}
190207

191208
/**
192-
* Renders the workbench GUI background texture at the screen's top-left position.
209+
* Draws the workbench GUI background texture at the screen's top-left corner.
193210
*
194-
* @param guiGraphics the graphics context for drawing
195-
* @param f partial tick time used for interpolation
196-
* @param i current mouse x coordinate
197-
* @param j current mouse y coordinate
211+
* @param guiGraphics the graphics context used to draw GUI elements
212+
* @param f partial tick time for interpolation
213+
* @param i current mouse x coordinate relative to the window
214+
* @param j current mouse y coordinate relative to the window
198215
*/
199216
protected void renderBg(GuiGraphics guiGraphics, float f, int i, int j) {
200217
int k = this.leftPos;
@@ -227,9 +244,9 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
227244
}
228245

229246
/**
230-
* Compute the on-screen position for the recipe book toggle button for this GUI.
247+
* Computes the on-screen position for the recipe book toggle button for this GUI.
231248
*
232-
* @return the ScreenPosition located 5 pixels from the GUI's left edge and 49 pixels above the GUI's vertical center
249+
* @return the screen position placed 5 pixels from the GUI's left edge and 49 pixels above the GUI's vertical center
233250
*/
234251
@Override
235252
protected ScreenPosition getRecipeBookButtonPosition() {

src/client/java/com/tcm/MineTale/datagen/ModRecipeProvider.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,26 @@ public ModRecipeProvider(FabricDataOutput output, CompletableFuture<HolderLookup
3030
}
3131

3232
/**
33-
* Creates a RecipeProvider that registers a campfire cooking recipe for porkchop.
33+
* Creates a RecipeProvider that registers the mod's recipe set: a campfire cooking recipe
34+
* that cooks a porkchop into a cooked porkchop, a furnace cooking recipe that converts a
35+
* porkchop into an acacia boat, and a workbench crafting recipe that assembles a chest from
36+
* logs and sticks.
3437
*
35-
* The produced provider builds a single recipe that cooks a porkchop into a cooked porkchop,
36-
* requires 10 time units, is unlocked when the player has a porkchop, and is saved under
37-
* the mod namespace with path "campfire_pork_cooking".
38+
* Each recipe includes its unlock condition, crafting category, book category, processing time,
39+
* and is saved to the provided exporter under the mod-specific paths:
40+
* "campfire_pork_cooking", "furnace_pork_cooking", and "workbench_wood_chest".
3841
*
39-
* @param registryLookup provider for looking up game registries used when building recipes
40-
* @param exporter destination used to write the generated recipe JSON
41-
* @return a RecipeProvider that produces the described campfire cooking recipe
42+
* @param registryLookup provider for looking up game registries and tags used when building recipes
43+
* @param exporter destination used to write the generated recipe JSON files
44+
* @return a RecipeProvider that produces and saves the described recipes to the exporter
4245
*/
4346
@Override
4447
protected RecipeProvider createRecipeProvider(HolderLookup.Provider registryLookup, RecipeOutput exporter) {
4548
return new RecipeProvider(registryLookup, exporter) {
49+
/**
50+
* Registers three mod-specific recipes with the recipe exporter:
51+
* a campfire pork cooking recipe producing cooked porkchop (unlocked by having a porkchop, saved as "campfire_pork_cooking"), a furnace pork cooking recipe producing an acacia boat (unlocked by having a porkchop, saved as "furnace_pork_cooking"), and a workbench recipe that crafts a chest from 5 logs and 10 sticks (unlocked by having logs, saved as "workbench_wood_chest").
52+
*/
4653
@Override
4754
public void buildRecipes() {
4855
new WorkbenchRecipeBuilder(ModRecipes.CAMPFIRE_TYPE, ModRecipes.CAMPFIRE_SERIALIZER)

src/client/java/com/tcm/MineTale/datagen/builders/WorkbenchRecipeBuilder.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ public WorkbenchRecipeBuilder input(Ingredient ingredient) {
9595
}
9696

9797
/**
98-
* Adds an item input with a specific count.
98+
* Adds the given item as an ingredient multiple times to this builder.
99+
*
100+
* @param item the item to use as an ingredient
101+
* @param count the number of times to add the ingredient (if less than or equal to zero, no ingredients are added)
102+
* @return this builder instance
99103
*/
100104
public WorkbenchRecipeBuilder input(ItemLike item, int count) {
101105
Ingredient ingredient = Ingredient.of(item);
@@ -106,8 +110,12 @@ public WorkbenchRecipeBuilder input(ItemLike item, int count) {
106110
}
107111

108112
/**
109-
* Adds a tag input with a specific count.
110-
* Requires a RegistryLookup (usually available in your RecipeProvider).
113+
* Adds the ingredient represented by the given item tag to the recipe inputs the specified number of times.
114+
*
115+
* @param tag the item tag whose matching items will be used as the ingredient
116+
* @param registries a registry lookup provider used to resolve the tag (typically the provider from a RecipeProvider)
117+
* @param count the number of times to add the resolved ingredient; if zero nothing is added
118+
* @return this builder instance
111119
*/
112120
public WorkbenchRecipeBuilder input(TagKey<Item> tag, HolderLookup.Provider registries, int count) {
113121
// 1. Get the lookup for the Item registry from the provider
@@ -124,6 +132,12 @@ public WorkbenchRecipeBuilder input(TagKey<Item> tag, HolderLookup.Provider regi
124132

125133

126134

135+
/**
136+
* Set the crafting book category used to classify the recipe in the crafting book.
137+
*
138+
* @param category the crafting book category to assign to the recipe
139+
* @return the same WorkbenchRecipeBuilder instance
140+
*/
127141
public WorkbenchRecipeBuilder category(CraftingBookCategory category) {
128142
this.category = category;
129143
return this;
@@ -196,10 +210,12 @@ public Item getResult() {
196210
}
197211

198212
/**
199-
* Saves this builder's recipe to the given exporter under the specified identifier.
213+
* Registers this builder's recipe with the provided RecipeOutput using the given recipe name.
214+
*
215+
* The provided name is used as the path component to construct a recipe ResourceKey scoped to the MineTale mod.
200216
*
201217
* @param exporter the RecipeOutput that will receive the recipe
202-
* @param id the identifier to use for the saved recipe
218+
* @param name the recipe name (path component) to use when creating the recipe's Identifier
203219
*/
204220
public void save(RecipeOutput exporter, String name) {
205221
this.save(exporter, ResourceKey.create(Registries.RECIPE, Identifier.fromNamespaceAndPath(MineTale.MOD_ID, name)));

src/client/java/com/tcm/MineTale/mixin/client/ClientRecipeBookMixin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
@Mixin(ClientRecipeBook.class)
1717
public abstract class ClientRecipeBookMixin {
18+
/**
19+
* Maps supported custom recipe types to their corresponding recipe-book category and overrides the original method's result when a match is found.
20+
*
21+
* @param recipe the recipe holder to inspect for its recipe type
22+
* @param cir callback that will be used to set and return the mapped {@code RecipeBookCategory} for the original method
23+
*/
1824
@Inject(method = "getCategory", at = @At("HEAD"), cancellable = true)
1925
private static void minetale$addCustomCategory(RecipeHolder<?> recipe, CallbackInfoReturnable<RecipeBookCategory> cir) {
2026
RecipeType<?> type = recipe.value().getType();

src/client/java/com/tcm/MineTale/mixin/client/RecipeBookComponentAccessor.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,59 @@
1111

1212
@Mixin(RecipeBookComponent.class)
1313
public interface RecipeBookComponentAccessor {
14-
// GETTERS for the Screen to read what is selected
14+
/**
15+
* Gets the currently selected recipe display identifier from the recipe book component.
16+
*
17+
* @return the currently selected RecipeDisplayId
18+
*/
1519
@Accessor("lastRecipe")
1620
RecipeDisplayId getLastRecipe();
1721

22+
/**
23+
* Access the last-selected recipe collection from the recipe book component.
24+
*
25+
* @return the last-selected {@link RecipeCollection}
26+
*/
1827
@Accessor("lastRecipeCollection")
1928
RecipeCollection getLastRecipeCollection();
2029

21-
// SETTERS for the Component to update the selection
30+
/**
31+
* Set the currently selected recipe display identifier on the component.
32+
*
33+
* @param id the RecipeDisplayId to set as the current selection
34+
*/
2235
@Accessor("lastRecipe")
2336
void setLastRecipe(RecipeDisplayId id);
2437

38+
/**
39+
* Sets the currently selected recipe collection in the recipe book component.
40+
*
41+
* @param collection the recipe collection to set as the selected collection
42+
*/
2543
@Accessor("lastRecipeCollection")
2644
void setLastRecipeCollection(RecipeCollection collection);
2745

28-
// OTHER NECESSITIES
46+
/**
47+
* Accesses the component's current recipe book page.
48+
*
49+
* @return the current RecipeBookPage instance
50+
*/
2951
@Accessor("recipeBookPage")
3052
RecipeBookPage getRecipeBookPage();
3153

54+
/**
55+
* Obtain the X origin coordinate of the recipe book component.
56+
*
57+
* @return the X origin coordinate of the component
58+
*/
3259
@Invoker("getXOrigin")
3360
int invokeGetXOrigin();
3461

62+
/**
63+
* Obtain the Y origin (vertical coordinate) of the recipe book component.
64+
*
65+
* @return the Y origin (vertical coordinate) of the component
66+
*/
3567
@Invoker("getYOrigin")
3668
int invokeGetYOrigin();
3769
}

src/client/java/com/tcm/MineTale/recipe/MineTaleRecipeBookComponent.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ public class MineTaleRecipeBookComponent extends RecipeBookComponent<RecipeBookM
3636
Identifier.withDefaultNamespace("recipe_book/filter_disabled_focused")
3737
);
3838

39+
/**
40+
* Creates a MineTaleRecipeBookComponent bound to a specific machine recipe type.
41+
*
42+
* @param recipeBookMenu the recipe book menu instance this component is attached to
43+
* @param list the list of tab information to display in the recipe book
44+
* @param filterType the RecipeType used to filter which recipes are shown in this component
45+
*/
3946
public MineTaleRecipeBookComponent(RecipeBookMenu recipeBookMenu, List<TabInfo> list, RecipeType<?> filterType) {
4047
super(recipeBookMenu, list);
4148
this.filterType = filterType;
4249
}
4350

51+
/**
52+
* Get the ID of the last recipe clicked in the current recipe book page.
53+
*
54+
* @return the `RecipeDisplayId` of the last clicked recipe, or `null` if there is no open page or no recipe has been clicked
55+
*/
4456
public @Nullable RecipeDisplayId getSelectedRecipeId() {
4557
// Cast 'this' to the Accessor interface to call the generated getter
4658
RecipeBookPage page = ((RecipeBookComponentAccessor)this).getRecipeBookPage();
@@ -51,6 +63,15 @@ public MineTaleRecipeBookComponent(RecipeBookMenu recipeBookMenu, List<TabInfo>
5163
return null;
5264
}
5365

66+
/**
67+
* Filters the provided recipe collection to include only workbench displays that match this component's filterType.
68+
*
69+
* Uses the provided StackedItemContents to perform matching and retains only recipe displays whose type is
70+
* ModRecipeDisplay.WORKBENCH_TYPE and whose WorkbenchRecipeDisplay.getRecipeType() equals this.filterType.
71+
*
72+
* @param recipeCollection the collection whose selectable recipes will be updated
73+
* @param stackedItemContents the available stacked item contents used for recipe matching
74+
*/
5475
@Override
5576
protected void selectMatchingRecipes(RecipeCollection recipeCollection, StackedItemContents stackedItemContents) {
5677
// Force everything to be "selected"
@@ -74,6 +95,13 @@ protected void selectMatchingRecipes(RecipeCollection recipeCollection, StackedI
7495
});
7596
}
7697

98+
/**
99+
* Handle mouse clicks inside the recipe book and select MineTale workbench recipes when clicked.
100+
*
101+
* @param mouseButtonEvent the mouse event to process
102+
* @param bl a pass-through boolean flag forwarded to the underlying recipe page click handler
103+
* @return `true` if the click was handled by selecting a MineTale workbench recipe, `false` otherwise
104+
*/
77105
@Override
78106
public boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean bl) {
79107
if (!this.isVisible() || this.minecraft.player.isSpectator()) {
@@ -110,6 +138,11 @@ public boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean bl) {
110138
return super.mouseClicked(mouseButtonEvent, bl);
111139
}
112140

141+
/**
142+
* Provide the sprite set used by the recipe book's filter toggle button.
143+
*
144+
* @return the WidgetSprites used for the filter toggle (enabled/disabled and focused states)
145+
*/
113146
@Override
114147
protected WidgetSprites getFilterButtonTextures() {
115148
// Returns the textures for the "Toggle craftable" button

src/main/java/com/tcm/MineTale/MineTale.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ public void onInitialize() {
168168
LOGGER.info("Hello Fabric world!");
169169
}
170170

171-
private boolean hasIngredients(ServerPlayer player, WorkbenchRecipe recipe) {
171+
/**
172+
* Checks whether the player's inventory contains the necessary ingredients to craft the given recipe without modifying the real inventory.
173+
*
174+
* @param player the server player whose inventory will be simulated
175+
* @param recipe the workbench recipe to validate against the player's inventory
176+
* @return `true` if all required ingredients can be satisfied from the player's current inventory, `false` otherwise
177+
*/
178+
private boolean hasIngredients(ServerPlayer player, WorkbenchRecipe recipe) {
172179
// We simulate the craft using a copy of the inventory
173180
List<ItemStack> tempInv = new java.util.ArrayList<>();
174181
for (int i = 0; i < player.getInventory().getContainerSize(); i++) {
@@ -189,6 +196,12 @@ private boolean hasIngredients(ServerPlayer player, WorkbenchRecipe recipe) {
189196
return true;
190197
}
191198

199+
/**
200+
* Consumes one matching item from the player's inventory for each ingredient in the given workbench recipe.
201+
*
202+
* @param player the player whose inventory will be modified
203+
* @param recipe the workbench recipe whose ingredients should be consumed
204+
*/
192205
private void consumeIngredients(ServerPlayer player, WorkbenchRecipe recipe) {
193206
for (Ingredient ingredient : recipe.ingredients()) {
194207
for (int i = 0; i < player.getInventory().getContainerSize(); i++) {

src/main/java/com/tcm/MineTale/block/workbenches/WorkbenchWorkbench.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ public WorkbenchWorkbench(Properties properties, Supplier<BlockEntityType<? exte
5151
}
5252

5353
/**
54-
* Supplies a ticker that updates WorkbenchWorkbench block entities each tick.
54+
* Provides a ticker for workbench block entities when the supplied block entity type matches this block's entity type.
5555
*
56-
* @return a BlockEntityTicker that invokes AbstractWorkbenchEntity.tick for matching WorkbenchWorkbenchEntity instances, `null` if the supplied block entity type does not match
56+
* @param type the block entity type to match against this block's workbench entity type
57+
* @return a BlockEntityTicker that updates matching workbench block entities, or {@code null} if the types do not match
5758
*/
5859
@Nullable
5960
@Override
@@ -87,9 +88,7 @@ public RenderShape getRenderShape(BlockState state) {
8788
private static final VoxelShape SHAPE = Block.box(0, 0, 0, 16, 7, 16);
8889

8990
/**
90-
* Provides the block's collision and interaction shape.
91-
*
92-
* <p>Uses a fixed voxel shape covering a 1×1 footprint with a height of 7 units (coordinates: x 0–16, y 0–7, z 0–16).</p>
91+
* The block's collision and interaction shape as a 1×1 footprint with height 7 (x 0–16, y 0–7, z 0–16).
9392
*
9493
* @return the voxel shape used for collision and interaction
9594
*/

0 commit comments

Comments
 (0)