Skip to content

MerchantRecipe is inconsistent when calling setters #10708

Description

@Machine-Maker

Expected behavior

When you get a MerchantRecipe from a villager and call one of the setters on it, either they should not be reflected without setting back the recipe to the villager, or all of the setters should be reflected. In other words, it should be consistent one way or another.

Observed/Actual behavior

Some of the setters mutate the actual nms MerchantOffer, while others only take effect after a "refresh" of the trades by actually making a trade.

Steps/models to reproduce

Use the code below in a test plugin

@EventHandler
public void onEvent(PlayerChatEvent event) {
    final Location loc = event.getPlayer().getTargetBlockExact(5).getLocation();
    final Villager villager = event.getPlayer().getWorld().spawn(loc.toCenterLocation().add(0, 1.5, 0), Villager.class);
    villager.setProfession(Villager.Profession.CLERIC);
    final MerchantRecipe recipe = new MerchantRecipe(new ItemStack(Material.DIAMOND, 64), 0, 100, false);
    recipe.addIngredient(new ItemStack(Material.STICK, 10));
    Bukkit.getServer().getScheduler().runTaskLater(this, () -> {
        villager.setMemory(MemoryKey.JOB_SITE, loc);
        villager.setRecipes(List.of(recipe));
    }, 50L);
}

@EventHandler
public void onEvent(PlayerInteractEvent event) {
    final ItemStack item = event.getItem();
    if (item != null && item.getType() == Material.STICK) {
        final Villager villager = event.getPlayer().getLocation().getNearbyEntitiesByType(Villager.class, 5).iterator().next();
        final MerchantRecipe recipe = villager.getRecipe(0);
        recipe.setExperienceReward(true);
        recipe.setIngredients(List.of(new ItemStack(Material.STICK, 5)));
    }
}
  1. Look at brewing stand and type in chat
  2. Wait a few seconds, then check that the villager only has 1 trade (10 sticks -> 54 diamonds). If it's not there, just try again.
  3. Get some sticks and make a trade. NOTE how there is no xp rewarded.
  4. hold a stick and hit the ground
  5. Open the trade screen again (notice how its still 10 sticks)
  6. Make another trade, it should only take 5 sticks because the act of interacting with the trade screen refreshes the changes made
  7. XP is rewarded indicating the trade was changed, but the visuals only change after you actually interact. If you close and re-open now, you should see the changed trade.

Plugin and Datapack List

just a test plugin

Paper version

f041f48

Other

No response

Activity

  1. added
    type: bug
    on May 12, 2024
  2. moved this to 🕑 Needs Triage in Issues: Bugson May 12, 2024
  3. added
    status: accepted
    and removed on May 12, 2024
  4. moved this from 🕑 Needs Triage to ✅ Accepted in Issues: Bugson May 12, 2024
  5. added
    version: 1.20.6
    on May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: acceptedtype: bugversion: 1.20.6

    Type

    Fields

    MC Issue

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions