diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-07-27 23:25:27 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-08-26 10:52:27 +0900 |
| commit | 05069aa62b09f02a8cd6e526ec58a30347a56500 (patch) | |
| tree | acb90d01f0a06de7c6b540eefeeee8259016f8ac /shared-internals/src/main/java | |
| parent | 685861c91bbb8a8a882da51381d392f1310d061d (diff) | |
| download | RoughlyEnoughItems-05069aa62b09f02a8cd6e526ec58a30347a56500.tar.gz RoughlyEnoughItems-05069aa62b09f02a8cd6e526ec58a30347a56500.tar.bz2 RoughlyEnoughItems-05069aa62b09f02a8cd6e526ec58a30347a56500.zip | |
WIP Module
Diffstat (limited to 'shared-internals/src/main/java')
7 files changed, 446 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..48d2aec3b --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerInternal.java @@ -0,0 +1,41 @@ +/* + * 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.rei.api.client.config.ConfigManager; +import org.jetbrains.annotations.ApiStatus; + +@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); +} 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..0765cd0e4 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/favorites/MutableFavoritesList.java @@ -0,0 +1,9 @@ +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/hints/HintProvider.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/hints/HintProvider.java new file mode 100644 index 000000000..c7697c533 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/gui/hints/HintProvider.java @@ -0,0 +1,55 @@ +/* + * 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 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<Component> provide(); + + @Nullable + Tooltip provideTooltip(Point mouse); + + @Nullable + default Double getProgress() { + return null; + } + + Color getColor(); + + List<HintButton> getButtons(); + + record HintButton(Component name, Consumer<Rectangle> action) {} +} 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..1d3bfdecb --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java @@ -0,0 +1,148 @@ +/* + * 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 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 java.util.ArrayList; +import java.util.List; +import java.util.concurrent.*; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +public class AsyncSearchManager implements SearchManager { + private final Supplier<List<EntryStack<?>>> stacksProvider; + private final Supplier<Predicate<EntryStack<?>>> additionalPredicateSupplier; + private final UnaryOperator<EntryStack<?>> transformer; + private Predicate<EntryStack<?>> additionalPredicate; + private SearchFilter filter; + private boolean dirty = false; + private boolean filterDirty = false; + private CompletableFuture<List<EntryStack<?>>> future; + private List<EntryStack<?>> last; + + public AsyncSearchManager(Supplier<List<EntryStack<?>>> stacksProvider, Supplier<Predicate<EntryStack<?>>> additionalPredicateSupplier, UnaryOperator<EntryStack<?>> transformer) { + this.stacksProvider = stacksProvider; + this.additionalPredicateSupplier = additionalPredicateSupplier; + this.transformer = transformer; + } + + public void markDirty() { + this.dirty = true; + } + + public void markFilterDirty() { + this.filterDirty = true; + } + + public void updateFilter(String filter) { + if (this.filter == null || !this.filter.getFilter().equals(filter)) { + this.filter = SearchProvider.getInstance().createFilter(filter); + markDirty(); + markFilterDirty(); + } + } + + public boolean isDirty() { + return last == null || dirty; + } + + public boolean isFilterDirty() { + return filterDirty; + } + + public Future<Void> getAsync(Consumer<List<EntryStack<?>>> consumer) { + if (future == null || future.isCancelled() || future.isDone() || future.isCompletedExceptionally()) { + if (future != null) future.cancel(true); + future = CompletableFuture.supplyAsync(this) + .exceptionally(throwable -> { + throwable.printStackTrace(); + return null; + }); + } + return future.thenAccept(consumer); + } + + @Override + public List<EntryStack<?>> get() { + if (isDirty()) { + this.additionalPredicate = additionalPredicateSupplier.get(); + int searchPartitionSize = ConfigObject.getInstance().getAsyncSearchPartitionSize(); + List<EntryStack<?>> stacks = stacksProvider.get(); + last = new ArrayList<>(); + + if (!stacks.isEmpty()) { + if (filterDirty) { + filter.prepareFilter(stacks); + filterDirty = false; + } + + if (ConfigObject.getInstance().shouldAsyncSearch() && stacks.size() > searchPartitionSize * 4) { + List<CompletableFuture<List<EntryStack<?>>>> futures = Lists.newArrayList(); + for (Iterable<EntryStack<?>> partitionStacks : CollectionUtils.partition(stacks, searchPartitionSize)) { + futures.add(CompletableFuture.supplyAsync(() -> { + List<EntryStack<?>> filtered = Lists.newArrayList(); + for (EntryStack<?> stack : partitionStacks) { + if (stack != null && matches(stack) && additionalPredicate.test(stack)) { + filtered.add(transformer.apply(stack)); + } + } + return filtered; + })); + } + try { + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(10, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + for (CompletableFuture<List<EntryStack<?>>> future : futures) { + List<EntryStack<?>> now = future.getNow(null); + if (now != null) last.addAll(now); + } + } else { + for (EntryStack<?> stack : stacks) { + if (matches(stack) && additionalPredicate.test(stack)) { + last.add(transformer.apply(stack)); + } + } + } + } + + dirty = false; + } + + return last; + } + + public boolean matches(EntryStack<?> stack) { + return filter.test(stack); + } +} 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..92c44dc02 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/client/search/SearchManager.java @@ -0,0 +1,48 @@ +/* + * 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.SearchProvider; +import me.shedaniel.rei.api.common.entry.EntryStack; + +import java.util.List; +import java.util.concurrent.Future; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public interface SearchManager extends Supplier<List<EntryStack<?>>> { + void markDirty(); + + void markFilterDirty(); + + void updateFilter(String filter); + + boolean isDirty(); + + boolean isFilterDirty(); + + Future<Void> getAsync(Consumer<List<EntryStack<?>>> consumer); + + boolean matches(EntryStack<?> stack); +} 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..eab3a1c87 --- /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, + * 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.util; + +import me.shedaniel.rei.api.client.gui.Renderer; +import me.shedaniel.rei.impl.ClientInternals; +import net.minecraft.CrashReport; +import net.minecraft.CrashReportCategory; +import net.minecraft.ReportedException; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; + +public class CrashReportUtils { + public static CrashReport essential(Throwable throwable, String task) { + Throwable temp = throwable; + while (temp != null) { + temp = temp.getCause(); + if (temp instanceof ReportedException) { + return essential(temp, task); + } + } + CrashReport report = CrashReport.forThrowable(throwable, task); + screen(report, Minecraft.getInstance().screen); + return report; + } + + private static void screen(CrashReport report, Screen screen) { + if (screen != null) { + CrashReportCategory category = report.addCategory("Screen details"); + String screenName = screen.getClass().getCanonicalName(); + category.setDetail("Screen name", () -> screenName); + } + } + + public static void renderer(CrashReport report, Renderer renderer) { + if (renderer != null) { + CrashReportCategory category = report.addCategory("Renderer details"); + try { + renderer.fillCrashReport(report, category); + } catch (Throwable throwable) { + category.setDetailError("Filling Report", throwable); + } + } + } + + public static ReportedException throwReport(CrashReport report) { + return new ReportedException(report); + } + + public static void catchReport(CrashReport report) { + ClientInternals.crash(new ReportedException(report), report.getTitle()); + } +} diff --git a/shared-internals/src/main/java/me/shedaniel/rei/impl/common/util/HashedEntryStackWrapper.java b/shared-internals/src/main/java/me/shedaniel/rei/impl/common/util/HashedEntryStackWrapper.java new file mode 100644 index 000000000..9803c1797 --- /dev/null +++ b/shared-internals/src/main/java/me/shedaniel/rei/impl/common/util/HashedEntryStackWrapper.java @@ -0,0 +1,71 @@ +/* + * 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.common.util; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.util.EntryStacks; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Objects; + +@ApiStatus.Internal +public class HashedEntryStackWrapper { + private final EntryStack<?> stack; + private long hash; + private int hashInt; + + public HashedEntryStackWrapper(EntryStack<?> stack) { + this.stack = Objects.requireNonNull(stack); + this.hash = EntryStacks.hashExact(stack); + this.hashInt = Long.hashCode(this.hash); + } + + public HashedEntryStackWrapper(EntryStack<?> stack, long hash) { + this.stack = Objects.requireNonNull(stack); + this.hash = hash; + this.hashInt = Long.hashCode(this.hash); + } + + @Override + public boolean equals(Object o) { + return o instanceof HashedEntryStackWrapper && hash == ((HashedEntryStackWrapper) o).hash; + } + + @Override + public int hashCode() { + return hashInt; + } + + public boolean isEmpty() { + return stack.isEmpty(); + } + + public EntryStack<?> unwrap() { + return stack; + } + + public long hashExact() { + return hash; + } +}
\ No newline at end of file |
