• 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

6.45
/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 net.neoforged.neoforge.transfer.item.VanillaContainerWrapper;
16
import org.cyclops.cyclopscore.datastructure.DimPos;
17
import org.cyclops.cyclopscore.inventory.InventorySlotMasked;
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.blockentity.BlockEntityCableConnectableInventory;
32
import org.cyclops.integrateddynamics.core.evaluate.InventoryVariableEvaluator;
33
import org.cyclops.integrateddynamics.core.evaluate.ProxyVariableFacadeHandler;
34
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
35
import org.cyclops.integrateddynamics.core.helper.L10NValues;
36
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
37
import org.cyclops.integrateddynamics.core.item.ProxyVariableFacade;
38
import org.cyclops.integrateddynamics.inventory.container.ContainerProxy;
39
import org.cyclops.integrateddynamics.network.ProxyNetworkElement;
40

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

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

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

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

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

63
    @Setter
×
64
    private Player lastPlayer = null;
65
    private boolean writeVariable;
66

67
    public BlockEntityProxy(BlockPos blockPos, BlockState blockState) {
68
        this(RegistryEntries.BLOCK_ENTITY_PROXY.get(), blockPos, blockState, BlockEntityProxy.INVENTORY_SIZE);
×
69
    }
×
70

71
    public BlockEntityProxy(BlockEntityType<?> type, BlockPos blockPos, BlockState blockState, int inventorySize) {
72
        super(type, blockPos, blockState, inventorySize);
×
73
    }
×
74

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

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

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

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

120
    public boolean isWriteVariable() {
121
        return writeVariable;
×
122
    }
123

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

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

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

159
    @Override
160
    public int getSlotRead() {
161
        return SLOT_READ;
×
162
    }
163

164
    protected int getSlotWriteIn() {
165
        return SLOT_WRITE_IN;
×
166
    }
167

168
    protected int getSlotWriteOut() {
169
        return SLOT_WRITE_OUT;
×
170
    }
171

172
    @Override
173
    public void onDirty() {
174
        super.onDirty();
×
175
        if (!level.isClientSide()) {
×
176
            this.writeVariable = true;
×
177
        }
178
    }
×
179

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

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

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

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

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

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

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