Skip to content

Commit

Permalink
Allow batteries to charge curios (#2569)
Browse files Browse the repository at this point in the history
Co-authored-by: jtuc <[email protected]>
  • Loading branch information
jtuc and jtuc authored Dec 17, 2024
1 parent a44a8ae commit cab145c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
.<IItemHandler>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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,6 +32,7 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.theillusivec4.curios.api.CuriosApi;

import java.util.List;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cab145c

Please sign in to comment.