Skip to content

Commit

Permalink
feat: Update to Minecraft 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Nov 26, 2024
1 parent 20e7b74 commit 2aa5154
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.blay09.mods.craftingtweaks.crafting;

import net.minecraft.core.Holder;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand All @@ -12,7 +10,7 @@

public class CraftingContext {

private final Map<List<Holder<Item>>, Integer> cachedProviderIndexByIngredient = new HashMap<>();
private final Map<Ingredient, Integer> cachedProviderIndexByIngredient = new HashMap<>();
private final Map<CraftingOperation.IngredientTokenKey, IngredientCacheHint> cacheHintsByIngredient = new HashMap<>();

private final List<IngredientProvider> ingredientProviders;
Expand All @@ -30,7 +28,7 @@ public List<IngredientProvider> getIngredientProviders() {
}

public int getCachedIngredientProviderIndexFor(Ingredient ingredient) {
return cachedProviderIndexByIngredient.getOrDefault(ingredient.items(), -1);
return cachedProviderIndexByIngredient.getOrDefault(ingredient, -1);
}

public IngredientCacheHint getCacheHintFor(CraftingOperation.IngredientTokenKey ingredientTokenKey) {
Expand All @@ -39,6 +37,6 @@ public IngredientCacheHint getCacheHintFor(CraftingOperation.IngredientTokenKey

public void cache(CraftingOperation.IngredientTokenKey ingredientTokenKey, int itemProviderIndex, IngredientCacheHint cacheHint) {
cacheHintsByIngredient.put(ingredientTokenKey, cacheHint);
cachedProviderIndexByIngredient.put(ingredientTokenKey.items(), itemProviderIndex);
cachedProviderIndexByIngredient.put(ingredientTokenKey.ingredient(), itemProviderIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import net.blay09.mods.craftingtweaks.api.CraftingTweaksAPI;
import net.minecraft.core.Holder;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
Expand All @@ -16,18 +14,18 @@

public class CraftingOperation {

public record IngredientTokenKey(int providerIndex, List<Holder<Item>> items) {
public record IngredientTokenKey(int providerIndex, Ingredient ingredient) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IngredientTokenKey that = (IngredientTokenKey) o;
return providerIndex == that.providerIndex && Objects.equals(items, that.items);
return providerIndex == that.providerIndex && Objects.equals(ingredient, that.ingredient);
}

@Override
public int hashCode() {
return Objects.hash(providerIndex, items);
return Objects.hash(providerIndex, ingredient);
}
}

Expand Down Expand Up @@ -61,15 +59,15 @@ public CraftingOperation prepare() {
final List<Optional<Ingredient>> ingredients = recipeMapper.getIngredients(recipe);
for (int i = 0; i < ingredients.size(); i++) {
final var ingredient = ingredients.get(i).orElse(null);
if (ingredient == null || ingredient.items().isEmpty()) {
if (ingredient == null || ingredient.isEmpty()) {
ingredientTokens.add(IngredientToken.EMPTY);
continue;
}

final var lockedInput = lockedInputs != null ? lockedInputs.get(i) : ItemStack.EMPTY;
final var ingredientToken = accountForIngredient(ingredient, lockedInput);
if (ingredientToken != null) {
if (ingredient.items().size() > 1) {
if (ingredient.items().count() > 1) {
if (lockedInputs == null) {
lockedInputs = NonNullList.withSize(ingredients.size(), ItemStack.EMPTY);
}
Expand Down Expand Up @@ -109,7 +107,7 @@ private IngredientToken accountForIngredient(Ingredient ingredient, ItemStack lo

@Nullable
private IngredientToken accountForIngredient(int ingredientProviderIndex, IngredientProvider ingredientProvider, Ingredient ingredient, ItemStack lockedInput, boolean useCache) {
final var ingredientTokenKey = new IngredientTokenKey(ingredientProviderIndex, ingredient.items());
final var ingredientTokenKey = new IngredientTokenKey(ingredientProviderIndex, ingredient);
final var scopedIngredientTokens = tokensByIngredient.get(ingredientTokenKey);
final var cacheHint = useCache ? context.getCacheHintFor(ingredientTokenKey) : IngredientCacheHint.NONE;
final var ingredientToken = findIngredient(ingredientProvider, ingredient, lockedInput, scopedIngredientTokens, cacheHint);
Expand Down

0 comments on commit 2aa5154

Please sign in to comment.