|
@@ -24,26 +24,25 @@
|
|
|
package me.shedaniel.rei.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
-import com.google.common.collect.Queues;
|
|
|
import com.google.common.collect.Sets;
|
|
|
import me.shedaniel.rei.RoughlyEnoughItemsCore;
|
|
|
import me.shedaniel.rei.api.ConfigObject;
|
|
|
import me.shedaniel.rei.api.EntryRegistry;
|
|
|
import me.shedaniel.rei.api.EntryStack;
|
|
|
-import me.shedaniel.rei.api.RecipeHelper;
|
|
|
import me.shedaniel.rei.impl.filtering.FilteringContextImpl;
|
|
|
import me.shedaniel.rei.impl.filtering.FilteringRule;
|
|
|
+import me.shedaniel.rei.utils.CollectionUtils;
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
import net.fabricmc.api.Environment;
|
|
|
import net.minecraft.item.Item;
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
-import net.minecraft.util.Pair;
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
|
|
import org.jetbrains.annotations.ApiStatus;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
@ApiStatus.Internal
|
|
|
@Environment(EnvType.CLIENT)
|
|
@@ -51,9 +50,8 @@ public class EntryRegistryImpl implements EntryRegistry {
|
|
|
|
|
|
private final List<EntryStack> preFilteredList = Lists.newCopyOnWriteArrayList();
|
|
|
private final List<EntryStack> entries = Lists.newCopyOnWriteArrayList();
|
|
|
- private final Queue<Pair<EntryStack, Collection<? extends EntryStack>>> queueRegisterEntryStackAfter = Queues.newConcurrentLinkedQueue();
|
|
|
- private List<EntryStack> reloadList;
|
|
|
- private boolean doingDistinct = false;
|
|
|
+ private final List<AmountIgnoredEntryStackWrapper> reloadingRegistry = Lists.newArrayList();
|
|
|
+ private boolean reloading;
|
|
|
|
|
|
private static EntryStack findFirstOrNullEqualsEntryIgnoreAmount(Collection<EntryStack> list, EntryStack obj) {
|
|
|
for (EntryStack t : list) {
|
|
@@ -63,100 +61,67 @@ public class EntryRegistryImpl implements EntryRegistry {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public void distinct() {
|
|
|
+ public void finishReload() {
|
|
|
+ reloading = false;
|
|
|
preFilteredList.clear();
|
|
|
- doingDistinct = true;
|
|
|
- while (true) {
|
|
|
- Pair<EntryStack, Collection<? extends EntryStack>> pair = queueRegisterEntryStackAfter.poll();
|
|
|
- if (pair == null)
|
|
|
- break;
|
|
|
- registerEntriesAfter(pair.getLeft(), pair.getRight());
|
|
|
- }
|
|
|
- doingDistinct = false;
|
|
|
- Set<EntryStackWrapper> set = Sets.newLinkedHashSet();
|
|
|
- set.addAll(reloadList.stream().map(EntryStackWrapper::new).collect(Collectors.toList()));
|
|
|
- set.removeIf(EntryStackWrapper::isEmpty);
|
|
|
+ reloadingRegistry.removeIf(AmountIgnoredEntryStackWrapper::isEmpty);
|
|
|
entries.clear();
|
|
|
- entries.addAll(set.stream().map(EntryStackWrapper::unwrap).collect(Collectors.toList()));
|
|
|
- }
|
|
|
-
|
|
|
- private static class EntryStackWrapper {
|
|
|
- private final EntryStack stack;
|
|
|
-
|
|
|
- public EntryStackWrapper(EntryStack stack) {
|
|
|
- this.stack = Objects.requireNonNull(stack);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean equals(Object o) {
|
|
|
- if (this == o) return true;
|
|
|
- if (o == null || getClass() != o.getClass()) return false;
|
|
|
-
|
|
|
- EntryStackWrapper that = (EntryStackWrapper) o;
|
|
|
- return stack.equalsAll(that.stack);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int hashCode() {
|
|
|
- return stack.hashCode();
|
|
|
- }
|
|
|
-
|
|
|
- public boolean isEmpty() {
|
|
|
- return stack.isEmpty();
|
|
|
- }
|
|
|
-
|
|
|
- public EntryStack unwrap() {
|
|
|
- return stack;
|
|
|
- }
|
|
|
+ entries.addAll(CollectionUtils.map(reloadingRegistry, AmountIgnoredEntryStackWrapper::unwrap));
|
|
|
+ reloadingRegistry.clear();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<EntryStack> getStacksList() {
|
|
|
- return RecipeHelper.getInstance().arePluginsLoading() || doingDistinct ? reloadList : entries;
|
|
|
+ @NotNull
|
|
|
+ public Stream<EntryStack> getEntryStacks() {
|
|
|
+ return entries.stream();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @NotNull
|
|
|
public List<EntryStack> getPreFilteredList() {
|
|
|
return preFilteredList;
|
|
|
}
|
|
|
|
|
|
public void refilter() {
|
|
|
long started = System.currentTimeMillis();
|
|
|
- FilteringContextImpl context = new FilteringContextImpl(getStacksList());
|
|
|
+
|
|
|
+ FilteringContextImpl context = new FilteringContextImpl(entries);
|
|
|
List<FilteringRule<?>> rules = ConfigObject.getInstance().getFilteringRules();
|
|
|
for (int i = rules.size() - 1; i >= 0; i--) {
|
|
|
context.handleResult(rules.get(i).processFilteredStacks(context));
|
|
|
}
|
|
|
+
|
|
|
+ Set<AmountIgnoredEntryStackWrapper> set = Sets.newLinkedHashSet();
|
|
|
+ set.addAll(CollectionUtils.map(entries, AmountIgnoredEntryStackWrapper::new));
|
|
|
+ Collection<EntryStack> hiddenStacks = context.getHiddenStacks();
|
|
|
+ set.removeAll(CollectionUtils.map(hiddenStacks, AmountIgnoredEntryStackWrapper::new));
|
|
|
preFilteredList.clear();
|
|
|
- Collection<EntryStack> filteredStacks = context.getHiddenStacks();
|
|
|
- for (EntryStack stack : getStacksList()) {
|
|
|
- if (findFirstOrNullEqualsEntryIgnoreAmount(filteredStacks, stack) == null)
|
|
|
- preFilteredList.add(stack);
|
|
|
- }
|
|
|
+ preFilteredList.addAll(CollectionUtils.map(set, AmountIgnoredEntryStackWrapper::unwrap));
|
|
|
+
|
|
|
long time = System.currentTimeMillis() - started;
|
|
|
- RoughlyEnoughItemsCore.LOGGER.info("Refiltered %d entries with %d rules in %dms.", filteredStacks.size(), rules.size(), time);
|
|
|
+ RoughlyEnoughItemsCore.LOGGER.info("Refiltered %d entries with %d rules in %dms.", entries.size() - preFilteredList.size(), rules.size(), time);
|
|
|
}
|
|
|
|
|
|
public void reset() {
|
|
|
- doingDistinct = false;
|
|
|
- reloadList = Lists.newArrayList();
|
|
|
- queueRegisterEntryStackAfter.clear();
|
|
|
entries.clear();
|
|
|
- reloadList.clear();
|
|
|
+ reloadingRegistry.clear();
|
|
|
preFilteredList.clear();
|
|
|
+ reloading = true;
|
|
|
}
|
|
|
|
|
|
+ @NotNull
|
|
|
@Override
|
|
|
- public List<ItemStack> appendStacksForItem(Item item) {
|
|
|
- DefaultedList<ItemStack> list = new DefaultedLinkedList<>(Lists.newLinkedList(), null);
|
|
|
+ public List<ItemStack> appendStacksForItem(@NotNull Item item) {
|
|
|
+ DefaultedList<ItemStack> list = DefaultedList.of();
|
|
|
item.appendStacks(item.getGroup(), list);
|
|
|
if (list.isEmpty())
|
|
|
- list.add(item.getStackForRender());
|
|
|
+ return Collections.singletonList(item.getStackForRender());
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ @NotNull
|
|
|
@Override
|
|
|
- public ItemStack[] getAllStacksFromItem(Item item) {
|
|
|
+ public ItemStack[] getAllStacksFromItem(@NotNull Item item) {
|
|
|
List<ItemStack> list = appendStacksForItem(item);
|
|
|
ItemStack[] array = list.toArray(new ItemStack[0]);
|
|
|
Arrays.sort(array, (a, b) -> ItemStack.areEqualIgnoreDamage(a, b) ? 0 : 1);
|
|
@@ -164,52 +129,17 @@ public class EntryRegistryImpl implements EntryRegistry {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Deprecated
|
|
|
- @ApiStatus.Internal
|
|
|
- public void registerEntryAfter(EntryStack afterEntry, EntryStack stack, boolean checkAlreadyContains) {
|
|
|
- if (stack.isEmpty())
|
|
|
- return;
|
|
|
- if (afterEntry == null) {
|
|
|
- getStacksList().add(stack);
|
|
|
- } else {
|
|
|
- int last = getStacksList().size();
|
|
|
- for (int i = last - 1; i >= 0; i--)
|
|
|
- if (getStacksList().get(i).equalsAll(afterEntry)) {
|
|
|
- last = i + 1;
|
|
|
- break;
|
|
|
- }
|
|
|
- getStacksList().add(last, stack);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void queueRegisterEntryAfter(EntryStack afterEntry, Collection<? extends EntryStack> stacks) {
|
|
|
- if (RecipeHelper.getInstance().arePluginsLoading()) {
|
|
|
- queueRegisterEntryStackAfter.add(new Pair<>(afterEntry, stacks));
|
|
|
+ public void registerEntriesAfter(@Nullable EntryStack afterStack, @NotNull Collection<@NotNull ? extends EntryStack> stacks) {
|
|
|
+ if (reloading) {
|
|
|
+ int index = afterStack != null ? reloadingRegistry.lastIndexOf(new AmountIgnoredEntryStackWrapper(afterStack)) : -1;
|
|
|
+ if (index >= 0) {
|
|
|
+ reloadingRegistry.addAll(index, CollectionUtils.map(stacks, AmountIgnoredEntryStackWrapper::new));
|
|
|
+ } else reloadingRegistry.addAll(CollectionUtils.map(stacks, AmountIgnoredEntryStackWrapper::new));
|
|
|
} else {
|
|
|
- registerEntriesAfter(afterEntry, stacks);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void registerEntriesAfter(EntryStack afterStack, Collection<? extends EntryStack> stacks) {
|
|
|
- if (afterStack != null) {
|
|
|
- int index = getStacksList().size();
|
|
|
- for (int i = index - 1; i >= 0; i--) {
|
|
|
- if (getStacksList().get(i).equalsIgnoreAmount(afterStack)) {
|
|
|
- index = i + 1;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- getStacksList().addAll(index, stacks);
|
|
|
- } else
|
|
|
- getStacksList().addAll(stacks);
|
|
|
- }
|
|
|
-
|
|
|
- @ApiStatus.Internal
|
|
|
- public static class DefaultedLinkedList<E> extends DefaultedList<E> {
|
|
|
- public DefaultedLinkedList(List<E> delegate, @Nullable E initialElement) {
|
|
|
- super(delegate, initialElement);
|
|
|
+ if (afterStack != null) {
|
|
|
+ int index = entries.lastIndexOf(afterStack);
|
|
|
+ entries.addAll(index, stacks);
|
|
|
+ } else entries.addAll(stacks);
|
|
|
}
|
|
|
}
|
|
|
}
|