Skip to content

Commit

Permalink
fix: Cleanup some deprecations on Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jan 14, 2025
1 parent 3234310 commit 7c924e4
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public String getModName(String modId) {
public void initialize(String modId, ForgeLoadContext context, Runnable initializer) {
((ForgeBalmItems) items).register(modId, context.modEventBus());
((ForgeBalmEntities) entities).register(modId, context.modEventBus());
((ForgeBalmWorldGen) worldGen).register(modId, context.modEventBus());
((ForgeBalmStats) stats).register(modId, context.modEventBus());

initializer.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import net.blay09.mods.balm.api.client.keymappings.KeyModifier;
import net.blay09.mods.balm.api.client.keymappings.KeyModifiers;
import net.blay09.mods.balm.common.client.keymappings.CommonBalmKeyMappings;
import net.blay09.mods.balm.forge.client.rendering.ForgeBalmModels;
import net.minecraft.client.KeyMapping;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.client.settings.IKeyConflictContext;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -52,18 +51,14 @@ public boolean canResolve() {
return getRegistrations(identifier.getNamespace()).bakedStandaloneModels.containsKey(identifier);
}
};
getActiveRegistrations().additionalModels.add(identifier);
getRegistrations(identifier.getNamespace()).additionalModels.add(identifier);
return deferredModel;
}

