aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java
diff options
context:
space:
mode:
authorUnknown <shekwancheung0528@gmail.com>2019-05-01 13:22:23 +0800
committerUnknown <shekwancheung0528@gmail.com>2019-05-01 13:22:23 +0800
commitc1de02e7d2d42123cb207f9fa8e869c8a25fcdf6 (patch)
tree8dd9f6ea157ae0bf85561e7f85b085b9565141de /src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java
parent6345d24492242ba9ea26c6b83163aa81464c8cfd (diff)
parentc57ef35f9d99ec64ec1501932a70cab40fcc5107 (diff)
downloadRoughlyEnoughItems-c1de02e7d2d42123cb207f9fa8e869c8a25fcdf6.tar.gz
RoughlyEnoughItems-c1de02e7d2d42123cb207f9fa8e869c8a25fcdf6.tar.bz2
RoughlyEnoughItems-c1de02e7d2d42123cb207f9fa8e869c8a25fcdf6.zip
Merge branch '1.14-dev' into 1.14
Diffstat (limited to 'src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java')
-rw-r--r--src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java b/src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java
new file mode 100644
index 000000000..2548d23ab
--- /dev/null
+++ b/src/main/java/me/shedaniel/rei/client/BaseBoundsHandlerImpl.java
@@ -0,0 +1,93 @@
+package me.shedaniel.rei.client;
+
+import com.google.common.collect.Lists;
+import me.shedaniel.rei.api.BaseBoundsHandler;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.gui.Screen;
+import net.minecraft.util.ActionResult;
+import net.minecraft.util.Pair;
+
+import java.awt.*;
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class BaseBoundsHandlerImpl implements BaseBoundsHandler {
+
+ private static final Function<Rectangle, String> RECTANGLE_STRING_FUNCTION = rectangle -> rectangle.x + "," + rectangle.y + "," + rectangle.width + "," + rectangle.height;
+ private static final Comparator<Rectangle> RECTANGLE_COMPARATOR = BaseBoundsHandlerImpl::compare;
+ private static final Comparator<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> LIST_PAIR_COMPARATOR;
+
+ static {
+ Comparator<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> comparator = Comparator.comparingDouble(value -> value.getLeft().getRight());
+ LIST_PAIR_COMPARATOR = comparator.reversed();
+ }
+
+ private String lastArea = null;
+ private List<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> list = Lists.newArrayList();
+
+ private static int compare(Rectangle o1, Rectangle o2) {return RECTANGLE_STRING_FUNCTION.apply(o1).compareTo(RECTANGLE_STRING_FUNCTION.apply(o2));}
+
+ @Override
+ public Class getBaseSupportedClass() {
+ return Screen.class;
+ }
+
+ @Override
+ public Rectangle getLeftBounds(Screen screen) {
+ return new Rectangle();
+ }
+
+ @Override
+ public Rectangle getRightBounds(Screen screen) {
+ return new Rectangle();
+ }
+
+ @Override
+ public float getPriority() {
+ return -5f;
+ }
+
+ @Override
+ public boolean shouldRecalculateArea(boolean isOnRightSide, Rectangle rectangle) {
+ if (lastArea == null) {
+ lastArea = getStringFromAreas(rectangle, getCurrentExclusionZones(MinecraftClient.getInstance().currentScreen.getClass(), isOnRightSide));
+ return false;
+ }
+ String fromAreas = getStringFromAreas(rectangle, getCurrentExclusionZones(MinecraftClient.getInstance().currentScreen.getClass(), isOnRightSide));
+ if (lastArea.contentEquals(fromAreas))
+ return false;
+ lastArea = fromAreas;
+ return true;
+ }
+
+ @Override
+ public ActionResult canItemSlotWidgetFit(boolean isOnRightSide, int left, int top, Screen screen, Rectangle fullBounds) {
+ List<Rectangle> currentExclusionZones = getCurrentExclusionZones(MinecraftClient.getInstance().currentScreen.getClass(), isOnRightSide);
+ for(Rectangle currentExclusionZone : currentExclusionZones)
+ if (left + 18 >= currentExclusionZone.x && top + 18 >= currentExclusionZone.y && left <= currentExclusionZone.x + currentExclusionZone.width && top <= currentExclusionZone.y + currentExclusionZone.height)
+ return ActionResult.FAIL;
+ return ActionResult.PASS;
+ }
+
+ public List<Rectangle> getCurrentExclusionZones(Class<? extends Screen> currentScreenClass, boolean isOnRightSide) {
+ List<Pair<Pair<Class<? extends Screen>, Float>, ExclusionZoneSupplier>> only = list.stream().filter(pair -> pair.getLeft().getLeft().isAssignableFrom(currentScreenClass)).collect(Collectors.toList());
+ only.sort(LIST_PAIR_COMPARATOR);
+ List<Rectangle> rectangles = Lists.newArrayList();
+ only.forEach(pair -> rectangles.addAll(pair.getRight().apply(isOnRightSide)));
+ return rectangles;
+ }
+
+ @Override
+ public void registerExclusionZones(Class<? extends Screen> screenClass, ExclusionZoneSupplier supplier) {
+ list.add(new Pair<>(new Pair<>(screenClass, 0f), supplier));
+ }
+
+ public String getStringFromAreas(Rectangle rectangle, List<Rectangle> exclusionZones) {
+ List<Rectangle> sorted = Lists.newArrayList(exclusionZones);
+ sorted.sort(RECTANGLE_COMPARATOR);
+ return RECTANGLE_STRING_FUNCTION.apply(rectangle) + ":" + sorted.stream().map(RECTANGLE_STRING_FUNCTION::apply).collect(Collectors.joining("|"));
+ }
+
+}