From 5bbd3ec9e82c07de1769f12c3edb654fca464c5f Mon Sep 17 00:00:00 2001 From: 1024_byteeeee <2270484921@qq.com> Date: Tue, 29 Oct 2024 07:48:50 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carpet/api/recipe/AmsRecipeManager.java | 44 ++-------- .../carpet/api/recipe/AmsRecipeManager.java | 86 +++++++++++++++++++ 2 files changed, 91 insertions(+), 39 deletions(-) create mode 100644 versions/1.21.2/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java diff --git a/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java b/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java index 96d9ac98..18de314a 100644 --- a/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java +++ b/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java @@ -25,20 +25,15 @@ import club.mcams.carpet.api.recipe.template.SmeltingRecipeTemplate; import com.google.gson.JsonElement; -//#if MC>=12102 -//$$ import club.mcams.carpet.AmsServer; -//$$ import com.google.gson.JsonParseException; -//$$ import net.minecraft.recipe.*; -//$$ import net.minecraft.registry.RegistryKey; -//$$ import net.minecraft.registry.RegistryKeys; -//$$ import net.minecraft.registry.RegistryWrapper; -//$$ import java.util.HashMap; -//#endif + import net.minecraft.util.Identifier; +import top.byteeeee.annotationtoolbox.annotation.GameVersion; + import java.util.List; import java.util.Map; +@GameVersion(version = "Minecraft < 1.21.2") public class AmsRecipeManager { private final List shapelessRecipes; private final List shapedRecipes; @@ -50,39 +45,10 @@ public AmsRecipeManager(AmsRecipeBuilder builder) { this.smeltingRecipes = builder.getSmeltingRecipeList(); } - public void registerRecipes( - //#if MC>=12102 - //$$ Map> map, RegistryWrapper.WrapperLookup wrapperLookup - //#else - Map recipeMap - //#endif - ) { - //#if MC>=12102 - //$$ Map recipeMap = new HashMap<>(); - //#endif + public void registerRecipes(Map recipeMap) { registerAllRecipes(recipeMap); - //#if MC>=12102 - //$$ recipeMap.forEach((id, json) -> deserializeRecipe(map, wrapperLookup, id, json)); - //#endif } - //#if MC>=12102 - //$$ private void deserializeRecipe(Map> map, RegistryWrapper.WrapperLookup wrapperLookup, Identifier id, JsonElement json) { - //$$ try { - //$$ RecipeEntry recipeEntry = ServerRecipeManager.deserialize( - //$$ RegistryKey.of(RegistryKeys.RECIPE, id), - //$$ json.getAsJsonObject(), - //$$ wrapperLookup - //$$ ); - //$$ map.put(id, recipeEntry.value()); - //$$ } catch (JsonParseException e) { - //$$ AmsServer.LOGGER.warn("Failed to parse recipe: {}, error: {}", id, e.getMessage()); - //$$ } catch (Exception e) { - //$$ AmsServer.LOGGER.error("Unexpected error during recipe deserialization: {}, error: {}", id, e); - //$$ } - //$$ } - //#endif - private void registerAllRecipes(Map recipeMap) { shapelessRecipes.forEach(recipe -> recipe.addToRecipeMap(recipeMap)); shapedRecipes.forEach(recipe -> recipe.addToRecipeMap(recipeMap)); diff --git a/versions/1.21.2/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java b/versions/1.21.2/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java new file mode 100644 index 00000000..16a7d048 --- /dev/null +++ b/versions/1.21.2/src/main/java/club/mcams/carpet/api/recipe/AmsRecipeManager.java @@ -0,0 +1,86 @@ +/* + * This file is part of the Carpet AMS Addition project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2024 A Minecraft Server and contributors + * + * Carpet AMS Addition is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Carpet AMS Addition is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Carpet AMS Addition. If not, see . + */ + +package club.mcams.carpet.api.recipe; + +import club.mcams.carpet.AmsServer; +import club.mcams.carpet.api.recipe.template.ShapedRecipeTemplate; +import club.mcams.carpet.api.recipe.template.ShapelessRecipeTemplate; +import club.mcams.carpet.api.recipe.template.SmeltingRecipeTemplate; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeEntry; +import net.minecraft.recipe.ServerRecipeManager; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.RegistryWrapper; +import net.minecraft.util.Identifier; + +import top.byteeeee.annotationtoolbox.annotation.GameVersion; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@GameVersion(version = "Minecraft >= 1.21.2") +public class AmsRecipeManager { + private final List shapelessRecipes; + private final List shapedRecipes; + private final List smeltingRecipes; + + public AmsRecipeManager(AmsRecipeBuilder builder) { + this.shapelessRecipes = builder.getShapelessRecipeList(); + this.shapedRecipes = builder.getShapedRecipeList(); + this.smeltingRecipes = builder.getSmeltingRecipeList(); + } + + public void registerRecipes(Map> map, RegistryWrapper.WrapperLookup wrapperLookup) { + Map recipeMap = new HashMap<>(); + registerAllRecipes(recipeMap); + recipeMap.forEach((id, json) -> deserializeRecipe(map, wrapperLookup, id, json)); + } + + @SuppressWarnings("DuplicatedCode") + private void deserializeRecipe(Map> map, RegistryWrapper.WrapperLookup wrapperLookup, Identifier id, JsonElement json) { + try { + RecipeEntry recipeEntry = ServerRecipeManager.deserialize(RegistryKey.of(RegistryKeys.RECIPE, id), json.getAsJsonObject(), wrapperLookup); + map.put(id, recipeEntry.value()); + } catch (JsonParseException e) { + AmsServer.LOGGER.warn("Failed to parse recipe: {}, error: {}", id, e.getMessage()); + } catch (Exception e) { + AmsServer.LOGGER.error("Unexpected error during recipe deserialization: {}, error: {}", id, e); + } + } + + private void registerAllRecipes(Map recipeMap) { + shapelessRecipes.forEach(recipe -> recipe.addToRecipeMap(recipeMap)); + shapedRecipes.forEach(recipe -> recipe.addToRecipeMap(recipeMap)); + smeltingRecipes.forEach(recipe -> recipe.addToRecipeMap(recipeMap)); + } + + public static void clearRecipeListMemory(AmsRecipeBuilder amsRecipeBuilder) { + amsRecipeBuilder.getShapedRecipeList().clear(); + amsRecipeBuilder.getShapelessRecipeList().clear(); + amsRecipeBuilder.getSmeltingRecipeList().clear(); + } +}