Skip to content

Commit

Permalink
Add RegisterMenuScreensEvent to replace `MenuScreens#register(MenuT…
Browse files Browse the repository at this point in the history
…ype, ScreenConstructor)` (#508)
  • Loading branch information
Spinoscythe authored Jan 19, 2024
1 parent 560b0e7 commit 894da78
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
25 changes: 25 additions & 0 deletions patches/net/minecraft/client/gui/screens/MenuScreens.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,28 @@
}

@Nullable
@@ -58,6 +_,11 @@
return (MenuScreens.ScreenConstructor<T, ?>)SCREENS.get(p_96200_);
}

+ /**
+ @deprecated Use {@link net.neoforged.neoforge.client.event.RegisterMenuScreensEvent} to register menu screens
+ + TODO 1.20.5: remove AT related to this
+ */
+ @Deprecated
public static <M extends AbstractContainerMenu, U extends Screen & MenuAccess<M>> void register(
MenuType<? extends M> p_96207_, MenuScreens.ScreenConstructor<M, U> p_96208_
) {
@@ -65,6 +_,12 @@
if (screenconstructor != null) {
throw new IllegalStateException("Duplicate registration for " + BuiltInRegistries.MENU.getKey(p_96207_));
}
+ }
+
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public static void init() {
+ var event = new net.neoforged.neoforge.client.event.RegisterMenuScreensEvent(SCREENS);
+ net.neoforged.fml.ModLoader.get().postEvent(event);
}

public static boolean selfTest() {
2 changes: 2 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.minecraft.client.gui.components.LerpingBossEvent;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.screens.ConfirmScreen;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
Expand Down Expand Up @@ -978,6 +979,7 @@ public static void initClientHooks(Minecraft mc, ReloadableResourceManager resou

GameTestHooks.registerGametests();
registerSpriteSourceTypes();
MenuScreens.init();
ModLoader.get().postEvent(new RegisterClientReloadListenersEvent(resourceManager));
ModLoader.get().postEvent(new EntityRenderersEvent.RegisterLayerDefinitions());
ModLoader.get().postEvent(new EntityRenderersEvent.RegisterRenderers());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.event;

import java.util.Map;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.Event;
import net.neoforged.fml.event.IModBusEvent;
import org.jetbrains.annotations.ApiStatus;

public class RegisterMenuScreensEvent extends Event implements IModBusEvent {
private final Map<MenuType<?>, MenuScreens.ScreenConstructor<?, ?>> registeredScreens;

@ApiStatus.Internal
public RegisterMenuScreensEvent(Map<MenuType<?>, MenuScreens.ScreenConstructor<?, ?>> registeredScreens) {
this.registeredScreens = registeredScreens;
}

public <M extends AbstractContainerMenu, U extends Screen & MenuAccess<M>> void register(
MenuType<? extends M> menuType, MenuScreens.ScreenConstructor<M, U> screenConstructor) {
if (registeredScreens.containsKey(menuType)) {
throw new IllegalStateException("Duplicate attempt to register screen: " + BuiltInRegistries.MENU.getKey(menuType));
}
registeredScreens.put(menuType, screenConstructor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package net.neoforged.neoforge.oldtest.misc;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
Expand All @@ -25,7 +24,7 @@
import net.minecraft.world.level.block.Blocks;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.extensions.IMenuTypeExtension;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
Expand Down Expand Up @@ -75,16 +74,16 @@ protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int

public ContainerTypeTest(IEventBus modEventBus) {
modEventBus.addListener(this::registerContainers);
modEventBus.addListener(this::setup);
modEventBus.addListener(this::registerMenuScreens);
NeoForge.EVENT_BUS.addListener(this::onRightClick);
}

private void registerContainers(final RegisterEvent event) {
event.register(Registries.MENU, helper -> helper.register(new ResourceLocation("containertypetest", "container"), IMenuTypeExtension.create(TestContainer::new)));
}

private void setup(FMLClientSetupEvent event) {
MenuScreens.register(TYPE.get(), TestGui::new);
private void registerMenuScreens(RegisterMenuScreensEvent event) {
event.register(TYPE.get(), TestGui::new);
}

private void onRightClick(PlayerInteractEvent.RightClickBlock event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package net.neoforged.neoforge.oldtest.recipebook;

import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -21,7 +20,7 @@
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
import net.neoforged.neoforge.client.event.RegisterRecipeBookCategoriesEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.extensions.IMenuTypeExtension;
Expand Down Expand Up @@ -71,12 +70,10 @@ public static ResourceLocation getId(String name) {
@Mod.EventBusSubscriber(modid = MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public static class ClientHandler {
@SubscribeEvent
public static void clientSetup(FMLClientSetupEvent event) {
public static void clientSetup(RegisterMenuScreensEvent event) {
if (!ENABLED)
return;
event.enqueueWork(() -> {
MenuScreens.register(RECIPE_BOOK_TEST_MENU_TYPE.get(), RecipeBookTestScreen::new);
});
event.register(RECIPE_BOOK_TEST_MENU_TYPE.get(), RecipeBookTestScreen::new);
}

@SubscribeEvent
Expand Down

0 comments on commit 894da78

Please sign in to comment.