Skip to content

Commit

Permalink
Restore recipes in infobooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 25, 2024
1 parent b4e7b53 commit 1776f0b
Show file tree
Hide file tree
Showing 20 changed files with 510 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.minecraft.client.Minecraft;
import com.google.common.collect.Lists;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
import net.minecraft.world.level.Level;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand All @@ -40,58 +42,49 @@ public CraftingHelpersCommon(IModHelpers modHelpers) {
this.modHelpers = modHelpers;
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findRecipes(Level world, RecipeType<? extends T> recipeType) {
return world.isClientSide() ? getClientRecipes(recipeType) : findServerRecipes((ServerLevel) world, recipeType);
}

@Override
public RecipeManager getRecipeManager() {
return modHelpers.getMinecraftHelpers().isClientSide()
? (RecipeManager) Minecraft.getInstance().getConnection().recipes()
: Objects.requireNonNull(modHelpers.getMinecraftHelpers().getCurrentServer().getLevel(Level.OVERWORLD), "Server is still loading").recipeAccess();
return Objects.requireNonNull(modHelpers.getMinecraftHelpers().getCurrentServer().getLevel(Level.OVERWORLD), "Server is still loading").recipeAccess();
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> getServerRecipe(RecipeType<T> recipeType, ResourceKey<Recipe<?>> recipeName) {
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> getRecipe(RecipeType<T> recipeType, ResourceKey<Recipe<?>> recipeName) {
return Optional.ofNullable(getRecipeManager().byKeyTyped(recipeType, recipeName));
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> findServerRecipe(RecipeType<T> recipeType, C container, Level world) {
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> findRecipe(RecipeType<T> recipeType, C container, Level world) {
return ((RecipeManager) world.recipeAccess()).getRecipeFor(recipeType, container, world);
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findServerRecipes(RecipeType<? extends T> recipeType) {
return findServerRecipes(Objects.requireNonNull(modHelpers.getMinecraftHelpers().getCurrentServer().getLevel(Level.OVERWORLD)), recipeType);
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findRecipes(RecipeType<? extends T> recipeType) {
return findRecipes(Objects.requireNonNull(modHelpers.getMinecraftHelpers().getCurrentServer().getLevel(Level.OVERWORLD)), recipeType);
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findServerRecipes(ServerLevel world, RecipeType<? extends T> recipeType) {
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findRecipes(ServerLevel world, RecipeType<? extends T> recipeType) {
return (List<RecipeHolder<T>>) (List) world.recipeAccess().recipes.byType(recipeType);
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> getClientRecipe(RecipeType<T> recipeType, ResourceKey<Recipe<?>> recipeName) {
return Optional.ofNullable(getRecipeManager().byKeyTyped(recipeType, recipeName));
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> getClientRecipes(RecipeType<? extends T> recipeType) {
return (List<RecipeHolder<T>>) (List) getRecipeManager().recipes.byType(recipeType);
public List<RecipeDisplayEntry> getRecipeDisplays(RecipeType<?> recipeType, ResourceKey<Recipe<?>> recipeName) {
List<RecipeDisplayEntry> displays = Lists.newArrayList();
modHelpers.getMinecraftHelpers().getCurrentServer().overworld().recipeAccess().listDisplaysForRecipe(recipeName, displays::add);
return displays;
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> RecipeHolder<T> findClientRecipe(RegistryAccess registryAccess, ItemStack itemStack, RecipeType<T> recipeType, C recipeInput, int index) throws IllegalArgumentException {
int indexAttempt = index;
for(RecipeHolder<T> recipe : getClientRecipes(recipeType)) {
if(ItemStack.isSameItemSameComponents(recipe.value().assemble(recipeInput, registryAccess), itemStack) && indexAttempt-- == 0) {
return recipe;
public List<Pair<ResourceLocation, RecipeDisplayEntry>> getRecipeDisplays(RecipeType<?> recipeType, String recipeNameRegex) {
List<Pair<ResourceLocation, RecipeDisplayEntry>> displays = Lists.newArrayList();
for (Map.Entry<ResourceKey<Recipe<?>>, List<RecipeManager.ServerDisplayInfo>> entry : modHelpers.getMinecraftHelpers().getCurrentServer().overworld().recipeAccess().recipeToDisplay.entrySet()) {
if (recipeNameRegex.isEmpty() || entry.getKey().location().toString().matches(recipeNameRegex)) {
for (RecipeManager.ServerDisplayInfo display : entry.getValue()) {
displays.add(Pair.of(display.parent().id().location(), display.display()));
}
}
}
throw new IllegalArgumentException("Could not find recipe for " + itemStack + "::"
+ itemStack.getComponents() + " with index " + index);
return displays;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.cyclops.cyclopscore.helper;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
import net.minecraft.world.level.Level;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;
import java.util.Optional;
Expand All @@ -15,23 +16,19 @@
*/
public interface ICraftingHelpers {

public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findRecipes(Level world, RecipeType<? extends T> recipeType);

public RecipeManager getRecipeManager();

public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> getServerRecipe(RecipeType<T> recipeType, ResourceKey<Recipe<?>> recipeName);

public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> findServerRecipe(RecipeType<T> recipeType, C container, Level world);
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> getRecipe(RecipeType<T> recipeType, ResourceKey<Recipe<?>> recipeName);

public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findServerRecipes(RecipeType<? extends T> recipeType);
public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> findRecipe(RecipeType<T> recipeType, C container, Level world);

public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findServerRecipes(ServerLevel world, RecipeType<? extends T> recipeType);
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findRecipes(RecipeType<? extends T> recipeType);

public <C extends RecipeInput, T extends Recipe<C>> Optional<RecipeHolder<T>> getClientRecipe(RecipeType<T> recipeType, ResourceKey<Recipe<?>> recipeName);
public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> findRecipes(ServerLevel world, RecipeType<? extends T> recipeType);

public <C extends RecipeInput, T extends Recipe<C>> List<RecipeHolder<T>> getClientRecipes(RecipeType<? extends T> recipeType);
public List<RecipeDisplayEntry> getRecipeDisplays(RecipeType<?> recipeType, ResourceKey<Recipe<?>> recipeName);

public <C extends RecipeInput, T extends Recipe<C>> RecipeHolder<T> findClientRecipe(RegistryAccess registryAccess, ItemStack itemStack, RecipeType<T> recipeType, C recipeInput, int index) throws IllegalArgumentException;
public List<Pair<ResourceLocation, RecipeDisplayEntry>> getRecipeDisplays(RecipeType<?> recipeType, String recipeNameRegex);

/**
* A cache-based variant of {@link net.minecraft.world.item.crafting.RecipeManager#getRecipeFor(RecipeType, RecipeInput, Level)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ public net.minecraft.world.item.crafting.RecipeManager byKeyTyped(Lnet/minecraft
public net.minecraft.world.item.crafting.RecipeManager m_320711_(Lnet/minecraft/world/item/crafting/RecipeType;Lnet/minecraft/resources/ResourceKey;)Lnet/minecraft/world/item/crafting/RecipeHolder; # byKeyTyped
public net.minecraft.world.item.crafting.RecipeManager recipes
public net.minecraft.world.item.crafting.RecipeManager f_346395_ # recipes
public net.minecraft.world.item.crafting.RecipeManager recipeToDisplay
public net.minecraft.world.item.crafting.RecipeManager f_346716_ # recipeToDisplay

# Keybindings
public net.minecraft.client.KeyMapping ALL
Expand Down
1 change: 1 addition & 0 deletions loader-common/src/main/resources/cyclopscore.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ accessWidener v1 named
# Recipes
accessible method net/minecraft/world/item/crafting/RecipeManager byKeyTyped (Lnet/minecraft/world/item/crafting/RecipeType;Lnet/minecraft/resources/ResourceKey;)Lnet/minecraft/world/item/crafting/RecipeHolder;
accessible field net/minecraft/world/item/crafting/RecipeManager recipes Lnet/minecraft/world/item/crafting/RecipeMap;
accessible field net/minecraft/world/item/crafting/RecipeManager recipeToDisplay Ljava/util/Map;

# Screens
accessible method net/minecraft/client/gui/screens/Screen addRenderableWidget (Lnet/minecraft/client/gui/components/events/GuiEventListener;)Lnet/minecraft/client/gui/components/events/GuiEventListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<paragraph>Hi!</paragraph>
<paragraph>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam egestas nisl vel lorem rhoncus consectetur. Donec ut semper arcu. Etiam suscipit, ex ac varius accumsan, dolor nisi consectetur mauris, pellentesque viverra erat dolor eu turpis. Aliquam dictum, risus aliquet congue scelerisque, nunc elit ullamcorper ante, sit amet vehicula purus purus et nulla. Donec risus neque, ultrices at metus sed, pellentesque imperdiet mi. Etiam imperdiet lectus fermentum, consequat mi et, eleifend elit. Duis felis diam, efficitur id diam et, lacinia elementum libero. Maecenas rutrum nisl tellus, a condimentum elit facilisis sed. </paragraph>
<paragraph>Fusce massa ipsum, pharetra vel dui a, molestie congue nulla. Etiam maximus vehicula erat maximus tempor. Cras non commodo nisi. Fusce elementum fringilla lacus et rutrum. Proin viverra et augue sit amet malesuada. Phasellus justo sem, pretium nec sem id, molestie scelerisque massa. Aenean id mauris a ligula finibus faucibus. In varius, felis molestie luctus imperdiet, nisi risus elementum lacus, sed aliquam nisi neque vel felis. Morbi et massa neque. Vivamus imperdiet commodo imperdiet. Vivamus id est a diam dapibus convallis volutpat sit amet mauris. Fusce gravida quam et metus lacinia accumsan. Aliquam a consectetur ligula. Pellentesque ut lobortis turpis, nec vulputate lacus. </paragraph>
<appendix_list type="minecraft:crafting">minecraft:acacia_.*</appendix_list>
</section>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.cyclops.cyclopscore.modcompat.curios.ModCompatCurios;
import org.cyclops.cyclopscore.network.PacketCodecsNeoForge;
import org.cyclops.cyclopscore.persist.nbt.NBTClassTypesNeoForge;
import org.cyclops.cyclopscore.proxy.ClientProxy;
import org.cyclops.cyclopscore.proxy.CommonProxy;
import org.cyclops.cyclopscore.proxy.ClientProxyNeoForge;
import org.cyclops.cyclopscore.proxy.CommonProxyNeoForge;
import org.cyclops.cyclopscore.proxy.IClientProxy;
import org.cyclops.cyclopscore.proxy.ICommonProxy;
import org.cyclops.cyclopscore.tracking.ImportantUsers;
Expand Down Expand Up @@ -78,13 +78,13 @@ public CyclopsCoreNeoForge(IEventBus modEventBus) {
@Override
@OnlyIn(Dist.CLIENT)
protected IClientProxy constructClientProxy() {
return new ClientProxy();
return new ClientProxyNeoForge();
}

@Override
@OnlyIn(Dist.DEDICATED_SERVER)
protected ICommonProxy constructCommonProxy() {
return new CommonProxy();
return new CommonProxyNeoForge();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.cyclops.cyclopscore.helper;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
import org.apache.commons.lang3.tuple.Pair;
import org.cyclops.cyclopscore.CyclopsCoreNeoForge;
import org.cyclops.cyclopscore.infobook.IInfoBookRegistry;
import org.cyclops.cyclopscore.network.packet.RequestRecipeDisplayPacket;
import org.cyclops.cyclopscore.network.packet.RequestRecipeDisplaysRegexPacket;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author rubensworks
*/
public class RecipeHelpers {

public static final Map<Pair<RecipeType<?>, ResourceLocation>, RecipeDisplayEntry> CLIENT_RECIPE_DISPLAYS = Maps.newHashMap();
private static final Set<Pair<RecipeType<?>, ResourceLocation>> CLIENT_RECIPE_DISPLAYS_REQUESTING = Sets.newHashSet();
private static final Set<Pair<RecipeType<?>, String>> CLIENT_RECIPE_DISPLAYS_REGEX_REQUESTING = Sets.newHashSet();
private static final Set<Pair<RecipeType<?>, String>> CLIENT_RECIPE_DISPLAYS_REGEX_DONE = Sets.newHashSet();

public static void reset() {
CLIENT_RECIPE_DISPLAYS.clear();
CLIENT_RECIPE_DISPLAYS_REQUESTING.clear();
CLIENT_RECIPE_DISPLAYS_REGEX_REQUESTING.clear();
CLIENT_RECIPE_DISPLAYS_REGEX_DONE.clear();
}

public static void requestRecipeDisplay(RecipeType<?> recipeType, ResourceLocation recipe) {
Pair<RecipeType<?>, ResourceLocation> key = Pair.of(recipeType, recipe);
if (!CLIENT_RECIPE_DISPLAYS.containsKey(key) && !CLIENT_RECIPE_DISPLAYS_REQUESTING.contains(key)) {
CLIENT_RECIPE_DISPLAYS_REQUESTING.add(key);
CyclopsCoreNeoForge._instance.getPacketHandler().sendToServer(new RequestRecipeDisplayPacket(recipeType, recipe));
}
}

public static void requestRecipeDisplays(RecipeType<?> recipeType, String recipeRegex) {
Pair<RecipeType<?>, String> key = Pair.of(recipeType, recipeRegex);
if (!CLIENT_RECIPE_DISPLAYS_REGEX_REQUESTING.contains(key)) {
CLIENT_RECIPE_DISPLAYS_REGEX_REQUESTING.add(key);
CyclopsCoreNeoForge._instance.getPacketHandler().sendToServer(new RequestRecipeDisplaysRegexPacket(recipeType, recipeRegex));
}
}

public static void setRecipeDisplay(RecipeType<?> recipeType, ResourceLocation recipe, RecipeDisplayEntry recipeDisplay) {
CLIENT_RECIPE_DISPLAYS.put(Pair.of(recipeType, recipe), recipeDisplay);
}

@Nullable
public static RecipeDisplayEntry getRecipeDisplay(RecipeType<?> recipeType, ResourceLocation recipe) {
if (!IModHelpers.get().getMinecraftHelpers().isClientSide()) {
return null;
}

requestRecipeDisplay(recipeType, recipe);

Pair<RecipeType<?>, ResourceLocation> key = Pair.of(recipeType, recipe);
return CLIENT_RECIPE_DISPLAYS.get(key);
}

public static List<RecipeDisplayEntry> getRecipeDisplays(RecipeType<?> recipeType, String recipeRegex) {
if (!IModHelpers.get().getMinecraftHelpers().isClientSide()) {
return Lists.newArrayList();
}

requestRecipeDisplays(recipeType, recipeRegex);

List<RecipeDisplayEntry> displays = Lists.newArrayList();
for (Map.Entry<Pair<RecipeType<?>, ResourceLocation>, RecipeDisplayEntry> entry : CLIENT_RECIPE_DISPLAYS.entrySet()) {
if (entry.getKey().getLeft() == recipeType && (recipeRegex.isEmpty() || entry.getKey().getRight().toString().matches(recipeRegex))) {
displays.add(entry.getValue());
}
}
return displays;
}

public static void setRecipeDisplaysRegexDone(RecipeType<?> value, String recipe) {
CLIENT_RECIPE_DISPLAYS_REGEX_DONE.add(Pair.of(value, recipe));

// Re-initialize all books once all regex requests are done
if (!RecipeHelpers.areRecipeDisplayRegexRequestsPending()) {
CyclopsCoreNeoForge._instance.getRegistryManager().getRegistry(IInfoBookRegistry.class).initializeAllBooks();
}
}

public static boolean areRecipeDisplayRegexRequestsPending() {
return CLIENT_RECIPE_DISPLAYS_REGEX_REQUESTING.size() > CLIENT_RECIPE_DISPLAYS_REGEX_DONE.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface IInfoBookRegistry extends IRegistry {
*/
public InfoSection getRoot(IInfoBook infoBook);

public void initializeAllBooks();

}
Loading

0 comments on commit 1776f0b

Please sign in to comment.