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

CyclopsMC / IntegratedDynamics / 16552051255

27 Jul 2025 01:58PM UTC coverage: 53.206% (+8.0%) from 45.161%
16552051255

push

github

rubensworks
Resolve minor TODOs

2888 of 8740 branches covered (33.04%)

Branch coverage included in aggregate %.

17341 of 29280 relevant lines covered (59.22%)

3.08 hits per line

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

66.67
/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.network.chat.Component;
9
import net.minecraft.world.MenuProvider;
10
import net.minecraft.world.entity.player.Inventory;
11
import net.minecraft.world.entity.player.Player;
12
import net.minecraft.world.inventory.AbstractContainerMenu;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.level.Level;
15
import net.minecraft.world.level.block.entity.BlockEntityType;
16
import net.minecraft.world.level.block.state.BlockState;
17
import net.minecraft.world.level.storage.ValueInput;
18
import net.minecraft.world.level.storage.ValueOutput;
19
import org.cyclops.cyclopscore.capability.item.ItemHandlerSlotMasked;
20
import org.cyclops.cyclopscore.datastructure.DimPos;
21
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;
22
import org.cyclops.integrateddynamics.Capabilities;
23
import org.cyclops.integrateddynamics.IntegratedDynamics;
24
import org.cyclops.integrateddynamics.RegistryEntries;
25
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
26
import org.cyclops.integrateddynamics.api.evaluate.expression.VariableAdapter;
27
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
28
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
29
import org.cyclops.integrateddynamics.api.item.IDelayVariableFacade;
30
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
31
import org.cyclops.integrateddynamics.api.network.INetworkElement;
32
import org.cyclops.integrateddynamics.api.network.INetworkElementProvider;
33
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
34
import org.cyclops.integrateddynamics.capability.networkelementprovider.NetworkElementProviderSingleton;
35
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityActiveVariableBase;
36
import org.cyclops.integrateddynamics.core.evaluate.DelayVariableFacadeHandler;
37
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers;
38
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeList;
39
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
40
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
41
import org.cyclops.integrateddynamics.core.item.DelayVariableFacade;
42
import org.cyclops.integrateddynamics.inventory.container.ContainerDelay;
43
import org.cyclops.integrateddynamics.network.DelayNetworkElement;
44

45
import javax.annotation.Nullable;
46
import java.util.Optional;
47
import java.util.Queue;
48
import java.util.function.Supplier;
49

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

57
    public static final int INVENTORY_SIZE = 3;
58

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

70
    @Setter
3✔
71
    private Player lastPlayer = null;
72
    private EvaluationException lastError = null;
3✔
73

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

78
            @Override
79
            public ValueTypeList getType() {
80
                return ValueTypes.LIST;
2✔
81
            }
82

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

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

98
        @Override
99
        public void populate() {
100
            super.populate();
2✔
101

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

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

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

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

154
    @Override
155
    public IVariable<?> getVariable(IPartNetwork network) {
156
        return variable;
3✔
157
    }
158

159
    public IVariable<?> getVariableSuper(IPartNetwork network) {
160
        return super.getVariable(network);
4✔
161
    }
162

163
    public void setCapacity(int capacity) {
164
        this.capacity = Math.max(1, capacity);
×
165
        this.values = Queues.newArrayBlockingQueue(this.capacity);
×
166
    }
×
167

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

175
    @Override
176
    public void saveAdditional(ValueOutput output) {
177
        super.saveAdditional(output);
3✔
178
        ValueOutput.ValueOutputList valueList = output.childrenList("values");
4✔
179
        for (IValue value : getValues()) {
11✔
180
            ValueHelpers.serialize(valueList.addChild(), value);
4✔
181
        }
1✔
182
    }
1✔
183

184
    @Override
185
    public void read(ValueInput input) {
186
        super.read(input);
3✔
187
        if (this.capacity <= 0) this.capacity = 1;
3!
188
        values = Queues.newArrayBlockingQueue(this.capacity);
5✔
189

190
        for (ValueInput valueInput : input.childrenList("values").orElseThrow()) {
14✔
191
            IValue value = ValueHelpers.deserialize(valueInput);
3✔
192
            if (value != null) {
2!
193
                this.values.add(value);
5✔
194
            }
195
        }
1✔
196
    }
1✔
197

198
    @Nullable
199
    @Override
200
    public AbstractContainerMenu createMenu(int id, Inventory playerInventory, Player playerEntity) {
201
        return new ContainerDelay(id, playerInventory, this.getInventory(), Optional.of(this));
×
202
    }
203

204
    @Override
205
    public Component getDisplayName() {
206
        return Component.translatable("block.integrateddynamics.delay");
×
207
    }
208

209
    public static class Ticker extends BlockEntityProxy.Ticker<BlockEntityDelay> {
3✔
210
        @Override
211
        protected void update(Level level, BlockPos pos, BlockState blockState, BlockEntityDelay blockEntity) {
212
            super.update(level, pos, blockState, blockEntity);
6✔
213

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

220
                IPartNetwork partNetwork = NetworkHelpers.getPartNetwork(blockEntity.getNetwork()).orElse(null);
7✔
221
                if (partNetwork == null) {
2!
222
                    return;
×
223
                }
224

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

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