public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.entity.BalmEntities;
import net.blay09.mods.balm.forge.DeferredRegisters;
import net.blay09.mods.balm.forge.world.ForgeBalmWorldGen;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -14,8 +13,6 @@
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -48,7 +45,7 @@ public <T extends Entity> DeferredObject<EntityType<T>> registerEntity(ResourceL
@Override
public <T extends LivingEntity> DeferredObject<EntityType<T>> registerEntity(ResourceLocation identifier, EntityType.Builder<T> typeBuilder, Supplier<AttributeSupplier.Builder> attributeBuilder) {
final var register = DeferredRegisters.get(Registries.ENTITY_TYPE, identifier.getNamespace());
final var registrations = getActiveRegistrations();
final var registrations = getRegistrations(identifier.getNamespace());
final var registryObject = register.register(identifier.getPath(), () -> {
EntityType<T> entityType = typeBuilder.build(ResourceKey.create(Registries.ENTITY_TYPE, identifier));
registrations.attributeSuppliers.put(entityType, attributeBuilder.get().build());
Expand All @@ -61,10 +58,6 @@ public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package net.blay09.mods.balm.forge.event;

import net.blay09.mods.balm.api.event.client.RecipesUpdatedEvent;
import net.blay09.mods.balm.api.event.TickPhase;
import net.blay09.mods.balm.api.event.TickType;
import net.blay09.mods.balm.api.event.client.*;
import net.blay09.mods.balm.api.event.client.RenderHandEvent;
import net.blay09.mods.balm.api.event.client.screen.*;
import net.minecraft.client.Minecraft;
import net.minecraft.core.RegistryAccess;
import net.minecraftforge.client.event.*;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.jetbrains.annotations.Nullable;

public class ForgeBalmClientEvents {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.item.BalmItems;
import net.blay09.mods.balm.forge.DeferredRegisters;
import net.blay09.mods.balm.forge.client.rendering.ForgeBalmModels;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -13,10 +12,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -53,7 +49,7 @@ public DeferredObject<Item> registerItem(Function<ResourceLocation, Item> suppli
final var register = DeferredRegisters.get(Registries.ITEM, identifier.getNamespace());
final var registryObject = register.register(identifier.getPath(), () -> supplier.apply(identifier));
if (creativeTab != null) {
getActiveRegistrations().creativeTabContents.put(creativeTab, () -> new ItemLike[]{registryObject.get()});
getRegistrations(identifier.getNamespace()).creativeTabContents.put(creativeTab, () -> new ItemLike[]{registryObject.get()});
}
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
}
Expand All @@ -63,7 +59,7 @@ public DeferredObject<CreativeModeTab> registerCreativeModeTab(Supplier<ItemStac
DeferredRegister<CreativeModeTab> register = DeferredRegisters.get(Registries.CREATIVE_MODE_TAB, identifier.getNamespace());
RegistryObject<CreativeModeTab> registryObject = register.register(identifier.getPath(), () -> {
Component displayName = Component.translatable("itemGroup." + identifier.toString().replace(':', '.'));
final var registrations = getActiveRegistrations();
final var registrations = getRegistrations(identifier.getNamespace());
return CreativeModeTab.builder()
.title(displayName)
.icon(iconSupplier)
Expand All @@ -75,22 +71,18 @@ public DeferredObject<CreativeModeTab> registerCreativeModeTab(Supplier<ItemStac

@Override
public void addToCreativeModeTab(ResourceLocation tabIdentifier, Supplier<ItemLike[]> itemsSupplier) {
getActiveRegistrations().creativeTabContents.put(tabIdentifier, itemsSupplier);
getRegistrations(tabIdentifier.getNamespace()).creativeTabContents.put(tabIdentifier, itemsSupplier);
}

@Override
public void setCreativeModeTabSorting(ResourceLocation tabIdentifier, Comparator<ItemLike> comparator) {
getActiveRegistrations().creativeTabSorting.put(tabIdentifier, comparator);
getRegistrations(tabIdentifier.getNamespace()).creativeTabSorting.put(tabIdentifier, comparator);
}

public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,13 @@ public void commonSetup(FMLCommonSetupEvent event) {

@Override
public void registerCustomStat(ResourceLocation identifier) {
getActiveRegistrations().customStats.add(identifier);
getRegistrations(identifier.getNamespace()).customStats.add(identifier);
}

public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.blay09.mods.balm.forge.DeferredRegisters;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -21,40 +20,18 @@
import net.minecraftforge.common.world.BiomeModifier;
import net.minecraftforge.common.world.ModifiableBiomeInfo;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import net.minecraftforge.server.ServerLifecycleHooks;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class ForgeBalmWorldGen implements BalmWorldGen {

private static class Registrations {
public final List<DeferredObject<?>> configuredFeatures = new ArrayList<>();
public final List<DeferredObject<?>> placedFeatures = new ArrayList<>();
public final List<DeferredObject<?>> placementModifiers = new ArrayList<>();

@SubscribeEvent
public void commonSetup(FMLCommonSetupEvent event) {
event.enqueueWork(() -> {
placementModifiers.forEach(DeferredObject::resolve);
configuredFeatures.forEach(DeferredObject::resolve);
placedFeatures.forEach(DeferredObject::resolve);
});
}
}

public static final MapCodec<BalmBiomeModifier> BALM_BIOME_MODIFIER_CODEC = MapCodec.unit(BalmBiomeModifier.INSTANCE);
private final Map<String, Registrations> registrations = new ConcurrentHashMap<>();

public ForgeBalmWorldGen() {
MinecraftForge.EVENT_BUS.register(this);
Expand All @@ -69,13 +46,9 @@ public <T extends Feature<?>> DeferredObject<T> registerFeature(ResourceLocation

@Override
public <T extends PlacementModifierType<?>> DeferredObject<T> registerPlacementModifier(ResourceLocation identifier, Supplier<T> supplier) {
DeferredObject<T> deferredObject = new DeferredObject<>(identifier, () -> {
T placementModifierType = supplier.get();
Registry.register(BuiltInRegistries.PLACEMENT_MODIFIER_TYPE, identifier, placementModifierType);
return placementModifierType;
});
getActiveRegistrations().placementModifiers.add(deferredObject);
return deferredObject;
final var register = DeferredRegisters.get(Registries.PLACEMENT_MODIFIER_TYPE, identifier.getNamespace());
RegistryObject<T> registryObject = register.register(identifier.getPath(), supplier);
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
}

@Override
Expand Down Expand Up @@ -108,18 +81,6 @@ public void modifyBiome(Holder<Biome> biome, BiomeModifier.Phase phase, Modifiab
}
}

public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}

public static void initializeBalmBiomeModifiers(IEventBus modEventBus) {
var registry = DeferredRegister.create(ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, "balm");
registry.register("balm", () -> BALM_BIOME_MODIFIER_CODEC);
Expand Down

0 comments on commit 7c924e4

Please sign in to comment.