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

CyclopsMC / IntegratedDynamics / 15795349141

21 Jun 2025 11:31AM UTC coverage: 45.136% (-0.03%) from 45.164%
15795349141

push

github

rubensworks
Merge remote-tracking branch 'origin/master-1.21-lts' into master-1.21

2565 of 8514 branches covered (30.13%)

Branch coverage included in aggregate %.

11917 of 23571 relevant lines covered (50.56%)

2.4 hits per line

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

59.62
/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.network.chat.Component;
12
import net.minecraft.resources.ResourceLocation;
13
import net.minecraft.server.level.ServerPlayer;
14
import net.minecraft.tags.TagKey;
15
import net.minecraft.world.entity.Entity;
16
import net.minecraft.world.entity.player.Player;
17
import net.minecraft.world.item.BlockItem;
18
import net.minecraft.world.item.Item;
19
import net.minecraft.world.item.ItemStack;
20
import net.minecraft.world.level.BlockGetter;
21
import net.minecraft.world.level.Level;
22
import net.minecraft.world.level.block.Block;
23
import net.minecraft.world.level.block.LiquidBlock;
24
import net.minecraft.world.level.block.state.BlockState;
25
import net.minecraft.world.level.material.Fluid;
26
import net.neoforged.neoforge.fluids.FluidStack;
27
import net.neoforged.neoforge.fluids.FluidUtil;
28
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
29
import org.cyclops.cyclopscore.datastructure.DimPos;
30
import org.cyclops.cyclopscore.helper.IModHelpers;
31
import org.cyclops.cyclopscore.helper.IModHelpersNeoForge;
32

33
import java.util.List;
34
import java.util.Locale;
35
import java.util.Optional;
36
import java.util.stream.Stream;
37

38
/**
39
 * Helper methods.
40
 * @author rubensworks
41
 */
