Skip to content

Commit

Permalink
Make DeferredHolderCommon work with IForgeRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Sep 24, 2024
1 parent 117a0a8 commit f3a4fe8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
Expand All @@ -26,6 +27,10 @@
* @param <T> The type of object being held by this DeferredHolder.
*/
public class DeferredHolderCommon<R, T extends R> implements Holder<R> {

// Modloaders such as Forge override this, to also gain access to IForgeRegistry's.
public static Function<ResourceKey<?>, Holder<?>> BIND_OVERRIDE = null;

/**
* Creates a new DeferredHolder targeting the value with the specified name in the specified registry.
*
Expand Down Expand Up @@ -137,6 +142,14 @@ protected Registry<R> getRegistry() {
protected final void bind(boolean throwOnMissingRegistry) {
if (this.holder != null) return;

// Check if we have an override for specific mod loaders
if (DeferredHolderCommon.BIND_OVERRIDE != null) {
this.holder = (Holder<R>) DeferredHolderCommon.BIND_OVERRIDE.apply(this.key);
if (this.holder != null) {
return;
}
}

Registry<R> registry = getRegistry();
if (registry != null) {
this.holder = registry.getHolder(this.key).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.Holder;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.RegistryManager;
import org.cyclops.cyclopscore.advancement.criterion.GuiContainerOpenTriggerConfig;
import org.cyclops.cyclopscore.advancement.criterion.GuiContainerOpenTriggerEventHooksForge;
import org.cyclops.cyclopscore.advancement.criterion.ItemCraftedTriggerConfig;
Expand All @@ -24,6 +27,7 @@
import org.cyclops.cyclopscore.component.DataComponentCapacityConfig;
import org.cyclops.cyclopscore.component.DataComponentEnergyStorageConfig;
import org.cyclops.cyclopscore.config.ConfigHandlerCommon;
import org.cyclops.cyclopscore.config.DeferredHolderCommon;
import org.cyclops.cyclopscore.helper.CyclopsCoreInstance;
import org.cyclops.cyclopscore.init.ModBaseForge;
import org.cyclops.cyclopscore.inventory.IRegistryInventoryLocation;
Expand Down Expand Up @@ -54,6 +58,18 @@ public CyclopsCoreForge() {
});

getRegistryManager().addRegistry(IRegistryInventoryLocation.class, RegistryInventoryLocation.getInstance());

// Make DeferredHolderCommon compatible with IForgeRegistry's.
DeferredHolderCommon.BIND_OVERRIDE = (key) -> {
ForgeRegistry<Object> registry = RegistryManager.ACTIVE.getRegistry(key.registry());
if (registry != null) {
Object value = registry.getValue(key.location());
if (value != null) {
return Holder.direct(value);
}
}
return null;
};
}

@Override
Expand Down

0 comments on commit f3a4fe8

Please sign in to comment.