Skip to content

Commit

Permalink
Fix energy dupe with powerpack attached to another piece of armor
Browse files Browse the repository at this point in the history
This does not happen on 1.20.1, so it seems to be caused by the cap rewrite introducing a copy somewhere.
  • Loading branch information
malte0811 committed Feb 13, 2024
1 parent de3707f commit b6f4289
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,16 @@ else if(event.getEntity().getEffect(IEPotions.CONCRETE_FEET.value())!=null)
@SubscribeEvent
public void onLivingUpdate(LivingTickEvent event)
{
if(event.getEntity() instanceof Player&&!event.getEntity().getItemBySlot(EquipmentSlot.CHEST).isEmpty()&&ItemNBTHelper.hasKey(event.getEntity().getItemBySlot(EquipmentSlot.CHEST), Lib.NBT_Powerpack))
LivingEntity entity = event.getEntity();
ItemStack chestplate = entity.getItemBySlot(EquipmentSlot.CHEST);
if(entity instanceof Player player&&!chestplate.isEmpty()&&ItemNBTHelper.hasKey(chestplate, Lib.NBT_Powerpack))
{
ItemStack powerpack = ItemNBTHelper.getItemStack(event.getEntity().getItemBySlot(EquipmentSlot.CHEST), Lib.NBT_Powerpack);
ItemStack powerpack = ItemNBTHelper.getItemStack(chestplate, Lib.NBT_Powerpack);
if(!powerpack.isEmpty())
powerpack.getItem().onArmorTick(powerpack, event.getEntity().getCommandSenderWorld(), (Player)event.getEntity());
{
PowerpackItem.tickWornPack(powerpack, entity.level(), player);
ItemNBTHelper.setItemStack(chestplate, Lib.NBT_Powerpack, powerpack);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
import blusunrize.immersiveengineering.common.gui.IESlot;
import blusunrize.immersiveengineering.common.items.ItemCapabilityRegistration.ItemCapabilityRegistrar;
import blusunrize.immersiveengineering.common.network.MessagePowerpackAntenna;
import blusunrize.immersiveengineering.common.register.IEBlocks;
import blusunrize.immersiveengineering.common.register.IEBlocks.MetalDevices;
import blusunrize.immersiveengineering.common.util.*;
import blusunrize.immersiveengineering.common.util.IESounds;
import blusunrize.immersiveengineering.common.util.ItemGetterList;
import blusunrize.immersiveengineering.common.util.ItemNBTHelper;
import blusunrize.immersiveengineering.common.util.Utils;
import com.google.common.base.Suppliers;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.stats.Stats;
Expand All @@ -39,6 +42,7 @@
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand All @@ -51,7 +55,6 @@
import net.neoforged.neoforge.capabilities.Capabilities.ItemHandler;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.IItemHandlerModifiable;
import net.neoforged.neoforge.network.PacketDistributor;

import javax.annotation.Nullable;
Expand All @@ -66,6 +69,7 @@
*/
public class PowerpackItem extends UpgradeableToolItem
{
public static final int CHEST_SLOT = Inventory.INVENTORY_SIZE+EquipmentSlot.CHEST.getIndex();
private static final Supplier<Map<Item, CapacitorConfig>> capacitorConfigMap = Suppliers.memoize(() -> {
Map<Item, CapacitorConfig> capacitorConfigMap = new HashMap<>();
capacitorConfigMap.put(MetalDevices.CAPACITOR_LV.asItem(), IEServerConfig.MACHINES.lvCapConfig);
Expand Down Expand Up @@ -131,19 +135,27 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
}

@Override
public void onArmorTick(ItemStack itemStack, Level world, Player player)
public void inventoryTick(ItemStack stack, Level world, Entity entity, int itemSlot, boolean isSelected)
{
// We'll just have to assume that's Curios which sets the slot of -1
if(entity instanceof Player player&&(itemSlot==CHEST_SLOT||itemSlot==-1))
tickWornPack(stack, world, player);
}

public static void tickWornPack(ItemStack itemStack, Level world, Player player)
{
IEnergyStorage packEnergy = itemStack.getCapability(EnergyStorage.ITEM);
if(packEnergy==null)
return;
CompoundTag upgrades = getUpgradesStatic(itemStack);
int energy = packEnergy.getEnergyStored();
if(energy > 0)
{
final int pre = energy;
for(EquipmentSlot slot : EquipmentSlot.values())
energy -= insertInto(player.getItemBySlot(slot), energy);
// induction charging only happens every 4 ticks
if(getUpgrades(itemStack).getBoolean("induction")&&player.tickCount%4==0)
if(upgrades.getBoolean("induction")&&player.tickCount%4==0)
{
NonNullList<ItemStack> allItems = player.getInventory().items;
final int selected = player.getInventory().selected;
Expand All @@ -159,13 +171,13 @@ public void onArmorTick(ItemStack itemStack, Level world, Player player)
if(pre!=energy)
packEnergy.extractEnergy(pre-energy, false);
}
if(getUpgrades(itemStack).getBoolean("antenna"))
if(upgrades.getBoolean("antenna"))
handleAntennaTick(itemStack, world, player);
if(getUpgrades(itemStack).getBoolean("magnet")&&energy >= MAGNET_CONSUMPTION)
if(upgrades.getBoolean("magnet")&&energy >= MAGNET_CONSUMPTION)
handleMagnetTick(itemStack, world, player);
}

private int insertInto(ItemStack insertInto, int maxAmount)
private static int insertInto(ItemStack insertInto, int maxAmount)
{
IEnergyStorage equippedEnergy = insertInto.getCapability(Capabilities.EnergyStorage.ITEM);
Item insertItem = insertInto.getItem();
Expand All @@ -175,7 +187,7 @@ private int insertInto(ItemStack insertInto, int maxAmount)
return 0;
}

private void handleAntennaTick(ItemStack itemStack, Level world, Player player)
private static void handleAntennaTick(ItemStack itemStack, Level world, Player player)
{
// attachment only works when grounded
boolean grounded = player.getRootVehicle().onGround();
Expand Down Expand Up @@ -261,7 +273,7 @@ private void handleAntennaTick(ItemStack itemStack, Level world, Player player)
}
}

private Optional<EnergyConnector> findBestSource(GlobalWireNetwork globalNetwork, Connection connection)
private static Optional<EnergyConnector> findBestSource(GlobalWireNetwork globalNetwork, Connection connection)
{
EnergyTransferHandler energyHandler = connection.getContainingNet(globalNetwork).getHandler(EnergyTransferHandler.ID, EnergyTransferHandler.class);
if(energyHandler==null)
Expand All @@ -271,7 +283,7 @@ private Optional<EnergyConnector> findBestSource(GlobalWireNetwork globalNetwork
.max(Comparator.comparingInt(EnergyConnector::getAvailableEnergy));
}

private void handleMagnetTick(ItemStack itemStack, Level world, Player player)
private static void handleMagnetTick(ItemStack itemStack, Level world, Player player)
{
if(world.isClientSide())
return;
Expand Down Expand Up @@ -306,25 +318,6 @@ private void handleMagnetTick(ItemStack itemStack, Level world, Player player)
}
}

@Override
public void inventoryTick(ItemStack stack, Level world, Entity entity, int itemSlot, boolean isSelected)
{
// Migration for older powerpacks before they had item storage
if(ItemNBTHelper.hasKey(stack, "energy"))
{
int previousEnergy = ItemNBTHelper.getInt(stack, "energy");
IItemHandler inv = Objects.requireNonNull(stack.getCapability(ItemHandler.ITEM));
ItemStack newCapacitor = new ItemStack(IEBlocks.MetalDevices.CAPACITOR_LV);
ItemNBTHelper.putInt(newCapacitor, EnergyHelper.ENERGY_KEY, previousEnergy);
((IItemHandlerModifiable)inv).setStackInSlot(0, newCapacitor);
ItemNBTHelper.remove(stack, "energy");
}

// We'll just have to assume that's Curios which sets the slot of -1
if(itemSlot==-1&&entity instanceof Player)
onArmorTick(stack, world, (Player)entity);
}

public static ItemStack getCapacitorStatic(ItemStack container)
{
IItemHandler cap = container.getCapability(ItemHandler.ITEM);
Expand Down

0 comments on commit b6f4289

Please sign in to comment.