Skip to content

Commit

Permalink
fix: add workaround for NERB incompatibility ("unlocks" all recipes w…
Browse files Browse the repository at this point in the history
…hen NERB is detected)
  • Loading branch information
Elenterius committed Oct 28, 2023
1 parent 0abd696 commit 9dd10ee
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ dependencies {
// implementation fg.deobf("se.mickelus.mutil:mutil:1.19.2-5.1.0")
compileOnly fg.deobf("curse.maven:tetra-289712:4738567") // 1.19.2-5.5.0

// runtimeOnly fg.deobf("maven.modrinth:nerb:ANmCMdMt") //NERB 0.3

//TODO: Morph Mod Integration?
// implementation fg.deobf("curse.maven:identity-391390:3807264") // https://www.curseforge.com/minecraft/mc-mods/identity
// runtimeOnly fg.deobf("curse.maven:architectury-419699:4040966")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.elenterius.biomancy.init.ModBioForgeTabs;
import com.github.elenterius.biomancy.init.ModRecipeBookTypes;
import com.github.elenterius.biomancy.init.client.ModRecipeBookCategories;
import com.github.elenterius.biomancy.integration.BioForgeCompat;
import com.github.elenterius.biomancy.menu.BioForgeMenu;
import com.github.elenterius.biomancy.menu.BioForgeTab;
import com.github.elenterius.biomancy.mixin.client.RecipeCollectionAccessor;
Expand Down Expand Up @@ -267,7 +268,7 @@ private static void canCraftRecipe(RecipeCollection recipeCollection, StackedCon

private void updateAndSearchRecipes() {
LocalPlayer player = getPlayer();
boolean isCreativePlayer = player.isCreative();
boolean isCreativePlayer = player.isCreative() || BioForgeCompat.isRecipeCollectionOverwriteEnabled();

ClientRecipeBook recipeBook = player.getRecipeBook();
List<RecipeCollection> recipesForCategory = recipeBook.getCollection(ModRecipeBookCategories.getRecipeBookCategories(tabs.get(activeTab)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.elenterius.biomancy.integration;

import net.minecraftforge.fml.ModList;

public final class BioForgeCompat {

private BioForgeCompat() {}

public static boolean isRecipeCollectionOverwriteEnabled() {
return isNerbLoaded();
}

private static boolean isNerbLoaded() {
return ModList.get().isLoaded("nerb");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.github.elenterius.biomancy.mixin.client;

import com.github.elenterius.biomancy.init.ModRecipes;
import com.github.elenterius.biomancy.integration.BioForgeCompat;
import com.google.common.collect.*;
import net.minecraft.client.ClientRecipeBook;
import net.minecraft.client.RecipeBookCategories;
import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
import net.minecraft.world.item.crafting.Recipe;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mixin(value = ClientRecipeBook.class, priority = 999)
public abstract class ClientRecipeBookMixin {

@Shadow
private static RecipeBookCategories getCategory(Recipe<?> pRecipe) {
return null;
}

@Shadow
private List<RecipeCollection> allCollections;

@Shadow
private Map<RecipeBookCategories, List<RecipeCollection>> collectionsByTab;

@Inject(method = "setupCollections", at = @At("HEAD"))
private void onSetupCollections(Iterable<Recipe<?>> recipes, CallbackInfo ci) {
if (BioForgeCompat.isRecipeCollectionOverwriteEnabled()) {
biomancy$overwriteRecipeCollections(recipes);
}
}

@Unique
private void biomancy$overwriteRecipeCollections(Iterable<Recipe<?>> allRecipes) {
Map<RecipeBookCategories, List<List<Recipe<?>>>> categorizedRecipes = biomancy$categorizeBioForgeRecipes(allRecipes);
Map<RecipeBookCategories, List<RecipeCollection>> recipeCategories = new HashMap<>(); //we can't use EnumMap because of Forge modifying the enum at runtime

ImmutableList.Builder<RecipeCollection> builder = ImmutableList.builder();

categorizedRecipes.forEach((category, groupedRecipes) -> recipeCategories.put(category, groupedRecipes.stream().map(recipes -> {
RecipeCollection collection = new RecipeCollection(recipes);
builder.add(collection);
return collection;
}).toList()));

RecipeBookCategories.AGGREGATE_CATEGORIES.forEach((mainCategory, subCategories) -> recipeCategories.put(mainCategory, subCategories.stream()
.flatMap(category -> recipeCategories.getOrDefault(category, List.of()).stream()).toList())
);

collectionsByTab = Map.copyOf(recipeCategories);
allCollections = builder.build();
}

@Unique
private static Map<RecipeBookCategories, List<List<Recipe<?>>>> biomancy$categorizeBioForgeRecipes(Iterable<Recipe<?>> recipes) {
Map<RecipeBookCategories, List<List<Recipe<?>>>> map = Maps.newHashMap();
Table<RecipeBookCategories, String, List<Recipe<?>>> table = HashBasedTable.create();

for (Recipe<?> recipe : recipes) {
if (recipe.getType() != ModRecipes.BIO_FORGING_RECIPE_TYPE.get() || recipe.isSpecial() || recipe.isIncomplete()) continue;

RecipeBookCategories category = getCategory(recipe);
String group = recipe.getGroup().isEmpty() ? recipe.getId().toString() : recipe.getGroup();
if (group.isEmpty()) {
map.computeIfAbsent(category, categories -> Lists.newArrayList()).add(List.of(recipe));
}
else {
List<Recipe<?>> list = table.get(category, group);
if (list == null) {
list = Lists.newArrayList();
table.put(category, group, list);
map.computeIfAbsent(category, categories -> Lists.newArrayList()).add(list);
}
list.add(recipe);
}
}

return map;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/mixins.biomancy.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"MobEffectInstanceMixin", "MobEntityAccessor", "PhantomMixin", "PlayerMixin", "ServerLevelMixin", "SlimeAccessor", "SwordItemMixinAccessor",
"TadpoleAccessor", "TextureSlotAccessor", "ZombieVillagerMixinAccessor"
],
"client": [ "client.ClientPackListenerMixin", "client.PlayerRendererMixin", "client.RecipeCollectionAccessor", "client.ScreenMixin" ],
"client": [
"client.ClientPackListenerMixin", "client.ClientRecipeBookMixin", "client.PlayerRendererMixin", "client.RecipeCollectionAccessor", "client.ScreenMixin"
],
"server": [ ]
}

0 comments on commit 9dd10ee

Please sign in to comment.