-
Notifications
You must be signed in to change notification settings - Fork 130
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
Implement more inventory mocks #1011
base: v1.20
Are you sure you want to change the base?
Conversation
…InventorySimulation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm mainly wondering if there could be better names for Resultable and Matrixable. I'm not the best at names here, but it's better to give the name a bit more context, MatrixableInventorySimulation maybe?
Also is this abstraction going to be used elsewhere than in WorkbenchSimulation? If not, then it's not really necesary
I can change these names, maybe to |
Let's avoid that |
items = new ItemStack[type.getDefaultSize()]; | ||
items = new ItemStack[inventoryType2StorageContentsSize(type)]; | ||
} | ||
|
||
private static int inventoryType2StorageContentsSize(InventoryType type) | ||
{ | ||
return switch (type) | ||
{ | ||
// workbench has 1 result slot | ||
case WORKBENCH -> type.getDefaultSize() - 1; | ||
default -> type.getDefaultSize(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to change this because using type.getDefaultSize()
would create arrays larger than necessary, as result slots are not in this array. This also caused a unit test to fail in InventoryMockTest
because there getSize()
is not implemented adequately.
for (int i = 0; i < items.length; i++) | ||
for (int i = 0; i < this.getSize(); i++) | ||
{ | ||
ItemStack oItem = items[i]; | ||
if (oItem == null) | ||
{ | ||
int toAdd = Math.min(item.getAmount(), itemMaxStackSize); | ||
items[i] = item.clone(); | ||
items[i].setAmount(toAdd); | ||
this.setItem(i, item); | ||
this.getItem(i).setAmount(toAdd); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wrong with my initial comment. It seems like adding to an inventory can in fact also add it to the result slot. In this case items.length==9
(matrix) and getSize()==10
(workbench mock overrides and accounts the result slot). Subsequently calling setItem
is necessary instead of directly assigning it to the array, because adding to the result slot has to be possible as well.
src/main/java/be/seeseemelk/mockbukkit/inventory/InventoryMock.java
Outdated
Show resolved
Hide resolved
src/main/java/be/seeseemelk/mockbukkit/inventory/InventoryMock.java
Outdated
Show resolved
Hide resolved
|
To summarize the weird implementation of inventories with result slots:
|
Going to postpone this until paper makes inventories more consistent (see here). |
See #1010 for more details.
Description
Barebone implementation for workbench inventory (tests and documentation missing).
My general idea is to use
items
inInventoryMock
as the "storage contents" of the inventory.That means this array contains every slot where methods likeWith this implementation this behaviour does not require any changes inaddItems
orremoveItems
have an effect. For example, adding (not setting) items to a workbench should only fill the crafting matrix but not the result slot.InventoryMock
, because from its point of view it only knows about the existence of the crafting matrix. Additional slots that have unique behaviour (as far as I know that only the result slot in different inventories), are handled individually in their respective mock implementation. This approach is somehwat similar to what Bukkit/Spigot/Paper does. They have a default "inventory" (rather its an abstraction layer above inventory) which for a crafting inventory would be the matrix. Then they have an additional "inventory" that represents the result slot.Checklist
The following items should be checked before the pull request can be merged.