Fix DeclareRecipePacket (#562)

This commit is contained in:
Gatt 2022-01-02 16:14:19 +11:00 committed by TheMode
parent a31f885cc7
commit 2fff62efd2
13 changed files with 44 additions and 51 deletions

View File

@ -34,7 +34,11 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { 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 @Override
@ -61,7 +65,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.writeVarIntList(ingredients, BinaryWriter::write); writer.writeVarIntList(ingredients, BinaryWriter::write);
writer.writeItemStack(result); writer.writeItemStack(result);
@ -88,11 +91,12 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeVarInt(width); writer.writeVarInt(width);
writer.writeVarInt(height); writer.writeVarInt(height);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.writeVarIntList(ingredients, BinaryWriter::write); for (Ingredient ingredient : ingredients) {
ingredient.write(writer);
}
writer.writeItemStack(result); writer.writeItemStack(result);
} }
@ -113,7 +117,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.write(ingredient); writer.write(ingredient);
writer.writeItemStack(result); writer.writeItemStack(result);
@ -138,7 +141,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.write(ingredient); writer.write(ingredient);
writer.writeItemStack(result); writer.writeItemStack(result);
@ -163,7 +165,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.write(ingredient); writer.write(ingredient);
writer.writeItemStack(result); writer.writeItemStack(result);
@ -188,7 +189,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.write(ingredient); writer.write(ingredient);
writer.writeItemStack(result); writer.writeItemStack(result);
@ -211,7 +211,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.writeSizedString(group); writer.writeSizedString(group);
writer.write(ingredient); writer.write(ingredient);
writer.writeItemStack(result); writer.writeItemStack(result);
@ -231,7 +230,6 @@ public record DeclareRecipesPacket(@NotNull List<DeclaredRecipe> recipes) implem
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(recipeId);
writer.write(base); writer.write(base);
writer.write(addition); writer.write(addition);
writer.writeItemStack(result); writer.writeItemStack(result);

View File

