DefaultRecipeBookHandler.java 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.plugin.autocrafting;
  24. import me.shedaniel.rei.api.AutoTransferHandler;
  25. import me.shedaniel.rei.api.RecipeDisplay;
  26. import me.shedaniel.rei.api.TransferRecipeDisplay;
  27. import me.shedaniel.rei.impl.ScreenHelper;
  28. import me.shedaniel.rei.listeners.RecipeBookGuiHooks;
  29. import me.shedaniel.rei.plugin.cooking.DefaultCookingDisplay;
  30. import me.shedaniel.rei.plugin.crafting.DefaultCraftingDisplay;
  31. import net.minecraft.client.gui.screen.Screen;
  32. import net.minecraft.client.gui.screen.recipebook.RecipeBookProvider;
  33. import net.minecraft.client.resource.language.I18n;
  34. import net.minecraft.recipe.Recipe;
  35. import net.minecraft.screen.CraftingScreenHandler;
  36. import net.minecraft.screen.CraftingTableScreenHandler;
  37. import net.minecraft.screen.PlayerScreenHandler;
  38. public class DefaultRecipeBookHandler implements AutoTransferHandler {
  39. @Override
  40. public Result handle(Context context) {
  41. if (context.getRecipe() instanceof TransferRecipeDisplay && DefaultCategoryHandler.canUseMovePackets())
  42. return Result.createNotApplicable();
  43. RecipeDisplay display = context.getRecipe();
  44. if (!(context.getScreenHandler() instanceof CraftingScreenHandler))
  45. return Result.createNotApplicable();
  46. CraftingScreenHandler<?> container = (CraftingScreenHandler<?>) context.getScreenHandler();
  47. if (display instanceof DefaultCraftingDisplay) {
  48. DefaultCraftingDisplay craftingDisplay = (DefaultCraftingDisplay) display;
  49. if (craftingDisplay.getOptionalRecipe().isPresent()) {
  50. int h = -1, w = -1;
  51. if (container instanceof CraftingTableScreenHandler) {
  52. h = 3;
  53. w = 3;
  54. } else if (container instanceof PlayerScreenHandler) {
  55. h = 2;
  56. w = 2;
  57. }
  58. if (h == -1 || w == -1)
  59. return Result.createNotApplicable();
  60. Recipe<?> recipe = (craftingDisplay).getOptionalRecipe().get();
  61. if (craftingDisplay.getHeight() > h || craftingDisplay.getWidth() > w)
  62. return Result.createFailed(I18n.translate("error.rei.transfer.too_small", h, w));
  63. if (!context.getMinecraft().player.getRecipeBook().contains(recipe))
  64. return Result.createFailed(I18n.translate("error.rei.recipe.not.unlocked"));
  65. if (!context.isActuallyCrafting())
  66. return Result.createSuccessful();
  67. context.getMinecraft().openScreen(context.getScreenWithHandler());
  68. if (context.getScreenWithHandler() instanceof RecipeBookProvider)
  69. ((RecipeBookGuiHooks) ((RecipeBookProvider) context.getScreenWithHandler()).getRecipeBookWidget()).rei_getGhostSlots().reset();
  70. context.getMinecraft().interactionManager.clickRecipe(container.syncId, recipe, Screen.hasShiftDown());
  71. ScreenHelper.getLastOverlay().init();
  72. }
  73. } else if (display instanceof DefaultCookingDisplay) {
  74. DefaultCookingDisplay defaultDisplay = (DefaultCookingDisplay) display;
  75. if (defaultDisplay.getOptionalRecipe().isPresent()) {
  76. Recipe<?> recipe = (defaultDisplay).getOptionalRecipe().get();
  77. if (!context.getMinecraft().player.getRecipeBook().contains(recipe))
  78. return Result.createFailed(I18n.translate("error.rei.recipe.not.unlocked"));
  79. if (!context.isActuallyCrafting())
  80. return Result.createSuccessful();
  81. context.getMinecraft().openScreen(context.getScreenWithHandler());
  82. if (context.getScreenWithHandler() instanceof RecipeBookProvider)
  83. ((RecipeBookGuiHooks) ((RecipeBookProvider) context.getScreenWithHandler()).getRecipeBookWidget()).rei_getGhostSlots().reset();
  84. context.getMinecraft().interactionManager.clickRecipe(container.syncId, recipe, Screen.hasShiftDown());
  85. ScreenHelper.getLastOverlay().init();
  86. }
  87. }
  88. return Result.createNotApplicable();
  89. }
  90. @Override
  91. public double getPriority() {
  92. return -20;
  93. }
  94. }