Skip to content

Commit

Permalink
Ported the mod to 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kir-Antipov committed Dec 21, 2023
1 parent 1a89a1f commit 2c419d6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
minecraft_version=1.20
yarn_mappings=1.20+build.1
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.4
loader_version=0.15.3

# Mod Properties
Expand All @@ -12,4 +12,4 @@ maven_group=dev.kir
archives_base_name=smart-recipes

# Dependencies
fabric_version=0.83.0+1.20
fabric_version=0.89.0+1.20.2
21 changes: 13 additions & 8 deletions src/main/java/dev/kir/smartrecipes/api/RecipeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.Registries;
Expand All @@ -14,7 +15,7 @@
public class RecipeInfo {
private final Identifier recipeId;
private final JsonObject recipeObject;
private Recipe<?> recipe;
private RecipeEntry<?> recipeEntry;
private RecipeType<?> recipeType;

public RecipeInfo(Identifier recipeId, JsonObject recipeObject) {
Expand All @@ -31,8 +32,8 @@ public JsonObject getRecipeAsJson() {
}

public Optional<RecipeType<?>> getRecipeType() {
if (this.recipe != null) {
return Optional.of(this.recipe.getType());
if (this.recipeEntry != null) {
return Optional.of(this.recipeEntry.value().getType());
}

if (this.recipeType == null && this.recipeObject != null) {
Expand All @@ -45,15 +46,19 @@ public Optional<RecipeType<?>> getRecipeType() {
return Optional.ofNullable(this.recipeType);
}

public Optional<Recipe<?>> getRecipe() {
if (this.recipe == null && this.recipeId != null && this.recipeObject != null) {
public Optional<RecipeEntry<?>> getRecipeEntry() {
if (this.recipeEntry == null && this.recipeId != null && this.recipeObject != null) {
try {
this.recipe = RecipeManager.deserialize(this.recipeId, this.recipeObject);
this.recipeEntry = RecipeManager.deserialize(this.recipeId, this.recipeObject);
} catch (Throwable e) {
this.recipe = null;
this.recipeEntry = null;
}
}
return Optional.ofNullable(this.recipe);
return Optional.ofNullable(this.recipeEntry);
}

public Optional<Recipe<?>> getRecipe() {
return this.getRecipeEntry().map(RecipeEntry::value);
}

@Contract(value = "_, _ -> new", pure = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.*;
import net.minecraft.recipe.book.RecipeBook;
import net.minecraft.registry.Registries;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -60,16 +57,16 @@ public void send(Stream<ServerPlayerEntity> players) {
}

@SuppressWarnings("unchecked")
private static <T extends Recipe<?>> void writeRecipeEntry(PacketByteBuf buf, Pair<ReloadableRecipeManager.RecipeState, RecipeInfo> recipeEntry) {
private static <T extends RecipeEntry<U>, U extends Recipe<?>> void writeRecipeEntry(PacketByteBuf buf, Pair<ReloadableRecipeManager.RecipeState, RecipeInfo> recipeEntry) {
ReloadableRecipeManager.RecipeState state = recipeEntry.getLeft();
RecipeInfo recipeInfo = recipeEntry.getRight();
if (state == ReloadableRecipeManager.RecipeState.KEEP) {
T recipe = (T)recipeInfo.getRecipe().orElseThrow(() -> new IllegalArgumentException("Unable to parse recipe '" + recipeInfo.getRecipeId() + "'"));
T recipe = (T)recipeInfo.getRecipeEntry().orElseThrow(() -> new IllegalArgumentException("Unable to parse recipe '" + recipeInfo.getRecipeId() + "'"));

buf.writeBoolean(true);
buf.writeIdentifier(Registries.RECIPE_SERIALIZER.getId(recipe.getSerializer()));
buf.writeIdentifier(recipe.getId());
((RecipeSerializer<T>)recipe.getSerializer()).write(buf, recipe);
buf.writeIdentifier(Registries.RECIPE_SERIALIZER.getId(recipe.value().getSerializer()));
buf.writeIdentifier(recipe.id());
((RecipeSerializer<U>)recipe.value().getSerializer()).write(buf, recipe.value());
} else {
buf.writeBoolean(false);
buf.writeIdentifier(recipeInfo.getRecipeId());
Expand All @@ -82,7 +79,7 @@ private static Pair<ReloadableRecipeManager.RecipeState, RecipeInfo> readRecipeE
Identifier serializerId = buf.readIdentifier();
RecipeSerializer<?> serializer = Registries.RECIPE_SERIALIZER.getOrEmpty(serializerId).orElseThrow(() -> new IllegalArgumentException("Unknown recipe serializer " + serializerId));
Identifier recipeId = buf.readIdentifier();
Recipe<?> recipe = serializer.read(recipeId, buf);
Recipe<?> recipe = serializer.read(buf);
return new Pair<>(ReloadableRecipeManager.RecipeState.KEEP, new SerializableRecipeInfo(recipeId, recipe));
} else {
Identifier recipeId = buf.readIdentifier();
Expand All @@ -108,6 +105,11 @@ public SerializableRecipeInfo(Identifier recipeId, Recipe<?> recipe) {
this.recipeType = null;
}

@Override
public Optional<RecipeEntry<?>> getRecipeEntry() {
return this.getRecipe().map(recipe -> new RecipeEntry<>(this.getRecipeId(), recipe));
}

@Override
public Optional<Recipe<?>> getRecipe() {
return Optional.ofNullable(this.recipe);
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/dev/kir/smartrecipes/mixin/RecipeManagerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import dev.kir.smartrecipes.api.*;
import dev.kir.smartrecipes.util.JsonUtil;
import dev.kir.smartrecipes.SmartRecipes;
import dev.kir.smartrecipes.api.*;
import dev.kir.smartrecipes.api.networking.SynchronizeReloadedRecipesPacket;
import dev.kir.smartrecipes.util.JsonUtil;
import dev.kir.smartrecipes.util.recipe.RecipeBookUtil;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.recipe.RecipeType;
import net.minecraft.resource.ResourceManager;
Expand Down Expand Up @@ -41,7 +41,7 @@ class RecipeManagerMixin implements ReloadableRecipeManager {
private static final Identifier OBSOLETE_RELOAD_CONDITIONS = new Identifier("reload_conditions");

@Shadow
private Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipes;
private Map<RecipeType<?>, Map<Identifier, RecipeEntry<?>>> recipes;

@Unique
private Map<Identifier, Collection<Map.Entry<Identifier, JsonElement>>> waitingForReload;
Expand Down Expand Up @@ -157,7 +157,7 @@ public void apply(Collection<Pair<RecipeState, RecipeInfo>> diff) {
return;
}

Map<RecipeType<?>, Map<Identifier, Recipe<?>>> mutableRecipes = makeMutable(this.recipes);
Map<RecipeType<?>, Map<Identifier, RecipeEntry<?>>> mutableRecipes = makeMutable(this.recipes);
for (Pair<RecipeState, RecipeInfo> entry : diff) {
RecipeState recipeState = entry.getLeft();
RecipeInfo recipeInfo = entry.getRight();
Expand All @@ -166,11 +166,11 @@ public void apply(Collection<Pair<RecipeState, RecipeInfo>> diff) {
RecipeType<?> recipeType = recipeInfo.getRecipeType().orElseThrow(() -> new IllegalArgumentException("Recipe '" + recipeId + "' uses invalid or unsupported recipe type"));
switch (recipeState) {
case KEEP -> {
Map<Identifier, Recipe<?>> container = mutableRecipes.computeIfAbsent(recipeType, x -> new HashMap<>());
container.put(recipeId, recipeInfo.getRecipe().orElseThrow(() -> new IllegalArgumentException("Unable to parse recipe '" + recipeId + "'")));
Map<Identifier, RecipeEntry<?>> container = mutableRecipes.computeIfAbsent(recipeType, x -> new HashMap<>());
container.put(recipeId, recipeInfo.getRecipeEntry().orElseThrow(() -> new IllegalArgumentException("Unable to parse recipe '" + recipeId + "'")));
}
case REMOVE -> {
Map<Identifier, Recipe<?>> container = mutableRecipes.get(recipeType);
Map<Identifier, RecipeEntry<?>> container = mutableRecipes.get(recipeType);
if (container != null) {
container.remove(recipeId);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ private static void logParsingError(Identifier recipeId, Throwable error) {
}

@Unique
private static Map<RecipeType<?>, Map<Identifier, Recipe<?>>> makeMutable(Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipes) {
private static Map<RecipeType<?>, Map<Identifier, RecipeEntry<?>>> makeMutable(Map<RecipeType<?>, Map<Identifier, RecipeEntry<?>>> recipes) {
if (recipes instanceof HashMap<?, ?>) {
return recipes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.minecraft.client.recipebook.ClientRecipeBook;
import net.minecraft.client.search.SearchManager;
import net.minecraft.client.toast.RecipeToast;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.book.RecipeBook;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.server.network.ServerRecipeBook;
Expand All @@ -37,8 +37,8 @@ public static void apply(RecipeBook recipeBook, DynamicRegistryManager registryM
ReloadableRecipeManager.RecipeState recipeState = entry.getLeft();
RecipeInfo recipeInfo = entry.getRight();
Identifier recipeId = recipeInfo.getRecipeId();
if (recipeState == ReloadableRecipeManager.RecipeState.KEEP && recipeInfo.getRecipe().isPresent()) {
Recipe<?> recipe = recipeInfo.getRecipe().get();
if (recipeState == ReloadableRecipeManager.RecipeState.KEEP && recipeInfo.getRecipeEntry().isPresent()) {
RecipeEntry<?> recipe = recipeInfo.getRecipeEntry().get();
if (showRecipeToasts && isClient && !(recipeBook instanceof ServerRecipeBook) && !recipeBook.contains(recipeId)) {
recipeBook.display(recipe);
showRecipeToast(recipe);
Expand Down Expand Up @@ -76,7 +76,7 @@ private static void refreshRecipeBook(RecipeBook recipeBook, DynamicRegistryMana
}

@Environment(EnvType.CLIENT)
private static void showRecipeToast(Recipe<?> recipe) {
private static void showRecipeToast(RecipeEntry<?> recipe) {
RecipeToast.show(MinecraftClient.getInstance().getToastManager(), recipe);
}
}
5 changes: 3 additions & 2 deletions src/main/resources/smart-recipes.accesswidener
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
accessWidener v1 named
accessWidener v2 named

accessible method net/minecraft/recipe/book/RecipeBook remove (Lnet/minecraft/util/Identifier;)V
accessible method net/minecraft/recipe/book/RecipeBook remove (Lnet/minecraft/util/Identifier;)V
accessible method net/minecraft/recipe/RecipeManager deserialize (Lnet/minecraft/util/Identifier;Lcom/google/gson/JsonObject;)Lnet/minecraft/recipe/RecipeEntry;

0 comments on commit 2c419d6

Please sign in to comment.