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

CyclopsMC / IntegratedDynamics / 20210191346

14 Dec 2025 03:32PM UTC coverage: 19.514% (-33.5%) from 53.061%
20210191346

push

github

rubensworks
Remove deprecations

663 of 8728 branches covered (7.6%)

Branch coverage included in aggregate %.

6786 of 29445 relevant lines covered (23.05%)

1.09 hits per line

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

0.0
/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.transaction.SnapshotJournal;
7
import net.neoforged.neoforge.transfer.transaction.TransactionContext;
8
import org.cyclops.cyclopscore.RegistryEntries;
9
import org.cyclops.cyclopscore.helper.IModHelpers;
10
import org.cyclops.integrateddynamics.block.BlockEnergyBatteryBase;
11
import org.cyclops.integrateddynamics.block.BlockEnergyBatteryConfig;
12
import org.cyclops.integrateddynamics.core.item.ItemBlockEnergyContainer;
13

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

20
    private final ItemBlockEnergyContainer itemBlockEnergyContainer;
21
    private final ItemStack itemStack;
22
    private final ItemAccess itemAccess;
23
    private final int rate;
24
    private final Journal journal;
25

26
    public EnergyStorageItemBlockEnergyContainer(ItemBlockEnergyContainer itemBlockEnergyContainer, ItemStack itemStack, ItemAccess itemAccess, int rate) {
×
27
        this.itemBlockEnergyContainer = itemBlockEnergyContainer;
×
28
        this.itemStack = itemStack;
×
29
        this.itemAccess = itemAccess;
×
30
        this.rate = rate;
×
31
        this.journal = new Journal();
×
32

33
        if (!this.itemStack.has(RegistryEntries.COMPONENT_ENERGY_STORAGE)) {
×
34
            setItemStackEnergy(itemStack, 0);
×
35
        }
36
    }
×
37

38
    public EnergyStorageItemBlockEnergyContainer(ItemBlockEnergyContainer itemBlockEnergyContainer, ItemStack itemStack, ItemAccess itemAccess) {
39
        this(itemBlockEnergyContainer, itemStack, itemAccess, Integer.MAX_VALUE);
×
40
    }
×
41

42
    public int getRate() {
43
        return rate;
×
44
    }
45

46
    public boolean isCreative() {
47
        Block block = itemBlockEnergyContainer.get();
×
48
        return block instanceof BlockEnergyBatteryBase && ((BlockEnergyBatteryBase) block).isCreative();
×
49
    }
50

51
    protected int getEnergyStoredSingular() {
52
        if(isCreative()) return Integer.MAX_VALUE;
×
53
        return itemStack.get(RegistryEntries.COMPONENT_ENERGY_STORAGE);
×
54
    }
55

56
    @Override
57
    public int getAmountAsInt() {
58
        return IModHelpers.get().getBaseHelpers().multiplySafe(getEnergyStoredSingular(), this.itemStack.getCount());
×
59
    }
60

61
    @Override
62
    public long getAmountAsLong() {
63
        return ((long) getEnergyStoredSingular()) * this.itemStack.getCount();
×
64
    }
65

66
    public int getMaxEnergyStoredSingular() {
67
        if(isCreative()) return Integer.MAX_VALUE;
×
68
        if (!itemStack.has(RegistryEntries.COMPONENT_CAPACITY)) {
×
69
            return BlockEnergyBatteryConfig.capacity;
×
70
        }
71
        return itemStack.get(RegistryEntries.COMPONENT_CAPACITY);
×
72
    }
73

74
    @Override
75
    public int getCapacityAsInt() {
76
        return IModHelpers.get().getBaseHelpers().multiplySafe(getMaxEnergyStoredSingular(), this.itemStack.getCount());
×
77
    }
78

79
    @Override
80
    public long getCapacityAsLong() {
81
        return ((long) getMaxEnergyStoredSingular()) * this.itemStack.getCount();
×
82
    }
83

84
    @Override
85
    public int insert(int energy, TransactionContext transaction) {
86
        if(isCreative()) return 0;
×
87
        int stackSize = this.itemStack.getCount();
×
88
        if (stackSize == 0) return 0;
×
89
        energy /= stackSize;
×
90
        energy = Math.min(energy, getRate());
×
91
        int stored = getEnergyStoredSingular();
×
92
        int energyReceived = Math.min(getMaxEnergyStoredSingular() - stored, energy);
×
93
        this.journal.updateSnapshots(transaction);
×
94
        setItemStackEnergy(itemStack, stored + energyReceived);
×
95
        return energyReceived * stackSize;
×
96
    }
97

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

112
    protected void setItemStackEnergy(ItemStack itemStack, int energy) {
113
        if(isCreative()) return;
×
114
        itemStack.set(RegistryEntries.COMPONENT_ENERGY_STORAGE, energy);
×
115
    }
×
116

117
    @Override
118
    public void setCapacity(int capacity) {
119
        if (capacity == BlockEnergyBatteryConfig.capacity) {
×
120
            itemStack.remove(RegistryEntries.COMPONENT_CAPACITY);
×
121
        } else {
122
            itemStack.set(RegistryEntries.COMPONENT_CAPACITY, capacity);
×
123
        }
124
    }
×
125

126
    @Override
127
    public void setEnergy(int energy) {
128
        setItemStackEnergy(itemStack, energy);
×
129
    }
×
130

131
    public class Journal extends SnapshotJournal<Integer> {
×
132

133
        @Override
134
        protected Integer createSnapshot() {
135
            return getEnergyStoredSingular();
×
136
        }
137

138
        @Override
139
        protected void revertToSnapshot(Integer snapshot) {
140
            setItemStackEnergy(itemStack, snapshot);
×
141
        }
×
142
    }
143
}
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