• 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

16.67
/src/main/java/org/cyclops/integrateddynamics/core/part/PartTypeBase.java
1
package org.cyclops.integrateddynamics.core.part;
2

3
import lombok.Getter;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.nbt.CompoundTag;
7
import net.minecraft.network.RegistryFriendlyByteBuf;
8
import net.minecraft.network.chat.Component;
9
import net.minecraft.resources.ResourceLocation;
10
import net.minecraft.server.level.ServerPlayer;
11
import net.minecraft.world.InteractionHand;
12
import net.minecraft.world.InteractionResult;
13
import net.minecraft.world.entity.player.Player;
14
import net.minecraft.world.item.Item;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.level.Level;
17
import net.minecraft.world.level.block.Block;
18
import net.minecraft.world.level.block.state.BlockBehaviour;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.phys.BlockHitResult;
21
import org.cyclops.cyclopscore.config.extendedconfig.BlockConfigCommon;
22
import org.cyclops.cyclopscore.datastructure.DimPos;
23
import org.cyclops.cyclopscore.init.ModBaseNeoForge;
24
import org.cyclops.integrateddynamics.GeneralConfig;
25
import org.cyclops.integrateddynamics.IntegratedDynamics;
26
import org.cyclops.integrateddynamics.RegistryEntries;
27
import org.cyclops.integrateddynamics.api.network.INetwork;
28
import org.cyclops.integrateddynamics.api.network.INetworkElement;
29
import org.cyclops.integrateddynamics.api.network.IPartNetworkElement;
30
import org.cyclops.integrateddynamics.api.network.event.INetworkEvent;
31
import org.cyclops.integrateddynamics.api.part.*;
32
import org.cyclops.integrateddynamics.core.block.IgnoredBlock;
33
import org.cyclops.integrateddynamics.core.helper.L10NValues;
34
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
35
import org.cyclops.integrateddynamics.core.item.ItemPart;
36
import org.cyclops.integrateddynamics.core.network.PartNetworkElement;
37
import org.cyclops.integrateddynamics.item.ItemEnhancement;
38
import org.cyclops.integrateddynamics.item.ItemWrench;
39

40
import java.util.*;
41
import java.util.function.Consumer;
42

43
/**
44
 * An abstract {@link IPartType} with a default implementation for creating
45
 * network elements.
46
 * @author rubensworks
47
 */
