ProtocolLib

3M Downloads

Modifying Packet.Play.Server.OPEN_WINDOW_ITEMS recipes leads to server-side item change.

WillFP opened this issue · 2 comments

commented

ProtocolLib 4.6.0

This code here:

@Override
    public void onSend(@NotNull final PacketContainer packet,
                       @NotNull final Player player,
                       @NotNull final PacketEvent event) {
        List<MerchantRecipe> recipes = new ArrayList<>();

        Bukkit.getLogger().info("BEFORE: " + packet.getMerchantRecipeLists().read(0).stream().map(MerchantRecipe::getResult).map(ItemStack::toString).collect(Collectors.joining(", ")));

        for (MerchantRecipe recipe : packet.getMerchantRecipeLists().read(0)) {
            MerchantRecipe newRecipe = new MerchantRecipe(
                    Display.displayAndFinalize(recipe.getResult()),
                    recipe.getUses(),
                    recipe.getMaxUses(),
                    recipe.hasExperienceReward(),
                    recipe.getVillagerExperience(),
                    recipe.getPriceMultiplier()
            );

            for (ItemStack ingredient : recipe.getIngredients()) {
                newRecipe.addIngredient(Display.displayAndFinalize(ingredient));
            }
            recipes.add(newRecipe);
        }

        packet.getMerchantRecipeLists().write(0, recipes);

        Bukkit.getLogger().info("AFTER: " + packet.getMerchantRecipeLists().read(0).stream().map(MerchantRecipe::getResult).map(ItemStack::toString).collect(Collectors.joining(", ")));

        ((EcoPlugin) this.getPlugin()).getScheduler().runLater(() -> {
            Bukkit.getLogger().info("SERVER: " + ((Villager) Bukkit.getEntity(((Villager) player.getOpenInventory().getTopInventory().getHolder()).getUniqueId()))
                    .getRecipes().stream().map(MerchantRecipe::getResult).map(ItemStack::toString).collect(Collectors.joining(", ")));
        }, 1);
    }

Produces this console output when opening a merchant recipe window:

[21:46:06 INFO]: BEFORE: ItemStack{ENCHANTED_BOOK x 1, ENCHANTED_META:{meta-type=ENCHANTED, stored-enchants={Cerebral=4}}}, ItemStack{BOOKSHELF x 1}
[21:46:06 INFO]: AFTER: ItemStack{ENCHANTED_BOOK x 1, ENCHANTED_META:{meta-type=ENCHANTED, lore=[{"extra":[{"text":"§z"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"gray","text":"Cerebral IV"}],"text":""}], ItemFlags=[HIDE_ENCHANTS, HIDE_POTION_EFFECTS], PublicBukkitValues={eco:finalized=1i}, stored-enchants={Cerebral=4}}}, ItemStack{BOOKSHELF x 1}
[21:46:06 INFO]: SERVER: ItemStack{ENCHANTED_BOOK x 1, ENCHANTED_META:{meta-type=ENCHANTED, lore=[{"extra":[{"text":"§z"},{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"gray","text":"Cerebral IV"}],"text":""}], ItemFlags=[HIDE_ENCHANTS, HIDE_POTION_EFFECTS], PublicBukkitValues={eco:finalized=1i}, stored-enchants={Cerebral=4}}}, ItemStack{BOOKSHELF x 1}

Ignoring internal things related to eco, ecoenchants, et cetera, the crucial detail is that the server-side item has been modified. This should not happen at all: there isn't any reason why the server-side item would be changed at all as only the packet itself has been modified.

commented

The same happens if a separate packet is sent with the new recipes: it seems that the packets are modifying the entity?

commented

Mistake on my end