mirror of
https://github.com/Minestom/Minestom.git
synced 2024-10-31 15:59:35 +01:00
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>
This commit is contained in:
parent
d595ce3468
commit
845113b6a9
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package net.minestom.server.recipe;
|
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 net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -9,24 +11,24 @@ import java.util.Set;
|
|||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
|
||||||
public class RecipeManager {
|
public class RecipeManager {
|
||||||
private DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket(List.of());
|
private final CachedPacket declareRecipesPacket = new CachedPacket(this::createDeclareRecipesPacket);
|
||||||
private final Set<Recipe> recipes = new CopyOnWriteArraySet<>();
|
private final Set<Recipe> recipes = new CopyOnWriteArraySet<>();
|
||||||
|
|
||||||
public void addRecipes(@NotNull Recipe... recipe) {
|
public void addRecipes(@NotNull Recipe... recipe) {
|
||||||
if (recipes.addAll(List.of(recipe))) {
|
if (recipes.addAll(List.of(recipe))) {
|
||||||
refreshRecipesPacket();
|
declareRecipesPacket.invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRecipe(@NotNull Recipe recipe) {
|
public void addRecipe(@NotNull Recipe recipe) {
|
||||||
if (this.recipes.add(recipe)) {
|
if (this.recipes.add(recipe)) {
|
||||||
refreshRecipesPacket();
|
declareRecipesPacket.invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeRecipe(@NotNull Recipe recipe) {
|
public void removeRecipe(@NotNull Recipe recipe) {
|
||||||
if (this.recipes.remove(recipe)) {
|
if (this.recipes.remove(recipe)) {
|
||||||
refreshRecipesPacket();
|
declareRecipesPacket.invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,118 +38,26 @@ public class RecipeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public DeclareRecipesPacket getDeclareRecipesPacket() {
|
public SendablePacket getDeclareRecipesPacket() {
|
||||||
return declareRecipesPacket;
|
return declareRecipesPacket;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshRecipesPacket() {
|
private @NotNull DeclareRecipesPacket createDeclareRecipesPacket() {
|
||||||
List<DeclareRecipesPacket.DeclaredRecipe> recipesCache = new ArrayList<>();
|
var entries = new ArrayList<DeclareRecipesPacket.DeclaredRecipe>();
|
||||||
for (Recipe recipe : recipes) {
|
for (var recipe : recipes) {
|
||||||
switch (recipe.recipeType) {
|
entries.add(switch (recipe.recipeType) {
|
||||||
case SHAPELESS -> {
|
case SHAPELESS -> RecipeConversion.shapeless((ShapelessRecipe) recipe);
|
||||||
ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe;
|
case SHAPED -> RecipeConversion.shaped((ShapedRecipe) recipe);
|
||||||
recipesCache.add(
|
case SMELTING -> RecipeConversion.smelting((SmeltingRecipe) recipe);
|
||||||
new DeclareRecipesPacket.DeclaredShapelessCraftingRecipe(
|
case BLASTING -> RecipeConversion.blasting((BlastingRecipe) recipe);
|
||||||
shapelessRecipe.getRecipeId(),
|
case SMOKING -> RecipeConversion.smoking((SmokingRecipe) recipe);
|
||||||
shapelessRecipe.getGroup(),
|
case CAMPFIRE_COOKING -> RecipeConversion.campfire((CampfireCookingRecipe) recipe);
|
||||||
shapelessRecipe.getCategory(),
|
case STONECUTTING -> RecipeConversion.stonecutter((StonecutterRecipe) recipe);
|
||||||
shapelessRecipe.getIngredients(),
|
case SMITHING_TRANSFORM -> RecipeConversion.smithingTransform((SmithingTransformRecipe) recipe);
|
||||||
shapelessRecipe.getResult()));
|
case SMITHING_TRIM -> RecipeConversion.smithingTrim((SmithingTrimRecipe) recipe);
|
||||||
}
|
});
|
||||||
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()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return new DeclareRecipesPacket(entries);
|
||||||
declareRecipesPacket = new DeclareRecipesPacket(recipesCache);
|
|
||||||
// TODO; refresh and update players recipes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user