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

Allow Batteries to charge FE items #1083

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/gregtech/api/items/metaitem/ElectricStats.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gregtech.api.items.metaitem;

import gregtech.api.GTValues;
import gregtech.api.capability.FeCompat;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.IElectricItem;
import gregtech.api.capability.impl.ElectricItem;
import gregtech.api.items.metaitem.stats.IItemBehaviour;
import gregtech.api.items.metaitem.stats.IItemCapabilityProvider;
import gregtech.api.items.metaitem.stats.IItemComponent;
import gregtech.api.items.metaitem.stats.IItemMaxStackSizeProvider;
import gregtech.common.ConfigHolder;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -20,6 +22,8 @@
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;

import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -72,6 +76,7 @@ public void onUpdate(ItemStack itemStack, Entity entity) {
for (int i = 0; i < inventoryPlayer.getSizeInventory(); i++) {
ItemStack itemInSlot = inventoryPlayer.getStackInSlot(i);
IElectricItem slotElectricItem = itemInSlot.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
IEnergyStorage feEnergyItem = itemInSlot.getCapability(CapabilityEnergy.ENERGY, null);
if (slotElectricItem != null && !slotElectricItem.canProvideChargeExternally()) {

long chargedAmount = chargeElectricItem(transferLimit, electricItem, slotElectricItem);
Expand All @@ -80,6 +85,16 @@ public void onUpdate(ItemStack itemStack, Entity entity) {
if (transferLimit == 0L) break;
}
}
else if(ConfigHolder.compat.energy.nativeEUToFE && feEnergyItem != null) {
if(feEnergyItem.getEnergyStored() < feEnergyItem.getMaxEnergyStored()) {
int energyMissing = feEnergyItem.getMaxEnergyStored() - feEnergyItem.getEnergyStored();
long euToCharge = FeCompat.toEu(energyMissing, ConfigHolder.compat.energy.feToEuRatio);
long energyToTransfer = Math.min(euToCharge, transferLimit);
long maxDischargeAmount = Math.min(energyToTransfer, electricItem.discharge(energyToTransfer, electricItem.getTier(), false, true, true));
FeCompat.insertEu(feEnergyItem, maxDischargeAmount);
electricItem.discharge(maxDischargeAmount, electricItem.getTier(), false, true, false);
}
}
}
}
}
Expand Down