-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cache of last found ingredient spots for massive performanc…
…e improvements #676
- Loading branch information
1 parent
c3e01fe
commit 568fc7c
Showing
13 changed files
with
326 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...ge/src/main/java/net/blay09/mods/cookingforblockheads/ItemHandlerKitchenItemProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package net.blay09.mods.cookingforblockheads; | ||
|
||
import net.blay09.mods.cookingforblockheads.api.CacheHint; | ||
import net.blay09.mods.cookingforblockheads.api.IngredientToken; | ||
import net.blay09.mods.cookingforblockheads.api.KitchenItemProvider; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.Ingredient; | ||
import net.neoforged.neoforge.items.IItemHandler; | ||
import net.neoforged.neoforge.items.ItemHandlerHelper; | ||
|
||
import java.util.Collection; | ||
|
||
public class ItemHandlerKitchenItemProvider implements KitchenItemProvider { | ||
private final IItemHandler itemHandler; | ||
|
||
public ItemHandlerKitchenItemProvider(IItemHandler itemHandler) { | ||
this.itemHandler = itemHandler; | ||
} | ||
|
||
@Override | ||
public IngredientToken findIngredient(Ingredient ingredient, Collection<IngredientToken> ingredientTokens, CacheHint cacheHint) { | ||
if (cacheHint instanceof ItemHandlerIngredientToken itemHandlerIngredientToken) { | ||
final var slotStack = itemHandler.getStackInSlot(itemHandlerIngredientToken.slot); | ||
if (ingredient.test(slotStack) && hasUsesLeft(itemHandlerIngredientToken.slot, slotStack, ingredientTokens)) { | ||
return itemHandlerIngredientToken; | ||
} | ||
} | ||
|
||
for (int i = 0; i < itemHandler.getSlots(); i++) { | ||
final var slotStack = itemHandler.getStackInSlot(i); | ||
if (ingredient.test(slotStack) && hasUsesLeft(i, slotStack, ingredientTokens)) { | ||
return new ItemHandlerIngredientToken(i); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public IngredientToken findIngredient(ItemStack itemStack, Collection<IngredientToken> ingredientTokens, CacheHint cacheHint) { | ||
if (cacheHint instanceof ItemHandlerIngredientToken itemHandlerIngredientToken) { | ||
final var slotStack = itemHandler.getStackInSlot(itemHandlerIngredientToken.slot); | ||
if (ItemStack.isSameItemSameTags(slotStack, itemStack) && hasUsesLeft(itemHandlerIngredientToken.slot, slotStack, ingredientTokens)) { | ||
return itemHandlerIngredientToken; | ||
} | ||
} | ||
|
||
for (int i = 0; i < itemHandler.getSlots(); i++) { | ||
final var slotStack = itemHandler.getStackInSlot(i); | ||
if (ItemStack.isSameItemSameTags(slotStack, itemStack) && hasUsesLeft(i, slotStack, ingredientTokens)) { | ||
return new ItemHandlerIngredientToken(i); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private boolean hasUsesLeft(int slot, ItemStack slotStack, Collection<IngredientToken> ingredientTokens) { | ||
var uses = slotStack.getCount(); | ||
for (IngredientToken ingredientToken : ingredientTokens) { | ||
if (ingredientToken instanceof ItemHandlerIngredientToken itemHandlerIngredientToken) { | ||
if (itemHandlerIngredientToken.slot == slot) { | ||
uses--; | ||
} | ||
} | ||
} | ||
|
||
return uses > 0; | ||
} | ||
|
||
@Override | ||
public CacheHint getCacheHint(IngredientToken ingredientToken) { | ||
return ingredientToken instanceof ItemHandlerIngredientToken itemHandlerIngredientToken ? itemHandlerIngredientToken : CacheHint.NONE; | ||
} | ||
|
||
public class ItemHandlerIngredientToken implements IngredientToken, CacheHint { | ||
private final int slot; | ||
|
||
public ItemHandlerIngredientToken(int slot) { | ||
this.slot = slot; | ||
} | ||
|
||
@Override | ||
public ItemStack peek() { | ||
return itemHandler.getStackInSlot(slot); | ||
} | ||
|
||
@Override | ||
public ItemStack consume() { | ||
return itemHandler.extractItem(slot, 1, false); | ||
} | ||
|
||
@Override | ||
public ItemStack restore(ItemStack itemStack) { | ||
final var restItem = itemHandler.insertItem(slot, itemStack, false); | ||
if (!restItem.isEmpty()) { | ||
return ItemHandlerHelper.insertItemStacked(itemHandler, restItem, false); | ||
} | ||
|
||
return ItemStack.EMPTY; | ||
} | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
...main/java/net/blay09/mods/cookingforblockheads/compat/ItemHandlerKitchenItemProvider.java
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
shared/src/main/java/net/blay09/mods/cookingforblockheads/api/CacheHint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.blay09.mods.cookingforblockheads.api; | ||
|
||
public interface CacheHint { | ||
CacheHint NONE = new CacheHint() { | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.