Skip to content

Commit

Permalink
refactor(neoforge)!: Update to new NeoForge networking API
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jan 14, 2024
1 parent 401560c commit 2735230
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 167 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ forge_version_range=[49,)
forge_loader_version_range=[49,)

# NeoForge
neoforge_version = 20.4.47-beta
neoforge_version = 20.4.96-beta
neoforge_version_range=[20.4,)
neoforge_loader_version_range=[1,)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public NeoForgeBalm(IEventBus modBus) {
((AbstractBalmConfig) Balm.getConfig()).initialize();
ExampleConfig.initialize();

NeoForgeBalmWorldGen.initializeBalmBiomeModifiers();
NeoForgeBalmWorldGen.initializeBalmBiomeModifiers(modBus);
modBus.addListener(NeoForgeBalmClient::onInitializeClient);

NeoForgeBalmProviders providers = (NeoForgeBalmProviders) Balm.getProviders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,16 @@ public String getModName(String modId) {
}

@Override
@Deprecated
public void initialize(String modId, Runnable initializer) {
initialize(modId, new NeoForgeLoadContext(FMLJavaModLoadingContext.get().getModEventBus()), initializer);
}

@Override
public void initialize(String modId, NeoForgeLoadContext context, Runnable initializer) {
((NeoForgeBalmItems) items).register();
((NeoForgeBalmEntities) entities).register();
((NeoForgeBalmStats) stats).register();
((NeoForgeBalmNetworking) networking).register(modId, context.modBus());
((NeoForgeBalmEntities) entities).register(modId, context.modBus());
((NeoForgeBalmStats) stats).register(modId, context.modBus());

initializer.run();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package net.blay09.mods.balm.neoforge.client;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.BalmRuntimeLoadContext;
import net.blay09.mods.balm.api.client.BalmClientRuntime;
import net.blay09.mods.balm.api.client.keymappings.BalmKeyMappings;
import net.blay09.mods.balm.api.client.rendering.BalmModels;
import net.blay09.mods.balm.api.client.rendering.BalmRenderers;
import net.blay09.mods.balm.api.client.rendering.BalmTextures;
import net.blay09.mods.balm.api.client.screen.BalmScreens;
import net.blay09.mods.balm.neoforge.NeoForgeLoadContext;
import net.blay09.mods.balm.neoforge.event.NeoForgeBalmEvents;
import net.blay09.mods.balm.neoforge.client.keymappings.NeoForgeBalmKeyMappings;
import net.blay09.mods.balm.neoforge.client.rendering.NeoForgeBalmModels;
import net.blay09.mods.balm.neoforge.client.rendering.NeoForgeBalmRenderers;
import net.blay09.mods.balm.neoforge.client.rendering.NeoForgeBalmTextures;
import net.blay09.mods.balm.neoforge.client.screen.NeoForgeBalmScreens;
import net.blay09.mods.balm.neoforge.event.NeoForgeBalmClientEvents;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;

public class NeoForgeBalmClientRuntime implements BalmClientRuntime {
public class NeoForgeBalmClientRuntime implements BalmClientRuntime<NeoForgeLoadContext> {

private final BalmRenderers renderers = new NeoForgeBalmRenderers();
private final BalmTextures textures = new NeoForgeBalmTextures();
Expand Down Expand Up @@ -54,10 +57,15 @@ public BalmKeyMappings getKeyMappings() {

@Override
public void initialize(String modId, Runnable initializer) {
((NeoForgeBalmRenderers) renderers).register();
((NeoForgeBalmScreens) screens).register();
((NeoForgeBalmModels) models).register();
((NeoForgeBalmKeyMappings) keyMappings).register();
initialize(modId, new NeoForgeLoadContext(FMLJavaModLoadingContext.get().getModEventBus()), initializer);
}

@Override
public void initialize(String modId, NeoForgeLoadContext context, Runnable initializer) {
((NeoForgeBalmRenderers) renderers).register(modId, context.modBus());
((NeoForgeBalmScreens) screens).register(modId, context.modBus());
((NeoForgeBalmModels) models).register(modId, context.modBus());
((NeoForgeBalmKeyMappings) keyMappings).register(modId, context.modBus());

initializer.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.settings.IKeyConflictContext;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -109,12 +109,16 @@ private static net.neoforged.neoforge.client.settings.KeyModifier toForge(KeyMod
};
}

public void register() {
FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.neoforge.client.event.ModelEvent;
import net.neoforged.neoforge.client.model.SimpleModelState;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -177,12 +177,16 @@ public UnbakedModel getUnbakedMissingModel() {
return modelBakery.getModel(ModelBakery.MISSING_MODEL_LOCATION);
}

public void register() {
FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;

Expand Down Expand Up @@ -131,11 +131,15 @@ public void setBlockRenderType(Supplier<Block> block, RenderType renderType) {
// so we have to have both this call (for Fabric) and change the JSON (for Forge).
}

public void register() {
FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -55,11 +55,15 @@ public AbstractWidget addRenderableWidget(Screen screen, AbstractWidget widget)
return widget;
}

public void register() {
FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -55,11 +54,15 @@ public <T extends LivingEntity> DeferredObject<EntityType<T>> registerEntity(Res
return new DeferredObject<>(identifier, registryObject, registryObject::isBound);
}

public void register() {
FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
public void register(String modId, IEventBus eventBus) {
eventBus.register(getRegistrations(modId));
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
return getRegistrations(ModLoadingContext.get().getActiveNamespace());
}

private Registrations getRegistrations(String modId) {
return registrations.computeIfAbsent(modId, it -> new Registrations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static void registerEvents(NeoForgeBalmEvents events) {
orig.getButton());
events.fireEventHandlers(priority, event);
if (event.isCanceled()) {
event.setResult(Event.Result.ALLOW);
orig.setResult(Event.Result.ALLOW);
}
});
});
Expand Down Expand Up @@ -206,7 +206,7 @@ public static void registerEvents(NeoForgeBalmEvents events) {
orig.getButton());
events.fireEventHandlers(priority, event);
if (event.isCanceled()) {
event.setResult(Event.Result.ALLOW);
orig.setResult(Event.Result.ALLOW);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import org.jetbrains.annotations.Nullable;

import java.util.*;
Expand Down Expand Up @@ -77,11 +76,6 @@ public void addToCreativeModeTab(ResourceLocation tabIdentifier, Supplier<ItemLi
getActiveRegistrations().creativeTabContents.put(tabIdentifier, itemsSupplier);
}

public void register() {
// No longer needed since we have no SubscribeEvents
// FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations());
}

private Registrations getActiveRegistrations() {
return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new Registrations());
}
Expand Down
Loading

0 comments on commit 2735230

Please sign in to comment.