diff options
author | Matej Dipčár <492666@mail.muni.cz> | 2022-09-22 03:04:28 +0200 |
---|---|---|
committer | Matej Dipčár <492666@mail.muni.cz> | 2022-09-23 02:15:55 +0200 |
commit | 9b4ca40de856f5cad609d44c0ec680b253f5a4bc (patch) | |
tree | 95ed200ed46700ebf2c4b6d7652f9d0bea5022b0 /src/main/java/gregtech/common/covers | |
parent | a579d5eefc4af9cdb6f83fdd3ab8e16ed569633b (diff) | |
download | GT5-Unofficial-9b4ca40de856f5cad609d44c0ec680b253f5a4bc.tar.gz GT5-Unofficial-9b4ca40de856f5cad609d44c0ec680b253f5a4bc.tar.bz2 GT5-Unofficial-9b4ca40de856f5cad609d44c0ec680b253f5a4bc.zip |
Abstract `computeSignalBasedOnItems`
Diffstat (limited to 'src/main/java/gregtech/common/covers')
-rw-r--r-- | src/main/java/gregtech/common/covers/GT_Cover_ItemMeter.java | 82 |
1 files changed, 38 insertions, 44 deletions
diff --git a/src/main/java/gregtech/common/covers/GT_Cover_ItemMeter.java b/src/main/java/gregtech/common/covers/GT_Cover_ItemMeter.java index 82dffaf7a8..953318c455 100644 --- a/src/main/java/gregtech/common/covers/GT_Cover_ItemMeter.java +++ b/src/main/java/gregtech/common/covers/GT_Cover_ItemMeter.java @@ -73,67 +73,61 @@ public class GT_Cover_ItemMeter extends GT_CoverBehaviorBase<GT_Cover_ItemMeter. byte aSide, int aCoverID, ItemMeterData aCoverVariable, ICoverable aTileEntity, long aTimer) { return false; } - - @Override - protected ItemMeterData doCoverThingsImpl( - byte aSide, - byte aInputRedstone, - int aCoverID, - ItemMeterData aCoverVariable, - ICoverable aTileEntity, - long aTimer) { - long tMax = 0; - long tUsed = 0; - IMetaTileEntity mte = ((IGregTechTileEntity) aTileEntity).getMetaTileEntity(); + + public static byte computeSignalBasedOnItems(ICoverable tileEntity, boolean inverted, int threshold, int slot, int side) { + long max = 0; + long used = 0; + IMetaTileEntity mte = ((IGregTechTileEntity) tileEntity).getMetaTileEntity(); if (mte instanceof GT_MetaTileEntity_DigitalChestBase) { GT_MetaTileEntity_DigitalChestBase dc = (GT_MetaTileEntity_DigitalChestBase) mte; - tMax = dc.getMaxItemCount(); // currently it is limited by int, but there is not much reason for that + max = dc.getMaxItemCount(); ItemStack[] inv = dc.getStoredItemData(); - if (inv != null && inv.length > 1 && inv[1] != null) tUsed = inv[1].stackSize; + if (inv != null && inv.length > 1 && inv[1] != null) used = inv[1].stackSize; } else if (GregTech_API.mAE2 && mte instanceof GT_MetaTileEntity_Hatch_OutputBus_ME) { if (((GT_MetaTileEntity_Hatch_OutputBus_ME) mte).isLastOutputFailed()) { - tMax = 64; - tUsed = 64; + max = 64; + used = 64; } } else { - int[] tSlots = aCoverVariable.slot >= 0 - ? new int[] {aCoverVariable.slot} - : aTileEntity.getAccessibleSlotsFromSide(aSide); - - for (int i : tSlots) { - if (i >= 0 && i < aTileEntity.getSizeInventory()) { - tMax += 64; - ItemStack tStack = aTileEntity.getStackInSlot(i); - if (tStack != null) tUsed += (tStack.stackSize << 6) / tStack.getMaxStackSize(); + int[] slots = slot >= 0 ? new int[] {slot} : tileEntity.getAccessibleSlotsFromSide(side); + + for (int i : slots) { + if (i >= 0 && i < tileEntity.getSizeInventory()) { + max += 64; + ItemStack stack = tileEntity.getStackInSlot(i); + if (stack != null) used += ((long) stack.stackSize << 6) / stack.getMaxStackSize(); } } } - long redstoneSignal; - if (tUsed == 0L) { - // nothing - redstoneSignal = 0; - } else if (tUsed >= tMax) { - // full - redstoneSignal = 15; - } else { - // 1-14 range - redstoneSignal = 1 + (14 * tUsed) / tMax; - } + byte signal = GT_Utility.convertRatioToRedstone(used, max); - if (aCoverVariable.inverted) { - redstoneSignal = 15 - redstoneSignal; + if (inverted) { + signal = (byte) (15 - signal); } - if (aCoverVariable.threshold > 0) { - if (aCoverVariable.inverted && tUsed >= aCoverVariable.threshold) { - redstoneSignal = 0; - } else if (!aCoverVariable.inverted && tUsed < aCoverVariable.threshold) { - redstoneSignal = 0; + if (threshold > 0) { + if (inverted && used >= threshold) { + return 0; + } else if (!inverted && used < threshold) { + return 0; } } + + return signal; + } + + @Override + protected ItemMeterData doCoverThingsImpl( + byte aSide, + byte aInputRedstone, + int aCoverID, + ItemMeterData aCoverVariable, + ICoverable aTileEntity, + long aTimer) { + byte signal = computeSignalBasedOnItems(aTileEntity, aCoverVariable.inverted, aCoverVariable.threshold, aCoverVariable.slot, aSide); + aTileEntity.setOutputRedstoneSignal(aSide, signal); - aTileEntity.setOutputRedstoneSignal(aSide, (byte) redstoneSignal); return aCoverVariable; } |