Skip to content

Commit 030ccd2

Browse files
fix: AI comments
1 parent 8819d40 commit 030ccd2

11 files changed

Lines changed: 44 additions & 170 deletions

File tree

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

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
public class WorkbenchWorkbenchScreen extends AbstractRecipeBookScreen<WorkbenchWorkbenchMenu> {
3131
private static final Identifier TEXTURE =
32-
Identifier.fromNamespaceAndPath(MineTale.MOD_ID, "textures/gui/container/furnace_workbench.png");
32+
Identifier.fromNamespaceAndPath(MineTale.MOD_ID, "textures/gui/container/workbench_workbench.png");
3333

3434
private final MineTaleRecipeBookComponent mineTaleRecipeBook;
3535

@@ -105,65 +105,6 @@ protected void init() {
105105
}).bounds(this.leftPos + 144, this.topPos + 20, 30, 20).build());
106106
}
107107

108-
// private void handleCraftRequest(int amount) {
109-
// RecipeBookPage page = ((RecipeBookComponentAccessor)this.mineTaleRecipeBook).getRecipeBookPage();
110-
// RecipeCollection collection = page.getLastClickedRecipeCollection();
111-
// RecipeDisplayId displayId = page.getLastClickedRecipe();
112-
113-
// if (collection != null && displayId != null) {
114-
// // 1. Find the specific entry that was clicked
115-
// for (RecipeDisplayEntry entry : collection.getSelectedRecipes(RecipeCollection.CraftableStatus.ANY)) {
116-
// if (entry.id().equals(displayId)) {
117-
// // 2. Resolve the visual result into an actual ItemStack
118-
// List<ItemStack> results = entry.resultItems(SlotDisplayContext.fromLevel(this.minecraft.level));
119-
120-
// if (!results.isEmpty()) {
121-
// ItemStack resultStack = results.get(0);
122-
// // 3. Send the item and amount to the server
123-
// // Note: Update your CraftRequestPayload to accept ItemStack instead of Identifier
124-
// ClientPlayNetworking.send(new CraftRequestPayload(resultStack, amount));
125-
// }
126-
// break;
127-
// }
128-
// }
129-
// }
130-
// }
131-
132-
// private void handleCraftRequest(int amount) {
133-
// // 1. Get the current page from the recipe book
134-
// // We use your mixin/accessor to get the internal page object
135-
// RecipeBookPage page = ((RecipeBookComponentAccessor)this.mineTaleRecipeBook).getRecipeBookPage();
136-
137-
// // 2. Identify WHAT was clicked
138-
// RecipeCollection collection = page.getLastClickedRecipeCollection();
139-
// RecipeDisplayId displayId = page.getLastClickedRecipe();
140-
141-
// if (collection != null && displayId != null) {
142-
// // 3. Find the display entry
143-
// for (RecipeDisplayEntry entry : collection.getSelectedRecipes(RecipeCollection.CraftableStatus.ANY)) {
144-
// if (entry.id().equals(displayId)) {
145-
// // 4. Get the result item (the Chest)
146-
// // 1.21.1 uses SlotDisplayContext to handle dynamic results
147-
// List<ItemStack> results = entry.resultItems(SlotDisplayContext.fromLevel(this.minecraft.level));
148-
149-
// if (!results.isEmpty()) {
150-
// ItemStack resultStack = results.get(0);
151-
152-
// // 5. Send the packet to the Server
153-
// // IMPORTANT: Ensure your CraftRequestPayload is registered to handle
154-
// // an ItemStack and an Int.
155-
// ClientPlayNetworking.send(new CraftRequestPayload(resultStack, amount));
156-
157-
// // Optional: Play a click sound so the player knows it worked
158-
// this.minecraft.getSoundManager().play(net.minecraft.client.resources.sounds.SimpleSoundInstance.forUI(
159-
// net.minecraft.sounds.SoundEvents.UI_BUTTON_CLICK, 1.0F));
160-
// }
161-
// break;
162-
// }
163-
// }
164-
// }
165-
// }
166-
167108
/**
168109
* Sends a crafting request for the currently selected recipe in the integrated recipe book.
169110
*
@@ -191,7 +132,7 @@ private void handleCraftRequest(int amount) {
191132
if (!results.isEmpty()) {
192133
ItemStack resultStack = results.get(0);
193134

194-
// 4. LOG FOR DEBUGGING: Does this print in your console?
135+
// 4. LOG FOR DEBUGGING
195136
System.out.println("Sending craft request for: " + resultStack + " amount: " + amount);
196137

197138
ClientPlayNetworking.send(new CraftRequestPayload(resultStack, amount));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean bl) {
132132
return true;
133133
}
134134
}
135+
136+
// We return true here because page.mouseClicked was true.
137+
// This prevents the super call from processing the same click again.
138+
return true;
135139
}
136140

137141
return super.mouseClicked(mouseButtonEvent, bl);

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18+
import com.tcm.MineTale.block.workbenches.menu.WorkbenchWorkbenchMenu;
1819
import com.tcm.MineTale.network.CraftRequestPayload;
1920
import com.tcm.MineTale.recipe.WorkbenchRecipe;
2021
import com.tcm.MineTale.registry.ModBlockEntities;
@@ -124,17 +125,28 @@ public void onInitialize() {
124125
ServerPlayNetworking.registerGlobalReceiver(CraftRequestPayload.TYPE, (payload, context) -> {
125126
context.server().execute(() -> {
126127
ServerPlayer player = context.player();
128+
129+
// --- SECURITY GUARD ---
130+
// Ensure the player actually has the Workbench UI open before processing the craft
131+
if (!(player.containerMenu instanceof WorkbenchWorkbenchMenu)) {
132+
return;
133+
}
134+
127135
ItemStack requestedResult = payload.resultItem();
128136
int amount = payload.amount();
137+
129138
// 1. Get the RecipeManager from the server level
130139
RecipeManager recipeManager = player.level().recipeAccess();
131140

132141
// 2. Find the recipe by matching the output ItemStack
133-
Optional<RecipeHolder<WorkbenchRecipe>> recipeOpt = recipeManager.getRecipes().stream()
134-
.filter(holder -> holder.value() instanceof WorkbenchRecipe) // Check if it's your recipe class
135-
.map(holder -> (RecipeHolder<WorkbenchRecipe>) holder) // Cast to your type
142+
Optional<RecipeHolder<WorkbenchRecipe>> recipeOpt = recipeManager.getAllOfType(ModRecipes.WORKBENCH_TYPE).stream()
136143
.filter(holder -> {
137-
// Compare the recipe result to the item requested by the client
144+
// Guard against recipes with no results before accessing index 0
145+
if (holder.value().results().isEmpty()) {
146+
return false;
147+
}
148+
149+
// Compare the first result of the workbench recipe to the requested item
138150
ItemStack result = holder.value().results().get(0);
139151
return ItemStack.isSameItem(result, requestedResult);
140152
})
@@ -144,15 +156,11 @@ public void onInitialize() {
144156
WorkbenchRecipe recipe = recipeOpt.get().value();
145157

146158
// 2. Determine craft limit (Handle "All" logic)
147-
int limit = (amount == -1) ? 64 : amount;
159+
int limit = (amount == -1) ? 64 : Math.min(Math.max(amount, 0), 64);;
148160

149161
for (int i = 0; i < limit; i++) {
150-
// Check if player has the 15 items (5 logs, 10 sticks)
151162
if (hasIngredients(player, recipe)) {
152163
consumeIngredients(player, recipe);
153-
154-
// Add the result stack to player inventory
155-
// We copy it to avoid modifying the recipe instance
156164
player.getInventory().add(recipe.results().get(0).copy());
157165
} else {
158166
break;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public int getTier() {
6060
public BlockState getStateForPlacement(BlockPlaceContext context) {
6161
BlockPos pos = context.getClickedPos();
6262
Level level = context.getLevel();
63-
Direction facing = context.getHorizontalDirection().getOpposite();
63+
Direction facing = context.getHorizontalDirection();
6464

6565
// Check horizontal space
6666
if (isWide) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// ChestBlock
2525

2626
public class WorkbenchWorkbench extends AbstractWorkbench<WorkbenchWorkbenchEntity> {
27-
public static final boolean IS_WIDE = false;
27+
public static final boolean IS_WIDE = true;
2828
public static final boolean IS_TALL = false;
2929

3030
public static final MapCodec<WorkbenchWorkbench> CODEC = simpleCodec(WorkbenchWorkbench::new);
@@ -85,7 +85,7 @@ public RenderShape getRenderShape(BlockState state) {
8585
return RenderShape.MODEL;
8686
}
8787

88-
private static final VoxelShape SHAPE = Block.box(0, 0, 0, 16, 7, 16);
88+
private static final VoxelShape SHAPE = Block.box(0, 0, 0, 16, 16, 16);
8989

9090
/**
9191
* The block's collision and interaction shape as a 1×1 footprint with height 7 (x 0–16, y 0–7, z 0–16).

src/main/java/com/tcm/MineTale/block/workbenches/entity/WorkbenchWorkbenchEntity.java

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@
1919
import net.minecraft.world.inventory.ContainerData;
2020
import net.minecraft.world.item.ItemStack;
2121
import net.minecraft.world.item.crafting.RecipeType;
22-
import net.minecraft.world.level.Level;
2322
import net.minecraft.world.level.block.state.BlockState;
2423
import net.minecraft.world.level.storage.ValueInput;
2524
import net.minecraft.world.level.storage.ValueOutput;
2625

2726
public class WorkbenchWorkbenchEntity extends AbstractWorkbenchEntity {
28-
private int cookTime;
29-
private int cookTimeTotal = 200;
30-
private int fuelTime;
31-
private int inputEnd = 3;
32-
3327
protected final ContainerData data = new ContainerData() {
3428
/**
3529
* Retrieves an internal data value by index for UI synchronization.
@@ -41,10 +35,10 @@ public class WorkbenchWorkbenchEntity extends AbstractWorkbenchEntity {
4135
@Override
4236
public int get(int index) {
4337
return switch (index) {
44-
case 0 -> fuelTime;
38+
case 0 -> 0;
4539
case 1 -> 100; // Fuel total
46-
case 2 -> cookTime;
47-
case 3 -> cookTimeTotal;
40+
case 2 -> 0;
41+
case 3 -> 0;
4842
default -> 0;
4943
};
5044
}
@@ -64,10 +58,7 @@ public int get(int index) {
6458
*/
6559
@Override
6660
public void set(int index, int value) {
67-
switch (index) {
68-
case 0 -> fuelTime = value;
69-
case 2 -> cookTime = value;
70-
}
61+
// Not required on WorkbenchEntity
7162
}
7263

