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

CyclopsMC / IntegratedDynamics / 14430401152

13 Apr 2025 02:28PM UTC coverage: 42.575% (+4.0%) from 38.611%
14430401152

push

github

rubensworks
Add basic read aspect game tests

2310 of 8381 branches covered (27.56%)

Branch coverage included in aggregate %.

11016 of 22919 relevant lines covered (48.06%)

2.26 hits per line

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

60.22
/src/main/java/org/cyclops/integrateddynamics/blockentity/BlockEntityProxy.java
1
package org.cyclops.integrateddynamics.blockentity;
2

3
import lombok.Getter;
4
import lombok.Setter;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.network.chat.Component;
7
import net.minecraft.world.MenuProvider;
8
import net.minecraft.world.entity.player.Inventory;
9
import net.minecraft.world.entity.player.Player;
10
import net.minecraft.world.inventory.AbstractContainerMenu;
11
import net.minecraft.world.item.ItemStack;
12
import net.minecraft.world.level.Level;
13
import net.minecraft.world.level.block.entity.BlockEntityType;
14
import net.minecraft.world.level.block.state.BlockState;
15
import org.cyclops.cyclopscore.blockentity.BlockEntityTickerDelayed;
16
import org.cyclops.cyclopscore.capability.item.ItemHandlerSlotMasked;
17
import org.cyclops.cyclopscore.datastructure.DimPos;
18
import org.cyclops.cyclopscore.inventory.SimpleInventory;
19
import org.cyclops.cyclopscore.persist.nbt.NBTPersist;
20
import org.cyclops.integrateddynamics.Capabilities;
21
import org.cyclops.integrateddynamics.IntegratedDynamics;
22
import org.cyclops.integrateddynamics.RegistryEntries;
23
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
24
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
25
import org.cyclops.integrateddynamics.api.item.IProxyVariableFacade;
26
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
27
import org.cyclops.integrateddynamics.api.network.INetworkElement;
28
import org.cyclops.integrateddynamics.api.network.INetworkElementProvider;
29
import org.cyclops.integrateddynamics.capability.networkelementprovider.NetworkElementProviderSingleton;
30
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityActiveVariableBase;
31
import org.cyclops.integrateddynamics.core.evaluate.InventoryVariableEvaluator;
32
import org.cyclops.integrateddynamics.core.evaluate.ProxyVariableFacadeHandler;
33
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
34
import org.cyclops.integrateddynamics.core.helper.L10NValues;
35
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
36
import org.cyclops.integrateddynamics.core.item.ProxyVariableFacade;
37
import org.cyclops.integrateddynamics.inventory.container.ContainerProxy;
38
import org.cyclops.integrateddynamics.network.ProxyNetworkElement;
39

40
import javax.annotation.Nullable;
41
import java.util.Optional;
42
import java.util.function.Supplier;
43

44
/**
45
 * A part entity for the variable proxy.
46
 * @author rubensworks
47
 */
