Skip to content

Commit

Permalink
Allow GT++ charge pack to charge RF items (#3800)
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss authored Jan 13, 2025
1 parent 3524fdb commit fd4f1db
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 112 deletions.
176 changes: 68 additions & 108 deletions src/main/java/gtPlusPlus/core/item/bauble/BatteryPackBaseBauble.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import net.minecraft.world.World;

import baubles.api.BaubleType;
import cofh.api.energy.IEnergyContainerItem;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.enums.GTValues;
import gregtech.common.config.OPStuff;
import gtPlusPlus.core.creative.AddToCreativeTab;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.xmod.gregtech.common.helpers.ChargingHelper;
import ic2.api.item.ElectricItem;
import ic2.api.item.IElectricItem;
Expand All @@ -39,7 +40,7 @@ public BatteryPackBaseBauble(int tier) {

@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List itemList) {
public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List<ItemStack> itemList) {
ItemStack itemStack = new ItemStack(this, 1);
ItemStack charged;
if (this.getEmptyItem(itemStack) == this) {
Expand All @@ -60,31 +61,14 @@ public void onUpdate(final ItemStack itemStack, final World worldObj, final Enti
super.onUpdate(itemStack, worldObj, player, p_77663_4_, p_77663_5_);
}

@Override
public boolean canProvideEnergy(final ItemStack itemStack) {
double aItemCharge = ElectricItem.manager.getCharge(itemStack);
return aItemCharge > 0;
}

@Override
public String getItemStackDisplayName(final ItemStack p_77653_1_) {
return (EnumChatFormatting.BLUE + super.getItemStackDisplayName(p_77653_1_) + EnumChatFormatting.GRAY);
}

@Override
public boolean showDurabilityBar(final ItemStack stack) {
return true;
}

public int secondsLeft(final ItemStack stack) {
double r = 0;
r = this.getCharge(stack) / (10000 * 20);
return (int) MathUtils.decimalRounding(r);
}

@SuppressWarnings("unchecked")
@Override
public void addInformation(final ItemStack stack, final EntityPlayer aPlayer, final List list, final boolean bool) {
public void addInformation(final ItemStack stack, final EntityPlayer aPlayer, final List<String> list,
final boolean bool) {
list.add("");
String aString1 = StatCollector.translateToLocal("GTPP.battpack.tooltip.1");
String aString2 = StatCollector.translateToLocal("GTPP.battpack.tooltip.2");
Expand Down Expand Up @@ -122,110 +106,86 @@ public boolean canUnequip(final ItemStack arg0, final EntityLivingBase arg1) {
return true;
}

@Override // TODO
@Override
public void onEquipped(final ItemStack arg0, final EntityLivingBase arg1) {}

@Override // TODO
@Override
public void onUnequipped(final ItemStack arg0, final EntityLivingBase arg1) {}

@Override // TODO
public void onWornTick(final ItemStack aBaubleStack, final EntityLivingBase aPlayer) {
if (!aPlayer.worldObj.isRemote) {

@Override
public void onWornTick(final ItemStack aBaubleStack, final EntityLivingBase aLivingBase) {
if (!aLivingBase.worldObj.isRemote) {
try {

if (this.getCharge(aBaubleStack) >= getTransferLimit(aBaubleStack)) {
// Try To Iterate Armour Slots of Player
if (aPlayer instanceof EntityPlayer) {

// amour
for (final ItemStack aInvStack : ((EntityPlayer) aPlayer).inventory.armorInventory) {
if (aInvStack != null) {
if (aInvStack == aBaubleStack) {
continue;
}
if (ChargingHelper.isItemValid(aInvStack)) {
double aTransferRate;
final IElectricItem electricItem = (IElectricItem) aInvStack.getItem();
if (electricItem != null) {
aTransferRate = electricItem.getTransferLimit(aInvStack);
double aItemCharge = ElectricItem.manager.getCharge(aInvStack);
if (aItemCharge >= 0 && aItemCharge != electricItem.getMaxCharge(aInvStack)) {
if (aItemCharge <= (electricItem.getMaxCharge(aInvStack) - aTransferRate)) {
if (ElectricItem.manager.getCharge(aBaubleStack) >= aTransferRate) {
if (ElectricItem.manager.getCharge(aInvStack)
<= (electricItem.getMaxCharge(aInvStack) - aTransferRate)) {
double d = ElectricItem.manager
.charge(aInvStack, aTransferRate * 16, mTier, false, true);
if (d > 0) {
d = ElectricItem.manager.charge(
aInvStack,
aTransferRate * 16,
mTier,
false,
false);
ElectricItem.manager
.discharge(aBaubleStack, d, mTier, false, true, false);
}
}
}
}

}
if (aLivingBase instanceof EntityPlayer aPlayer) {

}
}
}
if (!(this.getCharge(aBaubleStack) > 0)) {
break;
}
}
// Armor
ItemStack[] inv = aPlayer.inventory.armorInventory;
chargeInventory(inv, inv.length, aBaubleStack);

// Hotbar Slots
int aSlotCounter = 0;
for (final ItemStack aInvStack : ((EntityPlayer) aPlayer).inventory.mainInventory) {
if (aSlotCounter > (InventoryPlayer.getHotbarSize() - 1)) {
break;
}
aSlotCounter++;
if (aInvStack != null) {
if (aInvStack == aBaubleStack) {
continue;
}
if (ChargingHelper.isItemValid(aInvStack)) {
double aTransferRate = 0;
final IElectricItem electricItem = (IElectricItem) aInvStack.getItem();
if (electricItem != null) {
aTransferRate = electricItem.getTransferLimit(aInvStack);
double aItemCharge = ElectricItem.manager.getCharge(aInvStack);
if (aItemCharge >= 0 && aItemCharge != electricItem.getMaxCharge(aInvStack)) {
if (aItemCharge <= (electricItem.getMaxCharge(aInvStack) - aTransferRate)) {
if (ElectricItem.manager.getCharge(aBaubleStack) >= aTransferRate) {
if (ElectricItem.manager.getCharge(aInvStack)
<= (electricItem.getMaxCharge(aInvStack) - aTransferRate)) {
double d = ElectricItem.manager
.charge(aInvStack, aTransferRate, mTier, false, true);
if (d > 0) {
d = ElectricItem.manager
.charge(aInvStack, aTransferRate, mTier, false, false);
ElectricItem.manager
.discharge(aBaubleStack, d, mTier, false, true, false);
}
}
}
}
inv = aPlayer.inventory.mainInventory;
chargeInventory(inv, InventoryPlayer.getHotbarSize(), aBaubleStack);
}
}
} catch (Throwable ignored) {}
}
}

private void chargeInventory(ItemStack[] inventory, int endIndex, ItemStack aBaubleStack) {
for (int i = 0; i < endIndex && i < inventory.length; i++) {
ItemStack aInvStack = inventory[i];
if (aInvStack != null && aInvStack != aBaubleStack) {
Item aItem = aInvStack.getItem();

if (ChargingHelper.isItemValid(aInvStack)) {
// IElectricItem (EU)
final IElectricItem electricItem = (IElectricItem) aItem;
if (electricItem != null) {
double aTransferRate = electricItem.getTransferLimit(aInvStack);
double aItemCharge = ElectricItem.manager.getCharge(aInvStack);
if (aItemCharge >= 0 && aItemCharge != electricItem.getMaxCharge(aInvStack)) {
if (aItemCharge <= (electricItem.getMaxCharge(aInvStack) - aTransferRate)) {
if (ElectricItem.manager.getCharge(aBaubleStack) >= aTransferRate) {
if (ElectricItem.manager.getCharge(aInvStack)
<= (electricItem.getMaxCharge(aInvStack) - aTransferRate)) {
double d = ElectricItem.manager
.charge(aInvStack, aTransferRate * 16, mTier, false, true);
if (d > 0) {
d = ElectricItem.manager
.charge(aInvStack, aTransferRate * 16, mTier, false, false);
ElectricItem.manager.discharge(aBaubleStack, d, mTier, false, true, false);
}
}
}
}
if (!(this.getCharge(aBaubleStack) > 0)) {
break;
}
}
}
} else if (OPStuff.outputRF) {
// IEnergyContainerItem (RF)
if (aItem instanceof IEnergyContainerItem energyItem) {
int aItemCharge = energyItem.getEnergyStored(aInvStack);
int aItemMaxCharge = energyItem.getMaxEnergyStored(aInvStack);
double aBaubleCharge = ElectricItem.manager.getCharge(aBaubleStack);
if (aItemCharge >= 0 && aItemCharge < aItemMaxCharge && aBaubleCharge > 0) {
int aMaxChargeAmount = aItemMaxCharge - aItemCharge;
double aMaxChargeAmountInEU = Math
.ceil(aMaxChargeAmount * 100.0D / OPStuff.howMuchRFWith100EUInInput);
double aActualChargeInEU = Math.min(aBaubleCharge, aMaxChargeAmountInEU);
int aActualCharge = (int) Math
.floor(aActualChargeInEU * OPStuff.howMuchRFWith100EUInInput / 100);

int aCharged = energyItem.receiveEnergy(aInvStack, aActualCharge, false);
double aDischarge = Math.ceil(aCharged * 100.0D / OPStuff.howMuchRFWith100EUInInput);
ElectricItem.manager.discharge(aBaubleStack, aDischarge, mTier, false, true, false);
}
}
}
} catch (Throwable ignored) {}
}

if (this.getCharge(aBaubleStack) <= 0) {
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public ElectricBaseBauble(BaubleType aType, int aTier, double aMaxEU, String aUn

public abstract String getTextureNameForBauble();

@SuppressWarnings("unchecked")
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List itemList) {
public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List<ItemStack> itemList) {
ItemStack itemStack = new ItemStack(this, 1);
ItemStack charged;
if (this.getEmptyItem(itemStack) == this) {
Expand Down Expand Up @@ -125,9 +124,9 @@ public boolean showDurabilityBar(final ItemStack stack) {
return true;
}

@SuppressWarnings("unchecked")
@Override
public void addInformation(final ItemStack stack, final EntityPlayer aPlayer, final List list, final boolean bool) {
public void addInformation(final ItemStack stack, final EntityPlayer aPlayer, final List<String> list,
final boolean bool) {
list.add("");
String aEuInfo = StatCollector.translateToLocal("GTPP.info.euInfo");
String aTier = StatCollector.translateToLocal("GTPP.machines.tier");
Expand Down

0 comments on commit fd4f1db

Please sign in to comment.