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 From a015ddaba5ba8b02e93db164d219b0c12d8bfa3a Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 14 Apr 2021 22:23:17 +0800 Subject: Apply generics to FilteringScreen Signed-off-by: shedaniel --- .../shedaniel/rei/impl/client/config/entries/FilteringScreen.java | 8 ++++---- .../src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java index 9717f1cf1..032359ff1 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringScreen.java @@ -68,7 +68,7 @@ import static me.shedaniel.rei.impl.client.gui.widget.EntryListWidget.entrySize; @ApiStatus.Internal public class FilteringScreen extends Screen { - protected List selected = Lists.newArrayList(); + protected List> selected = Lists.newArrayList(); protected final ScrollingContainer scrolling = new ScrollingContainer() { @Override public int getMaxScrollHeight() { @@ -89,7 +89,7 @@ public class FilteringScreen extends Screen { Screen parent; private FilteringEntry filteringEntry; private Tooltip tooltip = null; - private List entryStacks = null; + private List> entryStacks = null; private Rectangle innerBounds; private List entries = Collections.emptyList(); private List elements = Collections.emptyList(); @@ -129,7 +129,7 @@ public class FilteringScreen extends Screen { Component hideText = new TranslatableComponent("config.roughlyenoughitems.filteredEntries.hide"); this.hideButton = new Button(0, 0, Minecraft.getInstance().font.width(hideText) + 10, 20, hideText, button -> { for (int i = 0; i < entryStacks.size(); i++) { - EntryStack stack = entryStacks.get(i); + EntryStack stack = entryStacks.get(i); EntryListEntry entry = entries.get(i); entry.getBounds().y = (int) (entry.backupY - scrolling.scrollAmount); if (entry.isSelected() && !entry.isFiltered()) { @@ -144,7 +144,7 @@ public class FilteringScreen extends Screen { Component showText = new TranslatableComponent("config.roughlyenoughitems.filteredEntries.show"); this.showButton = new Button(0, 0, Minecraft.getInstance().font.width(showText) + 10, 20, showText, button -> { for (int i = 0; i < entryStacks.size(); i++) { - EntryStack stack = entryStacks.get(i); + EntryStack stack = entryStacks.get(i); EntryListEntry entry = entries.get(i); entry.getBounds().y = (int) (entry.backupY - scrolling.scrollAmount); if (entry.isSelected() && filteringEntry.configFiltered.remove(stack)) { diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java index f0f1dae76..c31c1e864 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/test/REITestPlugin.java @@ -53,7 +53,7 @@ public class REITestPlugin implements REIClientPlugin { @Override public void registerEntries(EntryRegistry registry) { - int times = 1000; + int times = 100; for (Item item : Registry.ITEM) { EntryStack base = EntryStacks.of(item); registry.addEntriesAfter(base, IntStream.range(0, times).mapToObj(value -> transformStack(EntryStacks.of(item))).collect(Collectors.toList())); -- cgit From 4e2c0be9a75e435956ca80d56df2ca609245671d Mon Sep 17 00:00:00 2001 From: shedaniel Date: Fri, 16 Apr 2021 17:29:21 +0800 Subject: Normalize fluid type Signed-off-by: shedaniel --- .../me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java index 9344eb9e8..75871dbca 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java @@ -63,6 +63,7 @@ import net.minecraft.tags.TagContainer; import net.minecraft.util.Mth; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.material.FlowingFluid; import net.minecraft.world.level.material.Fluid; import org.jetbrains.annotations.Nullable; @@ -116,7 +117,9 @@ public class FluidEntryDefinition implements EntryDefinition, EntryS @Override public FluidStack normalize(EntryStack entry, FluidStack value) { - FluidStack copy = value.copy(); + Fluid fluid = value.getFluid(); + if (fluid instanceof FlowingFluid) fluid = ((FlowingFluid) fluid).getSource(); + FluidStack copy = FluidStack.create(fluid, value.getAmount(), value.getTag()); copy.setAmount(FluidStack.bucketAmount()); return copy; } -- cgit