Skip to content

Commit

Permalink
fix: crashes in inventory screens with recipe book
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed Jan 22, 2024
1 parent 1d32a14 commit 8ba0aae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
package io.pyke.vitri.finorza.inference.mixin;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.*;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.client.ClientRecipeBook;
import net.minecraft.client.RecipeBookCategories;
import net.minecraft.world.item.crafting.Recipe;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(ClientRecipeBook.class)
public class ClientRecipeBookMixin {
@Inject(
method = "categorizeAndGroupRecipes(Ljava/lang/Iterable;)Ljava/util/Map;",
at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Ljava/util/ArrayList;add(Ljava/lang/Object;)Z"),
locals = LocalCapture.CAPTURE_FAILHARD
)
private static void injectCategorize(
Iterable<Recipe<?>> iterable,
CallbackInfoReturnable<Map<RecipeBookCategories, List<List<Recipe<?>>>>> cir,
ArrayList<Recipe<?>> list
) {
list.sort(Comparator.comparing(r -> r.getResultItem().toString()));
}

@Inject(method = "categorizeAndGroupRecipes(Ljava/lang/Iterable;)Ljava/util/Map;", at = @At("RETURN"), cancellable = true)
private static void injectSorted(CallbackInfoReturnable<Map<RecipeBookCategories, List<List<Recipe<?>>>>> cir) {
final Map<RecipeBookCategories, List<List<Recipe<?>>>> map = cir.getReturnValue();
/**
* @author
* @reason
*/
@Overwrite
private static Map<RecipeBookCategories, List<List<Recipe<?>>>> categorizeAndGroupRecipes(Iterable<Recipe<?>> iterable) {
HashMap<RecipeBookCategories, List<List<Recipe<?>>>> map = Maps.newHashMap();
HashBasedTable table = HashBasedTable.create();
for (Recipe<?> recipe : iterable) {
if (recipe.isSpecial()) continue;
RecipeBookCategories recipeBookCategories2 = ClientRecipeBook.getCategory(recipe);
String string = recipe.getGroup();
if (string.isEmpty()) {
map.computeIfAbsent(recipeBookCategories2, recipeBookCategories -> Lists.newArrayList()).add(
ImmutableList.of(recipe));
continue;
}
ArrayList<Recipe<?>> list = (ArrayList<Recipe<?>>)table.get((Object)recipeBookCategories2, string);
if (list == null) {
list = Lists.newArrayList();
table.put(recipeBookCategories2, string, list);
map.computeIfAbsent(recipeBookCategories2, recipeBookCategories -> Lists.newArrayList()).add(list);
}
list.add(recipe);
list.sort(Comparator.comparing(r -> r.getResultItem().toString()));
}
for (final List<List<Recipe<?>>> recipeList : map.values()) {
for (final List<Recipe<?>> subList : recipeList) {
if (subList.size() > 1) {
Expand All @@ -40,6 +48,6 @@ private static void injectSorted(CallbackInfoReturnable<Map<RecipeBookCategories
}
recipeList.sort(Comparator.comparing(r -> r.get(0).getResultItem().getItem().toString()));
}
cir.setReturnValue(map);
return map;
}
}
1 change: 1 addition & 0 deletions src/main/resources/finorza.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ accessWidener v2 named

accessible method net/minecraft/server/MinecraftServer haveTime ()Z
accessible method net/minecraft/server/MinecraftServer waitUntilNextTick ()V
accessible method net/minecraft/client/ClientRecipeBook getCategory (Lnet/minecraft/world/item/crafting/Recipe;)Lnet/minecraft/client/RecipeBookCategories;

accessible field net/minecraft/server/MinecraftServer isReady Z
accessible field net/minecraft/server/MinecraftServer mayHaveDelayedTasks Z

0 comments on commit 8ba0aae

Please sign in to comment.