42
public final class Helpers {
×
43

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

46
    /**
47
     * Get the fluidstack from the given itemstack.
48
     * @param itemStack The itemstack.
49
     * @return The fluidstack or null.
50
     */
51
    public static FluidStack getFluidStack(ItemStack itemStack) {
52
        FluidStack fluidStack = FluidUtil.getFluidContained(itemStack).orElse(FluidStack.EMPTY);
6✔
53
        if (fluidStack.isEmpty()
4✔
54
                && itemStack.getItem() instanceof BlockItem
3!
55
                && ((BlockItem) itemStack.getItem()).getBlock() instanceof LiquidBlock) {
×
56
            fluidStack = new FluidStack(((LiquidBlock) ((BlockItem) itemStack.getItem()).getBlock()).fluid, IModHelpersNeoForge.get().getFluidHelpers().getBucketVolume());
×
57
        }
58
        return fluidStack;
2✔
59
    }
60

61
    /**
62
     * Get the fluidstack capacity from the given itemstack.
63
     * @param itemStack The itemstack.
64
     * @return The capacity
65
     */
66
    public static int getFluidStackCapacity(ItemStack itemStack) {
67
        IFluidHandler fluidHandler = FluidUtil.getFluidHandler(itemStack).orElse(null);
6✔
68
        if (fluidHandler != null) {
2✔
69
            if (fluidHandler.getTanks() > 0) {
3!
70
                return fluidHandler.getTankCapacity(0);
4✔
71
            }
72
        }
73
        return 0;
2✔
74
    }
75

76
    /**
77
     * Retrieves a Stream of items that are registered to this tag name.
78
     *
79
     * @param name The tag name
80
     * @return A Stream containing ItemStacks registered for this tag
81
     */
82
    public static Stream<ItemStack> getTagValues(String name) throws ResourceLocationException {
83
        Optional<HolderSet.Named<Item>> tag = BuiltInRegistries.ITEM
3✔
84
                .get(TagKey.create(Registries.ITEM, ResourceLocation.parse(name)));
4✔
85
        return tag.stream().flatMap(named -> named.stream().map(ItemStack::new));
10✔
86
    }
87

88
    /**
89
     * Retrieves a Stream of blocks that are registered to this tag name.
90
     *
91
     * @param name The tag name
92
     * @return A Stream containing Blocks registered for this tag
93
     */
94
    public static Stream<BlockState> getBlockTagValues(String name) throws ResourceLocationException {
95
        Optional<HolderSet.Named<Block>> tag = BuiltInRegistries.BLOCK
3✔
96
                .get(TagKey.create(Registries.BLOCK, ResourceLocation.parse(name)));
4✔
97
        return tag.stream().flatMap(named -> named.stream().map(block -> block.value().defaultBlockState()));
15✔
98
    }
99

100
    /**
101
     * Retrieves a Stream of fluids that are registered to this tag name.
102
     *
103
     * @param name The tag name
104
     * @return A Stream containing Fluids registered for this tag
105
     */
106
    public static Stream<FluidStack> getFluidTagValues(String name) throws ResourceLocationException {
107
        Optional<HolderSet.Named<Fluid>> tag = BuiltInRegistries.FLUID
3✔
108
                .get(TagKey.create(Registries.FLUID, ResourceLocation.parse(name)));
4✔
109
        return tag.stream().flatMap(named -> named.stream().map(fluid -> new FluidStack(fluid, IModHelpersNeoForge.get().getFluidHelpers().getBucketVolume())));
18✔
110
    }
111

112
    /**
113
     * Add the given element to a copy of the given list/
114
     * @param list The list.
115
     * @param newElement The element.
116
     * @param <T> The type.
117
     * @return The new joined list.
118
     */
119
    public static <T> List<T> joinList(List<T> list, T newElement) {
120
        ImmutableList.Builder<T> builder = ImmutableList.<T>builder().addAll(list);
4✔
121
        if(newElement != null) {
2✔
122
            builder.add(newElement);
4✔
123
        }
124
        return builder.build();
3✔
125
    }
126

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

146
    private static final List<IInterfaceRetriever> INTERFACE_RETRIEVERS = Lists.newArrayList();
2✔
147
    static {
148
        addInterfaceRetriever(new IInterfaceRetriever() {
7✔
149
            @Override
150
            public <C> Optional<C> getInterface(BlockGetter world, BlockPos pos, Class<C> clazz) {
151
                return IModHelpers.get().getBlockEntityHelpers().get(world, pos, clazz);
×
152
            }
153
        });
154
    }
1✔
155

156
    /**
157
     * Check for the given interface at the given position.
158
     * @param world The world.
159
     * @param pos The position.
160
     * @param clazz The class to find.
161
     * @param <C> The class type.
162
     * @return The optional instance.
163
     */
164
    private static <C> Optional<C> getInterface(BlockGetter world, BlockPos pos, Class<C> clazz) {
165
        for(IInterfaceRetriever interfaceRetriever : INTERFACE_RETRIEVERS) {
×
166
            Optional<C> optionalInstance = interfaceRetriever.getInterface(world, pos, clazz);
×
167
            if(optionalInstance.isPresent()) {
×
168
                return optionalInstance;
×
169
            }
170
        }
×
171
        return Optional.empty();
×
172
    }
173

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

187
    /**
188
     * Get a localized string showing the ratio of stored energy vs the capacity.
189
     * @param stored The stored amount of energy.
190
     * @param capacity The capacity of the energy container.
191
     * @return The localized string.
192
     */
193
    public static Component getLocalizedEnergyLevel(int stored, int capacity) {
194
        return Component.literal(String.format(Locale.ROOT, "%,d", stored))
×
195
                .append(" / ")
×
196
                .append(String.format(Locale.ROOT, "%,d", capacity))
×
197
                .append(" ")
×
198
                .append(Component.translatable(L10NValues.GENERAL_ENERGY_UNIT));
×
199
    }
200

201
    // This is copied from Forge's TPSCommand
202
    public static double calculateTps(long[] times) {
203
        double worldTickTime = mean(times) * 1.0E-6D;
6✔
204
        double worldTPS = Math.min(1000.0 / worldTickTime, 20);
6✔
205
        return worldTPS;
2✔
206
    }
207

208
    public static long mean(long[] values) {
209
        long sum = 0L;
2✔
210
        for (long v : values)
16✔
211
            sum += v;
4✔
212
        return sum / values.length;
6✔
213
    }
214

215
    public static void addInterfaceRetriever(IInterfaceRetriever interfaceRetriever) {
216
        INTERFACE_RETRIEVERS.add(interfaceRetriever);
4✔
217
    }
1✔
218

219
    /**
220
     * Return a string with the first character capitalized.
221
     * @param value A string.
222
     * @return A capitalized string.
223
     */
224
    public static String capitalizeString(String value) {
225
        if (value == null) {
2!
226
            return null;
×
227
        } else if (value.length() == 0) {
3!
228
            return "";
×
229
        } else {
230
            return Character.toTitleCase(value.charAt(0)) + value.substring(1);
9✔
231
        }
232
    }
233

234
    public static void returnItemToPlayer(Player player, ItemStack itemStack) {
235
        if (player.isAlive() && (!(player instanceof ServerPlayer) || !((ServerPlayer)player).hasDisconnected())) {
×
236
            player.getInventory().placeItemBackInInventory(itemStack);
×
237
        } else {
238
            player.drop(itemStack, false);
×
239
        }
240
    }
×
241

242
    public static interface IInterfaceRetriever {
243

244
        /**
245
         * Attempt to get a given interface instance.
246
         * @param world The world.
247
         * @param pos The position.
248
         * @param clazz The class to find.
249
         * @param <C> The class type.
250
         * @return The optional instance.
251
         */
252
        public <C> Optional<C> getInterface(BlockGetter world, BlockPos pos, Class<C> clazz);
253

254
    }
255

256
    @SuppressWarnings("unchecked")
257
    public static <T extends Exception, R> R sneakyThrow(Exception t) throws T {
258
        throw (T) t;
×
259
    }
260

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