@ -18,7 +18,7 @@ public abstract class BlastingRecipe extends Recipe {
float experience, float experience,
int cookingTime int cookingTime
) { ) {
super(RecipeType.BLASTING, recipeId); super(Type.BLASTING, recipeId);
this.group = group; this.group = group;
this.result = result; this.result = result;
this.experience = experience; this.experience = experience;

View File

@ -18,7 +18,7 @@ public abstract class CampfireCookingRecipe extends Recipe {
float experience, float experience,
int cookingTime int cookingTime
) { ) {
super(RecipeType.CAMPFIRE_COOKING, recipeId); super(Type.CAMPFIRE_COOKING, recipeId);
this.group = group; this.group = group;
this.result = result; this.result = result;
this.experience = experience; this.experience = experience;

View File

@ -4,10 +4,10 @@ import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public abstract class Recipe { public abstract class Recipe {
protected final RecipeType recipeType; protected final Type recipeType;
protected final String recipeId; protected final String recipeId;
protected Recipe(@NotNull RecipeType recipeType, @NotNull String recipeId) { protected Recipe(@NotNull Type recipeType, @NotNull String recipeId) {
this.recipeType = recipeType; this.recipeType = recipeType;
this.recipeId = recipeId; this.recipeId = recipeId;
} }
@ -15,7 +15,7 @@ public abstract class Recipe {
public abstract boolean shouldShow(@NotNull Player player); public abstract boolean shouldShow(@NotNull Player player);
@NotNull @NotNull
public RecipeType getRecipeType() { public Type getRecipeType() {
return recipeType; return recipeType;
} }
@ -24,7 +24,7 @@ public abstract class Recipe {
return recipeId; return recipeId;
} }
protected enum RecipeType { public enum Type {
SHAPELESS, SHAPELESS,
SHAPED, SHAPED,
SMELTING, SMELTING,

View File

@ -12,18 +12,20 @@ public class RecipeManager {
private DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket(List.of()); private DeclareRecipesPacket declareRecipesPacket = new DeclareRecipesPacket(List.of());
private final Set<Recipe> recipes = new CopyOnWriteArraySet<>(); 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) { public void addRecipe(@NotNull Recipe recipe) {
if (this.recipes.add(recipe)) { if (this.recipes.add(recipe)) {
// TODO add to all players
refreshRecipesPacket(); refreshRecipesPacket();
} }
} }
public void removeRecipe(@NotNull Recipe recipe) { public void removeRecipe(@NotNull Recipe recipe) {
if (this.recipes.remove(recipe)) { if (this.recipes.remove(recipe)) {
// TODO remove to all players
refreshRecipesPacket(); refreshRecipesPacket();
} }
} }
@ -49,9 +51,7 @@ public class RecipeManager {
shapelessRecipe.getRecipeId(), shapelessRecipe.getRecipeId(),
shapelessRecipe.getGroup(), shapelessRecipe.getGroup(),
shapelessRecipe.getIngredients(), shapelessRecipe.getIngredients(),
shapelessRecipe.getResult() shapelessRecipe.getResult()));
)
);
} }
case SHAPED -> { case SHAPED -> {
ShapedRecipe shapedRecipe = (ShapedRecipe) recipe; ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
@ -62,9 +62,7 @@ public class RecipeManager {
shapedRecipe.getHeight(), shapedRecipe.getHeight(),
shapedRecipe.getGroup(), shapedRecipe.getGroup(),
shapedRecipe.getIngredients(), shapedRecipe.getIngredients(),
shapedRecipe.getResult() shapedRecipe.getResult()));
)
);
} }
case SMELTING -> { case SMELTING -> {
SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe; SmeltingRecipe smeltingRecipe = (SmeltingRecipe) recipe;
@ -75,9 +73,7 @@ public class RecipeManager {
smeltingRecipe.getIngredient(), smeltingRecipe.getIngredient(),
smeltingRecipe.getResult(), smeltingRecipe.getResult(),
smeltingRecipe.getExperience(), smeltingRecipe.getExperience(),
smeltingRecipe.getCookingTime() smeltingRecipe.getCookingTime()));
)
);
} }
case BLASTING -> { case BLASTING -> {
BlastingRecipe blastingRecipe = (BlastingRecipe) recipe; BlastingRecipe blastingRecipe = (BlastingRecipe) recipe;
@ -88,9 +84,7 @@ public class RecipeManager {
blastingRecipe.getIngredient(), blastingRecipe.getIngredient(),
blastingRecipe.getResult(), blastingRecipe.getResult(),
blastingRecipe.getExperience(), blastingRecipe.getExperience(),
blastingRecipe.getCookingTime() blastingRecipe.getCookingTime()));
)
);
} }
case SMOKING -> { case SMOKING -> {
SmokingRecipe smokingRecipe = (SmokingRecipe) recipe; SmokingRecipe smokingRecipe = (SmokingRecipe) recipe;
@ -101,9 +95,7 @@ public class RecipeManager {
smokingRecipe.getIngredient(), smokingRecipe.getIngredient(),
smokingRecipe.getResult(), smokingRecipe.getResult(),
smokingRecipe.getExperience(), smokingRecipe.getExperience(),
smokingRecipe.getCookingTime() smokingRecipe.getCookingTime()));
)
);
} }
case CAMPFIRE_COOKING -> { case CAMPFIRE_COOKING -> {
CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe; CampfireCookingRecipe campfireCookingRecipe = (CampfireCookingRecipe) recipe;
@ -114,9 +106,7 @@ public class RecipeManager {
campfireCookingRecipe.getIngredient(), campfireCookingRecipe.getIngredient(),
campfireCookingRecipe.getResult(), campfireCookingRecipe.getResult(),
campfireCookingRecipe.getExperience(), campfireCookingRecipe.getExperience(),
campfireCookingRecipe.getCookingTime() campfireCookingRecipe.getCookingTime()));
)
);
} }
case STONECUTTING -> { case STONECUTTING -> {
StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe; StonecutterRecipe stonecuttingRecipe = (StonecutterRecipe) recipe;
@ -125,9 +115,7 @@ public class RecipeManager {
stonecuttingRecipe.getRecipeId(), stonecuttingRecipe.getRecipeId(),
stonecuttingRecipe.getGroup(), stonecuttingRecipe.getGroup(),
stonecuttingRecipe.getIngredient(), stonecuttingRecipe.getIngredient(),
stonecuttingRecipe.getResult() stonecuttingRecipe.getResult()));
)
);
} }
case SMITHING -> { case SMITHING -> {
SmithingRecipe smithingRecipe = (SmithingRecipe) recipe; SmithingRecipe smithingRecipe = (SmithingRecipe) recipe;
@ -136,14 +124,13 @@ public class RecipeManager {
smithingRecipe.getRecipeId(), smithingRecipe.getRecipeId(),
smithingRecipe.getBaseIngredient(), smithingRecipe.getBaseIngredient(),
smithingRecipe.getAdditionIngredient(), smithingRecipe.getAdditionIngredient(),
smithingRecipe.getResult() smithingRecipe.getResult()));
)
);
} }
} }
} }
declareRecipesPacket = new DeclareRecipesPacket(recipesCache); declareRecipesPacket = new DeclareRecipesPacket(recipesCache);
// TODO; refresh and update players recipes
} }
} }

