From 7c2a510545e8280ff073e289e5d600e4614b27cc Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 14 Apr 2021 20:08:47 +0800 Subject: Refactor MenuInfo into a provider system and add more docs Signed-off-by: shedaniel --- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 26 +++++++------- .../rei/impl/common/transfer/InputSlotCrafter.java | 26 +++++++------- .../impl/common/transfer/MenuInfoRegistryImpl.java | 41 +++++++++++++++------- .../autocrafting/DefaultCategoryHandler.java | 2 +- 4 files changed, 57 insertions(+), 38 deletions(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 0ed0e6f78..6f7e36118 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -142,18 +142,18 @@ import java.util.stream.Stream; public class RoughlyEnoughItemsCore { @ApiStatus.Internal public static final Logger LOGGER = LogManager.getFormatterLogger("REI"); - private static final ExecutorService RELOAD_PLUGINS; + private static ExecutorService reloadPlugins; @ApiStatus.Experimental public static boolean isLeftMousePressed = false; static { - RELOAD_PLUGINS = Executors.newSingleThreadScheduledExecutor(r -> { - Thread thread = new Thread(r, "REI-ReloadPlugins"); - thread.setDaemon(true); - return thread; - }); attachCommonInternals(); if (Platform.getEnvironment() == Env.CLIENT) { + reloadPlugins = Executors.newSingleThreadScheduledExecutor(r -> { + Thread thread = new Thread(r, "REI-ReloadPlugins"); + thread.setDaemon(true); + return thread; + }); attachClientInternals(); } } @@ -401,15 +401,19 @@ public class RoughlyEnoughItemsCore { lastReload.setValue(System.currentTimeMillis()); } if (ConfigObject.getInstance().doesRegisterRecipesInAnotherThread()) { - CompletableFuture.runAsync(RoughlyEnoughItemsCore::_reloadPlugins, RELOAD_PLUGINS); + CompletableFuture.runAsync(RoughlyEnoughItemsCore::_reloadPlugins, reloadPlugins); } else { _reloadPlugins(); } } private static void _reloadPlugins() { - for (PluginManager> instance : PluginManager.getActiveInstances()) { - instance.startReload(); + try { + for (PluginManager> instance : PluginManager.getActiveInstances()) { + instance.startReload(); + } + } catch (Throwable throwable) { + throwable.printStackTrace(); } } @@ -421,9 +425,7 @@ public class RoughlyEnoughItemsCore { if (Platform.getEnvironment() == Env.SERVER) { MutableLong lastReload = new MutableLong(-1); ReloadListeners.registerReloadListener(PackType.SERVER_DATA, (preparationBarrier, resourceManager, profilerFiller, profilerFiller2, executor, executor2) -> { - return preparationBarrier.wait(Unit.INSTANCE).thenRunAsync(() -> { - CompletableFuture.runAsync(RoughlyEnoughItemsCore::_reloadPlugins, RELOAD_PLUGINS); - }, executor2); + return preparationBarrier.wait(Unit.INSTANCE).thenRunAsync(RoughlyEnoughItemsCore::_reloadPlugins, executor2); }); } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java index 17812fec9..f17ae6faa 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java @@ -31,7 +31,7 @@ import me.shedaniel.rei.api.common.transfer.RecipeFinder; import me.shedaniel.rei.api.common.transfer.info.MenuInfo; import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; -import me.shedaniel.rei.api.common.transfer.info.stack.StackAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import me.shedaniel.rei.api.common.util.CollectionUtils; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; @@ -51,8 +51,8 @@ public class InputSlotCrafter menuInfo; - private Iterable inputStacks; - private Iterable inventoryStacks; + private Iterable inputStacks; + private Iterable inventoryStacks; private ServerPlayer player; private InputSlotCrafter(CategoryIdentifier category, T container, CompoundTag display, MenuInfo menuInfo) { @@ -71,8 +71,8 @@ public class InputSlotCrafter ingredients = NonNullList.create(); - for (List itemStacks : this.menuInfo.getDisplayInputs(this)) { + for (List itemStacks : this.menuInfo.getInputs(this)) { ingredients.add(CollectionUtils.toIngredient(itemStacks)); } @@ -98,8 +98,8 @@ public class InputSlotCrafter inputStacks, Iterator recipeItemIds, int craftsAmount) { - for (StackAccessor inputStack : inputStacks) { + public void alignRecipeToGrid(Iterable inputStacks, Iterator recipeItemIds, int craftsAmount) { + for (SlotAccessor inputStack : inputStacks) { if (!recipeItemIds.hasNext()) { return; } @@ -108,7 +108,7 @@ public class InputSlotCrafter, Map, MenuInfo>> map = Maps.newLinkedHashMap(); - private final Map>, List>> mapGeneric = Maps.newLinkedHashMap(); + private final Map, Map, List>>> map = Maps.newLinkedHashMap(); + private final Map>, List>> mapGeneric = Maps.newLinkedHashMap(); @Override - public void register(CategoryIdentifier category, Class menuClass, MenuInfo menuInfo) { - map.computeIfAbsent(category, id -> Maps.newLinkedHashMap()).put(menuClass, menuInfo); + public void register(CategoryIdentifier category, Class menuClass, MenuInfoProvider menuInfo) { + map.computeIfAbsent(category, id -> Maps.newLinkedHashMap()) + .computeIfAbsent(menuClass, c -> Lists.newArrayList()) + .add(menuInfo); } @Override - public void registerGeneric(Predicate> categoryPredicate, MenuInfo menuInfo) { + public void registerGeneric(Predicate> categoryPredicate, MenuInfoProvider menuInfo) { mapGeneric.computeIfAbsent(categoryPredicate, id -> Lists.newArrayList()).add(menuInfo); } @Override public MenuInfo get(CategoryIdentifier category, Class menuClass) { - Map, MenuInfo> infoMap = map.get(category); + Map, List>> infoMap = map.get(category); if (infoMap != null && !infoMap.isEmpty()) { if (infoMap.containsKey(menuClass)) { - return (MenuInfo) infoMap.get(menuClass); + for (MenuInfoProvider provider : infoMap.get(menuClass)) { + Optional> info = ((MenuInfoProvider) provider).provide(category, menuClass); + if (info.isPresent()) { + return info.get(); + } + } } - for (Map.Entry, MenuInfo> entry : infoMap.entrySet()) { + for (Map.Entry, List>> entry : infoMap.entrySet()) { if (entry.getKey().isAssignableFrom(menuClass)) { - return (MenuInfo) entry.getValue(); + for (MenuInfoProvider provider : entry.getValue()) { + Optional> info = ((MenuInfoProvider) provider).provide(category, menuClass); + if (info.isPresent()) { + return info.get(); + } + } } } } - for (Map.Entry>, List>> entry : mapGeneric.entrySet()) { + for (Map.Entry>, List>> entry : mapGeneric.entrySet()) { if (entry.getKey().test(category) && !entry.getValue().isEmpty()) { - List> infoList = entry.getValue(); + List> infoList = entry.getValue(); if (!infoList.isEmpty()) { - return (MenuInfo) infoList.get(0); + Optional> info = ((MenuInfoProvider) infoList.get(0)).provide(category, menuClass); + if (info.isPresent()) { + return info.get(); + } } } } diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java b/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java index dba6fe449..a400c0155 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java @@ -74,7 +74,7 @@ public class DefaultCategoryHandler implements TransferHandler { } catch (MenuTransferException e) { return Result.createFailed(e.getError()); } - List> input = menuInfo.getDisplayInputs(menuInfoContext); + List> input = menuInfo.getInputs(menuInfoContext); IntList intList = hasItems(menu, menuInfo, display, input); if (!intList.isEmpty()) { return Result.createFailed(new TranslatableComponent("error.rei.not.enough.materials"), intList); -- cgit