/* * This file is licensed under the MIT License, part of Roughly Enough Items. * Copyright (c) 2018, 2019, 2020 shedaniel * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package me.shedaniel.rei.impl; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.mojang.blaze3d.platform.Window; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.gui.config.DisplayPanelLocation; import me.shedaniel.rei.api.ingredient.EntryStack; import me.shedaniel.rei.api.ingredient.util.EntryStacks; import me.shedaniel.rei.api.plugins.REIPlugin; import me.shedaniel.rei.api.registry.screen.*; import me.shedaniel.rei.api.util.CollectionUtils; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.inventory.AbstractContainerMenu; import org.apache.commons.lang3.mutable.Mutable; import org.apache.commons.lang3.mutable.MutableObject; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.stream.Collectors; @ApiStatus.Internal @Environment(EnvType.CLIENT) public class ScreenRegistryImpl implements ScreenRegistry { private Multimap, ClickArea> clickAreas = HashMultimap.create(); private List focusedStackProviders = new ArrayList<>(); private List deciders = new ArrayList<>(); private Map, List> cache = new HashMap<>(); private ExclusionZones exclusionZones; private Class tmpScreen; @Override public void acceptPlugin(REIPlugin plugin) { plugin.registerScreens(this); plugin.registerExclusionZones(exclusionZones()); } @Override public List getDeciders(R screen) { Class screenClass = screen.getClass(); List possibleCached = cache.get(screenClass); if (possibleCached != null) { return possibleCached; } tmpScreen = screenClass; List deciders = CollectionUtils.filterToList(this.deciders, this::filterResponsible); cache.put(screenClass, deciders); tmpScreen = null; return deciders; } private boolean filterResponsible(OverlayDecider handler) { return handler.isHandingScreen(tmpScreen); } @Override public List getDeciders() { return Collections.unmodifiableList(deciders); } @Override public Rectangle getOverlayBounds(DisplayPanelLocation location, T screen) { Window window = Minecraft.getInstance().getWindow(); int scaledWidth = window.getGuiScaledWidth(); int scaledHeight = window.getGuiScaledHeight(); for (OverlayDecider decider : getDeciders(screen)) { if (decider instanceof DisplayBoundsProvider) { Rectangle containerBounds = ((DisplayBoundsProvider) decider).getScreenBounds(screen); if (location == DisplayPanelLocation.LEFT) { if (containerBounds.x < 10) continue; return new Rectangle(2, 0, containerBounds.x - 2, scaledHeight); } else { if (scaledWidth - containerBounds.getMaxX() < 10) continue; return new Rectangle(containerBounds.getMaxX() + 2, 0, scaledWidth - containerBounds.getMaxX() - 4, scaledHeight); } } } return new Rectangle(); } @Nullable @Override public EntryStack getFocusedStack(T screen, Point mouse) { for (FocusedStackProvider provider : focusedStackProviders) { InteractionResultHolder> result = Objects.requireNonNull(provider.provide(screen, mouse)); if (result.getResult() == InteractionResult.SUCCESS) { if (result != null && !result.getObject().isEmpty()) return result.getObject(); return null; } else if (result.getResult() == InteractionResult.FAIL) return null; } return null; } @Override public void registerDecider(OverlayDecider decider) { deciders.add(decider); deciders.sort(Comparator.reverseOrder()); clickAreas.clear(); cache.clear(); tmpScreen = null; } @Override public void registerFocusedStack(FocusedStackProvider provider) { focusedStackProviders.add(provider); focusedStackProviders.sort(Comparator.reverseOrder()); } @Override public ExclusionZones exclusionZones() { return exclusionZones; } @Override public > void registerContainerClickArea(SimpleClickArea area, Class screenClass, ResourceLocation... categories) { registerClickArea(screen -> { Rectangle rectangle = area.provide(screen).clone(); rectangle.translate(screen.leftPos, screen.topPos); return rectangle; }, screenClass, categories); } @Override public void registerClickArea(Class screenClass, ClickArea area) { clickAreas.put(screenClass, area); } @Override @Nullable public Set handleClickArea(Class screenClass, ClickArea.ClickAreaContext context) { Mutable> categories = new MutableObject<>(null); for (ClickArea area : this.clickAreas.get(screenClass)) { ClickArea.Result result = ((ClickArea) area).handle(context); if (result.isSuccessful()) { if (categories.getValue() == null) { categories.setValue(new LinkedHashSet<>()); } result.getCategories().collect(Collectors.toCollection(categories::getValue)); } } return categories.getValue(); } @Override public void startReload() { clickAreas.clear(); deciders.clear(); cache.clear(); focusedStackProviders.clear(); tmpScreen = null; registerDefault(); } private void registerDefault() { registerDecider(this.exclusionZones = new ExclusionZonesImpl()); registerDecider(new OverlayDecider() { @Override public boolean isHandingScreen(Class screen) { return true; } @Override public InteractionResult shouldScreenBeOverlaid(Class screen) { return AbstractContainerScreen.class.isAssignableFrom(screen) ? InteractionResult.SUCCESS : InteractionResult.PASS; } @Override public float getPriority() { return -10; } }); registerFocusedStack(new FocusedStackProvider() { @Override @NotNull public InteractionResultHolder> provide(Screen screen, Point mouse) { if (screen instanceof AbstractContainerScreen) { AbstractContainerScreen containerScreen = (AbstractContainerScreen) screen; if (containerScreen.hoveredSlot != null && !containerScreen.hoveredSlot.getItem().isEmpty()) return InteractionResultHolder.success(EntryStacks.of(containerScreen.hoveredSlot.getItem())); } return InteractionResultHolder.pass(EntryStack.empty()); } @Override public double getPriority() { return -10.0; } }); } }