diff options
Diffstat (limited to 'shared-internals/src/main/java/me')
15 files changed, 1016 insertions, 0 deletions
diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerInternal.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerInternal.java new file mode 100644 index 000000000..7a60c7692 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerInternal.java @@ -0,0 +1,57 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.config; + +import me.shedaniel.autoconfig.gui.registry.GuiRegistry; +import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Jankson; +import me.shedaniel.clothconfig2.api.AbstractConfigListEntry; +import me.shedaniel.rei.api.client.config.ConfigManager; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Collection; +import java.util.Collections; + +@ApiStatus.Internal +public interface ConfigManagerInternal extends ConfigManager { + /** + * @return the instance of {@link ConfigManagerInternal} + */ + static ConfigManagerInternal getInstance() { + return (ConfigManagerInternal) ConfigManager.getInstance(); + } + + Object get(String fieldKey); + + boolean set(String fieldKey, Object value); + + interface SystemSetup { + default void setup(Jankson.Builder builder) {} + + default void setup(GuiRegistry registry) {} + + default Collection<? extends AbstractConfigListEntry<?>> collectAdvanced() { + return Collections.emptyList(); + } + } +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/favorites/MutableFavoritesList.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/favorites/MutableFavoritesList.java new file mode 100644 index 000000000..27ac4c940 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/favorites/MutableFavoritesList.java @@ -0,0 +1,32 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.favorites; + +import me.shedaniel.rei.api.client.favorites.FavoriteEntry; + +import java.util.List; + +public interface MutableFavoritesList extends List<FavoriteEntry> { + void setAll(List<FavoriteEntry> entries); +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/InternalCursorState.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/InternalCursorState.java new file mode 100644 index 000000000..5319924f7 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/InternalCursorState.java @@ -0,0 +1,32 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.gui; + +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class InternalCursorState { + public static boolean isLeftMousePressed; + public static boolean isRightMousePressed; +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/InternalTextures.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/InternalTextures.java new file mode 100644 index 000000000..4ab5c6961 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/InternalTextures.java @@ -0,0 +1,38 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.gui; + +import net.minecraft.resources.ResourceLocation; + +public class InternalTextures { + public static final ResourceLocation ARROW_LEFT_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/arrow_left.png"); + public static final ResourceLocation ARROW_RIGHT_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/arrow_right.png"); + public static final ResourceLocation ARROW_LEFT_SMALL_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/arrow_left_small.png"); + public static final ResourceLocation ARROW_RIGHT_SMALL_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/arrow_right_small.png"); + public static final ResourceLocation CHEST_GUI_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/recipecontainer.png"); + public static final ResourceLocation CHEST_GUI_TEXTURE_DARK = new ResourceLocation("roughlyenoughitems", "textures/gui/recipecontainer_dark.png"); + public static final ResourceLocation DISPLAY_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/display.png"); + public static final ResourceLocation DISPLAY_TEXTURE_DARK = new ResourceLocation("roughlyenoughitems", "textures/gui/display_dark.png"); + public static final ResourceLocation TOASTS = new ResourceLocation("roughlyenoughitems", "textures/gui/toasts.png"); +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/hints/HintProvider.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/hints/HintProvider.java new file mode 100644 index 000000000..475e48f44 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/hints/HintProvider.java @@ -0,0 +1,59 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.gui.hints; + +import me.shedaniel.math.Color; +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.impl.client.gui.menu.MenuAccess; +import me.shedaniel.rei.impl.common.Internals; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.function.Consumer; + +@Environment(EnvType.CLIENT) +public interface HintProvider { + List<HintProvider> PROVIDERS = Internals.resolveServices(HintProvider.class); + + List<Component> provide(); + + @Nullable + Tooltip provideTooltip(Point mouse); + + @Nullable + default Double getProgress() { + return null; + } + + Color getColor(); + + List<HintButton> getButtons(MenuAccess access); + + record HintButton(Component name, Consumer<Rectangle> action) {} +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/menu/MenuAccess.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/menu/MenuAccess.java new file mode 100644 index 000000000..79daeb3f0 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/menu/MenuAccess.java @@ -0,0 +1,51 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.gui.menu; + +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.favorites.FavoriteMenuEntry; + +import java.util.Collection; +import java.util.UUID; +import java.util.function.Predicate; +import java.util.function.Supplier; + +public interface MenuAccess { + boolean isOpened(UUID uuid); + + boolean isAnyOpened(); + + boolean isInBounds(UUID uuid); + + void open(UUID uuid, Rectangle selfBounds, Supplier<Collection<FavoriteMenuEntry>> menuSupplier); + + void open(UUID uuid, Rectangle selfBounds, Supplier<Collection<FavoriteMenuEntry>> menuSupplier, Predicate<Point> or, Predicate<Point> and); + + void openOrClose(UUID uuid, Rectangle selfBounds, Supplier<Collection<FavoriteMenuEntry>> menuSupplier); + + boolean isValidPoint(Point point); + + void close(); +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/screen/ConfigReloadingScreen.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/screen/ConfigReloadingScreen.java new file mode 100644 index 000000000..7d64bc935 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/screen/ConfigReloadingScreen.java @@ -0,0 +1,77 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.gui.screen; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.Util; +import net.minecraft.client.gui.chat.NarratorChatListener; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.BooleanSupplier; + +@ApiStatus.Internal +public final class ConfigReloadingScreen extends Screen { + private final Component title; + private final BooleanSupplier predicate; + private final Runnable parent; + + public ConfigReloadingScreen(Component title, BooleanSupplier predicate, Runnable parent) { + super(NarratorChatListener.NO_TITLE); + this.title = title; + this.predicate = predicate; + this.parent = parent; + } + + @Override + public boolean shouldCloseOnEsc() { + return false; + } + + @Override + public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { + this.renderDirtBackground(0); + if (!predicate.getAsBoolean()) { + parent.run(); + return; + } + drawCenteredString(matrices, this.font, title, this.width / 2, this.height / 2 - 50, 16777215); + String text; + switch ((int) (Util.getMillis() / 300L % 4L)) { + case 0: + default: + text = "O o o"; + break; + case 1: + case 3: + text = "o O o"; + break; + case 2: + text = "o o O"; + } + drawCenteredString(matrices, this.font, text, this.width / 2, this.height / 2 - 41, 8421504); + super.render(matrices, mouseX, mouseY, delta); + } +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java new file mode 100644 index 000000000..442fda91c --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java @@ -0,0 +1,226 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.search; + +import com.google.common.collect.Lists; +import dev.architectury.platform.Platform; +import me.shedaniel.rei.api.client.config.ConfigObject; +import me.shedaniel.rei.api.client.search.SearchFilter; +import me.shedaniel.rei.api.client.search.SearchProvider; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.util.CollectionUtils; +import me.shedaniel.rei.impl.client.util.ThreadCreator; +import org.jetbrains.annotations.Nullable; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.*; +import java.util.function.BiConsumer; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +public class AsyncSearchManager implements SearchManager { + private static final ExecutorService EXECUTOR_SERVICE = new ThreadCreator("REI-AsyncSearchManager").asService(); + private final Supplier<List<EntryStack<?>>> stacksProvider; + private final Supplier<Predicate<EntryStack<?>>> additionalPredicateSupplier; + private final UnaryOperator<EntryStack<?>> transformer; + private ExecutorTuple executor; + private SearchFilter filter; + private Map.Entry<List<EntryStack<?>>, SearchFilter> last; + + public AsyncSearchManager(Supplier<List<EntryStack<?>>> stacksProvider, Supplier<Predicate<EntryStack<?>>> additionalPredicateSupplier, UnaryOperator<EntryStack<?>> transformer) { + this.stacksProvider = stacksProvider; + this.additionalPredicateSupplier = additionalPredicateSupplier; + this.transformer = transformer; + } + + @Override + public void markDirty() { + synchronized (AsyncSearchManager.this) { + this.last = null; + } + } + + private record ExecutorTuple(SearchFilter filter, CompletableFuture<Map.Entry<List<EntryStack<?>>, SearchFilter>> future) { + } + + @Override + public void updateFilter(String filter) { + if (this.filter == null || !this.filter.getFilter().equals(filter)) { + if (this.executor != null) { + this.executor.future().cancel(Platform.isFabric()); + } + this.executor = null; + this.filter = SearchProvider.getInstance().createFilter(filter); + } + } + + @Override + public boolean isDirty() { + synchronized (AsyncSearchManager.this) { + return this.last == null || this.last.getValue() != this.filter; + } + } + + @Override + public Future<?> getAsync(BiConsumer<List<EntryStack<?>>, SearchFilter> consumer) { + if (this.executor == null || this.executor.filter() != filter) { + if (this.executor != null) { + this.executor.future().cancel(Platform.isFabric()); + } + this.executor = new ExecutorTuple(filter, get(EXECUTOR_SERVICE)); + } + SearchFilter savedFilter = filter; + return (this.executor = new ExecutorTuple(this.executor.filter(), this.executor.future().thenApplyAsync(result -> { + if (savedFilter == filter) { + consumer.accept(result.getKey(), result.getValue()); + } + + return result; + }, EXECUTOR_SERVICE))).future(); + } + + @Override + public List<EntryStack<?>> get() { + return getNow(); + } + + public List<EntryStack<?>> getNow() { + try { + return get(Runnable::run).get().getKey(); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } catch (InterruptedException | CancellationException e) { + return Lists.newArrayList(); + } + } + + public CompletableFuture<Map.Entry<List<EntryStack<?>>, SearchFilter>> get(Executor executor) { + if (isDirty()) { + Map.Entry<List<EntryStack<?>>, SearchFilter> last; + synchronized (AsyncSearchManager.this) { + last = this.last; + } + return get(this.filter, this.additionalPredicateSupplier.get(), this.transformer, + this.stacksProvider.get(), last, this, executor) + .thenApply(entry -> { + synchronized (AsyncSearchManager.this) { + this.last = entry; + } + return entry; + }); + } + + return CompletableFuture.completedFuture(last); + } + + public static CompletableFuture<Map.Entry<List<EntryStack<?>>, SearchFilter>> get(SearchFilter filter, Predicate<EntryStack<?>> additionalPredicate, + UnaryOperator<EntryStack<?>> transformer, List<EntryStack<?>> stacks, Map.Entry<List<EntryStack<?>>, SearchFilter> last, + AsyncSearchManager manager, Executor executor) { + int searchPartitionSize = ConfigObject.getInstance().getAsyncSearchPartitionSize(); + boolean shouldAsync = ConfigObject.getInstance().shouldAsyncSearch() && stacks.size() > searchPartitionSize * 4; + + if (!stacks.isEmpty()) { + CompletableFuture<Void> preparationFuture = CompletableFuture.completedFuture(null); + + if (last == null || last.getValue() != filter) { + Runnable prepare = () -> { + if (manager.filter == filter) { + filter.prepareFilter(stacks); + } else { + throw new CancellationException(); + } + }; + if (shouldAsync) { + preparationFuture = CompletableFuture.runAsync(prepare, executor); + } else { + prepare.run(); + preparationFuture = CompletableFuture.completedFuture(null); + } + } + + if (shouldAsync) { + List<CompletableFuture<List<EntryStack<?>>>> futures = Lists.newArrayList(); + for (Iterable<EntryStack<?>> partitionStacks : CollectionUtils.partition(stacks, Math.max(searchPartitionSize, stacks.size() * 3 / Runtime.getRuntime().availableProcessors()))) { + futures.add(CompletableFuture.supplyAsync(() -> { + List<EntryStack<?>> filtered = Lists.newArrayList(); + for (EntryStack<?> stack : partitionStacks) { + if (stack != null && filter.test(stack) && additionalPredicate.test(stack)) { + filtered.add(transformer.apply(stack)); + } + if (manager.filter != filter) throw new CancellationException(); + } + return filtered; + }, executor)); + } + return preparationFuture.thenCompose($ -> CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .orTimeout(30, TimeUnit.SECONDS)) + .thenApplyAsync($ -> { + List<EntryStack<?>> list = new ArrayList<>(); + + if (manager.filter == filter) { + for (CompletableFuture<List<EntryStack<?>>> future : futures) { + List<EntryStack<?>> now = future.getNow(null); + if (now != null) list.addAll(now); + } + } else { + throw new CancellationException(); + } + + return list; + }, executor) + .thenApply(result -> { + return new AbstractMap.SimpleImmutableEntry<>(result, filter); + }); + } else { + List<EntryStack<?>> list = new ArrayList<>(); + + for (EntryStack<?> stack : stacks) { + if (filter.test(stack) && additionalPredicate.test(stack)) { + list.add(transformer.apply(stack)); + } + if (manager.filter != filter) throw new CancellationException(); + } + + return CompletableFuture.completedFuture(new AbstractMap.SimpleImmutableEntry<>(list, filter)); + } + } + + return CompletableFuture.completedFuture(new AbstractMap.SimpleImmutableEntry<>(Lists.newArrayList(), filter)); + } + + @Override + public boolean matches(EntryStack<?> stack) { + return filter.test(stack); + } + + @Override + @Nullable + public SearchFilter getSearchFilter() { + return filter; + } +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/SearchManager.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/SearchManager.java new file mode 100644 index 000000000..e57d36f4c --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/SearchManager.java @@ -0,0 +1,49 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.client.search; + +import me.shedaniel.rei.api.client.search.SearchFilter; +import me.shedaniel.rei.api.common.entry.EntryStack; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.concurrent.Future; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public interface SearchManager extends Supplier<List<EntryStack<?>>> { + void markDirty(); + + void updateFilter(String filter); + + boolean isDirty(); + + Future<?> getAsync(BiConsumer<List<EntryStack<?>>, SearchFilter> consumer); + + boolean matches(EntryStack<?> stack); + + @Nullable + SearchFilter getSearchFilter(); +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/client/util/CrashReportUtils.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/util/CrashReportUtils.java new file mode 100644 index 000000000..96c4fd177 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/util/CrashReportUtils.java @@ -0,0 +1,74 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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 |
