From 845113b6a92a39ed9cad3abf9ed18a8ad53a62e4 Mon Sep 17 00:00:00 2001 From: Matt Worzala <35708499+mworzala@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:58:12 -0500 Subject: [PATCH] Recipe manager cleanup (#2010) * Improved code readability. This code improves code flow, simplicity and reduces complexity * chore: interface to final class, rename conversion functions to their recipe type for clarity, use cached packet --------- Co-authored-by: Phillipp Glanz <6745190+TheMeinerLP@users.noreply.github.com> --- .../server/recipe/RecipeConversion.java | 99 +++++++++++++ .../minestom/server/recipe/RecipeManager.java | 134 +++--------------- 2 files changed, 121 insertions(+), 112 deletions(-) create mode 100644 src/main/java/net/minestom/server/recipe/RecipeConversion.java diff --git a/src/main/java/net/minestom/server/recipe/RecipeConversion.java b/src/main/java/net/minestom/server/recipe/RecipeConversion.java new file mode 100644 index 000000000..102f3926b --- /dev/null +++ b/src/main/java/net/minestom/server/recipe/RecipeConversion.java @@ -0,0 +1,99 @@ +package net.minestom.server.recipe; + +import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; +import org.jetbrains.annotations.NotNull; + +final class RecipeConversion { + + static @NotNull DeclareRecipesPacket.DeclaredShapelessCraftingRecipe shapeless(@NotNull ShapelessRecipe shapelessRecipe) { + return new DeclareRecipesPacket.DeclaredShapelessCraftingRecipe(shapelessRecipe.getRecipeId(), + shapelessRecipe.getGroup(), + shapelessRecipe.getCategory(), + shapelessRecipe.getIngredients(), + shapelessRecipe.getResult()); + } + + static @NotNull DeclareRecipesPacket.DeclaredShapedCraftingRecipe shaped(@NotNull ShapedRecipe shapedRecipe) { + return new DeclareRecipesPacket.DeclaredShapedCraftingRecipe(shapedRecipe.getRecipeId(), + shapedRecipe.getGroup(), + shapedRecipe.getCategory(), + shapedRecipe.getWidth(), + shapedRecipe.getHeight(), + shapedRecipe.getIngredients(), + shapedRecipe.getResult(), + shapedRecipe.getShowNotification()); + } + + static @NotNull DeclareRecipesPacket.DeclaredSmeltingRecipe smelting(@NotNull SmeltingRecipe smeltingRecipe) { + return new DeclareRecipesPacket.DeclaredSmeltingRecipe( + smeltingRecipe.getRecipeId(), + smeltingRecipe.getGroup(), + smeltingRecipe.getCategory(), + smeltingRecipe.getIngredient(), + smeltingRecipe.getResult(), + smeltingRecipe.getExperience(), + smeltingRecipe.getCookingTime()); + } + + static @NotNull DeclareRecipesPacket.DeclaredBlastingRecipe blasting(@NotNull BlastingRecipe blastingRecipe) { + return new DeclareRecipesPacket.DeclaredBlastingRecipe( + blastingRecipe.getRecipeId(), + blastingRecipe.getGroup(), + blastingRecipe.getCategory(), + blastingRecipe.getIngredient(), + blastingRecipe.getResult(), + blastingRecipe.getExperience(), + blastingRecipe.getCookingTime()); + } + + static @NotNull DeclareRecipesPacket.DeclaredSmokingRecipe smoking(@NotNull SmokingRecipe smokingRecipe) { + return new DeclareRecipesPacket.DeclaredSmokingRecipe( + smokingRecipe.getRecipeId(), + smokingRecipe.getGroup(), + smokingRecipe.getCategory(), + smokingRecipe.getIngredient(), + smokingRecipe.getResult(), + smokingRecipe.getExperience(), + smokingRecipe.getCookingTime()); + } + + static @NotNull DeclareRecipesPacket.DeclaredCampfireCookingRecipe campfire(@NotNull CampfireCookingRecipe campfireCookingRecipe) { + return new DeclareRecipesPacket.DeclaredCampfireCookingRecipe( + campfireCookingRecipe.getRecipeId(), + campfireCookingRecipe.getGroup(), + campfireCookingRecipe.getCategory(), + campfireCookingRecipe.getIngredient(), + campfireCookingRecipe.getResult(), + campfireCookingRecipe.getExperience(), + campfireCookingRecipe.getCookingTime()); + } + + static @NotNull DeclareRecipesPacket.DeclaredStonecutterRecipe stonecutter(@NotNull StonecutterRecipe stonecuttingRecipe) { + return new DeclareRecipesPacket.DeclaredStonecutterRecipe( + stonecuttingRecipe.getRecipeId(), + stonecuttingRecipe.getGroup(), + stonecuttingRecipe.getIngredient(), + stonecuttingRecipe.getResult()); + } + + static @NotNull DeclareRecipesPacket.DeclaredSmithingTransformRecipe smithingTransform(@NotNull SmithingTransformRecipe smithingTransformRecipe) { + return new DeclareRecipesPacket.DeclaredSmithingTransformRecipe( + smithingTransformRecipe.getRecipeId(), + smithingTransformRecipe.getTemplate(), + smithingTransformRecipe.getBaseIngredient(), + smithingTransformRecipe.getAdditionIngredient(), + smithingTransformRecipe.getResult()); + } + + static @NotNull DeclareRecipesPacket.DeclaredSmithingTrimRecipe smithingTrim(@NotNull SmithingTrimRecipe smithingTrimRecipe) { + return new DeclareRecipesPacket.DeclaredSmithingTrimRecipe( + smithingTrimRecipe.getRecipeId(), + smithingTrimRecipe.getTemplate(), + smithingTrimRecipe.getBaseIngredient(), + smithingTrimRecipe.getAdditionIngredient()); + } + + private RecipeConversion() { + + } +} diff --git a/src/main/java/net/minestom/server/recipe/RecipeManager.java b/src/main/java/net/minestom/server/recipe/RecipeManager.java index 3f6db2898..3dac248fb 100644 --- a/src/main/java/net/minestom/server/recipe/RecipeManager.java +++ b/src/main/java/net/minestom/server/recipe/RecipeManager.java @@ -1,5 +1,7 @@ package net.minestom.server.recipe; +import net.minestom.server.network.packet.server.CachedPacket; +import net.minestom.server.network.packet.server.SendablePacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import org.jetbrains.annotations.NotNull; @@ -9,24 +11,24 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; public class RecipeManager { - private DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket(List.of()); + private final CachedPacket declareRecipesPacket = new CachedPacket(this::createDeclareRecipesPacket); private final Set recipes = new CopyOnWriteArraySet<>(); public void addRecipes(@NotNull Recipe... recipe) { if (recipes.addAll(List.of(recipe))) { - refreshRecipesPacket(); + declareRecipesPacket.invalidate(); } } public void addRecipe(@NotNull Recipe recipe) { if (this.recipes.add(recipe)) { - refreshRecipesPacket(); + declareRecipesPacket.invalidate(); } } public void removeRecipe(@NotNull Recipe recipe) { if (this.recipes.remove(recipe)) { - refreshRecipesPacket(); + declareRecipesPacket.invalidate(); } } @@ -36,118 +38,26 @@ public class RecipeManager { } @NotNull - public DeclareRecipesPacket getDeclareRecipesPacket() { + public SendablePacket getDeclareRecipesPacket() { return declareRecipesPacket; } - private void refreshRecipesPacket() { - List recipesCache = new ArrayList<>(); - for (Recipe recipe : recipes) { - switch (recipe.recipeType) { - case SHAPELESS -> { - ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredShapelessCraftingRecipe( - shapelessRecipe.getRecipeId(), - shapelessRecipe.getGroup(), - shapelessRecipe.getCategory(), - shapelessRecipe.getIngredients(), - shapelessRecipe.getResult())); - } - case SHAPED -> { - ShapedRecipe shapedRecipe = (ShapedRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredShapedCraftingRecipe( - shapedRecipe.getRecipeId(), - shapedRecipe.getGroup(), - shapedRecipe.getCategory(), - shapedRecipe.getWidth(), - shapedRecipe.getHeight(), - shapedRecipe.getIngredients(), - shapedRecipe.getResult(), - shapedRecipe.getShowNotification())); - } - case SMELTING -> { - SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredSmeltingRecipe( - smeltingRecipe.getRecipeId(), - smeltingRecipe.getGroup(), - smeltingRecipe.getCategory(), - smeltingRecipe.getIngredient(), - smeltingRecipe.getResult(), - smeltingRecipe.getExperience(), - smeltingRecipe.getCookingTime())); - } - case BLASTING -> { - BlastingRecipe blastingRecipe = (BlastingRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredBlastingRecipe( - blastingRecipe.getRecipeId(), - blastingRecipe.getGroup(), - blastingRecipe.getCategory(), - blastingRecipe.getIngredient(), - blastingRecipe.getResult(), - blastingRecipe.getExperience(), - blastingRecipe.getCookingTime())); - } - case SMOKING -> { - SmokingRecipe smokingRecipe = (SmokingRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredSmokingRecipe( - smokingRecipe.getRecipeId(), - smokingRecipe.getGroup(), - smokingRecipe.getCategory(), - smokingRecipe.getIngredient(), - smokingRecipe.getResult(), - smokingRecipe.getExperience(), - smokingRecipe.getCookingTime())); - } - case CAMPFIRE_COOKING -> { - CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredCampfireCookingRecipe( - campfireCookingRecipe.getRecipeId(), - campfireCookingRecipe.getGroup(), - campfireCookingRecipe.getCategory(), - campfireCookingRecipe.getIngredient(), - campfireCookingRecipe.getResult(), - campfireCookingRecipe.getExperience(), - campfireCookingRecipe.getCookingTime())); - } - case STONECUTTING -> { - StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredStonecutterRecipe( - stonecuttingRecipe.getRecipeId(), - stonecuttingRecipe.getGroup(), - stonecuttingRecipe.getIngredient(), - stonecuttingRecipe.getResult())); - } - case SMITHING_TRANSFORM -> { - SmithingTransformRecipe smithingTransformRecipe = (SmithingTransformRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredSmithingTransformRecipe( - smithingTransformRecipe.getRecipeId(), - smithingTransformRecipe.getTemplate(), - smithingTransformRecipe.getBaseIngredient(), - smithingTransformRecipe.getAdditionIngredient(), - smithingTransformRecipe.getResult())); - } - case SMITHING_TRIM -> { - SmithingTrimRecipe smithingTrimRecipe = (SmithingTrimRecipe) recipe; - recipesCache.add( - new DeclareRecipesPacket.DeclaredSmithingTrimRecipe( - smithingTrimRecipe.getRecipeId(), - smithingTrimRecipe.getTemplate(), - smithingTrimRecipe.getBaseIngredient(), - smithingTrimRecipe.getAdditionIngredient())); - } - } + private @NotNull DeclareRecipesPacket createDeclareRecipesPacket() { + var entries = new ArrayList(); + for (var recipe : recipes) { + entries.add(switch (recipe.recipeType) { + case SHAPELESS -> RecipeConversion.shapeless((ShapelessRecipe) recipe); + case SHAPED -> RecipeConversion.shaped((ShapedRecipe) recipe); + case SMELTING -> RecipeConversion.smelting((SmeltingRecipe) recipe); + case BLASTING -> RecipeConversion.blasting((BlastingRecipe) recipe); + case SMOKING -> RecipeConversion.smoking((SmokingRecipe) recipe); + case CAMPFIRE_COOKING -> RecipeConversion.campfire((CampfireCookingRecipe) recipe); + case STONECUTTING -> RecipeConversion.stonecutter((StonecutterRecipe) recipe); + case SMITHING_TRANSFORM -> RecipeConversion.smithingTransform((SmithingTransformRecipe) recipe); + case SMITHING_TRIM -> RecipeConversion.smithingTrim((SmithingTrimRecipe) recipe); + }); } - - declareRecipesPacket = new DeclareRecipesPacket(recipesCache); - // TODO; refresh and update players recipes + return new DeclareRecipesPacket(entries); } }