Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory#getItem not working when it is an inventory with a result slot #981

Open
2 tasks done
fhnaumann opened this issue Mar 4, 2024 · 3 comments
Open
2 tasks done

Comments

@fhnaumann
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Are you using the latest version of MockBukkit?

  • I am using the latest version of MockBukkit.

Minecraft Version

1.20

Describe the bug

I am testing player click behaviour and noticed, that the clicked slot in a WorkbenchInventoryMock does not behave as expected. I am trying to verify the result of the crafting table by checking index 0 in the contents of the inventory. In the current implementation the item at the underlying index from the array is returned. Now the array is filled only by the matrix, the result slot is ignored and instead has its own field in the class.

Reproducible Test

    @Test
    public void testInventoryResultClick() {
        Inventory inventory = stickCrafting(1);
        player.openInventory(inventory);
        player.simulateInventoryClick(player.getOpenInventory(), ClickType.LEFT, 0);
        assertEquals(new ItemStack(Material.STICK, 4), ((CraftingInventory) inventory).getResult()); // passes
        assertEquals(new ItemStack(Material.STICK, 4), inventory.getItem(0)); // fails
    }

    private static Inventory stickCrafting(int amountPerPlank) {
        WorkbenchInventoryMock workbenchInventoryMock = new WorkbenchInventoryMock(null);
        workbenchInventoryMock.setMatrix(new ItemStack[]{
                null, null, null,
                null, plank(amountPerPlank), null,
                null, plank(amountPerPlank), null
        });
        workbenchInventoryMock.setResult(new ItemStack(Material.STICK, Math.min(amountPerPlank*4, 64)));
        return workbenchInventoryMock;
    }

    private static ItemStack plank(int amount) {
        return new ItemStack(Material.OAK_PLANKS, amount);
    }

Anything else?

I know that the test itself is easily fixable by just casting it to CraftingInventory and then accessing the result. However, I am using the CraftingInventory in parameterized tests and I would prefer all inventories behaving similar.
A potential fix could be by overriding Inventory#getItem in WorkbenchInventoryMock and checking if the index is 0. If so, then return the result variable. If not, then return the ItemStack from the underlying array at index-1.

@fhnaumann
Copy link
Contributor Author

Currently I don't know how to simulate a player clicking in the result slot.

@fhnaumann
Copy link
Contributor Author

I could try to implement this behaviour along with support for the SlotType enum to add more functionality to the simulateInventoryClick methods. Potentially in the structure of #972 ?

@Thorinwasher
Copy link
Contributor

Thorinwasher commented May 2, 2024

I could try to implement this behaviour along with support for the SlotType enum to add more functionality to the simulateInventoryClick methods. Potentially in the structure of #972 ?

I failed to notice this issue (we should have an alert on issus opening in our discord). Anyhow, yes it's probably better to follow the same structure of #972 , but I would think that you should make the pull request to the 3.0 branch (not for 4.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants