• 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

28.57
/src/main/java/org/cyclops/integrateddynamics/core/helper/Helpers.java
1
package org.cyclops.integrateddynamics.core.helper;
2

3
import com.google.common.base.Predicate;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.Lists;
6
import net.minecraft.ResourceLocationException;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.HolderSet;
9
import net.minecraft.core.registries.BuiltInRegistries;
10
import net.minecraft.core.registries.Registries;
11
import net.minecraft.nbt.NbtOps;
12
import net.minecraft.nbt.Tag;
13
import net.minecraft.nbt.TagParser;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.resources.ResourceLocation;
16
import net.minecraft.server.level.ServerPlayer;
17
import net.minecraft.tags.TagKey;
18
import net.minecraft.world.entity.Entity;
19
import net.minecraft.world.entity.player.Player;
20
import net.minecraft.world.item.BlockItem;
21
import net.minecraft.world.item.Item;
22
import net.minecraft.world.item.ItemStack;
23
import net.minecraft.world.level.BlockGetter;
24
import net.minecraft.world.level.Level;
25
import net.minecraft.world.level.block.Block;
26
import net.minecraft.world.level.block.LiquidBlock;
27
import net.minecraft.world.level.block.state.BlockState;
28
import net.minecraft.world.level.material.Fluid;
29
import net.neoforged.neoforge.capabilities.Capabilities;
30
import net.neoforged.neoforge.fluids.FluidStack;
31
import net.neoforged.neoforge.transfer.ResourceHandler;
32
import net.neoforged.neoforge.transfer.access.ItemAccess;
33
import net.neoforged.neoforge.transfer.fluid.FluidResource;
34
import net.neoforged.neoforge.transfer.fluid.FluidUtil;
35
import org.cyclops.cyclopscore.datastructure.DimPos;
36
import org.cyclops.cyclopscore.helper.IModHelpers;
37
import org.cyclops.cyclopscore.helper.IModHelpersNeoForge;
38

39
import java.util.List;
40
import java.util.Locale;
41
import java.util.Optional;
42
import java.util.stream.Stream;
43

44
/**
45
 * Helper methods.
46
 * @author rubensworks
47
 */
48
public final class Helpers {
×
49

50
    public static final Predicate<Entity> SELECTOR_IS_PLAYER = entity -> entity instanceof Player;
2✔
51

52
    public static final TagParser<Tag> TAG_PARSER = TagParser.create(NbtOps.INSTANCE);
3✔
53

54
    /**
55
     * Get the fluidstack from the given itemstack.
56
     * @param itemStack The itemstack.
57
     * @return The fluidstack or null.
58
     */
59
    public static FluidStack getFluidStack(ItemStack itemStack) {
60
        FluidStack fluidStack = FluidUtil.getFirstStackContained(itemStack);
×
61
        if (fluidStack.isEmpty()
×
62
                && itemStack.getItem() instanceof BlockItem
×
63
                && ((BlockItem) itemStack.getItem()).getBlock() instanceof LiquidBlock) {
×
64
            fluidStack = new FluidStack(((LiquidBlock) ((BlockItem) itemStack.getItem()).getBlock()).fluid, IModHelpersNeoForge.get().getFluidHelpers().getBucketVolume());
×
65
        }
66
        return fluidStack;
×
67
    }
68

69
    /**
70
     * Get the fluidstack capacity from the given itemstack.
71
     * @param itemStack The itemstack.
72
     * @return The capacity
73
     */
74
    public static long getFluidStackCapacity(ItemStack itemStack) {
75
        ResourceHandler<FluidResource> fluidHandler = ItemAccess.forStack(itemStack).getCapability(Capabilities.Fluid.ITEM);
×
76
        if (fluidHandler != null) {
×
77
            if (fluidHandler.size() > 0) {
×
78
                return fluidHandler.getCapacityAsLong(0, FluidResource.EMPTY);
×
79
            }
80
        }
81
        return 0;
×
82
    }
83

84
    /**
85
     * Retrieves a Stream of items that are registered to this tag name.
86
     *
87
     * @param name The tag name
88
     * @return A Stream containing ItemStacks registered for this tag
89
     */
90
    public static Stream<ItemStack> getTagValues(String name) throws ResourceLocationException {
91
        Optional<HolderSet.Named<Item>> tag = BuiltInRegistries.ITEM
×
92
                .get(TagKey.create(Registries.ITEM, ResourceLocation.parse(name)));
×
93
        return tag.stream().flatMap(named -> named.stream().map(ItemStack::new));
×
94
    }
95

96
    /**
97
     * Retrieves a Stream of blocks that are registered to this tag name.
98
     *
99
     * @param name The tag name
100
     * @return A Stream containing Blocks registered for this tag
101
     */
102
    public static Stream<BlockState> getBlockTagValues(String name) throws ResourceLocationException {
103
        Optional<HolderSet.Named<Block>> tag = BuiltInRegistries.BLOCK
×
104
                .get(TagKey.create(Registries.BLOCK, ResourceLocation.parse(name)));
×
105
        return tag.stream().flatMap(named -> named.stream().map(block -> block.value().defaultBlockState()));
×
106
    }
107

108
    /**
109
     * Retrieves a Stream of fluids that are registered to this tag name.
110
     *
111
     * @param name The tag name
112
     * @return A Stream containing Fluids registered for this tag
113
     */
114
    public static Stream<FluidStack> getFluidTagValues(String name) throws ResourceLocationException {
115
        Optional<HolderSet.Named<Fluid>> tag = BuiltInRegistries.FLUID
×
116
                .get(TagKey.create(Registries.FLUID, ResourceLocation.parse(name)));
×
117
        return tag.stream().flatMap(named -> named.stream().map(fluid -> new FluidStack(fluid, IModHelpersNeoForge.get().getFluidHelpers().getBucketVolume())));
×
118
    }
119

120
    /**
121
     * Add the given element to a copy of the given list/
122
     * @param list The list.
123
     * @param newElement The element.
124
     * @param <T> The type.
125
     * @return The new joined list.
126
     */
127
    public static <T> List<T> joinList(List<T> list, T newElement) {
128
        ImmutableList.Builder<T> builder = ImmutableList.<T>builder().addAll(list);
4✔
129
        if(newElement != null) {
2✔
130
            builder.add(newElement);
4✔
131
        }
132
        return builder.build();
3✔
133
    }
134

135
    /**
136
     * Create a string of 'length' times '%s' seperated by ','.
137
     * @param length The length for the series of '%s'.
138
     * @return The string.
139
     */
140
    public static String createPatternOfLength(int length) {
141
        StringBuilder pattern = new StringBuilder();
4✔
142
        boolean first = true;
2✔
143
        for (int i = 0; i < length; i++) {
7✔
144
            if (first) {
2✔
145
                first = false;
3✔
146
            } else {
147
                pattern.append(",");
4✔
148
            }
149
            pattern.append("%s");
4✔
150
        }
151
        return pattern.toString();
3✔
152
    }
153

154
    private static final List<IInterfaceRetriever> INTERFACE_RETRIEVERS = Lists.newArrayList();
2✔
155
    static {
156
        addInterfaceRetriever(new IInterfaceRetriever() {
7✔
157
            @Override
158
            public <C> Optional<C> getInterface(BlockGetter world, BlockPos pos, Class<C> clazz) {
159
                return IModHelpers.get().getBlockEntityHelpers().get(world, pos, clazz);
×
160
            }
161
        });
162
    }
1✔
163

164
    /**
165
     * Check for the given interface at the given position.
166
     * @param world The world.
167
     * @param pos The position.
168
     * @param clazz The class to find.
169
     * @param <C> The class type.
170
     * @return The optional instance.
171
     */
172
    private static <C> Optional<C> getInterface(BlockGetter world, BlockPos pos, Class<C> clazz) {
173
        for(IInterfaceRetriever interfaceRetriever : INTERFACE_RETRIEVERS) {
×
174
            Optional<C> optionalInstance = interfaceRetriever.getInterface(world, pos, clazz);
×
175
            if(optionalInstance.isPresent()) {
×
176
                return optionalInstance;
×
177
            }
178
        }
×
179
        return Optional.empty();
×
180
    }
181

182
    /**
183
     * Check for the given interface at the given position.
184
     * @param dimPos The dimensional position.
185
     * @param clazz The class to find.
186
     * @param forceLoad If the world should be loaded if it was not loaded yet.
187
     * @param <C> The class type.
188
     * @return The optional instance.
189
     */
190
    public static <C> Optional<C> getInterface(DimPos dimPos, Class<C> clazz, boolean forceLoad) {
191
        Level world = dimPos.getLevel(forceLoad);
×
192
        return world != null ? getInterface(world, dimPos.getBlockPos(), clazz) : Optional.empty();
×
193
    }
194

195
    /**
196
     * Get a localized string showing the ratio of stored energy vs the capacity.
197
     * @param stored The stored amount of energy.
198
     * @param capacity The capacity of the energy container.
199
     * @return The localized string.
200
     */
201
    public static Component getLocalizedEnergyLevel(int stored, int capacity) {
202
        return Component.literal(String.format(Locale.ROOT, "%,d", stored))
×
203
                .append(" / ")
×
204
                .append(String.format(Locale.ROOT, "%,d", capacity))
×
205
                .append(" ")
×
206
                .append(Component.translatable(L10NValues.GENERAL_ENERGY_UNIT));
×
207
    }
208

209
    // This is copied from Forge's TPSCommand
210
    public static double calculateTps(long[] times) {
211
        double worldTickTime = mean(times) * 1.0E-6D;
×
212
        double worldTPS = Math.min(1000.0 / worldTickTime, 20);
×
213
        return worldTPS;
×
214
    }
215

216
    public static long mean(long[] values) {
217
        long sum = 0L;
×
218
        for (long v : values)
×
219
            sum += v;
×
220
        return sum / values.length;
×
221
    }
222

223
    public static void addInterfaceRetriever(IInterfaceRetriever interfaceRetriever) {
224
        INTERFACE_RETRIEVERS.add(interfaceRetriever);
4✔
225
    }
1✔
226

227
    /**
228
     * Return a string with the first character capitalized.
229
     * @param value A string.
230
     * @return A capitalized string.
231
     */
232
    public static String capitalizeString(String value) {
233
        if (value == null) {
2!
234
            return null;
×
235
        } else if (value.length() == 0) {
3!
236
            return "";
×
237
        } else {
238
            return Character.toTitleCase(value.charAt(0)) + value.substring(1);
9✔
239
        }
240
    }
241

242
    public static void returnItemToPlayer(Player player, ItemStack itemStack) {
243
        if (player.isAlive() && (!(player instanceof ServerPlayer) || !((ServerPlayer)player).hasDisconnected())) {
×
244
            player.getInventory().placeItemBackInInventory(itemStack);
×
245
        } else {
246
            player.drop(itemStack, false);
×
247
        }
248
    }
×
249

250
    public static interface IInterfaceRetriever {
251

252
        /**
253
         * Attempt to get a given interface instance.
254
         * @param world The world.
255
         * @param pos The position.
256
         * @param clazz The class to find.
257
         * @param <C> The class type.
258
         * @return The optional instance.
259
         */
260
        public <C> Optional<C> getInterface(BlockGetter world, BlockPos pos, Class<C> clazz);
261

262
    }
263

264
    @SuppressWarnings("unchecked")
265
    public static <T extends Exception, R> R sneakyThrow(Exception t) throws T {
266
        throw (T) t;
×
267
    }
268

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