Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace slicer recipe cache with a slot aware recipe cache #461

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public MachineInventoryLayout getInventoryLayout() {
}

private boolean isValidInput(int index, ItemStack stack) {
return RecipeCaches.SLICING.hasRecipe(List.of(stack));
return SlicerRecipeManager.isSlicerValid(stack, index);
}

private boolean validAxe(int slot, ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.enderio.machines.common.blockentity;

import com.enderio.machines.common.init.MachineRecipes;
import com.enderio.machines.common.recipe.SlicingRecipe;
import net.minecraft.Util;
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.RecipeManager;
import net.minecraftforge.client.event.RecipesUpdatedEvent;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.util.thread.EffectiveSide;
import net.minecraftforge.server.ServerLifecycleHooks;

import java.util.*;

@Mod.EventBusSubscriber
public class SlicerRecipeManager {
private static final List<Set<Item>> items = Util.make(() -> {
List<Set<Item>> tempList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
tempList.add(new HashSet<>());
}
return tempList;
});
private static final List<Set<Ingredient>> nonoptimizableingredients = Util.make(() -> {
List<Set<Ingredient>> tempList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
tempList.add(new HashSet<>());
}
return tempList;
});

private static boolean clearCache = false;

public static boolean isSlicerValid(ItemStack stack, int slot) {
checkCacheRebuild();
if (items.get(slot).contains(stack.getItem()))
return true;
for (Ingredient ingredient : nonoptimizableingredients.get(slot)) {
if (ingredient.test(stack))
return true;
}
return false;
}

@SubscribeEvent
public static void registerReloadListener(AddReloadListenerEvent event) {
//Fired on datapack reload
clearCache = true;
}
@SubscribeEvent
public static void onRecipesUpdated(RecipesUpdatedEvent event) {
rebuildCache(event.getRecipeManager());
}

private static void checkCacheRebuild() {
if (clearCache && EffectiveSide.get().isServer()) {
rebuildCache(ServerLifecycleHooks.getCurrentServer().getRecipeManager());
clearCache = false;
}
}

private static void rebuildCache(RecipeManager manager) {

// Wipe the lookup table
for (Set<Item> item : items) {
item.clear();
}
for (Set<Ingredient> nonoptimizableingredient : nonoptimizableingredients) {
nonoptimizableingredient.clear();
}

for (SlicingRecipe slicingRecipe : manager.getAllRecipesFor(MachineRecipes.SLICING.type().get())) {
for (int i = 0; i < 6; i++) {
Ingredient ingredient = slicingRecipe.getInputs().get(i);
if (ingredient.isSimple()) {
Set<Item> itemset = items.get(i);
Arrays.stream(ingredient.getItems()).map(ItemStack::getItem).forEach(itemset::add);
} else {
nonoptimizableingredients.get(i).add(ingredient);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.enderio.machines.common.recipe;

import com.enderio.machines.common.init.MachineRecipes;
import com.enderio.machines.common.integrations.vanilla.VanillaAlloySmeltingRecipe;
import com.enderio.machines.common.utility.RecipeInputCache;
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraftforge.client.event.RecipesUpdatedEvent;
Expand All @@ -14,8 +11,6 @@
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.items.wrapper.RecipeWrapper;

import java.util.List;
import java.util.stream.Collectors;

@Mod.EventBusSubscriber
public class RecipeCaches {
Expand All @@ -31,9 +26,6 @@ public class RecipeCaches {
public static final RecipeInputCache<SagMillingRecipe.Container, SagMillingRecipe> SAGMILLING
= new RecipeInputCache<>(MachineRecipes.SAGMILLING.type());

public static final RecipeInputCache<Container, SlicingRecipe> SLICING
= new RecipeInputCache<>(MachineRecipes.SLICING.type());

public static final RecipeInputCache<SoulBindingRecipe.Container, SoulBindingRecipe> SOUL_BINDING
= new RecipeInputCache<>(MachineRecipes.SOUL_BINDING.type());

Expand All @@ -43,7 +35,6 @@ public static void registerReloadListener(AddReloadListenerEvent event) {
SMELTING.markCacheDirty();
PAINTING.markCacheDirty();
SAGMILLING.markCacheDirty();
SLICING.markCacheDirty();
SOUL_BINDING.markCacheDirty();
}

Expand All @@ -53,7 +44,6 @@ public static void onRecipesUpdated(RecipesUpdatedEvent event) {
SMELTING.rebuildCache(event.getRecipeManager());
PAINTING.rebuildCache(event.getRecipeManager());
SAGMILLING.rebuildCache(event.getRecipeManager());
SLICING.rebuildCache(event.getRecipeManager());
SOUL_BINDING.rebuildCache(event.getRecipeManager());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.enderio.machines.common.io.item.MachineInventory;
import com.enderio.machines.common.io.item.MultiSlotAccess;
import com.enderio.machines.common.recipe.MachineRecipe;
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -69,7 +68,7 @@ public boolean hasRecipe(List<ItemStack> inputs) {
.collect(Collectors.toSet());
}

if (possibleMatches.size() == 0) {
if (possibleMatches.isEmpty()) {
return false;
}

Expand Down Expand Up @@ -119,9 +118,7 @@ private void checkCacheRebuild() {
public void rebuildCache(RecipeManager recipeManager) {
itemToRecipesCache.clear();
recipeToIngredientCache.clear();

var recipeType = this.recipeType.get();
recipeManager.getAllRecipesFor(recipeType)
recipeManager.getAllRecipesFor(recipeType.get())
.forEach(recipe -> {
var items = recipe.getIngredients().stream()
.flatMap(ingredient -> Arrays.stream(ingredient.getItems()))
Expand Down