Use public fluid tag container getter instead of reflection (#552)

This commit is contained in:
Virtuoel 2020-04-05 17:07:54 +03:00 committed by GitHub
parent 655c8d6a5c
commit 94ecb00e4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,8 +16,6 @@
package net.fabricmc.fabric.api.tag; package net.fabricmc.fabric.api.tag;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.function.Supplier; import java.util.function.Supplier;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -53,35 +51,10 @@ public final class TagRegistry {
} }
public static Tag<Fluid> fluid(Identifier id) { public static Tag<Fluid> fluid(Identifier id) {
return create(id, TagRegistry::getFluidTagContainer); return create(id, FluidTags::getContainer);
} }
public static Tag<Item> item(Identifier id) { public static Tag<Item> item(Identifier id) {
return create(id, ItemTags::getContainer); return create(id, ItemTags::getContainer);
} }
private static Field fluidTagContainer;
private static TagContainer<Fluid> getFluidTagContainer() {
if (fluidTagContainer == null) {
for (Field f : FluidTags.class.getDeclaredFields()) {
if ((f.getModifiers() & Modifier.STATIC) != 0 && f.getType() == TagContainer.class) {
f.setAccessible(true);
fluidTagContainer = f;
break;
}
}
if (fluidTagContainer == null) {
throw new RuntimeException("Could not find FluidTags.container!");
}
}
try {
//noinspection unchecked
return (TagContainer<Fluid>) fluidTagContainer.get(null);
} catch (IllegalAccessException e) {
throw new RuntimeException("Could not access FluidTags.container (" + fluidTagContainer.getName() + ")!", e);
}
}
} }