• 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

4.55
/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 net.neoforged.neoforge.transfer.item.VanillaContainerWrapper;
20
import org.cyclops.cyclopscore.datastructure.DimPos;
21
import org.cyclops.cyclopscore.inventory.InventorySlotMasked;
22
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;
23
import org.cyclops.integrateddynamics.Capabilities;
24
import org.cyclops.integrateddynamics.IntegratedDynamics;
25
import org.cyclops.integrateddynamics.RegistryEntries;
26
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
27
import org.cyclops.integrateddynamics.api.evaluate.expression.VariableAdapter;
28
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
29
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
30
import org.cyclops.integrateddynamics.api.item.IDelayVariableFacade;
31
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
32
import org.cyclops.integrateddynamics.api.network.INetworkElement;
33
import org.cyclops.integrateddynamics.api.network.INetworkElementProvider;
34
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
35
import org.cyclops.integrateddynamics.capability.networkelementprovider.NetworkElementProviderSingleton;
36
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityActiveVariableBase;
37
import org.cyclops.integrateddynamics.core.evaluate.DelayVariableFacadeHandler;
38
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers;
39
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeList;
40
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
41
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
42
import org.cyclops.integrateddynamics.core.item.DelayVariableFacade;
43
import org.cyclops.integrateddynamics.inventory.container.ContainerDelay;
44
import org.cyclops.integrateddynamics.network.DelayNetworkElement;
45

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

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

58
    public static final int INVENTORY_SIZE = 3;
59

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

71
    @Setter
×
72
    private Player lastPlayer = null;
73
    private EvaluationException lastError = null;
×
74

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

79
            @Override
80
            public ValueTypeList getType() {
81
                return ValueTypes.LIST;
×
82
            }
83

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

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

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

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

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

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

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

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

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

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

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

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

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

191
        for (ValueInput valueInput : input.childrenList("values").orElseThrow()) {
×
192
            IValue value = ValueHelpers.deserialize(valueInput);
×
193
            if (value != null) {
×
194
                this.values.add(value);
×
195
            }
196
        }
×
197
    }
×
198

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

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

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

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

221
                IPartNetwork partNetwork = NetworkHelpers.getPartNetwork(blockEntity.getNetwork()).orElse(null);
×
222
                if (partNetwork == null) {
×
223
                    return;
×
224
                }
225

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

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