BaseBoundsHandlerImpl.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.client;
  6. import com.google.common.collect.Lists;
  7. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  8. import me.shedaniel.rei.api.BaseBoundsHandler;
  9. import me.shedaniel.rei.api.DisplayHelper;
  10. import net.minecraft.client.MinecraftClient;
  11. import net.minecraft.client.gui.screen.Screen;
  12. import net.minecraft.util.ActionResult;
  13. import net.minecraft.util.Pair;
  14. import java.awt.*;
  15. import java.util.Comparator;
  16. import java.util.List;
  17. import java.util.function.Function;
  18. import java.util.stream.Collectors;
  19. public class BaseBoundsHandlerImpl implements BaseBoundsHandler {
  20. private static final Function<Rectangle, String> RECTANGLE_STRING_FUNCTION = rectangle -> rectangle.x + "," + rectangle.y + "," + rectangle.width + "," + rectangle.height;
  21. private static final Comparator<Rectangle> RECTANGLE_COMPARATOR = BaseBoundsHandlerImpl::compare;
  22. private static final Comparator<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> LIST_PAIR_COMPARATOR;
  23. static {
  24. Comparator<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> comparator = Comparator.comparingDouble(value -> value.getLeft().getRight());
  25. LIST_PAIR_COMPARATOR = comparator.reversed();
  26. }
  27. private String lastArea = null;
  28. private List<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> list = Lists.newArrayList();
  29. private static int compare(Rectangle o1, Rectangle o2) {return RECTANGLE_STRING_FUNCTION.apply(o1).compareTo(RECTANGLE_STRING_FUNCTION.apply(o2));}
  30. @Override
  31. public Class getBaseSupportedClass() {
  32. return Screen.class;
  33. }
  34. @Override
  35. public Rectangle getLeftBounds(Screen screen) {
  36. return new Rectangle();
  37. }
  38. @Override
  39. public Rectangle getRightBounds(Screen screen) {
  40. return new Rectangle();
  41. }
  42. @Override
  43. public float getPriority() {
  44. return -5f;
  45. }
  46. @Override
  47. public ActionResult isInZone(boolean isOnRightSide, double mouseX, double mouseY) {
  48. for(Rectangle zone : getCurrentExclusionZones(MinecraftClient.getInstance().currentScreen.getClass(), isOnRightSide))
  49. if (zone.contains(mouseX, mouseY))
  50. return ActionResult.FAIL;
  51. return ActionResult.PASS;
  52. }
  53. @Override
  54. public boolean shouldRecalculateArea(boolean isOnRightSide, Rectangle rectangle) {
  55. if (lastArea == null) {
  56. lastArea = getStringFromCurrent(isOnRightSide);
  57. return false;
  58. }
  59. if (lastArea.contentEquals(getStringFromCurrent(isOnRightSide)))
  60. return false;
  61. lastArea = getStringFromCurrent(isOnRightSide);
  62. return true;
  63. }
  64. private DisplayHelper.DisplayBoundsHandler getHandler() {
  65. return RoughlyEnoughItemsCore.getDisplayHelper().getResponsibleBoundsHandler(MinecraftClient.getInstance().currentScreen.getClass());
  66. }
  67. private String getStringFromCurrent(boolean isOnRightSide) {
  68. return getStringFromAreas(isOnRightSide ? getHandler().getRightBounds(MinecraftClient.getInstance().currentScreen) : getHandler().getLeftBounds(MinecraftClient.getInstance().currentScreen), getCurrentExclusionZones(MinecraftClient.getInstance().currentScreen.getClass(), isOnRightSide));
  69. }
  70. @Override
  71. public ActionResult canItemSlotWidgetFit(boolean isOnRightSide, int left, int top, Screen screen, Rectangle fullBounds) {
  72. List<Rectangle> currentExclusionZones = getCurrentExclusionZones(MinecraftClient.getInstance().currentScreen.getClass(), isOnRightSide);
  73. for(Rectangle currentExclusionZone : currentExclusionZones)
  74. if (left + 18 >= currentExclusionZone.x && top + 18 >= currentExclusionZone.y && left <= currentExclusionZone.x + currentExclusionZone.width && top <= currentExclusionZone.y + currentExclusionZone.height)
  75. return ActionResult.FAIL;
  76. return ActionResult.PASS;
  77. }
  78. public List<Rectangle> getCurrentExclusionZones(Class<? extends Screen> currentScreenClass, boolean isOnRightSide) {
  79. List<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> only = list.stream().filter(pair -> pair.getLeft().getLeft().isAssignableFrom(currentScreenClass)).collect(Collectors.toList());
  80. only.sort(LIST_PAIR_COMPARATOR);
  81. List<Rectangle> rectangles = Lists.newArrayList();
  82. only.forEach(pair -> rectangles.addAll(pair.getRight().apply(isOnRightSide)));
  83. return rectangles;
  84. }
  85. @Override
  86. public void registerExclusionZones(Class<? extends Screen> screenClass, ExclusionZoneSupplier supplier) {
  87. list.add(new Pair<>(new Pair<>(screenClass, 0f), supplier));
  88. }
  89. public String getStringFromAreas(Rectangle rectangle, List<Rectangle> exclusionZones) {
  90. List<Rectangle> sorted = Lists.newArrayList(exclusionZones);
  91. sorted.sort(RECTANGLE_COMPARATOR);
  92. return RECTANGLE_STRING_FUNCTION.apply(rectangle) + ":" + sorted.stream().map(RECTANGLE_STRING_FUNCTION::apply).collect(Collectors.joining("|"));
  93. }
  94. }