diff options
| author | Unknown <shekwancheung0528@gmail.com> | 2019-05-01 13:22:23 +0800 |
|---|---|---|
| committer | Unknown <shekwancheung0528@gmail.com> | 2019-05-01 13:22:23 +0800 |
| commit | c1de02e7d2d42123cb207f9fa8e869c8a25fcdf6 (patch) | |
| tree | 8dd9f6ea157ae0bf85561e7f85b085b9565141de /src/main/java/me/shedaniel/rei/client | |
| parent | 6345d24492242ba9ea26c6b83163aa81464c8cfd (diff) | |
| parent | c57ef35f9d99ec64ec1501932a70cab40fcc5107 (diff) | |
| download | RoughlyEnoughItems-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')
3 files changed, 126 insertions, 10 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("|")); + } + +} diff --git a/src/main/java/me/shedaniel/rei/client/DisplayHelperImpl.java b/src/main/java/me/shedaniel/rei/client/DisplayHelperImpl.java index e1f6c8144..28c5f5e43 100644 --- a/src/main/java/me/shedaniel/rei/client/DisplayHelperImpl.java +++ b/src/main/java/me/shedaniel/rei/client/DisplayHelperImpl.java @@ -2,6 +2,7 @@ package me.shedaniel.rei.client; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import me.shedaniel.rei.api.BaseBoundsHandler; import me.shedaniel.rei.api.DisplayHelper; import java.awt.*; @@ -33,9 +34,15 @@ public class DisplayHelperImpl implements DisplayHelper { public Rectangle getRightBounds(Object screen) { return new Rectangle(); } + + @Override + public float getPriority() { + return -10f; + } }; private List<DisplayBoundsHandler> screenDisplayBoundsHandlerMap = Lists.newArrayList(); private Map<Class, DisplayBoundsHandler> handlerCache = Maps.newHashMap(); + private BaseBoundsHandler baseBoundsHandler; @Override public List<DisplayBoundsHandler> getSortedBoundsHandlers(Class screenClass) { @@ -59,6 +66,15 @@ public class DisplayHelperImpl implements DisplayHelper { screenDisplayBoundsHandlerMap.add(handler); } + @Override + public BaseBoundsHandler getBaseBoundsHandler() { + return baseBoundsHandler; + } + + public void setBaseBoundsHandler(BaseBoundsHandler baseBoundsHandler) { + this.baseBoundsHandler = baseBoundsHandler; + } + public void resetCache() { handlerCache.clear(); } diff --git a/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java b/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java index 22df089ea..ec5b211d0 100644 --- a/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java +++ b/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java @@ -185,6 +185,9 @@ public class RecipeHelperImpl implements RecipeHelper { this.categoryDisplaySettingsMap.clear(); this.displayVisibilityHandlers.clear(); ((DisplayHelperImpl) RoughlyEnoughItemsCore.getDisplayHelper()).resetCache(); + BaseBoundsHandler baseBoundsHandler = new BaseBoundsHandlerImpl(); + RoughlyEnoughItemsCore.getDisplayHelper().registerBoundsHandler(baseBoundsHandler); + ((DisplayHelperImpl) RoughlyEnoughItemsCore.getDisplayHelper()).setBaseBoundsHandler(baseBoundsHandler); long startTime = System.currentTimeMillis(); List<REIPluginEntry> plugins = new LinkedList<>(RoughlyEnoughItemsCore.getPlugins()); plugins.sort((first, second) -> { @@ -196,16 +199,20 @@ public class RecipeHelperImpl implements RecipeHelper { PluginDisabler pluginDisabler = RoughlyEnoughItemsCore.getPluginDisabler(); plugins.forEach(plugin -> { Identifier identifier = plugin.getPluginIdentifier(); - if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_ITEMS)) - plugin.registerItems(RoughlyEnoughItemsCore.getItemRegisterer()); - if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_CATEGORIES)) - plugin.registerPluginCategories(this); - if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_RECIPE_DISPLAYS)) - plugin.registerRecipeDisplays(this); - if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_BOUNDS)) - plugin.registerBounds(RoughlyEnoughItemsCore.getDisplayHelper()); - if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_OTHERS)) - plugin.registerOthers(this); + try { + if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_ITEMS)) + plugin.registerItems(RoughlyEnoughItemsCore.getItemRegisterer()); + if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_CATEGORIES)) + plugin.registerPluginCategories(this); + if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_RECIPE_DISPLAYS)) + plugin.registerRecipeDisplays(this); + if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_BOUNDS)) + plugin.registerBounds(RoughlyEnoughItemsCore.getDisplayHelper()); + if (pluginDisabler.isFunctionEnabled(identifier, PluginFunction.REGISTER_OTHERS)) + plugin.registerOthers(this); + } catch (Exception e) { + RoughlyEnoughItemsCore.LOGGER.error("[REI] %s plugin failed to load: %s", identifier.toString(), e.getLocalizedMessage()); + } }); if (getDisplayVisibilityHandlers().size() == 0) registerRecipeVisibilityHandler(new DisplayVisibilityHandler() { |
