From 273523079f0529277ef5fdbc432152a420538e5b Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Sun, 14 Jan 2024 14:58:03 +0100 Subject: [PATCH] refactor(neoforge)!: Update to new NeoForge networking API --- gradle.properties | 2 +- .../mods/balm/neoforge/NeoForgeBalm.java | 2 +- .../balm/neoforge/NeoForgeBalmRuntime.java | 7 +- .../client/NeoForgeBalmClientRuntime.java | 18 +- .../keymappings/NeoForgeBalmKeyMappings.java | 12 +- .../client/rendering/NeoForgeBalmModels.java | 12 +- .../rendering/NeoForgeBalmRenderers.java | 12 +- .../client/screen/NeoForgeBalmScreens.java | 12 +- .../neoforge/entity/NeoForgeBalmEntities.java | 13 +- .../event/NeoForgeBalmClientEvents.java | 4 +- .../balm/neoforge/item/NeoForgeBalmItems.java | 6 - .../network/NeoForgeBalmNetworking.java | 212 +++++++++++------- .../neoforge/network/NetworkChannels.java | 38 ---- .../neoforge/stats/NeoForgeBalmStats.java | 15 +- .../neoforge/world/NeoForgeBalmWorldGen.java | 5 +- .../java/net/blay09/mods/balm/api/Balm.java | 6 + .../balm/api/client/BalmClientRuntime.java | 11 +- 17 files changed, 220 insertions(+), 167 deletions(-) delete mode 100644 neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NetworkChannels.java diff --git a/gradle.properties b/gradle.properties index 26eff5fd..973d8d1c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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,) diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java index 55d3dc53..f5e7530b 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java @@ -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(); diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java index 6da6eab9..eb1ff9d1 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java @@ -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(); diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/NeoForgeBalmClientRuntime.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/NeoForgeBalmClientRuntime.java index fb420d1f..c9ad217b 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/NeoForgeBalmClientRuntime.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/NeoForgeBalmClientRuntime.java @@ -1,12 +1,14 @@ 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; @@ -14,8 +16,9 @@ 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 { private final BalmRenderers renderers = new NeoForgeBalmRenderers(); private final BalmTextures textures = new NeoForgeBalmTextures(); @@ -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(); } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/keymappings/NeoForgeBalmKeyMappings.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/keymappings/NeoForgeBalmKeyMappings.java index ce2fd669..459ff6e5 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/keymappings/NeoForgeBalmKeyMappings.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/keymappings/NeoForgeBalmKeyMappings.java @@ -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; @@ -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()); } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmModels.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmModels.java index 862993cb..65765d4c 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmModels.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmModels.java @@ -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; @@ -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 diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmRenderers.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmRenderers.java index 53ae7c8b..a5322675 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmRenderers.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/rendering/NeoForgeBalmRenderers.java @@ -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; @@ -131,11 +131,15 @@ public void setBlockRenderType(Supplier 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()); } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/screen/NeoForgeBalmScreens.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/screen/NeoForgeBalmScreens.java index ca1b31e3..680b2e81 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/screen/NeoForgeBalmScreens.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/client/screen/NeoForgeBalmScreens.java @@ -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; @@ -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()); } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/entity/NeoForgeBalmEntities.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/entity/NeoForgeBalmEntities.java index 16f8c102..ebfff63d 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/entity/NeoForgeBalmEntities.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/entity/NeoForgeBalmEntities.java @@ -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; @@ -55,11 +54,15 @@ public DeferredObject> 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()); } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmClientEvents.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmClientEvents.java index cc3ed0a7..da298dbe 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmClientEvents.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/event/NeoForgeBalmClientEvents.java @@ -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); } }); }); @@ -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); } }); }); diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/item/NeoForgeBalmItems.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/item/NeoForgeBalmItems.java index 9bf4e72c..8d2b5f19 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/item/NeoForgeBalmItems.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/item/NeoForgeBalmItems.java @@ -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.*; @@ -77,11 +76,6 @@ public void addToCreativeModeTab(ResourceLocation tabIdentifier, Supplier new Registrations()); } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java index 5ab5b847..32e2377f 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NeoForgeBalmNetworking.java @@ -1,7 +1,6 @@ package net.blay09.mods.balm.neoforge.network; import net.blay09.mods.balm.api.Balm; -import net.blay09.mods.balm.api.client.BalmClient; import net.blay09.mods.balm.api.menu.BalmMenuProvider; import net.blay09.mods.balm.api.network.BalmNetworking; import net.blay09.mods.balm.api.network.ClientboundMessageRegistration; @@ -9,6 +8,7 @@ import net.blay09.mods.balm.api.network.ServerboundMessageRegistration; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; @@ -16,11 +16,13 @@ import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; -import net.neoforged.neoforge.network.NetworkEvent; -import net.neoforged.neoforge.network.NetworkHooks; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.ModLoadingContext; import net.neoforged.neoforge.network.PacketDistributor; -import net.neoforged.neoforge.network.PlayNetworkDirection; -import net.neoforged.neoforge.network.simple.SimpleChannel; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; +import net.neoforged.neoforge.network.handling.IReplyHandler; +import net.neoforged.neoforge.network.handling.PlayPayloadContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,84 +35,146 @@ public class NeoForgeBalmNetworking implements BalmNetworking { private static final Logger logger = LoggerFactory.getLogger(NeoForgeBalmNetworking.class); - private static final Map, MessageRegistration> messagesByClass = new ConcurrentHashMap<>(); - private static final Map> messagesByIdentifier = new ConcurrentHashMap<>(); - private static final Map discriminatorCounter = new ConcurrentHashMap<>(); + /** + * TODO remove in 1.20.5 and make all packets implement CustomPacketPayload + * + * @deprecated workaround to account for this mid-version breaking change without having to update 152518 mods + */ + @Deprecated + private record WrappedPacket(ResourceLocation id, Object message, BiConsumer encoder) implements CustomPacketPayload { + @Override + public void write(FriendlyByteBuf buf) { + encoder.accept(message, buf); + } + + @Override + public ResourceLocation id() { + return id; + } + } + + private static class Registrations { + private final String modId; + private final Map> messagesByIdentifier = new ConcurrentHashMap<>(); + private boolean optional; + + private Registrations(String modId) { + this.modId = modId; + } + + @SubscribeEvent + @SuppressWarnings("unchecked") + public void registerPayloadHandlers(final RegisterPayloadHandlerEvent event) { + var registrar = event.registrar(modId); + if (optional) { + registrar = registrar.optional(); + } + for (final var entry : messagesByIdentifier.entrySet()) { + final var messageId = entry.getKey(); + final var messageRegistration = entry.getValue(); + registrar = registrar.play(messageId, (FriendlyByteBuf.Reader) buf -> { + final var message = messageRegistration.getDecodeFunc().apply(buf); + return new WrappedPacket(messageId, message, (BiConsumer) messageRegistration.getEncodeFunc()); + }, it -> { + if (entry instanceof ServerboundMessageRegistration serverboundMessageRegistration) { + it.server((payload, context) -> context.workHandler().execute(() -> { + replyHandler = context.replyHandler(); + handleServerboundPacket(serverboundMessageRegistration, payload, context); + replyHandler = null; + })); + } else if (entry instanceof ClientboundMessageRegistration clientboundMessageRegistration) { + it.client(((payload, context) -> context.workHandler().execute(() -> { + replyHandler = context.replyHandler(); + handleClientboundPacket(clientboundMessageRegistration, payload, context); + replyHandler = null; + }))); + } + }); + } + } + + public void allowClientOnly() { + optional = true; + } + + public void allowServerOnly() { + optional = true; + } + } + + @SuppressWarnings("unchecked") + private static void handleServerboundPacket(ServerboundMessageRegistration registration, CustomPacketPayload payload, PlayPayloadContext context) { + final var player = context.player().orElse(null); + if (payload instanceof WrappedPacket wrappedPacket && player instanceof ServerPlayer serverPlayer) { + registration.getHandler().accept(serverPlayer, (T) wrappedPacket.message()); + } + } - private static NetworkEvent.Context replyContext; + @SuppressWarnings("unchecked") + private static void handleClientboundPacket(ClientboundMessageRegistration registration, CustomPacketPayload payload, PlayPayloadContext context) { + if (payload instanceof WrappedPacket wrappedPacket) { + registration.getHandler().accept(Balm.getProxy().getClientPlayer(), (T) wrappedPacket.message()); + } + } + + private final Map registrations = new ConcurrentHashMap<>(); + private final Map, MessageRegistration> messagesByClass = new ConcurrentHashMap<>(); + + private static IReplyHandler replyHandler; @Override public void allowClientOnly(String modId) { - NetworkChannels.allowClientOnly(modId); + getRegistrations(modId).allowClientOnly(); } @Override public void allowServerOnly(String modId) { - NetworkChannels.allowServerOnly(modId); + getRegistrations(modId).allowServerOnly(); } @Override public void openGui(Player player, MenuProvider menuProvider) { - if (player instanceof ServerPlayer) { + if (player instanceof ServerPlayer serverPlayer) { if (menuProvider instanceof BalmMenuProvider balmMenuProvider) { - NetworkHooks.openScreen((ServerPlayer) player, menuProvider, buf -> balmMenuProvider.writeScreenOpeningData((ServerPlayer) player, buf)); + serverPlayer.openMenu(menuProvider, buf -> balmMenuProvider.writeScreenOpeningData(serverPlayer, buf)); } else { - NetworkHooks.openScreen((ServerPlayer) player, menuProvider); + serverPlayer.openMenu(menuProvider); } } } @Override public void reply(T message) { - if (replyContext == null) { + if (replyHandler == null) { throw new IllegalStateException("No context to reply to"); } MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); ResourceLocation identifier = messageRegistration.getIdentifier(); - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.reply(message, replyContext); + replyHandler.send(new WrappedPacket(identifier, message, (BiConsumer) messageRegistration.getEncodeFunc())); } @Override public void sendTo(Player player, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.send(PacketDistributor.PLAYER.with(() -> ((ServerPlayer) player)), message); + if (player instanceof ServerPlayer serverPlayer) { + sendPacket(PacketDistributor.PLAYER.with(serverPlayer), message); + } } @Override - public void sendToTracking(ServerLevel world, BlockPos pos, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message); + public void sendToTracking(ServerLevel level, BlockPos pos, T message) { + sendPacket(PacketDistributor.TRACKING_CHUNK.with(level.getChunkAt(pos)), message); } @Override public void sendToTracking(Entity entity, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.send(PacketDistributor.TRACKING_ENTITY.with(() -> entity), message); + sendPacket(PacketDistributor.TRACKING_ENTITY.with(entity), message); } @Override public void sendToAll(MinecraftServer server, T message) { - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.send(PacketDistributor.ALL.noArg(), message); + sendPacket(PacketDistributor.ALL.noArg(), message); } @Override @@ -120,12 +184,7 @@ public void sendToServer(T message) { return; } - MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); - - ResourceLocation identifier = messageRegistration.getIdentifier(); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.sendToServer(message); + sendPacket(PacketDistributor.SERVER.noArg(), message); } @SuppressWarnings("unchecked") @@ -140,50 +199,43 @@ private MessageRegistration getMessageRegistrationOrThrow(T message) { @Override public void registerClientboundPacket(ResourceLocation identifier, Class clazz, BiConsumer encodeFunc, Function decodeFunc, BiConsumer handler) { ClientboundMessageRegistration messageRegistration = new ClientboundMessageRegistration<>(identifier, clazz, encodeFunc, decodeFunc, handler); - + final var registrations = getActiveRegistrations(); messagesByClass.put(clazz, messageRegistration); - messagesByIdentifier.put(identifier, messageRegistration); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); - channel.registerMessage(nextDiscriminator(identifier.getNamespace()), clazz, encodeFunc::accept, decodeFunc::apply, (message, context) -> { - if (context.getDirection() != PlayNetworkDirection.PLAY_TO_CLIENT) { - logger.warn("Received {} on incorrect side {}", identifier, context.getDirection()); - return; - } - - context.enqueueWork(() -> { - handler.accept(BalmClient.getClientPlayer(), message); - }); - context.setPacketHandled(true); - }); + registrations.messagesByIdentifier.put(identifier, messageRegistration); } @Override public void registerServerboundPacket(ResourceLocation identifier, Class clazz, BiConsumer encodeFunc, Function decodeFunc, BiConsumer handler) { MessageRegistration messageRegistration = new ServerboundMessageRegistration<>(identifier, clazz, encodeFunc, decodeFunc, handler); - + final var registrations = getActiveRegistrations(); messagesByClass.put(clazz, messageRegistration); - messagesByIdentifier.put(identifier, messageRegistration); - - SimpleChannel channel = NetworkChannels.get(identifier.getNamespace()); + registrations.messagesByIdentifier.put(identifier, messageRegistration); + } - channel.registerMessage(nextDiscriminator(identifier.getNamespace()), clazz, encodeFunc::accept, decodeFunc::apply, (message, context) -> { - if (context.getDirection() != PlayNetworkDirection.PLAY_TO_SERVER) { - logger.warn("Received {} on incorrect side {}", identifier, context.getDirection()); - return; + private void sendPacket(PacketDistributor.PacketTarget target, T message) { + MessageRegistration messageRegistration = getMessageRegistrationOrThrow(message); + target.send(new CustomPacketPayload() { + @Override + public void write(FriendlyByteBuf buf) { + messageRegistration.getEncodeFunc().accept(message, buf); } - context.enqueueWork(() -> { - replyContext = context; - ServerPlayer player = context.getSender(); - handler.accept(player, message); - replyContext = null; - }); - context.setPacketHandled(true); + @Override + public ResourceLocation id() { + return messageRegistration.getIdentifier(); + } }); } - private static int nextDiscriminator(String modId) { - return discriminatorCounter.compute(modId, (key, prev) -> prev != null ? prev + 1 : 0); + 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(modId)); } } \ No newline at end of file diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NetworkChannels.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NetworkChannels.java deleted file mode 100644 index ec372595..00000000 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/network/NetworkChannels.java +++ /dev/null @@ -1,38 +0,0 @@ -package net.blay09.mods.balm.neoforge.network; - -import net.minecraft.resources.ResourceLocation; -import net.neoforged.neoforge.network.NetworkRegistry; -import net.neoforged.neoforge.network.simple.SimpleChannel; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Predicate; - -public class NetworkChannels { - private static final String version = "1.0"; // all mods will just have same version for now until I clean this up - private static final Map channels = new ConcurrentHashMap<>(); - private static final Map> acceptedClientVersions = new ConcurrentHashMap<>(); - private static final Map> acceptedServerVersions = new ConcurrentHashMap<>(); - - public static SimpleChannel get(String modId) { - return channels.computeIfAbsent(modId, key -> { - ResourceLocation channelName = new ResourceLocation(key, "network"); - return NetworkRegistry.newSimpleChannel(channelName, - () -> version, - it -> acceptedClientVersions.getOrDefault(modId, NetworkChannels::defaultVersionCheck).test(it), - it -> acceptedServerVersions.getOrDefault(modId, NetworkChannels::defaultVersionCheck).test(it)); - }); - } - - public static void allowClientOnly(String modId) { - acceptedClientVersions.put(modId, it -> true); - } - - public static void allowServerOnly(String modId) { - acceptedServerVersions.put(modId, it -> true); - } - - private static boolean defaultVersionCheck(String it) { - return it.equals(version); - } -} \ No newline at end of file diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/stats/NeoForgeBalmStats.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/stats/NeoForgeBalmStats.java index 5c95524e..3466c860 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/stats/NeoForgeBalmStats.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/stats/NeoForgeBalmStats.java @@ -1,18 +1,15 @@ package net.blay09.mods.balm.neoforge.stats; -import net.blay09.mods.balm.api.DeferredObject; import net.blay09.mods.balm.api.stats.BalmStats; import net.blay09.mods.balm.neoforge.DeferredRegisters; -import net.minecraft.core.Registry; -import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; import net.minecraft.stats.StatFormatter; import net.minecraft.stats.Stats; +import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModLoadingContext; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; -import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; import java.util.ArrayList; import java.util.List; @@ -40,11 +37,15 @@ public void registerCustomStat(ResourceLocation identifier) { register.register(identifier.getPath(), () -> identifier); } - 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()); } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/world/NeoForgeBalmWorldGen.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/world/NeoForgeBalmWorldGen.java index 3f24fbf4..3e8c838e 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/world/NeoForgeBalmWorldGen.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/world/NeoForgeBalmWorldGen.java @@ -15,6 +15,7 @@ import net.minecraft.world.level.levelgen.feature.Feature; import net.minecraft.world.level.levelgen.placement.PlacedFeature; import net.minecraft.world.level.levelgen.placement.PlacementModifierType; +import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; import net.neoforged.neoforge.common.world.BiomeModifier; import net.neoforged.neoforge.common.world.ModifiableBiomeInfo; @@ -67,9 +68,9 @@ public void modifyBiome(Holder biome, BiomeModifier.Phase phase, Modifiab } } - public static void initializeBalmBiomeModifiers() { + public static void initializeBalmBiomeModifiers(IEventBus modBus) { var registry = DeferredRegister.create(NeoForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, "balm"); registry.register("balm", () -> BALM_BIOME_MODIFIER_CODEC); - registry.register(FMLJavaModLoadingContext.get().getModEventBus()); + registry.register(modBus); } } diff --git a/shared/src/main/java/net/blay09/mods/balm/api/Balm.java b/shared/src/main/java/net/blay09/mods/balm/api/Balm.java index 90a4da61..c8918648 100644 --- a/shared/src/main/java/net/blay09/mods/balm/api/Balm.java +++ b/shared/src/main/java/net/blay09/mods/balm/api/Balm.java @@ -27,15 +27,21 @@ public class Balm { private static final BalmRuntime runtime = BalmRuntimeSpi.create(); private static final SidedProxy proxy = sidedProxy("net.blay09.mods.balm.api.BalmProxy", "net.blay09.mods.balm.api.client.BalmClientProxy"); + @Deprecated public static void initialize(String modId) { runtime.initialize(modId, () -> { }); } + @Deprecated public static void initialize(String modId, Runnable initializer) { runtime.initialize(modId, initializer); } + public static void initialize(String modId, BalmRuntimeLoadContext context, Runnable initializer) { + runtime.initialize(modId, context, initializer); + } + public static boolean isModLoaded(String modId) { return runtime.isModLoaded(modId); } diff --git a/shared/src/main/java/net/blay09/mods/balm/api/client/BalmClientRuntime.java b/shared/src/main/java/net/blay09/mods/balm/api/client/BalmClientRuntime.java index 8ef28e56..70e09308 100644 --- a/shared/src/main/java/net/blay09/mods/balm/api/client/BalmClientRuntime.java +++ b/shared/src/main/java/net/blay09/mods/balm/api/client/BalmClientRuntime.java @@ -1,16 +1,25 @@ package net.blay09.mods.balm.api.client; +import net.blay09.mods.balm.api.BalmRuntimeLoadContext; 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; -public interface BalmClientRuntime { +public interface BalmClientRuntime { BalmRenderers getRenderers(); + BalmTextures getTextures(); + BalmScreens getScreens(); + BalmModels getModels(); + BalmKeyMappings getKeyMappings(); + + @Deprecated void initialize(String modId, Runnable initializer); + + void initialize(String modId, T context, Runnable initializer); }