48
public abstract class PartTypeBase<P extends IPartType<P, S>, S extends IPartState<P>>
49
        extends PartTypeAdapter<P, S> {
50

51
    @Getter
×
52
    private Item item;
53
    @Getter
×
54
    private Block block;
55
    private final String name;
56
    @Getter
×
57
    private final PartRenderPosition partRenderPosition;
58
    private final Map<Class<? extends INetworkEvent>, IEventAction> networkEventActions;
59

60
    public PartTypeBase(String name, PartRenderPosition partRenderPosition) {
2✔
61
        this.name = name;
3✔
62
        this.partRenderPosition = partRenderPosition;
3✔
63

64
        networkEventActions = constructNetworkEventActions();
4✔
65

66
        registerBlock();
2✔
67
    }
1✔
68

69
    @Override
70
    public ResourceLocation getUniqueName() {
71
        return ResourceLocation.fromNamespaceAndPath(getModId(), this.name);
6✔
72
    }
73

74
    protected ModBaseNeoForge getMod() {
75
        return IntegratedDynamics._instance;
2✔
76
    }
77

78
    protected String getModId() {
79
        return getMod().getModId();
4✔
80
    }
81

82
    /**
83
     * Creates and registers a block instance for this part type.
84
     * This is mainly used for the block facadeModel.
85
     */
86
    protected void registerBlock() {
87
        BlockConfigCommon blockConfig = new BlockConfigCommon<>(getMod(), "part_" + this.name,
14✔
88
                (eConfig, properties) -> block = createBlock(eConfig, properties),
8✔
89
                (eConfig, block) -> item  = createItem(eConfig, block)) {
18✔
90
            @Override
91
            public String getFullTranslationKey() {
92
                return PartTypeBase.this.getTranslationKey();
×
93
            }
94

95
            @Override
96
            public Collection<ItemStack> getDefaultCreativeTabEntries() {
97
                return Collections.singleton(new ItemStack(getItemInstance()));
7✔
98
            }
99
        };
100
        getMod().getConfigHandler().addConfigurable(blockConfig);
6✔
101
    }
1✔
102

103
    /**
104
     * Factory method for creating a block instance.
105
     *
106
     * @param blockConfig The config to register the block for.
107
     * @param properties
108
     * @return The block instance.
109
     */
110
    protected Block createBlock(BlockConfigCommon<?> blockConfig, BlockBehaviour.Properties properties) {
111
        return new IgnoredBlock(properties);
5✔
112
    }
113

114
    /**
115
     * Factory method for creating a item instance.
116
     * @param blockConfig The block config to register the item for.
117
     * @param block The block corresponding to the item.
118
     * @return The item instance.
119
     */
120
    protected Item createItem(BlockConfigCommon<?> blockConfig, Block block) {
121
        return new ItemPart<>(blockConfig.createDefaultItemProperties()
6✔
122
                .overrideDescription(this.getTranslationKey()), this);
4✔
123
    }
124

125
    @Override
126
    public ResourceLocation getBlockModelPath() {
127
        return ResourceLocation.fromNamespaceAndPath(getModId(), "part_" + this.name);
×
128
    }
129

130
    @Override
131
    protected String createTranslationKey() {
132
        return "parttype." + getModId() + "." + this.name;
6✔
133
    }
134

135
    @SuppressWarnings("unchecked")
136
    @Override
137
    public INetworkElement createNetworkElement(IPartContainer partContainer, DimPos pos, Direction side) {
138
        return new PartNetworkElement(this, PartPos.of(pos, side));
×
139
    }
140

141
    @Override
142
    public InteractionResult onPartActivated(S partState, BlockPos pos, Level world, Player player, InteractionHand hand,
143
                                            ItemStack heldItem, BlockHitResult hit) {
144
        // Drop through if the player is sneaking
145
        if(player.isSecondaryUseActive()) {
×
146
            return InteractionResult.PASS;
×
147
        }
148

149
        // Consume enhancement
150
        if (heldItem.getItem() instanceof ItemEnhancement itemEnhancement) {
×
151
            InteractionResult result = itemEnhancement.applyEnhancement(this, partState, heldItem, player, hand);
×
152
            if (result.consumesAction()) {
×
153
                return result;
×
154
            }
155
        }
156

157
        // Set offset and side
158
        PartPos partPos = PartPos.of(world, pos, hit.getDirection());
×
159
        if (heldItem.getItem() instanceof ItemWrench itemWrench) {
×
160
            InteractionResult result = itemWrench.performPartAction(hit, this, partState, heldItem, player, hand, partPos);
×
161
            if (result.consumesAction()) {
×
162
                return result;
×
163
            }
164
        }
165

166
        if(getContainerProvider(partPos).isPresent()) {
×
167
            if (!world.isClientSide()) {
×
168
                return PartHelpers.openContainerPart((ServerPlayer) player, partPos, this);
×
169
            }
170
            return InteractionResult.SUCCESS;
×
171
        }
172
        return InteractionResult.PASS;
×
173
    }
174

175
    @Override
176
    public void addDrops(PartTarget target, S state, List<ItemStack> itemStacks, boolean dropMainElement, boolean saveState) {
177
        super.addDrops(target, state, itemStacks, dropMainElement, saveState);
×
178

179
        // Save enhancements
180
        if (!saveState && state.getMaxOffset() > 0) {
×
181
            // Drop Part Offset items each with as maximum the GeneralConfig.enchancementOffsetPartDropValue offset value.
182
            int remainingOffset = state.getMaxOffset();
×
183
            while (remainingOffset > 0) {
×
184
                int offset;
185
                if (remainingOffset < GeneralConfig.enchancementOffsetPartDropValue) {
×
186
                    offset = remainingOffset;
×
187
                } else {
188
                    offset = GeneralConfig.enchancementOffsetPartDropValue;
×
189
                }
190
                remainingOffset -= offset;
×
191

192
                ItemStack itemStack = new ItemStack(RegistryEntries.ITEM_ENHANCEMENT_OFFSET);
×
193
                RegistryEntries.ITEM_ENHANCEMENT_OFFSET.get().setEnhancementValue(itemStack, offset);
×
194
                itemStacks.add(itemStack);
×
195
            }
×
196
            state.setMaxOffset(0);
×
197
        }
198
    }
×
199

200
    @Override
201
    public BlockState getBlockState(IPartContainer partContainer, Direction side) {
202
        return getBlock().defaultBlockState()
×
203
                .setValue(IgnoredBlock.FACING, side);
×
204
    }
205

206
    @Override
207
    public BlockState getBaseBlockState() {
208
        return getBlock().defaultBlockState();
×
209
    }
210

211
    @Override
212
    public void loadTooltip(S state, List<Component> lines) {
213
        if(!state.isEnabled()) {
×
214
            lines.add(Component.translatable(L10NValues.PART_TOOLTIP_DISABLED));
×
215
        }
216
        lines.add(Component.translatable(L10NValues.GENERAL_ITEM_ID, state.getId()));
×
217

218
        if (state.getMaxOffset() > 0) {
×
219
            lines.add(Component.translatable(L10NValues.PART_TOOLTIP_MAXOFFSET, state.getMaxOffset()));
×
220
        }
221
    }
×
222

223
    @Override
224
    public void loadTooltip(ItemStack itemStack, Consumer<Component> tooltipAdder) {
225
        if(itemStack.has(RegistryEntries.DATACOMPONENT_PART_STATE)) {
×
226
            CompoundTag tag = itemStack.get(RegistryEntries.DATACOMPONENT_PART_STATE);
×
227
            if(tag.contains("id")) {
×
228
                int id = tag.getInt("id").orElseThrow();
×
229
                tooltipAdder.accept(Component.translatable(L10NValues.GENERAL_ITEM_ID, id));
×
230
            }
231
            if(tag.contains("maxOffset")) {
×
232
                int maxOffset = tag.getInt("maxOffset").orElseThrow();
×
233
                tooltipAdder.accept(Component.translatable(L10NValues.PART_TOOLTIP_MAXOFFSET, maxOffset));
×
234
            }
235
        }
236

237
        super.loadTooltip(itemStack, tooltipAdder);
×
238
    }
×
239

240
    /**
241
     * Override this to register your network event actions.
242
     * @return The event actions.
243
     */
244
    protected Map<Class<? extends INetworkEvent>, IEventAction> constructNetworkEventActions() {
245
        return new IdentityHashMap<>();
4✔
246
    }
247

248
    @Override
249
    public final boolean hasEventSubscriptions() {
250
        return !networkEventActions.isEmpty();
×
251
    }
252

253
    @Override
254
    public final Set<Class<? extends INetworkEvent>> getSubscribedEvents() {
255
        return networkEventActions.keySet();
×
256
    }
257

258
    @SuppressWarnings("unchecked")
259
    @Override
260
    public final void onEvent(INetworkEvent event, IPartNetworkElement<P, S> networkElement) {
261
        if (networkElement.getTarget().getCenter().getPos().isLoaded()) {
×
262
            networkEventActions.get(event.getClass()).onAction(event.getNetwork(), networkElement.getTarget(),
×
263
                    networkElement.getPartState(), event);
×
264
        }
265
    }
×
266

267
    @Override
268
    public boolean forceLightTransparency(S state) {
269
        return false;
×
270
    }
271

272
    @Override
273
    public void writeExtraGuiData(RegistryFriendlyByteBuf packetBuffer, PartPos pos, ServerPlayer player) {
274
        packetBuffer.writeUtf(this.getUniqueName().toString());
×
275
    }
×
276

277
    public interface IEventAction<P extends IPartType<P, S>, S extends IPartState<P>, E extends INetworkEvent> {
278

279
        public void onAction(INetwork network, PartTarget target, S state, E event);
280

281
    }
282

283
}
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