Search in sources :

Example 1 with LootableContainerBlockEntity

use of net.minecraft.block.entity.LootableContainerBlockEntity in project lithium-fabric by CaffeineMC.

the class HopperHelper method determineComparatorUpdatePattern.

public static ComparatorUpdatePattern determineComparatorUpdatePattern(Inventory from, LithiumStackList fromStackList) {
    if ((from instanceof HopperBlockEntity) || !(from instanceof LootableContainerBlockEntity)) {
        return ComparatorUpdatePattern.NO_UPDATE;
    }
    // calculate the signal strength of the inventory, but also keep the content weight variable
    float contentWeight = 0f;
    int numOccupiedSlots = 0;
    for (int j = 0; j < from.size(); ++j) {
        ItemStack itemStack = from.getStack(j);
        if (!itemStack.isEmpty()) {
            int maxStackSize = Math.min(from.getMaxCountPerStack(), itemStack.getMaxCount());
            contentWeight += itemStack.getCount() / (float) maxStackSize;
            ++numOccupiedSlots;
        }
    }
    float f = contentWeight;
    f /= (float) from.size();
    int originalSignalStrength = MathHelper.floor(f * 14.0F) + (numOccupiedSlots > 0 ? 1 : 0);
    ComparatorUpdatePattern updatePattern = ComparatorUpdatePattern.NO_UPDATE;
    // check the signal strength change when failing to extract from each slot
    int[] availableSlots = from instanceof SidedInventory ? ((SidedInventory) from).getAvailableSlots(Direction.DOWN) : null;
    SidedInventory sidedInventory = from instanceof SidedInventory ? (SidedInventory) from : null;
    int fromSize = availableSlots != null ? availableSlots.length : from.size();
    for (int i = 0; i < fromSize; i++) {
        int fromSlot = availableSlots != null ? availableSlots[i] : i;
        ItemStack itemStack = fromStackList.get(fromSlot);
        if (!itemStack.isEmpty() && (sidedInventory == null || sidedInventory.canExtract(fromSlot, itemStack, Direction.DOWN))) {
            // contentWeight adaption can including rounding error for non power of 2 max stack sizes, which do not exist in vanilla anyways
            int maxStackSize = Math.min(from.getMaxCountPerStack(), itemStack.getMaxCount());
            int newNumOccupiedSlots = numOccupiedSlots - (itemStack.getCount() == 1 ? 1 : 0);
            float g = contentWeight - (1f / (float) maxStackSize);
            g /= (float) from.size();
            int newSignalStrength = MathHelper.floor(g * 14.0F) + (newNumOccupiedSlots > 0 ? 1 : 0);
            if (newSignalStrength != originalSignalStrength) {
                updatePattern = updatePattern.thenDecrementUpdateIncrementUpdate();
            } else {
                updatePattern = updatePattern.thenUpdate();
            }
            if (!updatePattern.isChainable()) {
                // if the pattern is indistinguishable from all extensions of the pattern, stop iterating
                break;
            }
        }
    }
    return updatePattern;
}
Also used : HopperBlockEntity(net.minecraft.block.entity.HopperBlockEntity) SidedInventory(net.minecraft.inventory.SidedInventory) LootableContainerBlockEntity(net.minecraft.block.entity.LootableContainerBlockEntity) ItemStack(net.minecraft.item.ItemStack)

Aggregations

HopperBlockEntity (net.minecraft.block.entity.HopperBlockEntity)1 LootableContainerBlockEntity (net.minecraft.block.entity.LootableContainerBlockEntity)1 SidedInventory (net.minecraft.inventory.SidedInventory)1 ItemStack (net.minecraft.item.ItemStack)1