BaseBoundsHandlerImpl.java 5.0 KB

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