mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-13 11:41:52 +01:00
Fix DeclareRecipePacket (#562)
This commit is contained in:
parent
a31f885cc7
commit
2fff62efd2
@ -34,7 +34,11 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeVarIntList(recipes, BinaryWriter::write);
|
||||
writer.writeVarIntList(recipes, (bWriter, recipe)->{
|
||||
bWriter.writeSizedString(recipe.type());
|
||||
bWriter.writeSizedString(recipe.recipeId());
|
||||
bWriter.write(recipe);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -61,7 +65,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeSizedString(group);
|
||||
writer.writeVarIntList(ingredients, BinaryWriter::write);
|
||||
writer.writeItemStack(result);
|
||||
@ -88,11 +91,12 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeVarInt(width);
|
||||
writer.writeVarInt(height);
|
||||
writer.writeSizedString(group);
|
||||
writer.writeVarIntList(ingredients, BinaryWriter::write);
|
||||
for (Ingredient ingredient : ingredients) {
|
||||
ingredient.write(writer);
|
||||
}
|
||||
writer.writeItemStack(result);
|
||||
}
|
||||
|
||||
@ -113,7 +117,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeSizedString(group);
|
||||
writer.write(ingredient);
|
||||
writer.writeItemStack(result);
|
||||
@ -138,7 +141,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeSizedString(group);
|
||||
writer.write(ingredient);
|
||||
writer.writeItemStack(result);
|
||||
@ -163,7 +165,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeSizedString(group);
|
||||
writer.write(ingredient);
|
||||
writer.writeItemStack(result);
|
||||
@ -188,7 +189,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeSizedString(group);
|
||||
writer.write(ingredient);
|
||||
writer.writeItemStack(result);
|
||||
@ -211,7 +211,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.writeSizedString(group);
|
||||
writer.write(ingredient);
|
||||
writer.writeItemStack(result);
|
||||
@ -231,7 +230,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeSizedString(recipeId);
|
||||
writer.write(base);
|
||||
writer.write(addition);
|
||||
writer.writeItemStack(result);
|
||||
|
@ -18,7 +18,7 @@ public abstract class BlastingRecipe extends Recipe {
|
||||
float experience,
|
||||
int cookingTime
|
||||
) {
|
||||
super(RecipeType.BLASTING, recipeId);
|
||||
super(Type.BLASTING, recipeId);
|
||||
this.group = group;
|
||||
this.result = result;
|
||||
this.experience = experience;
|
||||
|
@ -18,7 +18,7 @@ public abstract class CampfireCookingRecipe extends Recipe {
|
||||
float experience,
|
||||
int cookingTime
|
||||
) {
|
||||
super(RecipeType.CAMPFIRE_COOKING, recipeId);
|
||||
super(Type.CAMPFIRE_COOKING, recipeId);
|
||||
this.group = group;
|
||||
this.result = result;
|
||||
this.experience = experience;
|
||||
|
@ -4,10 +4,10 @@ import net.minestom.server.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class Recipe {
|
||||
protected final RecipeType recipeType;
|
||||
protected final Type recipeType;
|
||||
protected final String recipeId;
|
||||
|
||||
protected Recipe(@NotNull RecipeType recipeType, @NotNull String recipeId) {
|
||||
protected Recipe(@NotNull Type recipeType, @NotNull String recipeId) {
|
||||
this.recipeType = recipeType;
|
||||
this.recipeId = recipeId;
|
||||
}
|
||||
@ -15,7 +15,7 @@ public abstract class Recipe {
|
||||
public abstract boolean shouldShow(@NotNull Player player);
|
||||
|
||||
@NotNull
|
||||
public RecipeType getRecipeType() {
|
||||
public Type getRecipeType() {
|
||||
return recipeType;
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public abstract class Recipe {
|
||||
return recipeId;
|
||||
}
|
||||
|
||||
protected enum RecipeType {
|
||||
public enum Type {
|
||||
SHAPELESS,
|
||||
SHAPED,
|
||||
SMELTING,
|
||||
|
@ -12,18 +12,20 @@ public class RecipeManager {
|
||||
private DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket(List.of());
|
||||
private final Set<Recipe> recipes = new CopyOnWriteArraySet<>();
|
||||
|
||||
public void addRecipes(@NotNull Recipe... recipe) {
|
||||
if (recipes.addAll(List.of(recipe))) {
|
||||
refreshRecipesPacket();
|
||||
}
|
||||
}
|
||||
|
||||
public void addRecipe(@NotNull Recipe recipe) {
|
||||
if (this.recipes.add(recipe)) {
|
||||
// TODO add to all players
|
||||
|
||||
refreshRecipesPacket();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeRecipe(@NotNull Recipe recipe) {
|
||||
if (this.recipes.remove(recipe)) {
|
||||
// TODO remove to all players
|
||||
|
||||
refreshRecipesPacket();
|
||||
}
|
||||
}
|
||||
@ -49,9 +51,7 @@ public class RecipeManager {
|
||||
shapelessRecipe.getRecipeId(),
|
||||
shapelessRecipe.getGroup(),
|
||||
shapelessRecipe.getIngredients(),
|
||||
shapelessRecipe.getResult()
|
||||
)
|
||||
);
|
||||
shapelessRecipe.getResult()));
|
||||
}
|
||||
case SHAPED -> {
|
||||
ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
|
||||
@ -62,9 +62,7 @@ public class RecipeManager {
|
||||
shapedRecipe.getHeight(),
|
||||
shapedRecipe.getGroup(),
|
||||
shapedRecipe.getIngredients(),
|
||||
shapedRecipe.getResult()
|
||||
)
|
||||
);
|
||||
shapedRecipe.getResult()));
|
||||
}
|
||||
case SMELTING -> {
|
||||
SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe;
|
||||
@ -75,9 +73,7 @@ public class RecipeManager {
|
||||
smeltingRecipe.getIngredient(),
|
||||
smeltingRecipe.getResult(),
|
||||
smeltingRecipe.getExperience(),
|
||||
smeltingRecipe.getCookingTime()
|
||||
)
|
||||
);
|
||||
smeltingRecipe.getCookingTime()));
|
||||
}
|
||||
case BLASTING -> {
|
||||
BlastingRecipe blastingRecipe = (BlastingRecipe) recipe;
|
||||
@ -88,9 +84,7 @@ public class RecipeManager {
|
||||
blastingRecipe.getIngredient(),
|
||||
blastingRecipe.getResult(),
|
||||
blastingRecipe.getExperience(),
|
||||
blastingRecipe.getCookingTime()
|
||||
)
|
||||
);
|
||||
blastingRecipe.getCookingTime()));
|
||||
}
|
||||
case SMOKING -> {
|
||||
SmokingRecipe smokingRecipe = (SmokingRecipe) recipe;
|
||||
@ -101,9 +95,7 @@ public class RecipeManager {
|
||||
smokingRecipe.getIngredient(),
|
||||
smokingRecipe.getResult(),
|
||||
smokingRecipe.getExperience(),
|
||||
smokingRecipe.getCookingTime()
|
||||
)
|
||||
);
|
||||
smokingRecipe.getCookingTime()));
|
||||
}
|
||||
case CAMPFIRE_COOKING -> {
|
||||
CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe;
|
||||
@ -114,9 +106,7 @@ public class RecipeManager {
|
||||
campfireCookingRecipe.getIngredient(),
|
||||
campfireCookingRecipe.getResult(),
|
||||
campfireCookingRecipe.getExperience(),
|
||||
campfireCookingRecipe.getCookingTime()
|
||||
)
|
||||
);
|
||||
campfireCookingRecipe.getCookingTime()));
|
||||
}
|
||||
case STONECUTTING -> {
|
||||
StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe;
|
||||
@ -125,9 +115,7 @@ public class RecipeManager {
|
||||
stonecuttingRecipe.getRecipeId(),
|
||||
stonecuttingRecipe.getGroup(),
|
||||
stonecuttingRecipe.getIngredient(),
|
||||
stonecuttingRecipe.getResult()
|
||||
)
|
||||
);
|
||||
stonecuttingRecipe.getResult()));
|
||||
}
|
||||
case SMITHING -> {
|
||||
SmithingRecipe smithingRecipe = (SmithingRecipe) recipe;
|
||||
@ -136,14 +124,13 @@ public class RecipeManager {
|
||||
smithingRecipe.getRecipeId(),
|
||||
smithingRecipe.getBaseIngredient(),
|
||||
smithingRecipe.getAdditionIngredient(),
|
||||
smithingRecipe.getResult()
|
||||
)
|
||||
);
|
||||
smithingRecipe.getResult()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declareRecipesPacket = new DeclareRecipesPacket(recipesCache);
|
||||
// TODO; refresh and update players recipes
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ public abstract class ShapedRecipe extends Recipe {
|
||||
@NotNull String group,
|
||||
@Nullable List<DeclareRecipesPacket.Ingredient> ingredients,
|
||||
@NotNull ItemStack result) {
|
||||
super(RecipeType.SHAPED, recipeId);
|
||||
super(Type.SHAPED, recipeId);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.group = group;
|
||||
|
@ -20,7 +20,7 @@ public abstract class ShapelessRecipe extends Recipe {
|
||||
@Nullable List<DeclareRecipesPacket.Ingredient> ingredients,
|
||||
@NotNull ItemStack result
|
||||
) {
|
||||
super(RecipeType.SHAPELESS, recipeId);
|
||||
super(Type.SHAPELESS, recipeId);
|
||||
this.group = group;
|
||||
this.ingredients = Objects.requireNonNullElseGet(ingredients, LinkedList::new);
|
||||
this.result = result;
|
||||
|
@ -18,7 +18,7 @@ public abstract class SmeltingRecipe extends Recipe {
|
||||
float experience,
|
||||
int cookingTime
|
||||
) {
|
||||
super(RecipeType.SMELTING, recipeId);
|
||||
super(Type.SMELTING, recipeId);
|
||||
this.group = group;
|
||||
this.result = result;
|
||||
this.experience = experience;
|
||||
|
@ -15,7 +15,7 @@ public abstract class SmithingRecipe extends Recipe {
|
||||
@NotNull DeclareRecipesPacket.Ingredient additionIngredient,
|
||||
@NotNull ItemStack result
|
||||
) {
|
||||
super(RecipeType.SMITHING, recipeId);
|
||||
super(Type.SMITHING, recipeId);
|
||||
this.baseIngredient = baseIngredient;
|
||||
this.additionIngredient = additionIngredient;
|
||||
this.result = result;
|
||||
|
@ -18,7 +18,7 @@ public abstract class SmokingRecipe extends Recipe {
|
||||
float experience,
|
||||
int cookingTime
|
||||
) {
|
||||
super(RecipeType.SMOKING, recipeId);
|
||||
super(Type.SMOKING, recipeId);
|
||||
this.group = group;
|
||||
this.result = result;
|
||||
this.experience = experience;
|
||||
|
@ -15,7 +15,7 @@ public abstract class StonecutterRecipe extends Recipe {
|
||||
@NotNull DeclareRecipesPacket.Ingredient ingredient,
|
||||
@NotNull ItemStack result
|
||||
) {
|
||||
super(RecipeType.STONECUTTING, recipeId);
|
||||
super(Type.STONECUTTING, recipeId);
|
||||
this.group = group;
|
||||
this.ingredient = ingredient;
|
||||
this.result = result;
|
||||
|
@ -324,7 +324,7 @@ public class BinaryWriter extends OutputStream {
|
||||
MinecraftServer.getExceptionManager().handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes the given writeable object into this writer.
|
||||
*
|
||||
|
@ -26,6 +26,8 @@ import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket.Ingredient;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -80,6 +82,12 @@ public class PacketWriteReadTest {
|
||||
SERVER_PACKETS.add(new CollectItemPacket(5, 5, 5));
|
||||
SERVER_PACKETS.add(new CraftRecipeResponse((byte) 2, "recipe"));
|
||||
SERVER_PACKETS.add(new DeathCombatEventPacket(5, 5, COMPONENT));
|
||||
SERVER_PACKETS.add(new DeclareRecipesPacket(
|
||||
List.of(new DeclareRecipesPacket.DeclaredShapelessCraftingRecipe(
|
||||
"minecraft:sticks",
|
||||
"sticks",
|
||||
List.of(new Ingredient(List.of(ItemStack.of(Material.OAK_PLANKS)))),
|
||||
ItemStack.of(Material.STICK)))));
|
||||
SERVER_PACKETS.add(new DestroyEntitiesPacket(List.of(5, 5, 5)));
|
||||
SERVER_PACKETS.add(new DisconnectPacket(COMPONENT));
|
||||
SERVER_PACKETS.add(new DisplayScoreboardPacket((byte) 5, "scoreboard"));
|
||||
|
Loading…
Reference in New Issue
Block a user