48
public class BlockEntityProxy extends BlockEntityActiveVariableBase<ProxyNetworkElement> implements MenuProvider {
49

50
    public static final int INVENTORY_SIZE = 3;
51
    public static final int SLOT_READ = 0;
52
    public static final int SLOT_WRITE_IN = 1;
53
    public static final int SLOT_WRITE_OUT = 2;
54

55
    public static final String GLOBALCOUNTER_KEY = "proxy";
56

57
    @NBTPersist
3✔
58
    @Getter
3✔
59
    @Setter
×
60
    private int proxyId = -1;
61

62
    @Setter
3✔
63
    private Player lastPlayer = null;
64
    private boolean writeVariable;
65

66
    public BlockEntityProxy(BlockPos blockPos, BlockState blockState) {
67
        this(RegistryEntries.BLOCK_ENTITY_PROXY.get(), blockPos, blockState, BlockEntityProxy.INVENTORY_SIZE);
8✔
68
    }
1✔
69

70
    public BlockEntityProxy(BlockEntityType<?> type, BlockPos blockPos, BlockState blockState, int inventorySize) {
71
        super(type, blockPos, blockState, inventorySize);
6✔
72
    }
1✔
73

74
    public static class CapabilityRegistrar extends BlockEntityActiveVariableBase.CapabilityRegistrar<BlockEntityProxy> {
75
        public CapabilityRegistrar(Supplier<BlockEntityType<? extends BlockEntityProxy>> blockEntityType) {
76
            super(blockEntityType);
3✔
77
        }
1✔
78

79
        @Override
80
        public void populate() {
81
            super.populate();
2✔
82

83
            add(
4✔
84
                    net.neoforged.neoforge.capabilities.Capabilities.ItemHandler.BLOCK,
85
                    (blockEntity, direction) -> {
86
                        int slot = -1;
×
87
                        if (direction != null) {
×
88
                            switch (direction) {
×
89
                                case DOWN -> slot = SLOT_WRITE_OUT;
×
90
                                case UP -> slot = SLOT_WRITE_IN;
×
91
                                case NORTH -> slot = SLOT_READ;
×
92
                                case SOUTH -> slot = SLOT_READ;
×
93
                                case WEST -> slot = SLOT_READ;
×
94
                                case EAST -> slot = SLOT_READ;
×
95
                            }
96
                        } else {
97
                            slot = SLOT_READ;
×
98
                        }
99
                        return new ItemHandlerSlotMasked(blockEntity.getInventory(), slot);
×
100
                    }
101
            );
102
            add(
4✔
103
                    Capabilities.NetworkElementProvider.BLOCK,
104
                    (blockEntity, direction) -> blockEntity.getNetworkElementProvider()
3✔
105
            );
106
        }
1✔
107
    }
108

109
    @Override
110
    public INetworkElementProvider getNetworkElementProvider() {
111
        return new NetworkElementProviderSingleton() {
8✔
112
            @Override
113
            public INetworkElement createNetworkElement(Level world, BlockPos blockPos) {
114
                return new ProxyNetworkElement(DimPos.of(world, blockPos));
7✔
115
            }
116
        };
117
    }
118

119
    public boolean isWriteVariable() {
120
        return writeVariable;
3✔
121
    }
122

123
    @Override
124
    protected SimpleInventory createInventory(int inventorySize, int stackSize) {
125
        return new SimpleInventory(inventorySize, stackSize) {
12✔
126
            @Override
127
            public boolean canPlaceItem(int slot, ItemStack itemStack) {
128
                return slot != SLOT_WRITE_OUT && super.canPlaceItem(slot, itemStack);
×
129
            }
130
        };
131
    }
132

133
    @Override
134
    protected InventoryVariableEvaluator<IValue> createEvaluator() {
135
        return new InventoryVariableEvaluator<IValue>(this.getInventory(), getSlotRead(), () -> ValueDeseralizationContext.of(getLevel()), ValueTypes.CATEGORY_ANY) {
26✔
136
            @Override
137
            protected void preValidate() {
138
                super.preValidate();
2✔
139
                // Hard check to make sure the variable is not directly referring to this proxy.
140
                if(getVariableFacade() instanceof IProxyVariableFacade) {
4!
141
                    if(((IProxyVariableFacade) getVariableFacade()).getProxyId() == getProxyId()) {
×
142
                        addError(Component.translatable(L10NValues.VARIABLE_ERROR_RECURSION, getVariableFacade().getId()));
×
143
                    }
144
                }
145
            }
1✔
146
        };
147
    }
148

149
    /**
150
     * This will generate a new proxy id.
151
     * Be careful when calling this!
152
     */
153
    public void generateNewProxyId() {
154
        this.proxyId = IntegratedDynamics.globalCounters.getNext(GLOBALCOUNTER_KEY);
5✔
155
        setChanged();
2✔
156
    }
1✔
157

158
    @Override
159
    public int getSlotRead() {
160
        return SLOT_READ;
2✔
161
    }
162

163
    protected int getSlotWriteIn() {
164
        return SLOT_WRITE_IN;
2✔
165
    }
166

167
    protected int getSlotWriteOut() {
168
        return SLOT_WRITE_OUT;
2✔
169
    }
170

171
    @Override
172
    public void onDirty() {
173
        super.onDirty();
2✔
174
        if (!level.isClientSide()) {
4!
175
            this.writeVariable = true;
3✔
176
        }
177
    }
1✔
178

179
    public ItemStack writeProxyInfo(boolean generateId, ItemStack itemStack, final int proxyId) {
180
        IVariableFacadeHandlerRegistry registry = IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class);
6✔
181
        return registry.writeVariableFacadeItem(generateId, itemStack, ProxyVariableFacadeHandler.getInstance(), new IVariableFacadeHandlerRegistry.IVariableFacadeFactory<IProxyVariableFacade>() {
18✔
182
            @Override
183
            public IProxyVariableFacade create(boolean generateId) {
184
                return new ProxyVariableFacade(generateId, proxyId);
7✔
185
            }
186

187
            @Override
188
            public IProxyVariableFacade create(int id) {
189
                return new ProxyVariableFacade(id, proxyId);
×
190
            }
191
        }, getLevel(), lastPlayer, getBlockState());
5✔
192
    }
193

194
    @Nullable
195
    @Override
196
    public AbstractContainerMenu createMenu(int id, Inventory playerInventory, Player playerEntity) {
197
        return new ContainerProxy(id, playerInventory, this.getInventory(), Optional.of(this));
×
198
    }
199

200
    @Override
201
    public Component getDisplayName() {
202
        return Component.translatable("block.integrateddynamics.proxy");
×
203
    }
204

205
    public static class Ticker<T extends BlockEntityProxy> extends BlockEntityTickerDelayed<T> {
3✔
206
        @Override
207
        protected void update(Level level, BlockPos pos, BlockState blockState, T blockEntity) {
208
            super.update(level, pos, blockState, blockEntity);
6✔
209

210
            // If the block was placed by a non-player, this is needed.
211
            if (blockEntity.getProxyId() < 0) {
3✔
212
                blockEntity.generateNewProxyId();
2✔
213
                NetworkHelpers.initNetwork(level, pos, null);
5✔
214
            }
215

216
            if (blockEntity.isWriteVariable()) {
3✔
217
                if (!blockEntity.getInventory().getItem(blockEntity.getSlotWriteIn()).isEmpty() && blockEntity.getInventory().getItem(blockEntity.getSlotWriteOut()).isEmpty()) {
14!
218
                    // Write proxy reference
219
                    ItemStack outputStack = blockEntity.writeProxyInfo(!blockEntity.getLevel().isClientSide, blockEntity.getInventory().removeItemNoUpdate(blockEntity.getSlotWriteIn()), blockEntity.getProxyId());
16!
220
                    blockEntity.getInventory().setItem(blockEntity.getSlotWriteOut(), outputStack);
6✔
221
                }
222
            }
223
        }
1✔
224
    }
225
}
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