View File

@ -22,7 +22,7 @@ public abstract class ShapedRecipe extends Recipe {
@NotNull String group, @NotNull String group,
@Nullable List<DeclareRecipesPacket.Ingredient> ingredients, @Nullable List<DeclareRecipesPacket.Ingredient> ingredients,
@NotNull ItemStack result) { @NotNull ItemStack result) {
super(RecipeType.SHAPED, recipeId); super(Type.SHAPED, recipeId);
this.width = width; this.width = width;
this.height = height; this.height = height;
this.group = group; this.group = group;

View File

@ -20,7 +20,7 @@ public abstract class ShapelessRecipe extends Recipe {
@Nullable List<DeclareRecipesPacket.Ingredient> ingredients, @Nullable List<DeclareRecipesPacket.Ingredient> ingredients,
@NotNull ItemStack result @NotNull ItemStack result
) { ) {
super(RecipeType.SHAPELESS, recipeId); super(Type.SHAPELESS, recipeId);
this.group = group; this.group = group;
this.ingredients = Objects.requireNonNullElseGet(ingredients, LinkedList::new); this.ingredients = Objects.requireNonNullElseGet(ingredients, LinkedList::new);
this.result = result; this.result = result;

View File

@ -18,7 +18,7 @@ public abstract class SmeltingRecipe extends Recipe {
float experience, float experience,
int cookingTime int cookingTime
) { ) {
super(RecipeType.SMELTING, recipeId); super(Type.SMELTING, recipeId);
this.group = group; this.group = group;
this.result = result; this.result = result;
this.experience = experience; this.experience = experience;

View File

@ -15,7 +15,7 @@ public abstract class SmithingRecipe extends Recipe {
@NotNull DeclareRecipesPacket.Ingredient additionIngredient, @NotNull DeclareRecipesPacket.Ingredient additionIngredient,
@NotNull ItemStack result @NotNull ItemStack result
) { ) {
super(RecipeType.SMITHING, recipeId); super(Type.SMITHING, recipeId);
this.baseIngredient = baseIngredient; this.baseIngredient = baseIngredient;
this.additionIngredient = additionIngredient; this.additionIngredient = additionIngredient;
this.result = result; this.result = result;

View File

@ -18,7 +18,7 @@ public abstract class SmokingRecipe extends Recipe {
float experience, float experience,
int cookingTime int cookingTime
) { ) {
super(RecipeType.SMOKING, recipeId); super(Type.SMOKING, recipeId);
this.group = group; this.group = group;
this.result = result; this.result = result;
this.experience = experience; this.experience = experience;

View File

@ -15,7 +15,7 @@ public abstract class StonecutterRecipe extends Recipe {
@NotNull DeclareRecipesPacket.Ingredient ingredient, @NotNull DeclareRecipesPacket.Ingredient ingredient,
@NotNull ItemStack result @NotNull ItemStack result
) { ) {
super(RecipeType.STONECUTTING, recipeId); super(Type.STONECUTTING, recipeId);
this.group = group; this.group = group;
this.ingredient = ingredient; this.ingredient = ingredient;
this.result = result; this.result = result;

View File

@ -324,7 +324,7 @@ public class BinaryWriter extends OutputStream {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} }
} }
/** /**
* Writes the given writeable object into this writer. * Writes the given writeable object into this writer.
* *

View File

@ -26,6 +26,8 @@ import org.jglrxavpok.hephaistos.nbt.NBT;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import net.minestom.server.network.packet.server.play.DeclareRecipesPacket.Ingredient;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -80,6 +82,12 @@ public class PacketWriteReadTest {
SERVER_PACKETS.add(new CollectItemPacket(5, 5, 5)); SERVER_PACKETS.add(new CollectItemPacket(5, 5, 5));
SERVER_PACKETS.add(new CraftRecipeResponse((byte) 2, "recipe")); SERVER_PACKETS.add(new CraftRecipeResponse((byte) 2, "recipe"));
SERVER_PACKETS.add(new DeathCombatEventPacket(5, 5, COMPONENT)); 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 DestroyEntitiesPacket(List.of(5, 5, 5)));
SERVER_PACKETS.add(new DisconnectPacket(COMPONENT)); SERVER_PACKETS.add(new DisconnectPacket(COMPONENT));
SERVER_PACKETS.add(new DisplayScoreboardPacket((byte) 5, "scoreboard")); SERVER_PACKETS.add(new DisplayScoreboardPacket((byte) 5, "scoreboard"));