Skip to content

Commit 375074d

Browse files
committedNov 25, 2021
add TagInjector for adding to tags at runtime
1 parent 65bdf6c commit 375074d

File tree

7 files changed

+103
-13
lines changed

7 files changed

+103
-13
lines changed
 

‎build.gradle

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id 'io.github.juuxel.loom-quiltflower-mini' version '1.1.0'
55
}
66

7+
// TODO: declare mc dependency correctly
78
version = "${project.mod_version}+${project.minecraft_base_version}"
89
group = project.maven_group
910

@@ -46,7 +47,7 @@ dependencies {
4647
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
4748

4849
modCompileOnly "me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}"
49-
modRuntime "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"
50+
modRuntimeOnly "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"
5051

5152
testmodImplementation sourceSets.main.output
5253
}
@@ -96,18 +97,14 @@ javadoc {
9697
options.addStringOption("Xdoclint:-missing", "-quiet")
9798
}
9899

99-
// configure the maven publication
100100
publishing {
101101
publications {
102102
mavenJava(MavenPublication) {
103103
from components.java
104104
}
105105
}
106106

107-
// select the repositories you want to publish to
108107
repositories {
109-
// uncomment to publish to the local maven
110-
// mavenLocal()
111108
maven {
112109
url ENV.MAVEN_URL
113110
credentials {

‎gradle.properties

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G
33
# Fabric Properties
44
# check these on https://modmuss50.me/fabric.html
55
minecraft_base_version=1.18
6-
minecraft_version=21w40a
7-
yarn_mappings=21w40a+build.6
8-
loader_version=0.12.1
6+
minecraft_version=1.18-pre8
7+
yarn_mappings=1.18-pre8+build.2
8+
loader_version=0.12.5
99
# Mod Properties
10-
mod_version=0.3.5
10+
mod_version=0.3.6
1111
maven_group=io.wispforest
12-
archives_base_name=owo
12+
archives_base_name=owo-lib
1313
# Dependencies
1414
# check this on https://modmuss50.me/fabric.html
15-
fabric_version=0.40.8+1.18
15+
fabric_version=0.43.0+1.18
1616

1717
# https://www.curseforge.com/minecraft/mc-mods/roughly-enough-items/files
1818
rei_version=7.0.339
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.wispforest.owo.mixin;
2+
3+
import io.wispforest.owo.util.TagInjector;
4+
import net.minecraft.resource.ResourceManager;
5+
import net.minecraft.tag.Tag;
6+
import net.minecraft.tag.TagGroupLoader;
7+
import net.minecraft.util.Identifier;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
12+
13+
import java.util.Map;
14+
15+
@Mixin(TagGroupLoader.class)
16+
public class TagGroupLoaderMixin {
17+
18+
@Inject(method = "loadTags", at = @At("TAIL"))
19+
public void injectValues(ResourceManager manager, CallbackInfoReturnable<Map<Identifier, Tag.Builder>> cir) {
20+
var map = cir.getReturnValue();
21+
22+
TagInjector.ADDITIIONS.forEach((identifier, identifiers) -> {
23+
var builder = map.computeIfAbsent(identifier, identifier1 -> new Tag.Builder());
24+
identifiers.forEach(addition -> builder.add(addition, "owo"));
25+
});
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.wispforest.owo.util;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.item.Item;
5+
import net.minecraft.util.Identifier;
6+
import net.minecraft.util.registry.Registry;
7+
8+
import java.util.*;
9+
10+
/**
11+
* A simple utility for inserting values into Tags at runtime
12+
*/
13+
public class TagInjector {
14+
15+
public static final HashMap<Identifier, Set<Identifier>> ADDITIIONS = new HashMap<>();
16+
17+
/**
18+
* Injects the given Identifiers into the given Tag.
19+
* If the Identifiers don't correspond to an object in the
20+
* relevant Registry, you <i>will</i> break the Tag.
21+
* If the Tag does not exist, it will be created.
22+
*
23+
* @param tag The tag to insert into, this could contain all kinds of values
24+
* @param values The values to insert
25+
*/
26+
public static void injectRaw(Identifier tag, Collection<Identifier> values) {
27+
ADDITIIONS.computeIfAbsent(tag, identifier -> new HashSet<>()).addAll(values);
28+
}
29+
30+
public static void injectRaw(Identifier tag, Identifier... values) {
31+
injectRaw(tag, Arrays.asList(values));
32+
}
33+
34+
/**
35+
* A convenience method for directly inserting Blocks into the given Tag.
36+
* If any of Blocks are not registered yet, this <i>will</i> throw an
37+
* exception.
38+
*/
39+
public static void injectBlocks(Identifier tag, Collection<Block> values) {
40+
injectRaw(tag, values.stream().map(Registry.BLOCK::getId).toList());
41+
}
42+
43+
public static void injectBlocks(Identifier tag, Block... values) {
44+
injectRaw(tag, Arrays.stream(values).map(Registry.BLOCK::getId).toList());
45+
}
46+
47+
/**
48+
* A convenience method for directly inserting Items into the given Tag.
49+
* If any of Items are not registered yet, this <i>will</i> throw an
50+
* exception.
51+
*/
52+
public static void injectItems(Identifier tag, Collection<Item> values) {
53+
injectRaw(tag, values.stream().map(Registry.ITEM::getId).toList());
54+
}
55+
56+
public static void injectItems(Identifier tag, Item... values) {
57+
injectRaw(tag, Arrays.stream(values).map(Registry.ITEM::getId).toList());
58+
}
59+
60+
}

‎src/main/resources/owo.mixins.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"ItemMixin",
99
"ItemSettingsMixin",
1010
"LevelInfoMixin",
11-
"ScreenHandlerInvoker"
11+
"ScreenHandlerInvoker",
12+
"TagGroupLoaderMixin"
1213
],
1314
"client": [
1415
"CreativeInventoryScreenMixin",

‎src/testmod/java/io/wispforest/uwu/Uwu.java

+4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import io.wispforest.owo.itemgroup.gui.ItemGroupButton;
66
import io.wispforest.owo.itemgroup.gui.ItemGroupTab;
77
import io.wispforest.owo.registration.reflect.FieldRegistrationHandler;
8+
import io.wispforest.owo.util.TagInjector;
89
import io.wispforest.uwu.items.UwuItems;
910
import net.fabricmc.api.ModInitializer;
1011
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
1112
import net.fabricmc.fabric.api.tag.TagFactory;
13+
import net.minecraft.block.Blocks;
1214
import net.minecraft.client.MinecraftClient;
1315
import net.minecraft.item.Item;
1416
import net.minecraft.item.ItemGroup;
@@ -75,6 +77,8 @@ public void onInitialize() {
7577

7678
FieldRegistrationHandler.register(UwuItems.class, "uwu", true);
7779

80+
TagInjector.injectBlocks(new Identifier("base_stone_overworld"), Blocks.GLASS);
81+
7882
FOUR_TAB_GROUP.initialize();
7983
SIX_TAB_GROUP.initialize();
8084
}

0 commit comments

Comments
 (0)
Please sign in to comment.