Cooking Machines Always Set The First Cook Time To 200 Ticks

by ADMIN 61 views

Introduction

Cooking machines in Minecraft are an essential part of any player's inventory, allowing them to cook food and other items. However, a common issue arises when the first cook time is hardcoded to 200 ticks, which can be problematic for certain recipes. In this article, we will explore the issue and provide a solution to make cooking machines more flexible and level-aware.

The Problem with Hardcoded Cook Time

The original BlockEntity code for cooking machines does a lookup for the cook time instead of hardcoding it to 200. This is because cooking recipes can provide times that are not 200, and BlockEntities are level-aware, allowing them to perform this lookup. However, the container in question is not level-aware, making it difficult to perform this lookup.

A Solution: Using a Boolean to Set Recipe Lookup Required

To solve this issue, we can use a boolean to set a recipe lookup required and perform the lookup on the next tick. This approach allows us to make the cooking machine more flexible and level-aware.

...
    protected NonNullList<ItemStack> items = NonNullList.withSize(3, ItemStack.EMPTY);
    private int litTime, litDuration, cookingProgress, cookingTotalTime;
+    private boolean recipeDirty=false;
...
    public void tick(Level level) {
+        if (recipeDirty) {
+                if (level instanceof ServerLevel serverLevel) {
+                        this.cookingTotalTime=getTotalCookTime(serverLevel);
+                        this.cookingProgress = 0;
+                }
+                this.recipeDirty=false;
+                setChanged();
+        }
        boolean isLit = this.isLit();
...
    public void load(CompoundTag tag, HolderLookup.Provider provider) {
+        this.recipeDirty = tag.getBoolean("RecipeDirty");
...
    public void save(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider provider) {
+        tag.putBoolean("RecipeDirty", this.recipeDirty);
...
        if (i == 0 && !bl) {
-            this.cookingTotalTime = 200;
-            this.cookingProgress = 0;
+            this.recipeDirty = true;
            this.setChanged();
        }

How it Works

The solution involves adding a boolean field recipeDirty to the cooking machine's state. When the machine is first initialized, the recipeDirty flag is set to true. In the tick method, we check if the recipeDirty flag is true. If it is, we perform the lookup for the cook time using the getTotalCookTime method, which is level-aware. We then reset the recipeDirty flag to false and call setChanged() to notify any listeners that the machine's state has changed.

Benefits of the Solution

The solution provides several benefits, including:

  • Flexibility: The cooking machine can now handle recipes with cook times that are not 200.
  • Level-awareness: The machine is now level-aware, allowing it to perform the lookup for the cook time.
  • Improved performance: By performing the lookup on the next tick, we avoid the overhead of performing the lookup every time the machine is initialized.

Conclusion

Q&A: Frequently Asked Questions

In this section, we will answer some frequently asked questions about the solution to the issue of cooking machines always setting the first cook time to 200 ticks.

Q: Why is the cook time hardcoded to 200 ticks in the first place?

A: The cook time is hardcoded to 200 ticks in the first place because the original BlockEntity code for cooking machines does a lookup for the cook time instead of hardcoding it to 200. However, the container in question is not level-aware, making it difficult to perform this lookup.

Q: What is the benefit of using a boolean to set a recipe lookup required?

A: The benefit of using a boolean to set a recipe lookup required is that it allows us to make the cooking machine more flexible and level-aware. By performing the lookup on the next tick, we avoid the overhead of performing the lookup every time the machine is initialized.

Q: How does the solution improve the performance of the cooking machine?

A: The solution improves the performance of the cooking machine by performing the lookup for the cook time on the next tick, rather than every time the machine is initialized. This reduces the overhead of performing the lookup and improves the overall performance of the machine.

Q: Can the solution be applied to other types of machines?

A: Yes, the solution can be applied to other types of machines that have a similar issue with hardcoded cook times. By using a boolean to set a recipe lookup required and performing the lookup on the next tick, we can make these machines more flexible and level-aware.

Q: What are the potential drawbacks of using a boolean to set a recipe lookup required?

A: One potential drawback of using a boolean to set a recipe lookup required is that it may introduce additional complexity to the code. However, this complexity is minimal and can be easily managed with proper testing and debugging.

Q: How can I implement the solution in my own code?

A: To implement the solution in your own code, you can follow these steps:

  1. Add a boolean field recipeDirty to the cooking machine's state.
  2. Set the recipeDirty flag to true when the machine is first initialized.
  3. In the tick method, check if the recipeDirty flag is true. If it is, perform the lookup for the cook time using the getTotalCookTime method.
  4. Reset the recipeDirty flag to false and call setChanged() to notify any listeners that the machine's state has changed.

Conclusion

In this Q&A article, we have answered some frequently asked questions about the solution to the issue of cooking machines always setting the first cook time to 200 ticks. By using a boolean to set a recipe lookup required and performing the lookup on the next tick, we can make the cooking machine more flexible and level-aware, improving its performance and overall functionality.

Additional Resources

For more information on the solution to the issue of cooking machines always setting the first cook time to 200 ticks, please refer to the following resources:

Related Articles

For more information on related topics, please refer to the following articles: