RoughlyEnoughItemsCore.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import me.shedaniel.cloth.hooks.ClothClientHooks;
  27. import me.shedaniel.rei.api.*;
  28. import me.shedaniel.rei.api.plugins.REIPluginV0;
  29. import me.shedaniel.rei.gui.ContainerScreenOverlay;
  30. import me.shedaniel.rei.impl.*;
  31. import me.shedaniel.rei.listeners.RecipeBookButtonWidgetHooks;
  32. import me.shedaniel.rei.listeners.RecipeBookGuiHooks;
  33. import me.shedaniel.rei.tests.plugin.REITestPlugin;
  34. import net.fabricmc.api.ClientModInitializer;
  35. import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
  36. import net.fabricmc.loader.api.FabricLoader;
  37. import net.fabricmc.loader.api.ModContainer;
  38. import net.minecraft.client.MinecraftClient;
  39. import net.minecraft.client.gui.Element;
  40. import net.minecraft.client.gui.screen.Screen;
  41. import net.minecraft.client.gui.screen.ingame.CraftingTableScreen;
  42. import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
  43. import net.minecraft.client.gui.screen.ingame.InventoryScreen;
  44. import net.minecraft.client.gui.screen.ingame.ScreenWithHandler;
  45. import net.minecraft.client.gui.screen.recipebook.RecipeBookGhostSlots;
  46. import net.minecraft.client.gui.screen.recipebook.RecipeBookProvider;
  47. import net.minecraft.client.gui.screen.recipebook.RecipeBookWidget;
  48. import net.minecraft.client.gui.widget.TextFieldWidget;
  49. import net.minecraft.client.gui.widget.TexturedButtonWidget;
  50. import net.minecraft.client.resource.language.I18n;
  51. import net.minecraft.item.ItemStack;
  52. import net.minecraft.item.Items;
  53. import net.minecraft.recipe.Ingredient;
  54. import net.minecraft.recipe.RecipeManager;
  55. import net.minecraft.screen.CraftingTableScreenHandler;
  56. import net.minecraft.screen.slot.Slot;
  57. import net.minecraft.text.LiteralText;
  58. import net.minecraft.util.ActionResult;
  59. import net.minecraft.util.Identifier;
  60. import org.apache.logging.log4j.LogManager;
  61. import org.apache.logging.log4j.Logger;
  62. import org.jetbrains.annotations.ApiStatus;
  63. import java.util.LinkedList;
  64. import java.util.List;
  65. import java.util.Map;
  66. import java.util.Optional;
  67. import java.util.concurrent.CompletableFuture;
  68. import java.util.concurrent.ExecutorService;
  69. import java.util.concurrent.Executors;
  70. @ApiStatus.Internal
  71. public class RoughlyEnoughItemsCore implements ClientModInitializer {
  72. @ApiStatus.Internal public static final Logger LOGGER = LogManager.getFormatterLogger("REI");
  73. private static final RecipeHelper RECIPE_HELPER = new RecipeHelperImpl();
  74. private static final EntryRegistry ENTRY_REGISTRY = new EntryRegistryImpl();
  75. private static final DisplayHelper DISPLAY_HELPER = new DisplayHelperImpl();
  76. private static final Map<Identifier, REIPluginEntry> plugins = Maps.newHashMap();
  77. private static final ExecutorService SYNC_RECIPES = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "REI-SyncRecipes"));
  78. private static ConfigManager configManager;
  79. @ApiStatus.Internal
  80. public static RecipeHelper getRecipeHelper() {
  81. return RECIPE_HELPER;
  82. }
  83. @ApiStatus.Internal
  84. public static ConfigManager getConfigManager() {
  85. return configManager;
  86. }
  87. @ApiStatus.Internal
  88. public static EntryRegistry getEntryRegistry() {
  89. return ENTRY_REGISTRY;
  90. }
  91. @ApiStatus.Internal
  92. public static DisplayHelper getDisplayHelper() {
  93. return DISPLAY_HELPER;
  94. }
  95. /**
  96. * Registers a REI plugin
  97. *
  98. * @param plugin the plugin instance
  99. * @return the plugin itself
  100. */
  101. @ApiStatus.Internal
  102. public static REIPluginEntry registerPlugin(REIPluginEntry plugin) {
  103. plugins.put(plugin.getPluginIdentifier(), plugin);
  104. RoughlyEnoughItemsCore.LOGGER.debug("[REI] Registered plugin %s from %s", plugin.getPluginIdentifier().toString(), plugin.getClass().getSimpleName());
  105. return plugin;
  106. }
  107. public static List<REIPluginEntry> getPlugins() {
  108. return new LinkedList<>(plugins.values());
  109. }
  110. public static Optional<Identifier> getPluginIdentifier(REIPluginEntry plugin) {
  111. for (Identifier identifier : plugins.keySet())
  112. if (identifier != null && plugins.get(identifier).equals(plugin))
  113. return Optional.of(identifier);
  114. return Optional.empty();
  115. }
  116. public static boolean hasPermissionToUsePackets() {
  117. try {
  118. MinecraftClient.getInstance().getNetworkHandler().getCommandSource().hasPermissionLevel(0);
  119. return hasOperatorPermission() && canUsePackets();
  120. } catch (NullPointerException e) {
  121. return true;
  122. }
  123. }
  124. public static boolean hasOperatorPermission() {
  125. try {
  126. return MinecraftClient.getInstance().getNetworkHandler().getCommandSource().hasPermissionLevel(1);
  127. } catch (NullPointerException e) {
  128. return true;
  129. }
  130. }
  131. public static boolean canUsePackets() {
  132. return ClientSidePacketRegistry.INSTANCE.canServerReceive(RoughlyEnoughItemsNetwork.CREATE_ITEMS_PACKET) && ClientSidePacketRegistry.INSTANCE.canServerReceive(RoughlyEnoughItemsNetwork.DELETE_ITEMS_PACKET);
  133. }
  134. @ApiStatus.Internal
  135. public static void syncRecipes(long[] lastSync) {
  136. if (lastSync != null) {
  137. if (lastSync[0] > 0 && System.currentTimeMillis() - lastSync[0] <= 5000) {
  138. RoughlyEnoughItemsCore.LOGGER.warn("[REI] Suppressing Sync Recipes!");
  139. return;
  140. }
  141. lastSync[0] = System.currentTimeMillis();
  142. }
  143. RecipeManager recipeManager = MinecraftClient.getInstance().getNetworkHandler().getRecipeManager();
  144. if (ConfigObject.getInstance().doesRegisterRecipesInAnotherThread()) {
  145. CompletableFuture.runAsync(() -> ((RecipeHelperImpl) RecipeHelper.getInstance()).recipesLoaded(recipeManager), SYNC_RECIPES);
  146. } else {
  147. ((RecipeHelperImpl) RecipeHelper.getInstance()).recipesLoaded(recipeManager);
  148. }
  149. }
  150. @ApiStatus.Internal
  151. public static boolean isDebugModeEnabled() {
  152. return System.getProperty("rei.test", "false").equals("true");
  153. }
  154. @SuppressWarnings("deprecation")
  155. @Override
  156. public void onInitializeClient() {
  157. configManager = new ConfigManagerImpl();
  158. registerClothEvents();
  159. discoverPluginEntries();
  160. for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
  161. if (modContainer.getMetadata().containsCustomElement("roughlyenoughitems:plugins"))
  162. RoughlyEnoughItemsCore.LOGGER.error("[REI] REI plugin from " + modContainer.getMetadata().getId() + " is not loaded because it is too old!");
  163. }
  164. ClientSidePacketRegistry.INSTANCE.register(RoughlyEnoughItemsNetwork.CREATE_ITEMS_MESSAGE_PACKET, (packetContext, packetByteBuf) -> {
  165. ItemStack stack = packetByteBuf.readItemStack();
  166. String player = packetByteBuf.readString(32767);
  167. packetContext.getPlayer().addMessage(new LiteralText(I18n.translate("text.rei.cheat_items").replaceAll("\\{item_name}", SearchArgument.tryGetItemStackName(stack.copy())).replaceAll("\\{item_count}", stack.copy().getCount() + "").replaceAll("\\{player_name}", player)), false);
  168. });
  169. ClientSidePacketRegistry.INSTANCE.register(RoughlyEnoughItemsNetwork.NOT_ENOUGH_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  170. Screen currentScreen = MinecraftClient.getInstance().currentScreen;
  171. if (currentScreen instanceof CraftingTableScreen) {
  172. RecipeBookWidget recipeBookGui = ((RecipeBookProvider) currentScreen).getRecipeBookWidget();
  173. RecipeBookGhostSlots ghostSlots = ((RecipeBookGuiHooks) recipeBookGui).rei_getGhostSlots();
  174. ghostSlots.reset();
  175. List<List<ItemStack>> input = Lists.newArrayList();
  176. int mapSize = packetByteBuf.readInt();
  177. for (int i = 0; i < mapSize; i++) {
  178. List<ItemStack> list = Lists.newArrayList();
  179. int count = packetByteBuf.readInt();
  180. for (int j = 0; j < count; j++) {
  181. list.add(packetByteBuf.readItemStack());
  182. }
  183. input.add(list);
  184. }
  185. ghostSlots.addSlot(Ingredient.ofItems(Items.STONE), 381203812, 12738291);
  186. CraftingTableScreenHandler screenHandler = ((CraftingTableScreen) currentScreen).getScreenHandler();
  187. for (int i = 0; i < input.size(); i++) {
  188. List<ItemStack> stacks = input.get(i);
  189. if (!stacks.isEmpty()) {
  190. Slot slot = screenHandler.getSlot(i + screenHandler.getCraftingResultSlotIndex() + 1);
  191. ghostSlots.addSlot(Ingredient.ofStacks(stacks.toArray(new ItemStack[0])), slot.xPosition, slot.yPosition);
  192. }
  193. }
  194. }
  195. });
  196. }
  197. private void discoverPluginEntries() {
  198. for (REIPluginEntry reiPlugin : FabricLoader.getInstance().getEntrypoints("rei_plugins", REIPluginEntry.class)) {
  199. try {
  200. if (!REIPluginV0.class.isAssignableFrom(reiPlugin.getClass()))
  201. throw new IllegalArgumentException("REI plugin is too old!");
  202. registerPlugin(reiPlugin);
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. RoughlyEnoughItemsCore.LOGGER.error("[REI] Can't load REI plugins from %s: %s", reiPlugin.getClass(), e.getLocalizedMessage());
  206. }
  207. }
  208. for (REIPluginV0 reiPlugin : FabricLoader.getInstance().getEntrypoints("rei_plugins_v0", REIPluginV0.class)) {
  209. try {
  210. registerPlugin(reiPlugin);
  211. } catch (Exception e) {
  212. e.printStackTrace();
  213. RoughlyEnoughItemsCore.LOGGER.error("[REI] Can't load REI plugins from %s: %s", reiPlugin.getClass(), e.getLocalizedMessage());
  214. }
  215. }
  216. // Test Only
  217. loadTestPlugins();
  218. }
  219. private void loadTestPlugins() {
  220. if (isDebugModeEnabled()) {
  221. registerPlugin(new REITestPlugin());
  222. }
  223. }
  224. private boolean shouldReturn(Class<?> screen) {
  225. for (OverlayDecider decider : DisplayHelper.getInstance().getAllOverlayDeciders()) {
  226. if (!decider.isHandingScreen(screen))
  227. continue;
  228. ActionResult result = decider.shouldScreenBeOverlayed(screen);
  229. if (result != ActionResult.PASS)
  230. return result == ActionResult.FAIL || ScreenHelper.getLastScreenWithHandlerHooks() == null;
  231. }
  232. return true;
  233. }
  234. private void registerClothEvents() {
  235. final Identifier recipeButtonTex = new Identifier("textures/gui/recipe_button.png");
  236. long[] lastSync = {-1};
  237. ClothClientHooks.SYNC_RECIPES.register((minecraftClient, recipeManager, synchronizeRecipesS2CPacket) -> syncRecipes(lastSync));
  238. ClothClientHooks.SCREEN_ADD_BUTTON.register((minecraftClient, screen, abstractButtonWidget) -> {
  239. if (ConfigObject.getInstance().doesDisableRecipeBook() && screen instanceof ScreenWithHandler && abstractButtonWidget instanceof TexturedButtonWidget)
  240. if (((RecipeBookButtonWidgetHooks) abstractButtonWidget).rei_getTexture().equals(recipeButtonTex))
  241. return ActionResult.FAIL;
  242. return ActionResult.PASS;
  243. });
  244. ClothClientHooks.SCREEN_INIT_POST.register((minecraftClient, screen, screenHooks) -> {
  245. if (screen instanceof InventoryScreen && minecraftClient.interactionManager.hasCreativeInventory())
  246. return;
  247. if (shouldReturn(screen.getClass()))
  248. return;
  249. if (screen instanceof ScreenWithHandler)
  250. ScreenHelper.setLastScreenWithHandler((ScreenWithHandler<?>) screen);
  251. boolean alreadyAdded = false;
  252. for (Element element : Lists.newArrayList(screenHooks.cloth_getChildren()))
  253. if (ContainerScreenOverlay.class.isAssignableFrom(element.getClass()))
  254. if (alreadyAdded)
  255. screenHooks.cloth_getChildren().remove(element);
  256. else
  257. alreadyAdded = true;
  258. if (!alreadyAdded)
  259. screenHooks.cloth_getChildren().add(ScreenHelper.getLastOverlay(true, false));
  260. });
  261. ClothClientHooks.SCREEN_RENDER_POST.register((minecraftClient, screen, i, i1, v) -> {
  262. if (shouldReturn(screen.getClass()))
  263. return;
  264. ScreenHelper.getLastOverlay().render(i, i1, v);
  265. });
  266. ClothClientHooks.SCREEN_MOUSE_DRAGGED.register((minecraftClient, screen, v, v1, i, v2, v3) -> {
  267. if (shouldReturn(screen.getClass()))
  268. return ActionResult.PASS;
  269. if (ScreenHelper.isOverlayVisible() && ScreenHelper.getLastOverlay().mouseDragged(v, v1, i, v2, v3))
  270. return ActionResult.SUCCESS;
  271. return ActionResult.PASS;
  272. });
  273. ClothClientHooks.SCREEN_MOUSE_CLICKED.register((minecraftClient, screen, v, v1, i) -> {
  274. if (screen instanceof CreativeInventoryScreen)
  275. if (ScreenHelper.isOverlayVisible() && ScreenHelper.getLastOverlay().mouseClicked(v, v1, i)) {
  276. screen.setFocused(ScreenHelper.getLastOverlay());
  277. if (i == 0)
  278. screen.setDragging(true);
  279. return ActionResult.SUCCESS;
  280. }
  281. return ActionResult.PASS;
  282. });
  283. ClothClientHooks.SCREEN_MOUSE_SCROLLED.register((minecraftClient, screen, v, v1, v2) -> {
  284. if (shouldReturn(screen.getClass()))
  285. return ActionResult.PASS;
  286. if (ScreenHelper.isOverlayVisible() && ScreenHelper.getLastOverlay().mouseScrolled(v, v1, v2))
  287. return ActionResult.SUCCESS;
  288. return ActionResult.PASS;
  289. });
  290. ClothClientHooks.SCREEN_CHAR_TYPED.register((minecraftClient, screen, character, keyCode) -> {
  291. if (shouldReturn(screen.getClass()))
  292. return ActionResult.PASS;
  293. if (ScreenHelper.getLastOverlay().charTyped(character, keyCode))
  294. return ActionResult.SUCCESS;
  295. return ActionResult.PASS;
  296. });
  297. ClothClientHooks.SCREEN_LATE_RENDER.register((minecraftClient, screen, i, i1, v) -> {
  298. if (!ScreenHelper.isOverlayVisible())
  299. return;
  300. if (shouldReturn(screen.getClass()))
  301. return;
  302. ScreenHelper.getLastOverlay().lateRender(i, i1, v);
  303. });
  304. ClothClientHooks.SCREEN_KEY_PRESSED.register((minecraftClient, screen, i, i1, i2) -> {
  305. if (screen.getFocused() != null && screen.getFocused() instanceof TextFieldWidget || (screen.getFocused() instanceof RecipeBookWidget && ((RecipeBookGuiHooks) screen.getFocused()).rei_getSearchField() != null && ((RecipeBookGuiHooks) screen.getFocused()).rei_getSearchField().isFocused()))
  306. return ActionResult.PASS;
  307. if (shouldReturn(screen.getClass()))
  308. return ActionResult.PASS;
  309. if (ScreenHelper.getLastOverlay().keyPressed(i, i1, i2))
  310. return ActionResult.SUCCESS;
  311. if (screen instanceof ScreenWithHandler && ConfigObject.getInstance().doesDisableRecipeBook() && ConfigObject.getInstance().doesFixTabCloseContainer())
  312. if (i == 258 && minecraftClient.options.keyInventory.matchesKey(i, i1)) {
  313. minecraftClient.player.closeHandledScreen();
  314. return ActionResult.SUCCESS;
  315. }
  316. return ActionResult.PASS;
  317. });
  318. }
  319. }