AutoCraftingButtonWidget.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.gui.widget;
  6. import me.shedaniel.rei.api.AutoTransferHandler;
  7. import me.shedaniel.rei.api.RecipeDisplay;
  8. import me.shedaniel.rei.api.RecipeHelper;
  9. import me.shedaniel.rei.client.ScreenHelper;
  10. import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
  11. import net.minecraft.client.resource.language.I18n;
  12. import net.minecraft.util.Formatting;
  13. import net.minecraft.util.Identifier;
  14. import java.awt.*;
  15. import java.util.Optional;
  16. import java.util.function.Supplier;
  17. public class AutoCraftingButtonWidget extends ButtonWidget {
  18. private final Supplier<RecipeDisplay> displaySupplier;
  19. private String extraTooltip;
  20. private String errorTooltip;
  21. private AbstractContainerScreen<?> containerScreen;
  22. public AutoCraftingButtonWidget(Rectangle rectangle, String text, Supplier<RecipeDisplay> displaySupplier) {
  23. super(rectangle, text);
  24. this.displaySupplier = () -> displaySupplier.get();
  25. Optional<Identifier> recipe = displaySupplier.get().getRecipeLocation();
  26. extraTooltip = recipe.isPresent() ? I18n.translate("text.rei.recipe_id", Formatting.GRAY.toString(), recipe.get().toString()) : "";
  27. this.containerScreen = ScreenHelper.getLastContainerScreen();
  28. }
  29. @Override
  30. public void onPressed() {
  31. AutoTransferHandler.Context context = AutoTransferHandler.Context.create(true, containerScreen, displaySupplier.get());
  32. for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler())
  33. try {
  34. AutoTransferHandler.Result result = autoTransferHandler.handle(context);
  35. if (result.isSuccessful())
  36. return;
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. minecraft.openScreen(containerScreen);
  41. ScreenHelper.getLastOverlay().init();
  42. }
  43. @Override
  44. public void render(int mouseX, int mouseY, float delta) {
  45. this.enabled = false;
  46. String error = null;
  47. AutoTransferHandler.Context context = AutoTransferHandler.Context.create(false, containerScreen, displaySupplier.get());
  48. for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler()) {
  49. AutoTransferHandler.Result result = autoTransferHandler.handle(context);
  50. if (result.isSuccessful()) {
  51. enabled = true;
  52. error = null;
  53. break;
  54. } else if (error == null) {
  55. error = result.getErrorKey();
  56. }
  57. }
  58. errorTooltip = error;
  59. super.render(mouseX, mouseY, delta);
  60. }
  61. @Override
  62. public Optional<String> getTooltips() {
  63. if (this.minecraft.options.advancedItemTooltips)
  64. if (errorTooltip == null)
  65. return Optional.ofNullable(I18n.translate("text.auto_craft.move_items") + extraTooltip);
  66. else
  67. return Optional.ofNullable("§c" + I18n.translate(errorTooltip) + extraTooltip);
  68. if (errorTooltip == null)
  69. return Optional.ofNullable(I18n.translate("text.auto_craft.move_items"));
  70. else
  71. return Optional.ofNullable("§c" + I18n.translate(errorTooltip));
  72. }
  73. }