• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / IntegratedDynamics / 20263245678

16 Dec 2025 09:36AM UTC coverage: 53.072% (-0.009%) from 53.081%
20263245678

push

github

rubensworks
Fix battery items not being modified through the energy capability

2856 of 8730 branches covered (32.71%)

Branch coverage included in aggregate %.

17406 of 29448 relevant lines covered (59.11%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.76
/src/main/java/org/cyclops/integrateddynamics/capability/energystorage/EnergyStorageItemBlockEnergyContainer.java
1
package org.cyclops.integrateddynamics.capability.energystorage;
2

3
import net.minecraft.world.item.ItemStack;
4
import net.minecraft.world.level.block.Block;
5
import net.neoforged.neoforge.transfer.access.ItemAccess;
6
import net.neoforged.neoforge.transfer.item.ItemResource;
7
import net.neoforged.neoforge.transfer.transaction.SnapshotJournal;
8
import net.neoforged.neoforge.transfer.transaction.TransactionContext;
9
import org.cyclops.cyclopscore.RegistryEntries;
10
import org.cyclops.cyclopscore.helper.IModHelpers;
11
import org.cyclops.integrateddynamics.block.BlockEnergyBatteryBase;
12
import org.cyclops.integrateddynamics.block.BlockEnergyBatteryConfig;
13
import org.cyclops.integrateddynamics.core.item.ItemBlockEnergyContainer;
14

15
import javax.annotation.Nullable;
16

17
/**
18
 * Energy Battery implementation for ItemBlock's.
19
 * @author rubensworks
20
 */
21
public class EnergyStorageItemBlockEnergyContainer implements IEnergyStorageCapacity, IEnergyStorageMutable {
22

23
    private final ItemBlockEnergyContainer itemBlockEnergyContainer;
24
    private final ItemStack itemStack;
25
    private final ItemAccess itemAccess;
26
    private final int rate;
27
    private final Journal journal;
28

29
    public EnergyStorageItemBlockEnergyContainer(ItemBlockEnergyContainer itemBlockEnergyContainer, ItemStack itemStack, ItemAccess itemAccess, int rate) {
2✔
30
        this.itemBlockEnergyContainer = itemBlockEnergyContainer;
3✔
31
        this.itemStack = itemStack;
3✔
32
        this.itemAccess = itemAccess;
3✔
33
        this.rate = rate;
3✔
34
        this.journal = new Journal();
6✔
35

36
        if (!this.itemStack.has(RegistryEntries.COMPONENT_ENERGY_STORAGE)) {
5✔
37
            setItemStackEnergy(itemStack, 0, null);
5✔
38
        }
39
    }
1✔
40

41
    public EnergyStorageItemBlockEnergyContainer(ItemBlockEnergyContainer itemBlockEnergyContainer, ItemStack itemStack, ItemAccess itemAccess) {
42
        this(itemBlockEnergyContainer, itemStack, itemAccess, Integer.MAX_VALUE);
6✔
43
    }
1✔
44

45
    public int getRate() {
46
        return rate;
×
47
    }
48

49
    public boolean isCreative() {
50
        Block block = itemBlockEnergyContainer.get();
4✔
51
        return block instanceof BlockEnergyBatteryBase && ((BlockEnergyBatteryBase) block).isCreative();
9!
52
    }
53

54
    protected int getEnergyStoredSingular() {
55
        if(isCreative()) return Integer.MAX_VALUE;
3!
56
        return itemStack.get(RegistryEntries.COMPONENT_ENERGY_STORAGE);
7✔
57
    }
58

59
    @Override
60
    public int getAmountAsInt() {
61
        return IModHelpers.get().getBaseHelpers().multiplySafe(getEnergyStoredSingular(), this.itemStack.getCount());
9✔
62
    }
63

64
    @Override
65
    public long getAmountAsLong() {
66
        return ((long) getEnergyStoredSingular()) * this.itemStack.getCount();
9✔
67
    }
68

69
    public int getMaxEnergyStoredSingular() {
70
        if(isCreative()) return Integer.MAX_VALUE;
3!
71
        if (!itemStack.has(RegistryEntries.COMPONENT_CAPACITY)) {
5✔
72
            return BlockEnergyBatteryConfig.capacity;
2✔
73
        }
74
        return itemStack.get(RegistryEntries.COMPONENT_CAPACITY);
7✔
75
    }
76

77
    @Override
78
    public int getCapacityAsInt() {
79
        return IModHelpers.get().getBaseHelpers().multiplySafe(getMaxEnergyStoredSingular(), this.itemStack.getCount());
9✔
80
    }
81

82
    @Override
83
    public long getCapacityAsLong() {
84
        return ((long) getMaxEnergyStoredSingular()) * this.itemStack.getCount();
9✔
85
    }
86

87
    @Override
88
    public int insert(int energy, TransactionContext transaction) {
89
        if(isCreative()) return 0;
3!
90
        int stackSize = this.itemStack.getCount();
4✔
91
        if (stackSize == 0) return 0;
2!
92
        energy /= stackSize;
4✔
93
        energy = Math.min(energy, getRate());
5✔
94
        int stored = getEnergyStoredSingular();
3✔
95
        int energyReceived = Math.min(getMaxEnergyStoredSingular() - stored, energy);
7✔
96
        this.journal.updateSnapshots(transaction);
4✔
97
        setItemStackEnergy(itemStack, stored + energyReceived, transaction);
8✔
98
        return energyReceived * stackSize;
4✔
99
    }
100

101
    @Override
102
    public int extract(int energy, TransactionContext transaction) {
103
        if(isCreative()) return energy;
×
104
        int stackSize = this.itemStack.getCount();
×
105
        if (stackSize == 0) return energy;
×
106
        energy /= stackSize;
×
107
        energy = Math.min(energy, getRate());
×
108
        int stored = getEnergyStoredSingular();
×
109
        int newEnergy = Math.max(stored - energy, 0);
×
110
        this.journal.updateSnapshots(transaction);
×
111
        setItemStackEnergy(itemStack, newEnergy, transaction);
×
112
        return (stored - newEnergy) * stackSize;
×
113
    }
114

115
    protected void setItemStackEnergy(ItemStack itemStack, int energy, @Nullable TransactionContext transaction) {
116
        if(isCreative()) return;
3!
117
        itemStack.set(RegistryEntries.COMPONENT_ENERGY_STORAGE, energy);
6✔
118
        if (transaction != null) {
2✔
119
            itemAccess.exchange(ItemResource.of(itemStack), itemAccess.getAmount(), transaction);
10✔
120
        }
121
    }
1✔
122

123
    @Override
124
    public void setCapacity(int capacity) {
125
        if (capacity == BlockEnergyBatteryConfig.capacity) {
3✔
126
            itemStack.remove(RegistryEntries.COMPONENT_CAPACITY);
6✔
127
        } else {
128
            itemStack.set(RegistryEntries.COMPONENT_CAPACITY, capacity);
7✔
129
        }
130
    }
1✔
131

132
    @Override
133
    public void setEnergy(int energy) {
134
        setItemStackEnergy(itemStack, energy, null);
6✔
135
    }
1✔
136

137
    public class Journal extends SnapshotJournal<Integer> {
6✔
138

139
        @Override
140
        protected Integer createSnapshot() {
141
            return getEnergyStoredSingular();
5✔
142
        }
143

144
        @Override
145
        protected void revertToSnapshot(Integer snapshot) {
146
            setItemStackEnergy(itemStack, snapshot, null);
×
147
        }
×
148
    }
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc