MenuRegistry.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 architectury
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury.registry;
  20. import me.shedaniel.architectury.annotations.ExpectPlatform;
  21. import me.shedaniel.architectury.registry.menu.ExtendedMenuProvider;
  22. import net.fabricmc.api.EnvType;
  23. import net.fabricmc.api.Environment;
  24. import net.minecraft.client.gui.screens.Screen;
  25. import net.minecraft.client.gui.screens.inventory.MenuAccess;
  26. import net.minecraft.network.FriendlyByteBuf;
  27. import net.minecraft.network.chat.Component;
  28. import net.minecraft.server.level.ServerPlayer;
  29. import net.minecraft.world.MenuProvider;
  30. import net.minecraft.world.entity.player.Inventory;
  31. import net.minecraft.world.entity.player.Player;
  32. import net.minecraft.world.inventory.AbstractContainerMenu;
  33. import net.minecraft.world.inventory.MenuType;
  34. import org.jetbrains.annotations.Nullable;
  35. import java.util.function.Consumer;
  36. /**
  37. * A utility class to register {@link MenuType}s and {@link Screen}s for containers
  38. */
  39. public final class MenuRegistry {
  40. private MenuRegistry() {}
  41. /**
  42. * Opens the menu.
  43. *
  44. * @param player The player affected
  45. * @param provider The {@link MenuProvider} that provides the menu
  46. * @param bufWriter That writer that sends extra data for {@link MenuType} created with {@link MenuRegistry#ofExtended(ExtendedMenuTypeFactory)}
  47. */
  48. public static void openExtendedMenu(ServerPlayer player, MenuProvider provider, Consumer<FriendlyByteBuf> bufWriter) {
  49. openExtendedMenu(player, new ExtendedMenuProvider() {
  50. @Override
  51. public void saveExtraData(FriendlyByteBuf buf) {
  52. bufWriter.accept(buf);
  53. }
  54. @Override
  55. public Component getDisplayName() {
  56. return provider.getDisplayName();
  57. }
  58. @Nullable
  59. @Override
  60. public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
  61. return provider.createMenu(i, inventory, player);
  62. }
  63. });
  64. }
  65. /**
  66. * Opens the menu.
  67. *
  68. * @param player The player affected
  69. * @param provider The {@link ExtendedMenuProvider} that provides the menu
  70. */
  71. @ExpectPlatform
  72. public static void openExtendedMenu(ServerPlayer player, ExtendedMenuProvider provider) {
  73. throw new AssertionError();
  74. }
  75. /**
  76. * Opens the menu.
  77. *
  78. * @param player The player affected
  79. * @param provider The {@link MenuProvider} that provides the menu
  80. */
  81. public static void openMenu(ServerPlayer player, MenuProvider provider) {
  82. player.openMenu(provider);
  83. }
  84. /**
  85. * Creates a simple {@link MenuType}.
  86. *
  87. * @param factory A functional interface to create the {@link MenuType} from an id (Integer) and inventory
  88. * @param <T> The type of {@link AbstractContainerMenu} that handles the logic for the {@link MenuType}
  89. * @return The {@link MenuType} for your {@link AbstractContainerMenu}
  90. */
  91. @ExpectPlatform
  92. public static <T extends AbstractContainerMenu> MenuType<T> of(SimpleMenuTypeFactory<T> factory) {
  93. throw new AssertionError();
  94. }
  95. /**
  96. * Creates a extended {@link MenuType}.
  97. *
  98. * @param factory A functional interface to create the {@link MenuType} from an id (Integer), {@link Inventory}, and {@link FriendlyByteBuf}
  99. * @param <T> The type of {@link AbstractContainerMenu} that handles the logic for the {@link MenuType}
  100. * @return The {@link MenuType} for your {@link AbstractContainerMenu}
  101. */
  102. @ExpectPlatform
  103. public static <T extends AbstractContainerMenu> MenuType<T> ofExtended(ExtendedMenuTypeFactory<T> factory) {
  104. throw new AssertionError();
  105. }
  106. /**
  107. * Registers a Screen Factory on the client to display.
  108. *
  109. * @param type The {@link MenuType} the screen visualizes
  110. * @param factory A functional interface that is used to create new {@link Screen}s
  111. * @param <H> The type of {@link AbstractContainerMenu} for the screen
  112. * @param <S> The type for the {@link Screen}
  113. */
  114. @Environment(EnvType.CLIENT)
  115. @ExpectPlatform
  116. public static <H extends AbstractContainerMenu, S extends Screen & MenuAccess<H>> void registerScreenFactory(MenuType<? extends H> type, ScreenFactory<H, S> factory) {
  117. throw new AssertionError();
  118. }
  119. /**
  120. * Creates new screens.
  121. *
  122. * @param <H> The type of {@link AbstractContainerMenu} for the screen
  123. * @param <S> The type for the {@link Screen}
  124. */
  125. @Environment(EnvType.CLIENT)
  126. @FunctionalInterface
  127. public interface ScreenFactory<H extends AbstractContainerMenu, S extends Screen & MenuAccess<H>> {
  128. /**
  129. * Creates a new {@link S} that extends {@link Screen}
  130. *
  131. * @param containerMenu The {@link AbstractContainerMenu} that controls the game logic for the screen
  132. * @param inventory The {@link Inventory} for the screen
  133. * @param component The {@link Component} for the screen
  134. * @return A new {@link S} that extends {@link Screen}
  135. */
  136. S create(H containerMenu, Inventory inventory, Component component);
  137. }
  138. /**
  139. * Creates simple menus.
  140. *
  141. * @param <T> The {@link AbstractContainerMenu} type
  142. */
  143. @FunctionalInterface
  144. public interface SimpleMenuTypeFactory<T extends AbstractContainerMenu> {
  145. /**
  146. * Creates a new {@link T} that extends {@link AbstractContainerMenu}
  147. *
  148. * @param id The id for the menu
  149. * @return A new {@link T} that extends {@link AbstractContainerMenu}
  150. */
  151. T create(int id, Inventory inventory);
  152. }
  153. /**
  154. * Creates extended menus.
  155. *
  156. * @param <T> The {@link AbstractContainerMenu} type
  157. */
  158. @FunctionalInterface
  159. public interface ExtendedMenuTypeFactory<T extends AbstractContainerMenu> {
  160. /**
  161. * Creates a new {@link T} that extends {@link AbstractContainerMenu}.
  162. *
  163. * @param id The id for the menu
  164. * @param inventory The {@link Inventory} for the menu
  165. * @param buf The {@link FriendlyByteBuf} for the menu to provide extra data
  166. * @return A new {@link T} that extends {@link AbstractContainerMenu}
  167. */
  168. T create(int id, Inventory inventory, FriendlyByteBuf buf);
  169. }
  170. }