Update Recipe code, add abstraction for different recipe packet types.

This commit is contained in:
Articdive 2020-10-02 18:18:04 +02:00
parent 29c6d92bdc
commit 64ee739e1f
No known key found for this signature in database
GPG Key ID: B069585F0F7D90DE
11 changed files with 589 additions and 240 deletions

View File

@ -5,17 +5,18 @@ import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
public class DeclareRecipesPacket implements ServerPacket { public class DeclareRecipesPacket implements ServerPacket {
public Recipe[] recipes; public DeclaredRecipe[] recipes;
@Override @Override
public void write(BinaryWriter writer) { public void write(BinaryWriter writer) {
Check.notNull(recipes, "Recipes cannot be null!"); Check.notNull(recipes, "Recipes cannot be null!");
writer.writeVarInt(recipes.length); writer.writeVarInt(recipes.length);
for (Recipe recipe : recipes) { for (DeclaredRecipe recipe : recipes) {
recipe.write(writer); recipe.write(writer);
} }
} }
@ -25,91 +26,287 @@ public class DeclareRecipesPacket implements ServerPacket {
return ServerPacketIdentifier.DECLARE_RECIPES; return ServerPacketIdentifier.DECLARE_RECIPES;
} }
public static class Recipe { public abstract static class DeclaredRecipe {
protected final String recipeId;
protected final String recipeType;
public String recipeId; protected DeclaredRecipe(@NotNull String recipeId, @NotNull String recipeType) {
public String recipeType; this.recipeId = recipeId;
this.recipeType = recipeType;
}
public String group; public void write(@NotNull BinaryWriter writer) {
// crafting_shapeless
// ++ group
// ++ ingredients
// ++ result
// crafting_shaped
public int width;
public int height;
// ++ group
// ++ ingredients
// ++ result
// smelting, blasting, smoking and campfire
// ++ group
// ++ ingredient
// ++ result
public float experience;
public int cookingTime;
// smithing
// ++ ingredient (base)
public Ingredient additionIngredient;
// ++ result
public Ingredient ingredient;
public Ingredient[] ingredients;
public ItemStack result;
private void write(BinaryWriter writer) {
writer.writeSizedString(recipeType); writer.writeSizedString(recipeType);
writer.writeSizedString(recipeId); writer.writeSizedString(recipeId);
}
}
switch (recipeType) { public final static class DeclaredShapelessCraftingRecipe extends DeclaredRecipe {
case "crafting_shapeless": { private final String group;
writer.writeSizedString(group); private final Ingredient[] ingredients;
writer.writeVarInt(ingredients.length); private final ItemStack result;
for (Ingredient ingredient : ingredients) {
ingredient.write(writer); public DeclaredShapelessCraftingRecipe(
} @NotNull String recipeId,
writer.writeItemStack(result); @NotNull String group,
break; @NotNull Ingredient[] ingredients,
} @NotNull ItemStack result
case "crafting_shaped": { ) {
writer.writeVarInt(width); super(recipeId, "crafting_shapeless");
writer.writeVarInt(height); this.group = group;
writer.writeSizedString(group); this.ingredients = ingredients;
for (Ingredient ingredient : ingredients) { this.result = result;
ingredient.write(writer); }
}
writer.writeItemStack(result); @Override
break; public void write(@NotNull BinaryWriter writer) {
} // Write type & id
case "smelting": super.write(writer);
case "blasting": // Write recipe specific stuff.
case "smoking": writer.writeSizedString(group);
case "campfire_cooking": { writer.writeVarInt(ingredients.length);
writer.writeSizedString(group); for (Ingredient ingredient : ingredients) {
ingredient.write(writer); ingredient.write(writer);
writer.writeItemStack(result);
writer.writeFloat(experience);
writer.writeVarInt(cookingTime);
break;
}
case "stonecutting": {
writer.writeSizedString(group);
ingredient.write(writer);
writer.writeItemStack(result);
break;
}
case "smithing": {
ingredient.write(writer);
additionIngredient.write(writer);
writer.writeItemStack(result);
break;
}
} }
writer.writeItemStack(result);
}
}
public final static class DeclaredShapedCraftingRecipe extends DeclaredRecipe {
public final int width;
public final int height;
private final String group;
private final Ingredient[] ingredients;
private final ItemStack result;
public DeclaredShapedCraftingRecipe(
@NotNull String recipeId,
int width,
int height,
@NotNull String group,
@NotNull Ingredient[] ingredients,
@NotNull ItemStack result
) {
super(recipeId, "crafting_shaped");
this.group = group;
this.ingredients = ingredients;
this.result = result;
this.width = width;
this.height = height;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
writer.writeVarInt(width);
writer.writeVarInt(height);
writer.writeSizedString(group);
for (Ingredient ingredient : ingredients) {
ingredient.write(writer);
}
writer.writeItemStack(result);
}
}
public final static class DeclaredSmeltingRecipe extends DeclaredRecipe {
private final String group;
private final Ingredient ingredient;
private final ItemStack result;
private final float experience;
private final int cookingTime;
public DeclaredSmeltingRecipe(
@NotNull String recipeId,
@NotNull String group,
@NotNull Ingredient ingredient,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(recipeId, "smelting");
this.group = group;
this.ingredient = ingredient;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
writer.writeSizedString(group);
ingredient.write(writer);
writer.writeItemStack(result);
writer.writeFloat(experience);
writer.writeVarInt(cookingTime);
}
}
public final static class DeclaredBlastingRecipe extends DeclaredRecipe {
private final String group;
private final Ingredient ingredient;
private final ItemStack result;
private final float experience;
private final int cookingTime;
public DeclaredBlastingRecipe(
@NotNull String recipeId,
@NotNull String group,
@NotNull Ingredient ingredient,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(recipeId, "blasting");
this.group = group;
this.ingredient = ingredient;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
writer.writeSizedString(group);
ingredient.write(writer);
writer.writeItemStack(result);
writer.writeFloat(experience);
writer.writeVarInt(cookingTime);
}
}
public final static class DeclaredSmokingRecipe extends DeclaredRecipe {
private final String group;
private final Ingredient ingredient;
private final ItemStack result;
private final float experience;
private final int cookingTime;
public DeclaredSmokingRecipe(
@NotNull String recipeId,
@NotNull String group,
@NotNull Ingredient ingredient,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(recipeId, "smoking");
this.group = group;
this.ingredient = ingredient;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
writer.writeSizedString(group);
ingredient.write(writer);
writer.writeItemStack(result);
writer.writeFloat(experience);
writer.writeVarInt(cookingTime);
}
}
public final static class DeclaredCampfireCookingRecipe extends DeclaredRecipe {
private final String group;
private final Ingredient ingredient;
private final ItemStack result;
private final float experience;
private final int cookingTime;
public DeclaredCampfireCookingRecipe(
@NotNull String recipeId,
@NotNull String group,
@NotNull Ingredient ingredient,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(recipeId, "campfire_cooking");
this.group = group;
this.ingredient = ingredient;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
writer.writeSizedString(group);
ingredient.write(writer);
writer.writeItemStack(result);
writer.writeFloat(experience);
writer.writeVarInt(cookingTime);
}
}
public final static class DeclaredStonecutterRecipe extends DeclaredRecipe {
private final String group;
private final Ingredient ingredient;
private final ItemStack result;
public DeclaredStonecutterRecipe(
@NotNull String recipeId,
@NotNull String group,
@NotNull Ingredient ingredient,
@NotNull ItemStack result
) {
super(recipeId, "stonecutter");
this.group = group;
this.ingredient = ingredient;
this.result = result;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
writer.writeSizedString(group);
ingredient.write(writer);
writer.writeItemStack(result);
}
}
public final static class DeclaredSmithingRecipe extends DeclaredRecipe {
private final Ingredient base;
private final Ingredient addition;
private final ItemStack result;
public DeclaredSmithingRecipe(
@NotNull String recipeId,
@NotNull Ingredient base,
@NotNull Ingredient addition,
@NotNull ItemStack result
) {
super(recipeId, "smithing");
this.base = base;
this.addition = addition;
this.result = result;
}
@Override
public void write(@NotNull BinaryWriter writer) {
// Write type & id
super.write(writer);
// Write recipe specific stuff.
base.write(writer);
addition.write(writer);
writer.writeItemStack(result);
} }
} }

View File

@ -2,35 +2,53 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
public abstract class BlastingRecipe extends Recipe { public abstract class BlastingRecipe extends Recipe {
private String group;
private DeclareRecipesPacket.Ingredient ingredient; private DeclareRecipesPacket.Ingredient ingredient;
private ItemStack result;
private ItemStack result = ItemStack.getAirItem();
private float experience; private float experience;
private int cookingTime; private int cookingTime;
public BlastingRecipe(String recipeId, String group) { protected BlastingRecipe(
super(RecipeType.SMELTING, recipeId); @NotNull String recipeId,
setGroup(group); @NotNull String group,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(RecipeType.BLASTING, recipeId);
this.group = group;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
} }
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
}
@NotNull
public DeclareRecipesPacket.Ingredient getIngredient() { public DeclareRecipesPacket.Ingredient getIngredient() {
return ingredient; return ingredient;
} }
public void setIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void setIngredient(@NotNull DeclareRecipesPacket.Ingredient ingredient) {
this.ingredient = ingredient; this.ingredient = ingredient;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }

View File

@ -2,35 +2,53 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
public abstract class CampfireCookingRecipe extends Recipe { public abstract class CampfireCookingRecipe extends Recipe {
private String group;
private DeclareRecipesPacket.Ingredient ingredient; private DeclareRecipesPacket.Ingredient ingredient;
private ItemStack result;
private ItemStack result = ItemStack.getAirItem();
private float experience; private float experience;
private int cookingTime; private int cookingTime;
public CampfireCookingRecipe(String recipeId, String group) { protected CampfireCookingRecipe(
super(RecipeType.SMELTING, recipeId); @NotNull String recipeId,
setGroup(group); @NotNull String group,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(RecipeType.CAMPFIRE_COOKING, recipeId);
this.group = group;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
} }
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
}
@NotNull
public DeclareRecipesPacket.Ingredient getIngredient() { public DeclareRecipesPacket.Ingredient getIngredient() {
return ingredient; return ingredient;
} }
public void setIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void setIngredient(@NotNull DeclareRecipesPacket.Ingredient ingredient) {
this.ingredient = ingredient; this.ingredient = ingredient;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }

View File

@ -1,32 +1,25 @@
package net.minestom.server.recipe; package net.minestom.server.recipe;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
public abstract class Recipe { public abstract class Recipe {
protected final RecipeType recipeType;
protected final String recipeId;
protected RecipeType recipeType; protected Recipe(@NotNull RecipeType recipeType, @NotNull String recipeId) {
protected String recipeId;
protected String group;
protected Recipe(RecipeType recipeType, String recipeId) {
this.recipeType = recipeType; this.recipeType = recipeType;
this.recipeId = recipeId; this.recipeId = recipeId;
} }
public abstract boolean shouldShow(Player player); public abstract boolean shouldShow(@NotNull Player player);
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
@NotNull
public RecipeType getRecipeType() { public RecipeType getRecipeType() {
return recipeType; return recipeType;
} }
@NotNull
public String getRecipeId() { public String getRecipeId() {
return recipeId; return recipeId;
} }

View File

@ -1,6 +1,7 @@
package net.minestom.server.recipe; package net.minestom.server.recipe;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -8,12 +9,10 @@ import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
public class RecipeManager { public class RecipeManager {
private final DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket();
private final Set<Recipe> recipes = new CopyOnWriteArraySet<>();
private Set<Recipe> recipes = new CopyOnWriteArraySet<>(); public void addRecipe(@NotNull Recipe recipe) {
private DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket();
public void addRecipe(Recipe recipe) {
if (this.recipes.add(recipe)) { if (this.recipes.add(recipe)) {
// TODO add to all players // TODO add to all players
@ -21,7 +20,7 @@ public class RecipeManager {
} }
} }
public void removeRecipe(Recipe recipe) { public void removeRecipe(@NotNull Recipe recipe) {
if (this.recipes.remove(recipe)) { if (this.recipes.remove(recipe)) {
// TODO remove to all players // TODO remove to all players
@ -29,118 +28,130 @@ public class RecipeManager {
} }
} }
@NotNull
public Set<Recipe> getRecipes() { public Set<Recipe> getRecipes() {
return recipes; return recipes;
} }
@NotNull
public DeclareRecipesPacket getDeclareRecipesPacket() { public DeclareRecipesPacket getDeclareRecipesPacket() {
return declareRecipesPacket; return declareRecipesPacket;
} }
private void refreshRecipesPacket() { private void refreshRecipesPacket() {
List<DeclareRecipesPacket.Recipe> recipesCache = new ArrayList<>(); List<DeclareRecipesPacket.DeclaredRecipe> recipesCache = new ArrayList<>();
for (Recipe recipe : recipes) { for (Recipe recipe : recipes) {
DeclareRecipesPacket.Recipe packetRecipe = new DeclareRecipesPacket.Recipe();
switch (recipe.recipeType) { switch (recipe.recipeType) {
case SHAPELESS: { case SHAPELESS: {
packetRecipe.recipeType = "crafting_shapeless";
packetRecipe.group = recipe.getGroup();
ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe; ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe;
recipesCache.add(
List<DeclareRecipesPacket.Ingredient> ingredients = shapelessRecipe.getIngredients(); new DeclareRecipesPacket.DeclaredShapelessCraftingRecipe(
packetRecipe.ingredients = ingredients.toArray(new DeclareRecipesPacket.Ingredient[0]); shapelessRecipe.getRecipeId(),
packetRecipe.result = shapelessRecipe.getResult(); shapelessRecipe.getGroup(),
shapelessRecipe.getIngredients().toArray(new DeclareRecipesPacket.Ingredient[0]),
shapelessRecipe.getResult()
)
);
break; break;
} }
case SHAPED: { case SHAPED: {
packetRecipe.recipeType = "crafting_shaped";
packetRecipe.group = recipe.getGroup();
ShapedRecipe shapedRecipe = (ShapedRecipe) recipe; ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
recipesCache.add(
List<DeclareRecipesPacket.Ingredient> ingredients2 = shapedRecipe.getIngredients(); new DeclareRecipesPacket.DeclaredShapedCraftingRecipe(
packetRecipe.ingredients = ingredients2.toArray(new DeclareRecipesPacket.Ingredient[0]); shapedRecipe.getRecipeId(),
packetRecipe.result = shapedRecipe.getResult(); shapedRecipe.getWidth(),
shapedRecipe.getHeight(),
shapedRecipe.getGroup(),
shapedRecipe.getIngredients().toArray(new DeclareRecipesPacket.Ingredient[0]),
shapedRecipe.getResult()
)
);
break; break;
} }
case SMELTING: { case SMELTING: {
packetRecipe.recipeType = "smelting";
packetRecipe.group = recipe.getGroup();
SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe; SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe;
recipesCache.add(
packetRecipe.ingredient = smeltingRecipe.getIngredient(); new DeclareRecipesPacket.DeclaredSmeltingRecipe(
packetRecipe.result = smeltingRecipe.getResult(); smeltingRecipe.getRecipeId(),
packetRecipe.experience = smeltingRecipe.getExperience(); smeltingRecipe.getGroup(),
packetRecipe.cookingTime = smeltingRecipe.getCookingTime(); smeltingRecipe.getIngredient(),
smeltingRecipe.getResult(),
smeltingRecipe.getExperience(),
smeltingRecipe.getCookingTime()
)
);
break; break;
} }
case BLASTING: { case BLASTING: {
packetRecipe.recipeType = "blasting";
packetRecipe.group = recipe.getGroup();
BlastingRecipe blastingRecipe = (BlastingRecipe) recipe; BlastingRecipe blastingRecipe = (BlastingRecipe) recipe;
recipesCache.add(
packetRecipe.ingredient = blastingRecipe.getIngredient(); new DeclareRecipesPacket.DeclaredBlastingRecipe(
packetRecipe.result = blastingRecipe.getResult(); blastingRecipe.getRecipeId(),
packetRecipe.experience = blastingRecipe.getExperience(); blastingRecipe.getGroup(),
packetRecipe.cookingTime = blastingRecipe.getCookingTime(); blastingRecipe.getIngredient(),
blastingRecipe.getResult(),
blastingRecipe.getExperience(),
blastingRecipe.getCookingTime()
)
);
break; break;
} }
case SMOKING: { case SMOKING: {
packetRecipe.recipeType = "smoking";
packetRecipe.group = recipe.getGroup();
SmokingRecipe smokingRecipe = (SmokingRecipe) recipe; SmokingRecipe smokingRecipe = (SmokingRecipe) recipe;
recipesCache.add(
packetRecipe.ingredient = smokingRecipe.getIngredient(); new DeclareRecipesPacket.DeclaredSmokingRecipe(
packetRecipe.result = smokingRecipe.getResult(); smokingRecipe.getRecipeId(),
packetRecipe.experience = smokingRecipe.getExperience(); smokingRecipe.getGroup(),
packetRecipe.cookingTime = smokingRecipe.getCookingTime(); smokingRecipe.getIngredient(),
smokingRecipe.getResult(),
smokingRecipe.getExperience(),
smokingRecipe.getCookingTime()
)
);
break; break;
} }
case CAMPFIRE_COOKING: { case CAMPFIRE_COOKING: {
packetRecipe.recipeType = "campfire_cooking";
packetRecipe.group = recipe.getGroup();
CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe; CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe;
recipesCache.add(
packetRecipe.ingredient = campfireCookingRecipe.getIngredient(); new DeclareRecipesPacket.DeclaredCampfireCookingRecipe(
packetRecipe.result = campfireCookingRecipe.getResult(); campfireCookingRecipe.getRecipeId(),
packetRecipe.experience = campfireCookingRecipe.getExperience(); campfireCookingRecipe.getGroup(),
packetRecipe.cookingTime = campfireCookingRecipe.getCookingTime(); campfireCookingRecipe.getIngredient(),
campfireCookingRecipe.getResult(),
campfireCookingRecipe.getExperience(),
campfireCookingRecipe.getCookingTime()
)
);
break; break;
} }
case STONECUTTING: { case STONECUTTING: {
packetRecipe.recipeType = "stonecutting";
packetRecipe.group = recipe.getGroup();
StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe; StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe;
recipesCache.add(
packetRecipe.ingredient = stonecuttingRecipe.getIngredient(); new DeclareRecipesPacket.DeclaredStonecutterRecipe(
packetRecipe.result = stonecuttingRecipe.getResult(); stonecuttingRecipe.getRecipeId(),
stonecuttingRecipe.getGroup(),
stonecuttingRecipe.getIngredient(),
stonecuttingRecipe.getResult()
)
);
break; break;
} }
case SMITHING: { case SMITHING: {
packetRecipe.recipeType = "smithing";
SmithingRecipe smithingRecipe = (SmithingRecipe) recipe; SmithingRecipe smithingRecipe = (SmithingRecipe) recipe;
recipesCache.add(
packetRecipe.ingredient = smithingRecipe.getBaseIngredient(); new DeclareRecipesPacket.DeclaredSmithingRecipe(
packetRecipe.additionIngredient = smithingRecipe.getAdditionIngredient(); smithingRecipe.getRecipeId(),
packetRecipe.result = smithingRecipe.getResult(); smithingRecipe.getBaseIngredient(),
smithingRecipe.getAdditionIngredient(),
smithingRecipe.getResult()
)
);
break; break;
} }
} }
packetRecipe.recipeId = recipe.recipeId;
recipesCache.add(packetRecipe);
} }
declareRecipesPacket.recipes = recipesCache.toArray(new DeclareRecipesPacket.Recipe[0]); declareRecipesPacket.recipes = recipesCache.toArray(new DeclareRecipesPacket.DeclaredRecipe[0]);
} }
} }

View File

@ -2,41 +2,70 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
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.Nullable;
import java.util.ArrayList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects;
public abstract class ShapedRecipe extends Recipe { public abstract class ShapedRecipe extends Recipe {
private final int width;
private final int height;
private String group;
private final List<DeclareRecipesPacket.Ingredient> ingredients;
private ItemStack result;
private int width, height; protected ShapedRecipe(@NotNull String recipeId,
int width,
private List<DeclareRecipesPacket.Ingredient> ingredients = new ArrayList<>(); int height,
@NotNull String group,
private ItemStack result = ItemStack.getAirItem(); @Nullable List<DeclareRecipesPacket.Ingredient> ingredients,
@NotNull ItemStack result) {
public ShapedRecipe(String recipeId, String group, int width, int height) {
super(RecipeType.SHAPED, recipeId); super(RecipeType.SHAPED, recipeId);
setGroup(group);
this.width = width; this.width = width;
this.height = height; this.height = height;
this.group = group;
this.ingredients = Objects.requireNonNullElseGet(ingredients, LinkedList::new);
this.result = result;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
} }
public void addIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void addIngredient(DeclareRecipesPacket.Ingredient ingredient) {
if (ingredients.size() + 1 > width * height) if (ingredients.size() + 1 > width * height) {
throw new IndexOutOfBoundsException("You cannot add more ingredients than width*height"); throw new IndexOutOfBoundsException("You cannot add more ingredients than width*height");
}
ingredients.add(ingredient); ingredients.add(ingredient);
} }
@NotNull
public List<DeclareRecipesPacket.Ingredient> getIngredients() { public List<DeclareRecipesPacket.Ingredient> getIngredients() {
return ingredients; return ingredients;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }
} }

View File

@ -2,34 +2,54 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
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.Nullable;
import java.util.ArrayList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects;
public abstract class ShapelessRecipe extends Recipe { public abstract class ShapelessRecipe extends Recipe {
private String group;
private final List<DeclareRecipesPacket.Ingredient> ingredients;
private ItemStack result;
private List<DeclareRecipesPacket.Ingredient> ingredients = new ArrayList<>(); protected ShapelessRecipe(
@NotNull String recipeId,
private ItemStack result = ItemStack.getAirItem(); @NotNull String group,
@Nullable List<DeclareRecipesPacket.Ingredient> ingredients,
public ShapelessRecipe(String recipeId, String group) { @NotNull ItemStack result
) {
super(RecipeType.SHAPELESS, recipeId); super(RecipeType.SHAPELESS, recipeId);
setGroup(group); this.group = group;
this.ingredients = Objects.requireNonNullElseGet(ingredients, LinkedList::new);
this.result = result;
}
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
} }
public void addIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void addIngredient(DeclareRecipesPacket.Ingredient ingredient) {
ingredients.add(ingredient); ingredients.add(ingredient);
} }
@NotNull
public List<DeclareRecipesPacket.Ingredient> getIngredients() { public List<DeclareRecipesPacket.Ingredient> getIngredients() {
return ingredients; return ingredients;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }
} }

View File

@ -2,35 +2,53 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
public abstract class SmeltingRecipe extends Recipe { public abstract class SmeltingRecipe extends Recipe {
private String group;
private DeclareRecipesPacket.Ingredient ingredient; private DeclareRecipesPacket.Ingredient ingredient;
private ItemStack result;
private ItemStack result = ItemStack.getAirItem();
private float experience; private float experience;
private int cookingTime; private int cookingTime;
public SmeltingRecipe(String recipeId, String group) { protected SmeltingRecipe(
@NotNull String recipeId,
@NotNull String group,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(RecipeType.SMELTING, recipeId); super(RecipeType.SMELTING, recipeId);
setGroup(group); this.group = group;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
} }
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
}
@NotNull
public DeclareRecipesPacket.Ingredient getIngredient() { public DeclareRecipesPacket.Ingredient getIngredient() {
return ingredient; return ingredient;
} }
public void setIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void setIngredient(@NotNull DeclareRecipesPacket.Ingredient ingredient) {
this.ingredient = ingredient; this.ingredient = ingredient;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }

View File

@ -2,40 +2,49 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
public abstract class SmithingRecipe extends Recipe { public abstract class SmithingRecipe extends Recipe {
private DeclareRecipesPacket.Ingredient baseIngredient; private DeclareRecipesPacket.Ingredient baseIngredient;
private DeclareRecipesPacket.Ingredient additionIngredient; private DeclareRecipesPacket.Ingredient additionIngredient;
private ItemStack result;
private ItemStack result = ItemStack.getAirItem(); protected SmithingRecipe(
@NotNull String recipeId,
protected SmithingRecipe(String recipeId, String group) { @NotNull DeclareRecipesPacket.Ingredient baseIngredient,
@NotNull DeclareRecipesPacket.Ingredient additionIngredient,
@NotNull ItemStack result
) {
super(RecipeType.SMITHING, recipeId); super(RecipeType.SMITHING, recipeId);
setGroup(group); this.baseIngredient = baseIngredient;
this.additionIngredient = additionIngredient;
this.result = result;
} }
@NotNull
public DeclareRecipesPacket.Ingredient getBaseIngredient() { public DeclareRecipesPacket.Ingredient getBaseIngredient() {
return baseIngredient; return baseIngredient;
} }
public void setBaseIngredient(DeclareRecipesPacket.Ingredient baseIngredient) { public void setBaseIngredient(@NotNull DeclareRecipesPacket.Ingredient baseIngredient) {
this.baseIngredient = baseIngredient; this.baseIngredient = baseIngredient;
} }
@NotNull
public DeclareRecipesPacket.Ingredient getAdditionIngredient() { public DeclareRecipesPacket.Ingredient getAdditionIngredient() {
return additionIngredient; return additionIngredient;
} }
public void setAdditionIngredient(DeclareRecipesPacket.Ingredient additionIngredient) { public void setAdditionIngredient(@NotNull DeclareRecipesPacket.Ingredient additionIngredient) {
this.additionIngredient = additionIngredient; this.additionIngredient = additionIngredient;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }
} }

View File

@ -2,35 +2,53 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
public abstract class SmokingRecipe extends Recipe { public abstract class SmokingRecipe extends Recipe {
private String group;
private DeclareRecipesPacket.Ingredient ingredient; private DeclareRecipesPacket.Ingredient ingredient;
private ItemStack result;
private ItemStack result = ItemStack.getAirItem();
private float experience; private float experience;
private int cookingTime; private int cookingTime;
public SmokingRecipe(String recipeId, String group) { protected SmokingRecipe(
super(RecipeType.SMELTING, recipeId); @NotNull String recipeId,
setGroup(group); @NotNull String group,
@NotNull ItemStack result,
float experience,
int cookingTime
) {
super(RecipeType.SMOKING, recipeId);
this.group = group;
this.result = result;
this.experience = experience;
this.cookingTime = cookingTime;
} }
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
}
@NotNull
public DeclareRecipesPacket.Ingredient getIngredient() { public DeclareRecipesPacket.Ingredient getIngredient() {
return ingredient; return ingredient;
} }
public void setIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void setIngredient(@NotNull DeclareRecipesPacket.Ingredient ingredient) {
this.ingredient = ingredient; this.ingredient = ingredient;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }

View File

@ -2,31 +2,49 @@ package net.minestom.server.recipe;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket;
import org.jetbrains.annotations.NotNull;
public abstract class StonecutterRecipe extends Recipe { public abstract class StonecutterRecipe extends Recipe {
private String group;
private DeclareRecipesPacket.Ingredient ingredient; private DeclareRecipesPacket.Ingredient ingredient;
private ItemStack result;
private ItemStack result = ItemStack.getAirItem(); protected StonecutterRecipe(
@NotNull String recipeId,
protected StonecutterRecipe(String recipeId, String group) { @NotNull String group,
@NotNull DeclareRecipesPacket.Ingredient ingredient,
@NotNull ItemStack result
) {
super(RecipeType.STONECUTTING, recipeId); super(RecipeType.STONECUTTING, recipeId);
setGroup(group); this.group = group;
this.ingredient = ingredient;
this.result = result;
} }
@NotNull
public String getGroup() {
return group;
}
public void setGroup(@NotNull String group) {
this.group = group;
}
@NotNull
public DeclareRecipesPacket.Ingredient getIngredient() { public DeclareRecipesPacket.Ingredient getIngredient() {
return ingredient; return ingredient;
} }
public void setIngredient(DeclareRecipesPacket.Ingredient ingredient) { public void setIngredient(@NotNull DeclareRecipesPacket.Ingredient ingredient) {
this.ingredient = ingredient; this.ingredient = ingredient;
} }
@NotNull
public ItemStack getResult() { public ItemStack getResult() {
return result; return result;
} }
public void setResult(ItemStack result) { public void setResult(@NotNull ItemStack result) {
this.result = result; this.result = result;
} }
} }