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

CyclopsMC / IntegratedDynamics / 22181387183

19 Feb 2026 12:15PM UTC coverage: 46.054% (-0.1%) from 46.172%
22181387183

push

github

rubensworks
Tweak alert-threshold in performance workflow

2829 of 8848 branches covered (31.97%)

Branch coverage included in aggregate %.

12377 of 24170 relevant lines covered (51.21%)

2.44 hits per line

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

57.14
/src/main/java/org/cyclops/integrateddynamics/blockentity/BlockEntityDelay.java
1
package org.cyclops.integrateddynamics.blockentity;
2

3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Queues;
5
import lombok.Getter;
6
import lombok.Setter;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.HolderLookup;
9
import net.minecraft.nbt.CompoundTag;
10
import net.minecraft.nbt.ListTag;
11
import net.minecraft.nbt.Tag;
12
import net.minecraft.network.chat.Component;
13
import net.minecraft.world.MenuProvider;
14
import net.minecraft.world.entity.player.Inventory;
15
import net.minecraft.world.entity.player.Player;
16
import net.minecraft.world.inventory.AbstractContainerMenu;
17
import net.minecraft.world.item.ItemStack;
18
import net.minecraft.world.level.Level;
19
import net.minecraft.world.level.block.entity.BlockEntityType;
20
import net.minecraft.world.level.block.state.BlockState;
21
import org.cyclops.cyclopscore.capability.item.ItemHandlerSlotMasked;
22
import org.cyclops.cyclopscore.datastructure.DimPos;
23
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;
24
import org.cyclops.integrateddynamics.Capabilities;
25
import org.cyclops.integrateddynamics.IntegratedDynamics;
26
import org.cyclops.integrateddynamics.RegistryEntries;
27
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
28
import org.cyclops.integrateddynamics.api.evaluate.expression.VariableAdapter;
29
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
30
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
31
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
32
import org.cyclops.integrateddynamics.api.item.IDelayVariableFacade;
33
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
34
import org.cyclops.integrateddynamics.api.network.INetworkElement;
35
import org.cyclops.integrateddynamics.api.network.INetworkElementProvider;
36
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
37
import org.cyclops.integrateddynamics.capability.networkelementprovider.NetworkElementProviderSingleton;
38
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityActiveVariableBase;
39
import org.cyclops.integrateddynamics.core.evaluate.DelayVariableFacadeHandler;
40
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers;
41
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeList;
42
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
43
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
44
import org.cyclops.integrateddynamics.core.item.DelayVariableFacade;
45
import org.cyclops.integrateddynamics.inventory.container.ContainerDelay;
46
import org.cyclops.integrateddynamics.network.DelayNetworkElement;
47

48
import javax.annotation.Nullable;
49
import java.util.Optional;
50
import java.util.Queue;
51
import java.util.function.Supplier;
52

53
/**
54
 * A part entity for the variable delay.
55
 *
56
 * @author rubensworks
57
 */
