Skip to content

Commit

Permalink
Fix hopper counters command error when guessing item color (#1826)
Browse files Browse the repository at this point in the history
* Fix hopper counters command crash when guessing item color

* Less List types, slightly less copying and clearer Stream chain

* Reverse result equal logic to not search through registries as much

Gets the item being searched once outside instead of getting the id for
every recipe
  • Loading branch information
altrisi authored Oct 6, 2023
1 parent 453f5c0 commit 9039bba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/carpet/fakes/RecipeManagerInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface RecipeManagerInterface
* Gets all the recipes for a given item. Also used for {@link carpet.helpers.HopperCounter#guessColor} to guess the
* colour of an item to display it prettily
*/
List<Recipe<?>> getAllMatching(final RecipeType<?> type, final ResourceLocation output, final RegistryAccess registryAccess);
List<Recipe<?>> getAllMatching(final RecipeType<?> type, final ResourceLocation itemId, final RegistryAccess registryAccess);
}
27 changes: 15 additions & 12 deletions src/main/java/carpet/mixins/RecipeManager_scarpetMixin.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
package carpet.mixins;

import carpet.fakes.RecipeManagerInterface;
import com.google.common.collect.Lists;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Item;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.RecipeType;

@Mixin(RecipeManager.class)
public class RecipeManager_scarpetMixin implements RecipeManagerInterface
{

@Shadow private Map<RecipeType<?>, Map<ResourceLocation, Recipe<?>>> recipes;
@Shadow private Map<RecipeType<?>, Map<ResourceLocation, RecipeHolder<?>>> recipes;

@Override
public List<Recipe<?>> getAllMatching(RecipeType<?> type, ResourceLocation output, final RegistryAccess registryAccess)
public List<Recipe<?>> getAllMatching(RecipeType<?> type, ResourceLocation itemId, final RegistryAccess registryAccess)
{
Map<ResourceLocation, Recipe<?>> typeRecipes = recipes.get(type);
Map<ResourceLocation, RecipeHolder<?>> typeRecipes = recipes.get(type);
// happens when mods add recipe to the registry without updating recipe manager
if (typeRecipes == null) return Collections.emptyList();
if (typeRecipes.containsKey(output)) return Collections.singletonList(typeRecipes.get(output));
final Registry<Item> regs = registryAccess.registryOrThrow(Registries.ITEM);
return Lists.newArrayList(typeRecipes.values().stream().filter(
r -> regs.getKey(r.getResultItem(registryAccess).getItem()).equals(output)).collect(Collectors.toList()));
if (typeRecipes == null) return List.of();
RecipeHolder<?> recipeFromMap = typeRecipes.get(itemId);
if (recipeFromMap != null)
return List.of(recipeFromMap.value());
Registry<Item> regs = registryAccess.registryOrThrow(Registries.ITEM);
Item item = regs.get(itemId);
return typeRecipes.values()
.stream()
.<Recipe<?>>map(RecipeHolder::value)
.filter(r -> r.getResultItem(registryAccess).getItem() == item)
.toList();
}
}

0 comments on commit 9039bba

Please sign in to comment.