5252import net .minecraft .world .item .component .TypedEntityData ;
5353import net .minecraft .world .item .crafting .*;
5454import net .minecraft .world .item .crafting .BlastingRecipe ;
55+ import net .minecraft .world .item .crafting .CraftingRecipe ;
5556import net .minecraft .world .item .crafting .Recipe ;
5657import net .minecraft .world .item .crafting .ShapelessRecipe ;
5758import net .minecraft .world .item .crafting .SmithingTransformRecipe ;
9697
9798public class ItemHelperImpl extends ItemHelper {
9899
100+ public static final Recipe .CommonInfo BASE_RECIPE_INFO = new Recipe .CommonInfo (true );
101+
99102 public static net .minecraft .world .item .crafting .RecipeHolder <?> getNMSRecipe (NamespacedKey key ) {
100103 ResourceKey <Recipe <?>> nmsKey = ResourceKey .create (Registries .RECIPE , CraftNamespacedKey .toMinecraft (key ));
101104 return ((CraftServer ) Bukkit .getServer ()).getServer ().getRecipeManager ().byKey (nmsKey ).orElse (null );
102105 }
103106
104- public static final Field Item_components = ReflectionHelper .getFields (Item .class ).get ("components" , DataComponentMap .class );
105-
106107 public static final Field RecipeManager_featureFlagSet = ReflectionHelper .getFields (RecipeManager .class ).getFirstOfType (FeatureFlagSet .class );
107108
108109 public void setMaxStackSize (Material material , int size ) {
109110 try {
110111 ReflectionHelper .getFinalSetter (Material .class , "maxStack" ).invoke (material , size );
111- Item nmsItem = BuiltInRegistries .ITEM .getValue (CraftNamespacedKey .toMinecraft (material .getKey ()));
112- DataComponentMap currentComponents = nmsItem .components ();
113- Item_components .set (nmsItem , DataComponentMap .composite (currentComponents , DataComponentMap .builder ().set (DataComponents .MAX_STACK_SIZE , size ).build ()));
112+ Holder .Reference <Item > nmsItemHolder = BuiltInRegistries .ITEM .get (CraftNamespacedKey .toMinecraft (material .getKey ())).orElseThrow ();
113+ nmsItemHolder .bindComponents (DataComponentMap .composite (nmsItemHolder .components (), DataComponentMap .builder ().set (DataComponents .MAX_STACK_SIZE , size ).build ()));
114114 }
115115 catch (Throwable ex ) {
116116 Debug .echoError (ex );
@@ -211,18 +211,18 @@ public void registerFurnaceRecipe(String keyName, String group, ItemStack result
211211 ResourceKey <Recipe <?>> key = createRecipeKey (keyName );
212212 Ingredient itemRecipe = itemArrayToRecipe (ingredient , exact );
213213 AbstractCookingRecipe recipe ;
214- CookingBookCategory categoryValue = category == null ? CookingBookCategory .MISC : CookingBookCategory .valueOf (CoreUtilities .toUpperCase (category ));
214+ AbstractCookingRecipe . CookingBookInfo bookInfo = new AbstractCookingRecipe . CookingBookInfo ( category == null ? CookingBookCategory .MISC : CookingBookCategory .valueOf (CoreUtilities .toUpperCase (category )), group );
215215 if (type .equalsIgnoreCase ("smoker" )) {
216- recipe = new SmokingRecipe (group , categoryValue , itemRecipe , CraftItemStack .asNMSCopy (result ), exp , time );
216+ recipe = new SmokingRecipe (BASE_RECIPE_INFO , bookInfo , itemRecipe , CraftItemStack .asNMSTemplate (result ), exp , time );
217217 }
218218 else if (type .equalsIgnoreCase ("blast" )) {
219- recipe = new BlastingRecipe (group , categoryValue , itemRecipe , CraftItemStack .asNMSCopy (result ), exp , time );
219+ recipe = new BlastingRecipe (BASE_RECIPE_INFO , bookInfo , itemRecipe , CraftItemStack .asNMSTemplate (result ), exp , time );
220220 }
221221 else if (type .equalsIgnoreCase ("campfire" )) {
222- recipe = new CampfireCookingRecipe (group , categoryValue , itemRecipe , CraftItemStack .asNMSCopy (result ), exp , time );
222+ recipe = new CampfireCookingRecipe (BASE_RECIPE_INFO , bookInfo , itemRecipe , CraftItemStack .asNMSTemplate (result ), exp , time );
223223 }
224224 else {
225- recipe = new SmeltingRecipe (group , categoryValue , itemRecipe , CraftItemStack .asNMSCopy (result ), exp , time );
225+ recipe = new SmeltingRecipe (BASE_RECIPE_INFO , bookInfo , itemRecipe , CraftItemStack .asNMSTemplate (result ), exp , time );
226226 }
227227 RecipeHolder <AbstractCookingRecipe > holder = new RecipeHolder <>(key , recipe );
228228 getRecipeManager ().addRecipe (holder );
@@ -232,7 +232,7 @@ else if (type.equalsIgnoreCase("campfire")) {
232232 public void registerStonecuttingRecipe (String keyName , String group , ItemStack result , ItemStack [] ingredient , boolean exact ) {
233233 ResourceKey <Recipe <?>> key = createRecipeKey (keyName );
234234 Ingredient itemRecipe = itemArrayToRecipe (ingredient , exact );
235- StonecutterRecipe recipe = new StonecutterRecipe (group , itemRecipe , CraftItemStack .asNMSCopy (result ));
235+ StonecutterRecipe recipe = new StonecutterRecipe (BASE_RECIPE_INFO , itemRecipe , CraftItemStack .asNMSTemplate (result ));
236236 RecipeHolder <StonecutterRecipe > holder = new RecipeHolder <>(key , recipe );
237237 getRecipeManager ().addRecipe (holder );
238238 }
@@ -243,8 +243,7 @@ public void registerSmithingRecipe(String keyName, ItemStack result, ItemStack[]
243243 Ingredient templateItemRecipe = templateItem .length == 0 ? null : itemArrayToRecipe (templateItem , templateExact );
244244 Ingredient baseItemRecipe = itemArrayToRecipe (baseItem , baseExact );
245245 Ingredient upgradeItemRecipe = itemArrayToRecipe (upgradeItem , upgradeExact );
246- net .minecraft .world .item .ItemStack nmsCopy = CraftItemStack .asNMSCopy (result );
247- SmithingTransformRecipe recipe = new SmithingTransformRecipe (Optional .ofNullable (templateItemRecipe ), baseItemRecipe , Optional .of (upgradeItemRecipe ), new TransmuteResult (nmsCopy .getItemHolder (), nmsCopy .getCount (), nmsCopy .getComponentsPatch ()));
246+ SmithingTransformRecipe recipe = new SmithingTransformRecipe (BASE_RECIPE_INFO , Optional .ofNullable (templateItemRecipe ), baseItemRecipe , Optional .of (upgradeItemRecipe ), CraftItemStack .asNMSTemplate (result ));
248247 RecipeHolder <SmithingTransformRecipe > holder = new RecipeHolder <>(key , recipe );
249248 getRecipeManager ().addRecipe (holder );
250249 }
@@ -257,8 +256,7 @@ public void registerShapelessRecipe(String keyName, String group, ItemStack resu
257256 for (int i = 0 ; i < ingredients .size (); i ++) {
258257 ingredientList .add (itemArrayToRecipe (ingredients .get (i ), exact [i ]));
259258 }
260- // TODO: 1.19.3: Add support for choosing a CraftingBookCategory
261- ShapelessRecipe recipe = new ShapelessRecipe (group , categoryValue , CraftItemStack .asNMSCopy (result ), NonNullList .of (null , ingredientList .toArray (new Ingredient [0 ])));
259+ ShapelessRecipe recipe = new ShapelessRecipe (BASE_RECIPE_INFO , new CraftingRecipe .CraftingBookInfo (categoryValue , group ), CraftItemStack .asNMSTemplate (result ), NonNullList .of (null , ingredientList .toArray (new Ingredient [0 ])));
262260 RecipeHolder <ShapelessRecipe > holder = new RecipeHolder <>(key , recipe );
263261 getRecipeManager ().addRecipe (holder );
264262 }
0 commit comments