58
public class BlockEntityDelay extends BlockEntityProxy implements MenuProvider {
59

60
    public static final int INVENTORY_SIZE = 3;
61

62
    protected Queue<IValue> values = null;
3✔
63
    @NBTPersist
3✔
64
    @Getter
×
65
    private int capacity = 5;
66
    @NBTPersist
3✔
67
    @Getter
×
68
    @Setter
×
69
    private int updateInterval = 1;
70
    private ValueTypeList.ValueList list = ValueTypes.LIST.getDefault();
4✔
71
    private final IVariable<?> variable;
72

73
    @Setter
3✔
74
    private Player lastPlayer = null;
75
    private EvaluationException lastError = null;
3✔
76

77
    public BlockEntityDelay(BlockPos blockPos, BlockState blockState) {
78
        super(RegistryEntries.BLOCK_ENTITY_DELAY.get(), blockPos, blockState, BlockEntityDelay.INVENTORY_SIZE);
8✔
79
        this.variable = new VariableAdapter<ValueTypeList.ValueList>() {
12✔
80

81
            @Override
82
            public ValueTypeList getType() {
83
                return ValueTypes.LIST;
2✔
84
            }
85

86
            @Override
87
            public ValueTypeList.ValueList getValue() throws EvaluationException {
88
                if (lastError != null) {
4!
89
                    throw lastError;
×
90
                }
91
                return list;
4✔
92
            }
93
        };
94
    }
1✔
95

96
    public static class CapabilityRegistrar extends BlockEntityActiveVariableBase.CapabilityRegistrar<BlockEntityDelay> {
97
        public CapabilityRegistrar(Supplier<BlockEntityType<? extends BlockEntityDelay>> blockEntityType) {
98
            super(blockEntityType);
3✔
99
        }
1✔
100

101
        @Override
102
        public void populate() {
103
            super.populate();
2✔
104

105
            add(
4✔
106
                    net.neoforged.neoforge.capabilities.Capabilities.ItemHandler.BLOCK,
107
                    (blockEntity, direction) -> {
108
                        int slot = -1;
×
109
                        if (direction != null) {
×
110
                            switch (direction) {
×
111
                                case DOWN -> slot = SLOT_WRITE_OUT;
×
112
                                case UP -> slot = SLOT_WRITE_IN;
×
113
                                case NORTH -> slot = SLOT_READ;
×
114
                                case SOUTH -> slot = SLOT_READ;
×
115
                                case WEST -> slot = SLOT_READ;
×
116
                                case EAST -> slot = SLOT_READ;
×
117
                            }
118
                        } else {
119
                            slot = SLOT_READ;
×
120
                        }
121
                        return new ItemHandlerSlotMasked(blockEntity.getInventory(), slot);
×
122
                    }
123
            );
124
            add(
4✔
125
                    Capabilities.NetworkElementProvider.BLOCK,
126
                    (blockEntity, direction) -> blockEntity.getNetworkElementProvider()
3✔
127
            );
128
        }
1✔
129
    }
130

131
    @Override
132
    public INetworkElementProvider getNetworkElementProvider() {
133
        return new NetworkElementProviderSingleton() {
8✔
134
            @Override
135
            public INetworkElement createNetworkElement(Level world, BlockPos blockPos) {
136
                return new DelayNetworkElement(DimPos.of(world, blockPos));
7✔
137
            }
138
        };
139
    }
140

141
    @Override
142
    public ItemStack writeProxyInfo(boolean generateId, ItemStack itemStack, final int proxyId) {
143
        IVariableFacadeHandlerRegistry registry = IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class);
6✔
144
        return registry.writeVariableFacadeItem(generateId, itemStack, DelayVariableFacadeHandler.getInstance(), new IVariableFacadeHandlerRegistry.IVariableFacadeFactory<IDelayVariableFacade>() {
18✔
145
            @Override
146
            public IDelayVariableFacade create(boolean generateId) {
147
                return new DelayVariableFacade(generateId, proxyId);
7✔
148
            }
149

150
            @Override
151
            public IDelayVariableFacade create(int id) {
152
                return new DelayVariableFacade(id, proxyId);
×
153
            }
154
        }, getLevel(), lastPlayer, getBlockState());
5✔
155
    }
156

157
    @Override
158
    public IVariable<?> getVariable(IPartNetwork network) {
159
        return variable;
3✔
160
    }
161

162
    public IVariable<?> getVariableSuper(IPartNetwork network) {
163
        return super.getVariable(network);
4✔
164
    }
165

166
    public void setCapacity(int capacity) {
167
        this.capacity = Math.max(1, capacity);
×
168
        this.values = Queues.newArrayBlockingQueue(this.capacity);
×
169
    }
×
170

171
    public Queue<IValue> getValues() {
172
        if (values == null) {
3✔
173
            values = Queues.newArrayBlockingQueue(this.capacity);
5✔
174
        }
175
        return values;
3✔
176
    }
