From cab145cf058d3f69b218c6ec845ed4b843da6c5f Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 17 Dec 2024 06:35:02 +0000 Subject: [PATCH] Allow batteries to charge curios (#2569) Co-authored-by: jtuc <6748724+jtuc@users.noreply.github.com> --- .../api/item/component/ElectricStats.java | 54 +++++++++++++------ .../gtceu/common/item/ItemMagnetBehavior.java | 24 ++++++--- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/item/component/ElectricStats.java b/src/main/java/com/gregtechceu/gtceu/api/item/component/ElectricStats.java index 743671d6c3..eff4f272f1 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/item/component/ElectricStats.java +++ b/src/main/java/com/gregtechceu/gtceu/api/item/component/ElectricStats.java @@ -1,5 +1,6 @@ package com.gregtechceu.gtceu.api.item.component; +import com.gregtechceu.gtceu.GTCEu; import com.gregtechceu.gtceu.api.GTValues; import com.gregtechceu.gtceu.api.capability.GTCapabilityHelper; import com.gregtechceu.gtceu.api.capability.IElectricItem; @@ -27,9 +28,13 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.energy.IEnergyStorage; +import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.wrapper.EmptyHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import top.theillusivec4.curios.api.CuriosApi; +import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler; import java.time.Duration; import java.time.Instant; @@ -97,33 +102,48 @@ public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotI if (!level.isClientSide && entity instanceof Player player && electricItem != null && electricItem.canProvideChargeExternally() && isInDischargeMode(stack) && electricItem.getCharge() > 0L) { - var inventoryPlayer = player.getInventory(); long transferLimit = electricItem.getTransferLimit(); - for (int i = 0; i < inventoryPlayer.getContainerSize(); i++) { - var itemInSlot = inventoryPlayer.getItem(i); - var slotElectricItem = GTCapabilityHelper.getElectricItem(itemInSlot); - if (slotElectricItem != null && !slotElectricItem.canProvideChargeExternally()) { - long chargedAmount = chargeElectricItem(transferLimit, electricItem, slotElectricItem); + if (GTCEu.isCuriosLoaded()) { + IItemHandler curios = CuriosApi.getCuriosInventory(player) + .map(ICuriosItemHandler::getEquippedCurios) + .orElse(EmptyHandler.INSTANCE); + for (int i = 0; i < curios.getSlots(); i++) { + var itemInSlot = curios.getStackInSlot(i); + long chargedAmount = chargeItemStack(transferLimit, electricItem, itemInSlot); if (chargedAmount > 0L) { transferLimit -= chargedAmount; - if (transferLimit == 0L) break; - } - } else if (ConfigHolder.INSTANCE.compat.energy.nativeEUToFE) { - var feEnergyItem = GTCapabilityHelper.getForgeEnergyItem(itemInSlot); - if (feEnergyItem != null && feEnergyItem.canReceive() && - feEnergyItem.getEnergyStored() < feEnergyItem.getMaxEnergyStored()) { - long chargedAmount = chargeForgeEnergyItem(transferLimit, electricItem, feEnergyItem); - if (chargedAmount > 0L) { - transferLimit -= chargedAmount; - if (transferLimit == 0L) break; - } } + if (transferLimit == 0L) break; } } + + var inventoryPlayer = player.getInventory(); + for (int i = 0; i < inventoryPlayer.getContainerSize(); i++) { + var itemInSlot = inventoryPlayer.getItem(i); + long chargedAmount = chargeItemStack(transferLimit, electricItem, itemInSlot); + if (chargedAmount > 0L) { + transferLimit -= chargedAmount; + } + if (transferLimit == 0L) break; + } } } + private static long chargeItemStack(long maxDischargeAmount, IElectricItem source, ItemStack target) { + var slotElectricItem = GTCapabilityHelper.getElectricItem(target); + if (slotElectricItem != null && !slotElectricItem.canProvideChargeExternally()) { + return chargeElectricItem(maxDischargeAmount, source, slotElectricItem); + } else if (ConfigHolder.INSTANCE.compat.energy.nativeEUToFE) { + var feEnergyItem = GTCapabilityHelper.getForgeEnergyItem(target); + if (feEnergyItem != null && feEnergyItem.canReceive() && + feEnergyItem.getEnergyStored() < feEnergyItem.getMaxEnergyStored()) { + return chargeForgeEnergyItem(maxDischargeAmount, source, feEnergyItem); + } + } + return 0; + } + private static long chargeElectricItem(long maxDischargeAmount, IElectricItem source, IElectricItem target) { long maxDischarged = source.discharge(maxDischargeAmount, source.getTier(), false, false, true); long maxReceived = target.charge(maxDischarged, source.getTier(), false, true); diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/ItemMagnetBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/ItemMagnetBehavior.java index 77cc797b4b..6ac7d511f2 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/ItemMagnetBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/ItemMagnetBehavior.java @@ -1,5 +1,6 @@ package com.gregtechceu.gtceu.common.item; +import com.gregtechceu.gtceu.GTCEu; import com.gregtechceu.gtceu.api.GTValues; import com.gregtechceu.gtceu.api.capability.GTCapabilityHelper; import com.gregtechceu.gtceu.api.capability.IElectricItem; @@ -31,6 +32,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import top.theillusivec4.curios.api.CuriosApi; import java.util.List; @@ -159,20 +161,26 @@ public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotI @SubscribeEvent public void onItemToss(@NotNull ItemTossEvent event) { if (event.getPlayer() == null) return; + if (hasMagnet(event.getPlayer())) { + event.getEntity().setPickUpDelay(60); + } + } - Inventory inventory = event.getPlayer().getInventory(); - // TODO work out curios compat - // if (Platform.isModLoaded(GTValues.MODID_CURIOS)) { - // inventory = BaublesModule.getBaublesWrappedInventory(event.getPlayer()); - // } - + private boolean hasMagnet(@NotNull Player player) { + Inventory inventory = player.getInventory(); for (int i = 0; i < inventory.getContainerSize(); i++) { ItemStack stackInSlot = inventory.getItem(i); if (isMagnet(stackInSlot) && isActive(stackInSlot)) { - event.getEntity().setPickUpDelay(60); - return; + return true; } } + + if (!GTCEu.isCuriosLoaded()) { + return false; + } + return CuriosApi.getCuriosInventory(player) + .map(curios -> curios.findFirstCurio(i -> isMagnet(i) && isActive(i)).isPresent()) + .orElse(false); } private boolean isMagnet(@NotNull ItemStack stack) {