7364
/**
@@ -96,66 +87,6 @@ public WorkbenchWorkbenchEntity(BlockPos blockPos, BlockState blockState) {
9687
this.tier = 1;
9788
}
9889

99-
/**
100-
* Run server-side per-tick updates for this workbench entity.
101-
*
102-
* <p>Does nothing on the client. On the server this advances the input queue so the next
103-
* item becomes ready and, if any state changes occur, marks the block entity as changed.
104-
*
105-
* @param level the world in which the workbench exists
106-
* @param pos the position of the workbench block
107-
* @param state the current block state at the workbench's position
108-
*/
109-
public void tick(Level level, BlockPos pos, BlockState state) {
110-
if (level.isClientSide()) return;
111-
112-
boolean changed = false;
113-
114-
// 1. Shift queue forward so the next item is ready to smelt
115-
if (shiftQueueForward()) {
116-
changed = true;
117-
}
118-
119-
if (changed) {
120-
setChanged();
121-
}
122-
}
123-
124-
/**
125-
* Compacts the input queue by moving items forward into the first empty slot ahead.
126-
*
127-
* @return true if any items were moved, false otherwise.
128-
*/
129-
private boolean shiftQueueForward() {
130-
boolean moved = false;
131-
// Start from the front and pull from the back
132-
for (int i = Constants.INPUT_START; i < this.inputEnd; i++) {
133-
ItemStack current = inventory.getItem(i);
134-
ItemStack next = inventory.getItem(i + 1);
135-
136-
if (current.isEmpty() && !next.isEmpty()) {
137-
inventory.setItem(i, next.copy());
138-
inventory.setItem(i + 1, ItemStack.EMPTY);
139-
moved = true;
140-
}
141-
}
142-
return moved;
143-
}
144-
145-
/**
146-
* Determines whether the provided item stack is a supported food.
147-
*
148-
* @param stack the item stack to test
149-
*/
150-
// private boolean isCookable(List<ItemStack> stacks) { return stack.is(ItemTags.MEAT); }
151-
152-
/**
153-
* Determines whether the given item stack represents a fuel item.
154-
*
155-
* @param stack the item stack to inspect
156-
*/
157-
// private boolean isFuel(ItemStack stack) { return stack.is(ItemTags.LOGS_THAT_BURN) || stack.is(Items.STICK); }
158-
15990
/**
16091
* Persist this workbench's state to the given ValueOutput.
16192
*

src/main/java/com/tcm/MineTale/block/workbenches/menu/AbstractWorkbenchContainerMenu.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ protected void addPlayerHotbar(Inventory playerInventory) {
188188
* @return the cook progress scaled to a 24-pixel width (`0` if there is no progress or total cook time is zero)
189189
*/
190190
public int getCookProgress() {
191+
// Guard against null data or missing indices (needs at least index 2 and 3)
192+
if (this.data == null || this.data.getCount() < 4) {
193+
return 0;
194+
}
195+
191196
int i = this.data.get(2); // cookTime
192197
int j = this.data.get(3); // cookTimeTotal
193198
return j != 0 && i != 0 ? i * 24 / j : 0;
@@ -202,6 +207,11 @@ public int getCookProgress() {
202207
* @return an integer between 0 and 13 representing the current burn progress in pixels
203208
*/
204209
public int getBurnProgress() {
210+
// Guard against null data or missing indices (needs at least index 0 and 1)
211+
if (this.data == null || this.data.getCount() < 2) {
212+
return 0;
213+
}
214+
205215
int i = this.data.get(1); // fuelTimeTotal
206216
if (i == 0) i = 200;
207217
return this.data.get(0) * 13 / i; // fuelTime

src/main/java/com/tcm/MineTale/block/workbenches/menu/WorkbenchWorkbenchMenu.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public WorkbenchWorkbenchMenu(int syncId, Inventory playerInventory, ContainerDa
7777
*/
7878
@Override
7979
public void fillCraftSlotsStackedContents(StackedItemContents stackedItemContents) {
80-
// This is vital for the recipe book to "see" what is currently in your furnace.
80+
// This is vital for the recipe book to "see" what is currently in your workbench.
8181
// It allows the book to calculate if you have enough items to craft more.
8282
this.playerInventory.fillStackedContents(stackedItemContents);
8383
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,6 @@ public List<RecipeDisplay> display() {
146146
}
147147

148148
// --- SERIALIZER ---
149-
public record SizedIngredient(Ingredient ingredient, int count) {
150-
public static final Codec<SizedIngredient> CODEC = RecordCodecBuilder.create(inst -> inst.group(
151-
Ingredient.CODEC.fieldOf("ingredient").forGetter(SizedIngredient::ingredient),
152-
Codec.INT.optionalFieldOf("count", 1).forGetter(SizedIngredient::count)
153-
).apply(inst, SizedIngredient::new));
154-
155-
public static final StreamCodec<RegistryFriendlyByteBuf, SizedIngredient> STREAM_CODEC = StreamCodec.composite(
156-
Ingredient.CONTENTS_STREAM_CODEC, SizedIngredient::ingredient,
157-
ByteBufCodecs.VAR_INT, SizedIngredient::count,
158-
SizedIngredient::new
159-
);
160-
}
161-
162149
public static class Serializer implements RecipeSerializer<WorkbenchRecipe> {
163150
private final RecipeType<WorkbenchRecipe> recipeType;
164151
private final MapCodec<WorkbenchRecipe> codec;

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,16 @@ public class ModRecipeDisplay {
2828
new RecipeDisplay.Type<>(WorkbenchRecipeDisplay.CODEC, STREAM_CODEC);
2929

3030
// 1. Declare the fields but don't assign them yet
31-
public static RecipeBookCategory CAMPFIRE_SEARCH;
32-
public static RecipeBookCategory WORKBENCH_SEARCH;
33-
public static RecipeBookCategory FURNACE_T1_SEARCH;
34-
35-
31+
public static final RecipeBookCategory CAMPFIRE_SEARCH = registerCategory("campfire_recipe_book_category");;
32+
public static final RecipeBookCategory WORKBENCH_SEARCH = registerCategory("workbench_recipe_book_category");
33+
public static final RecipeBookCategory FURNACE_T1_SEARCH = registerCategory("furnace_t1_recipe_book_category");
3634

3735
/**
3836
* Registers the workbench recipe display type into the built-in recipe display registry.
3937
*
4038
* The registration uses this mod's ID combined with the path "workbench_recipe_display" as the identifier.
4139
*/
4240
public static void initialize() {
43-
CAMPFIRE_SEARCH = registerCategory("campfire_recipe_book_category");
44-
45-
WORKBENCH_SEARCH = registerCategory("workbench_recipe_book_category");
46-
47-
FURNACE_T1_SEARCH = registerCategory("furnace_t1_recipe_book_category");
4841
// Register the Display TYPE
4942
Registry.register(
5043
BuiltInRegistries.RECIPE_DISPLAY,

0 commit comments

Comments
 (0)