177

178
    @Override
179
    public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
180
        super.saveAdditional(tag, provider);
4✔
181
        ListTag valueList = new ListTag();
4✔
182
        for (IValue value : getValues()) {
11✔
183
            valueList.add(ValueHelpers.serialize(ValueDeseralizationContext.of(provider), value));
7✔
184
        }
1✔
185
        tag.put("values", valueList);
5✔
186
    }
1✔
187

188
    @Override
189
    public void read(CompoundTag tag, HolderLookup.Provider provider) {
190
        super.read(tag, provider);
×
191
        if (this.capacity <= 0) this.capacity = 1;
×
192
        values = Queues.newArrayBlockingQueue(this.capacity);
×
193

194
        ListTag valueList = tag.getList("values", Tag.TAG_COMPOUND);
×
195
        for (int i = 0; i < valueList.size(); i++) {
×
196
            IValue value = ValueHelpers.deserialize(ValueDeseralizationContext.of(getLevel()), valueList.getCompound(i));
×
197
            if (value != null) {
×
198
                this.values.add(value);
×
199
            }
200
        }
201
    }
×
202

203
    @Nullable
204
    @Override
205
    public AbstractContainerMenu createMenu(int id, Inventory playerInventory, Player playerEntity) {
206
        return new ContainerDelay(id, playerInventory, this.getInventory(), Optional.of(this));
×
207
    }
208

209
    @Override
210
    public Component getDisplayName() {
211
        return Component.translatable("block.integrateddynamics.delay");
×
212
    }
213

214
    public static class Ticker extends BlockEntityProxy.Ticker<BlockEntityDelay> {
3✔
215
        @Override
216
        protected void update(Level level, BlockPos pos, BlockState blockState, BlockEntityDelay blockEntity) {
217
            super.update(level, pos, blockState, blockEntity);
6✔
218

219
            if (!level.isClientSide && blockEntity.updateInterval > 0 && level.getGameTime() % blockEntity.updateInterval == 0) {
15!
220
                // Remove oldest elements from the queue until we have room for a new one.
221
                while (blockEntity.getValues().size() >= blockEntity.capacity) {
6✔
222
                    blockEntity.getValues().poll();
5✔
223
                }
224

225
                IPartNetwork partNetwork = NetworkHelpers.getPartNetwork(blockEntity.getNetwork()).orElse(null);
7✔
226
                if (partNetwork == null) {
2!
227
                    return;
×
228
                }
229

230
                // Add new value to the queue
231
                IVariable<?> variable = blockEntity.getVariableSuper(partNetwork);
4✔
232
                IValue value = null;
2✔
233
                if (variable != null) {
2✔
234
                    try {
235
                        value = variable.getValue();
3✔
236
                        blockEntity.lastError = null;
3✔
237
                    } catch (EvaluationException e) {
×
238
                        blockEntity.getEvaluator().addError(e.getErrorMessage());
×
239
                        blockEntity.lastError = e;
×
240
                    }
1✔
241
                    if (value != null) {
2!
242
                        try {
243
                            if (blockEntity.list.getRawValue().getLength() > 0 && blockEntity.list.getRawValue().getValueType() != value.getType()) {
12!
244
                                blockEntity.getValues().clear();
×
245
                            }
246
                        } catch (EvaluationException e) {}
1✔
247
                        blockEntity.getValues().add(value);
5✔
248

249
                        // Update variable with as value the materialized queue list
250
                        blockEntity.list = ValueTypeList.ValueList.ofList(value.getType(), Lists.newArrayList(blockEntity.values));
9✔
251
                    }
252
                } else {
253
                    blockEntity.getValues().clear();
3✔
254
                    blockEntity.list = ValueTypes.LIST.getDefault();
4✔
255
                }
256
                blockEntity.variable.invalidate();
3✔
257
            }
258
        }
1✔
259
    }
260
}
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