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

fix: ME Bridge: Add support for child classes created by other mods #575

Merged
merged 9 commits into from
Apr 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import appeng.api.storage.AEKeyFilter;
import appeng.api.storage.IStorageProvider;
import appeng.api.storage.MEStorage;
import appeng.api.storage.cells.IBasicCellItem;
import appeng.blockentity.storage.DriveBlockEntity;
import appeng.items.storage.BasicStorageCell;
import appeng.parts.storagebus.StorageBusPart;
import com.the9grounds.aeadditions.item.storage.StorageCell;
import com.the9grounds.aeadditions.item.storage.SuperStorageCell;
import dan200.computercraft.shared.util.NBTUtil;
import de.srendi.advancedperipherals.AdvancedPeripherals;
Expand Down Expand Up @@ -310,11 +311,11 @@ public static boolean isFluidCrafting(MEStorage monitor, ICraftingService grid,
public static long getTotalItemStorage(IGridNode node) {
long total = 0;

Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
// note: do not query DriveBlockEntity.class specifically here, because it will avoid subclasses, e.g. the ME Extended Drive from ExtendedAE
Iterator<IGridNode> iterator = node.getGrid().getNodes().iterator();

while (iterator.hasNext()) {
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
if (entity == null)
if (!(iterator.next().getService(IStorageProvider.class) instanceof DriveBlockEntity entity))
continue;

InternalInventory inventory = entity.getInternalInventory();
Expand All @@ -325,7 +326,7 @@ public static long getTotalItemStorage(IGridNode node) {
if (stack.isEmpty())
continue;

if (stack.getItem() instanceof BasicStorageCell cell) {
if (stack.getItem() instanceof IBasicCellItem cell) {
if (cell.getKeyType().getClass().isAssignableFrom(AEKeyType.items().getClass())) {
total += cell.getBytes(null);
}
Expand All @@ -335,6 +336,10 @@ public static long getTotalItemStorage(IGridNode node) {
}
} else if (APAddons.aeAdditionsLoaded && (stack.getItem() instanceof SuperStorageCell superStorageCell)) {
total += superStorageCell.getKiloBytes() * 1024L;
} else if (APAddons.aeAdditionsLoaded && (stack.getItem() instanceof StorageCell storageCell)) {
if (storageCell.getKeyType() != AEKeyType.items())
continue;
total += storageCell.getKiloBytes() * 1024L;
}
}
}
Expand Down Expand Up @@ -362,11 +367,10 @@ public static long getTotalItemStorage(IGridNode node) {
public static long getTotalFluidStorage(IGridNode node) {
long total = 0;

Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
Iterator<IGridNode> iterator = node.getGrid().getNodes().iterator();

while (iterator.hasNext()) {
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
if (entity == null)
if (!(iterator.next().getService(IStorageProvider.class) instanceof DriveBlockEntity entity))
continue;

InternalInventory inventory = entity.getInternalInventory();
Expand All @@ -377,12 +381,16 @@ public static long getTotalFluidStorage(IGridNode node) {
if (stack.isEmpty())
continue;

if (stack.getItem() instanceof BasicStorageCell cell) {
if (stack.getItem() instanceof IBasicCellItem cell) {
if (cell.getKeyType().getClass().isAssignableFrom(AEKeyType.fluids().getClass())) {
total += cell.getBytes(null);
}
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof SuperStorageCell superStorageCell) {
total += superStorageCell.getKiloBytes() * 1024L;
} else if (APAddons.aeAdditionsLoaded && (stack.getItem() instanceof StorageCell storageCell)) {
if (storageCell.getKeyType() != AEKeyType.fluids())
continue;
total += storageCell.getKiloBytes() * 1024L;
}
}
}
Expand Down Expand Up @@ -410,11 +418,10 @@ public static long getTotalFluidStorage(IGridNode node) {
public static long getUsedItemStorage(IGridNode node) {
long used = 0;

Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
Iterator<IGridNode> iterator = node.getGrid().getNodes().iterator();

while (iterator.hasNext()) {
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
if (entity == null)
if (!(iterator.next().getService(IStorageProvider.class) instanceof DriveBlockEntity entity))
continue;

InternalInventory inventory = entity.getInternalInventory();
Expand All @@ -425,7 +432,7 @@ public static long getUsedItemStorage(IGridNode node) {
if (stack.isEmpty())
continue;

if (stack.getItem() instanceof BasicStorageCell cell) {
if (stack.getItem() instanceof IBasicCellItem cell) {
int bytesPerType = cell.getBytesPerType(null);

if (cell.getKeyType().getClass().isAssignableFrom(AEKeyType.items().getClass())) {
Expand All @@ -448,6 +455,14 @@ public static long getUsedItemStorage(IGridNode node) {
continue;
long numItemsInCell = stack.getTag().getLong("ic");

used += numItemsInCell;
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof StorageCell storageCell) {
if (storageCell.getKeyType() != AEKeyType.items())
continue;
if (stack.getTag() == null)
continue;
long numItemsInCell = stack.getTag().getLong("ic");

used += numItemsInCell;
}
}
Expand All @@ -472,19 +487,18 @@ public static long getUsedItemStorage(IGridNode node) {
public static long getUsedFluidStorage(IGridNode node) {
long used = 0;

Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
Iterator<IGridNode> iterator = node.getGrid().getNodes().iterator();

while (iterator.hasNext()) {
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
if (entity == null)
if (!(iterator.next().getService(IStorageProvider.class) instanceof DriveBlockEntity entity))
continue;

InternalInventory inventory = entity.getInternalInventory();

for (int i = 0; i < inventory.size(); i++) {
ItemStack stack = inventory.getStackInSlot(i);

if (stack.getItem() instanceof BasicStorageCell cell) {
if (stack.getItem() instanceof IBasicCellItem cell) {
int bytesPerType = cell.getBytesPerType(null);

if (cell.getKeyType().getClass().isAssignableFrom(AEKeyType.fluids().getClass())) {
Expand All @@ -495,7 +509,15 @@ public static long getUsedFluidStorage(IGridNode node) {

used += ((int) Math.ceil(((double) numBucketsInCell) / 8)) + ((long) bytesPerType * numOfType);
}
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof SuperStorageCell superStorageCell) {
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof SuperStorageCell) {
if (stack.getTag() == null)
continue;
long numItemsInCell = stack.getTag().getLong("ic");

used += numItemsInCell;
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof StorageCell storageCell) {
if (storageCell.getKeyType() != AEKeyType.fluids())
continue;
if (stack.getTag() == null)
continue;
long numItemsInCell = stack.getTag().getLong("ic");
Expand Down Expand Up @@ -548,7 +570,7 @@ public static List<Object> listCells(IGridNode node) {
if (stack.isEmpty())
continue;

if (stack.getItem() instanceof BasicStorageCell cell) {
if (stack.getItem() instanceof IBasicCellItem cell) {
items.add(getObjectFromCell(cell, stack));
} else if (APAddons.aeThingsLoaded && stack.getItem() instanceof DISKDrive disk) {
items.add(getObjectFromDisk(disk, stack));
Expand All @@ -561,7 +583,7 @@ public static List<Object> listCells(IGridNode node) {
return items;
}

private static Map<String, Object> getObjectFromCell(BasicStorageCell cell, ItemStack stack) {
private static Map<String, Object> getObjectFromCell(IBasicCellItem cell, ItemStack stack) {
Map<String, Object> map = new HashMap<>();

map.put("item", ItemUtil.getRegistryKey(stack.getItem()).toString());
Expand Down