PaperMC / PaperMC/Paper

Inconsistencies with inventories that have result slots that were created from different sources

Open
#10,720 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug version: 1.20.4
Dominant language
Java
Stars
12.7k
Forks
3.5k
Avg merge
3d 13h
Merged PRs (30d)
11

Description

Expected behavior

No matter how an inventory is created, it should behave identical regarding methods like isEmpty. For example, setting the result slot in an anvil should always cause isEmpty on that inventory to return false.

Observed/Actual behavior

Depending on how the inventory was created, these methods behave differently. Inventories that were created using open... (e.g. openAnvil) create an instance of CraftInventoryAnvil. This class (and every other subclass of CraftResultInventory) do not override getContents or getStorageContents and therefore isEmpty in CraftInventory do not work properly. Inventories that were created using createInventory always create inventories of type CraftCustomInventory which holds a single container for all slots in the inventory and does not hold a container specifically for the result. Therefore isEmpty checks all slots, including the result slot.

Steps/models to reproduce
Player player = Bukkit.getOnlinePlayers().stream().findAny().get();
Inventory anvilInv = Bukkit.createInventory(null, InventoryType.ANVIL);
anvilInv.setItem(2, new ItemStack(Material.STONE));
player.openInventory(anvilInv);


Bukkit.getScheduler().runTaskLater(this, () -> {
    System.out.println(anvilInv.isEmpty()); // false
    System.out.println(player.getOpenInventory().getTopInventory().isEmpty()); // false
    player.closeInventory();

    InventoryView view = player.openAnvil(player.getLocation(), true);
    view.getTopInventory().setItem(2, new ItemStack(Material.STONE));

    Bukkit.getScheduler().runTaskLater(this, () -> {
        System.out.println(view.getTopInventory().isEmpty()); // true
        System.out.println(player.getOpenInventory().getTopInventory().isEmpty()); // true
    }, 2L);
}, 2L);

Plugin and Datapack List

None

Paper version

This server is running Paper version git-Paper-496 (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT) (Git: 7ac24a1 on ver/1.20.4)
You are running the latest version
Previous version: git-Paper-398 (MC: 1.20.4)

Other

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing getContents, getStorageContents, and isEmpty across CraftInventoryAnvil, CraftResultInventory, CraftInventory, and CraftCustomInventory, using the reproduction in the issue to exercise openAnvil and createInventory. Done means an inventory with an item in its result slot reports false from isEmpty through both creation paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.