Creators-of-Create / Create

[Forge Mod] Building Tools and Aesthetic Technology
MIT License
2.65k stars 860 forks source link

Fix wrong slot limit reading on threshold switches #6492

Open hubmelco opened 2 weeks ago

hubmelco commented 2 weeks ago

Adds compatibility with sophisticated storage entities (chests, barrels, limited barrels) by fixing the threshold switch capacity limit reading.

Images

Setup

image

Sophisticated Storage Chest Inventory with 2x Upgrade

Pre-Fix

image

Post-Fix

image

Explanation

The line below is bad because it doesn't account for having space for multiple stacks in one slot from other mods such as sophisticated storage int space = Math.min(stackInSlot.getMaxStackSize(), inv.getSlotLimit(slot)); i.e. take the minimum of [ (1,16, or 64), 64 or greater]. When written like this, its clear that the item stack size is always chosen regardless of what the slot limit is.

From my understanding of the code: stackInSlot.getMaxStackSize() -> current item max stack size (typically 64, 16, or 1) inv.getSlotLimit(slot) -> inventory slot max number of items (typically 64, can be increased by mods) Added in: VANILLA_SLOT_LIMIT -> always 64

Replacement code

int space = inv.getSlotLimit(slot) / (VANILLA_SLOT_LIMIT / stackInSlot.getMaxStackSize());

(VANILLA_SLOT_LIMIT / stackInSlot.getMaxStackSize()) -> Conversion factor for changing the slot limit into the correct item stack amount

Math

ASSUMPTIONS: stackInSlot.getMaxStackSize() is never 0 as that item should never exist in inventories because you can't have > 0 of it (could result in division by 0 error)

Vanilla

Unstackable (1 item stack) = 64 / (64/1) -> 1 item limit Snowballs (16 item stack) = 64 / (64/16) -> 16 item limit Regular (64) = 64 / (64/64) -> 64 item limit

Bigger Slot Limits

Unstackable = 128 / (64/1) -> 2 item limit Snowballs = 128 / (64/16) -> 32 item limit Regular = 128 / (64/64) -> 128 item limit