Skip to content

Commit e3b9b69

Browse files
committed
Added helper methods to LibTags to get tag values or a default when they aren't present.
1 parent 83757be commit e3b9b69

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## New Features
22
- Added `IngredientStack` - A class designed to help in recipe serialization for machines that take non-one amounts of inputs of both items and fluids.
3+
- Added helper methods to `LibTags` to get tag values or a default when they aren't present.
34

45
## Bug Fixes
56
- Fixed connected texture baked model not ported correctly

src/main/java/net/pedroksl/ae2addonlib/registry/helpers/LibTags.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package net.pedroksl.ae2addonlib.registry.helpers;
22

3+
import java.util.function.BiFunction;
4+
35
import net.minecraft.nbt.CompoundTag;
6+
import net.minecraft.world.item.ItemStack;
47

58
/**
69
* <p>Lib provided component set.</p>
@@ -18,4 +21,45 @@ public final class LibTags {
1821
* An integer tag to save tint color data in an {@link net.minecraft.world.item.ItemStack}.
1922
*/
2023
public static final String TINT_COLOR_TAG = "tint_color";
24+
25+
/**
26+
* Helper function to get an int tag with a default value return for non-existent tags.
27+
* @param stack The stack to read tags from.
28+
* @param tagId The id of the tag to read.
29+
* @param defaultValue The default value to return for non-existent tags.
30+
* @return The tag value or the default value.
31+
*/
32+
public static int getIntOrDefault(ItemStack stack, String tagId, int defaultValue) {
33+
return getTagOrDefault(stack, tagId, defaultValue, CompoundTag::getInt);
34+
}
35+
36+
/**
37+
* Helper function to get an int tag with a default value return for non-existent tags.
38+
* @param stack The stack to read tags from.
39+
* @param tagId The id of the tag to read.
40+
* @param defaultValue The default value to return for non-existent tags.
41+
* @return The tag value or the default value.
42+
*/
43+
public static boolean getBooleanOrDefault(ItemStack stack, String tagId, boolean defaultValue) {
44+
return getTagOrDefault(stack, tagId, defaultValue, CompoundTag::getBoolean);
45+
}
46+
47+
/**
48+
* Generic helper function to get a tag with a default value return for non-existent tags.
49+
* @param stack The stack to read tags from.
50+
* @param tagId The id of the tag to read.
51+
* @param defaultValue The default value to return for non-existent tags.
52+
* @param getter Bi function called when reading the tag for the desired value type.
53+
* @param <V> Class of the value returned.
54+
* @return The tag value or the default value.
55+
*/
56+
public static <V> V getTagOrDefault(
57+
ItemStack stack, String tagId, V defaultValue, BiFunction<CompoundTag, String, V> getter) {
58+
var tag = stack.getOrCreateTag();
59+
if (tag.contains(tagId)) {
60+
return getter.apply(tag, tagId);
61+
} else {
62+
return defaultValue;
63+
}
64+
}
2165
}

0 commit comments

Comments
 (0)