Skip to content

Commit

Permalink
feat: give creative players the ability to see all Bio-Forge recipe r…
Browse files Browse the repository at this point in the history
…egardless of if they have unlocked them
  • Loading branch information
Elenterius committed Aug 28, 2023
1 parent bd4dcb4 commit 507954c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.elenterius.biomancy.init.client.ModRecipeBookCategories;
import com.github.elenterius.biomancy.inventory.menu.BioForgeMenu;
import com.github.elenterius.biomancy.inventory.menu.BioForgeTab;
import com.github.elenterius.biomancy.mixin.client.RecipeCollectionAccessor;
import com.github.elenterius.biomancy.network.ModNetworkHandler;
import com.github.elenterius.biomancy.recipe.BioForgeRecipe;
import com.google.common.collect.Lists;
Expand All @@ -17,16 +18,14 @@
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.searchtree.SearchRegistry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.stats.RecipeBook;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;

import javax.annotation.Nullable;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.*;

class BioForgeScreenController {

Expand Down Expand Up @@ -241,13 +240,41 @@ public void updateSearchString(String searchString) {
currentSearchString = searchString;
}

private static void canCraftRecipe(RecipeCollection recipeCollection, StackedContents handler, RecipeBook book, boolean isCreativePlayer) {
RecipeCollectionAccessor accessor = (RecipeCollectionAccessor) recipeCollection;
Set<Recipe<?>> fitDimensions = accessor.getFitDimensions();
Set<Recipe<?>> craftable = accessor.getCraftable();

for (Recipe<?> recipe : recipeCollection.getRecipes()) {
boolean isRecipeKnown = isCreativePlayer || book.contains(recipe);
boolean canCraftRecipe = recipe.canCraftInDimensions(0, 0) && isRecipeKnown;

if (canCraftRecipe) {
fitDimensions.add(recipe);
}
else {
fitDimensions.remove(recipe);
}

if (canCraftRecipe && handler.canCraft(recipe, null)) {
craftable.add(recipe);
}
else {
craftable.remove(recipe);
}
}
}

private void updateAndSearchRecipes() {
ClientRecipeBook recipeBook = getPlayer().getRecipeBook();
LocalPlayer player = getPlayer();
boolean isCreativePlayer = player.isCreative();

ClientRecipeBook recipeBook = player.getRecipeBook();
List<RecipeCollection> recipesForCategory = recipeBook.getCollection(ModRecipeBookCategories.getRecipeBookCategories(tabs.get(activeTab)));
recipesForCategory.forEach(recipeCollection -> recipeCollection.canCraft(itemCounter, 0, 0, recipeBook));
recipesForCategory.forEach(recipeCollection -> canCraftRecipe(recipeCollection, itemCounter, recipeBook, isCreativePlayer));

List<RecipeCollection> recipes = Lists.newArrayList(recipesForCategory);
recipes.removeIf(recipeCollection -> !recipeCollection.hasKnownRecipes());
if (!isCreativePlayer) recipes.removeIf(recipeCollection -> !recipeCollection.hasKnownRecipes());
recipes.removeIf(recipeCollection -> !recipeCollection.hasFitting());

if (!currentSearchString.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.elenterius.biomancy.mixin.client;

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.gen.Accessor;

import java.util.Set;

@Mixin(RecipeCollection.class)
public interface RecipeCollectionAccessor {
@Accessor("fitsDimensions")
Set<Recipe<?>> getFitDimensions();

@Accessor("craftable")
Set<Recipe<?>> getCraftable();
}
6 changes: 1 addition & 5 deletions src/main/resources/mixins.biomancy.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
"MobEntityAccessor", "PhantomMixin", "PlayerMixin", "SlimeAccessor", "SwordItemMixinAccessor", "TadpoleAccessor", "TextureSlotAccessor",
"ZombieVillagerMixinAccessor"
],
"client": [
"client.ClientPackListenerMixin",
"client.PlayerRendererMixin",
"client.ScreenMixin"
],
"client": [ "client.ClientPackListenerMixin", "client.PlayerRendererMixin", "client.RecipeCollectionAccessor", "client.ScreenMixin" ],
"server": [ ]
}

0 comments on commit 507954c

Please sign in to comment.