Skip to content

Commit 5377407

Browse files
committed
Tag support for massRegistryGet
1 parent d28024d commit 5377407

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed
Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,75 @@
11
package org.violetmoon.zeta.util;
22

3-
import java.util.Collection;
4-
import java.util.Collections;
5-
import java.util.List;
6-
import java.util.Objects;
7-
import java.util.function.Function;
8-
9-
import org.jetbrains.annotations.Nullable;
10-
import org.violetmoon.zeta.Zeta;
11-
import org.violetmoon.zeta.block.IZetaBlock;
12-
3+
import com.google.common.collect.ImmutableList;
134
import net.minecraft.core.Holder;
145
import net.minecraft.core.Registry;
156
import net.minecraft.core.RegistryAccess;
167
import net.minecraft.core.registries.BuiltInRegistries;
178
import net.minecraft.resources.ResourceLocation;
189
import net.minecraft.tags.TagKey;
1910
import net.minecraft.world.level.block.Block;
11+
import org.jetbrains.annotations.Nullable;
12+
import org.violetmoon.zeta.Zeta;
13+
import org.violetmoon.zeta.block.IZetaBlock;
14+
15+
import java.util.Collection;
16+
import java.util.Collections;
17+
import java.util.List;
18+
import java.util.function.Function;
2019

2120
public class RegistryUtil {
22-
23-
public static <T> List<T> massRegistryGet(Collection<String> coll, Registry<T> reg) {
24-
return coll.stream().map(ResourceLocation::new).map(reg::get).filter(Objects::nonNull).toList();
25-
}
26-
27-
public static <T> List<T> getTagValues(RegistryAccess access, TagKey<T> tag) {
28-
return access.registryOrThrow(tag.registry())
29-
.getTag(tag)
30-
.map(holderset -> holderset.stream().map(Holder::value).toList()) //tag exists, grab all items from it
31-
.orElseGet(Collections::emptyList); //tag doesn't exist
32-
}
33-
34-
//TODO: Can be made more 'static' when there's a nicer way to get a block's ID, instead of having to consult a particular Zeta
35-
// (this is one reason i want to write the fancier block registry system - quat)
36-
// This is just Duct taped as fuck to get quark-specific stuff out of IQuarkBlock so i can make it IZetaBlock
37-
38-
protected final Zeta z;
39-
40-
public RegistryUtil(Zeta z) {
41-
this.z = z;
42-
}
43-
44-
public @Nullable String inheritQuark(IZetaBlock parent, String format) {
45-
return inherit(parent.getBlock(), format);
46-
}
47-
48-
public @Nullable String inherit(Block parent, String format) {
49-
ResourceLocation parentName = z.registry.getRegistryName(parent, BuiltInRegistries.BLOCK);
50-
if(parentName == null)
51-
return null;
52-
else
53-
return String.format(String.format("%s:%s", z.modid, format), parentName.getPath());
54-
}
55-
56-
public @Nullable String inherit(Block parent, Function<String, String> fun) {
57-
ResourceLocation parentName = z.registry.getRegistryName(parent, BuiltInRegistries.BLOCK);
58-
if(parentName == null)
59-
return null;
60-
else
61-
return String.format(String.format("%s:%s", z.modid, fun.apply(parentName.getPath())));
62-
}
21+
22+
/**
23+
* Gets a list of objects from a collection of strings. These can be either IDS or Tags (prefixed by '#').
24+
* For tags to work tho you HAVE to call this AFTER the tags are loaded.
25+
*/
26+
public static <T> List<T> massRegistryGet(Collection<String> coll, Registry<T> reg) {
27+
ImmutableList.Builder<T> builder = new ImmutableList.Builder<>();
28+
for (String s : coll) {
29+
if (s.startsWith("#")) {
30+
TagKey<T> tag = new TagKey<>(reg.key(), new ResourceLocation(s.substring(1)));
31+
reg.getTagOrEmpty(tag).forEach(tHolder -> builder.add(tHolder.value()));
32+
} else {
33+
reg.getOptional(new ResourceLocation(s)).ifPresent(builder::add);
34+
}
35+
}
36+
return builder.build();
37+
}
38+
39+
public static <T> List<T> getTagValues(RegistryAccess access, TagKey<T> tag) {
40+
return access.registryOrThrow(tag.registry())
41+
.getTag(tag)
42+
.map(holderset -> holderset.stream().map(Holder::value).toList()) //tag exists, grab all items from it
43+
.orElseGet(Collections::emptyList); //tag doesn't exist
44+
}
45+
46+
//TODO: Can be made more 'static' when there's a nicer way to get a block's ID, instead of having to consult a particular Zeta
47+
// (this is one reason i want to write the fancier block registry system - quat)
48+
// This is just Duct taped as fuck to get quark-specific stuff out of IQuarkBlock so i can make it IZetaBlock
49+
50+
protected final Zeta z;
51+
52+
public RegistryUtil(Zeta z) {
53+
this.z = z;
54+
}
55+
56+
public @Nullable String inheritQuark(IZetaBlock parent, String format) {
57+
return inherit(parent.getBlock(), format);
58+
}
59+
60+
public @Nullable String inherit(Block parent, String format) {
61+
ResourceLocation parentName = z.registry.getRegistryName(parent, BuiltInRegistries.BLOCK);
62+
if (parentName == null)
63+
return null;
64+
else
65+
return String.format(String.format("%s:%s", z.modid, format), parentName.getPath());
66+
}
67+
68+
public @Nullable String inherit(Block parent, Function<String, String> fun) {
69+
ResourceLocation parentName = z.registry.getRegistryName(parent, BuiltInRegistries.BLOCK);
70+
if (parentName == null)
71+
return null;
72+
else
73+
return String.format(String.format("%s:%s", z.modid, fun.apply(parentName.getPath())));
74+
}
6375
}

0 commit comments

Comments
 (0)