DefaultRecipeBookHandler.java 5.2 KB

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