Skip to content

Commit

Permalink
fix compile for forge 19.2
Browse files Browse the repository at this point in the history
and make makeReachAttribute() lazy
  • Loading branch information
rhysdh540 committed May 15, 2024
1 parent 48ddfcf commit 621a663
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import net.minecraftforge.registries.DeferredRegister;

import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;

import static dev.rdh.createunlimited.multiversion.SupportedMinecraftVersion.*;

@Mod(CreateUnlimited.ID)
public final class CreateUnlimitedForge {

static final DeferredRegister<ArgumentTypeInfo<?, ?>> ARGUMENTS = DeferredRegister.create(Registries.COMMAND_ARGUMENT_TYPE, CreateUnlimited.ID);
static final DeferredRegister<ArgumentTypeInfo<?, ?>> ARGUMENTS = DeferredRegister.create(getCommandArgumentTypeRegistry(), CreateUnlimited.ID);

public CreateUnlimitedForge() {
IEventBus modEventBus = FMLJavaModLoadingContext.get()
Expand All @@ -27,4 +30,21 @@ public CreateUnlimitedForge() {
modEventBus.addListener(Events.ClientModBusEvents::onLoadComplete);
CreateUnlimited.init();
}

@SuppressWarnings({"unchecked", "JavaReflectionMemberAccess", "RedundantSuppression"})
private static ResourceKey<Registry<ArgumentTypeInfo<?, ?>>> getCommandArgumentTypeRegistry() {
try {
if(v1_19_2 >= CURRENT) {
return (ResourceKey<Registry<ArgumentTypeInfo<?, ?>>>)
Registry.class.getDeclaredField("COMMAND_ARGUMENT_TYPE_REGISTRY").get(null);
} else if(v1_20_1 <= CURRENT) {
return (ResourceKey<Registry<ArgumentTypeInfo<?, ?>>>)
Class.forName("net.minecraft.core.registries.Registries").getDeclaredField("COMMAND_ARGUMENT_TYPE").get(null);
} else {
throw new IllegalStateException("Unsupported Minecraft version: " + CURRENT);
}
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
throw unchecked(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static boolean isDevEnv() {
return !FMLLoader.isProduction();
}

private static final RegistryObject<Attribute> reachAttribute = makeReachAttribute();
private static RegistryObject<Attribute> reachAttribute = null;

@SuppressWarnings({"unchecked", "JavaReflectionMemberAccess"})
private static RegistryObject<Attribute> makeReachAttribute() {
Expand All @@ -73,6 +73,9 @@ private static RegistryObject<Attribute> makeReachAttribute() {
}

public static Attribute getReachAttribute() {
if(reachAttribute == null) {
reachAttribute = makeReachAttribute();
}
return reachAttribute.get();
}

Expand Down

0 comments on commit 621a663

Please sign in to comment.