-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hopper counters command error when guessing item color (#1826)
* 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
Showing
2 changed files
with
16 additions
and
13 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
27 changes: 15 additions & 12 deletions
27
src/main/java/carpet/mixins/RecipeManager_scarpetMixin.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 |
---|---|---|
@@ -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(); | ||
} | ||
} |