diff options
| author | shedaniel <daniel@shedaniel.me> | 2020-08-20 22:18:59 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2020-08-20 22:18:59 +0800 |
| commit | 38779f9b8e3804a6efa0e47d28226017e2584c13 (patch) | |
| tree | a65afdd6351938108300b322de5f94ec7253aff1 /RoughlyEnoughItems-api/src/main/java | |
| parent | 2ea877295f2fd55eb208af2e296b83df6d443f84 (diff) | |
| download | RoughlyEnoughItems-38779f9b8e3804a6efa0e47d28226017e2584c13.tar.gz RoughlyEnoughItems-38779f9b8e3804a6efa0e47d28226017e2584c13.tar.bz2 RoughlyEnoughItems-38779f9b8e3804a6efa0e47d28226017e2584c13.zip | |
Some changes to partitioning search and optimisations to asFormattedText in ItemEntryStack.
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'RoughlyEnoughItems-api/src/main/java')
5 files changed, 168 insertions, 3 deletions
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ConfigObject.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ConfigObject.java index 4073d626d..044b5e659 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ConfigObject.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ConfigObject.java @@ -155,7 +155,7 @@ public interface ConfigObject { boolean shouldAsyncSearch(); @ApiStatus.Experimental - int getNumberAsyncSearch(); + int getAsyncSearchPartitionSize(); @ApiStatus.Experimental boolean doDebugSearchTimeRequired(); diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java index 782ed3f40..168098f72 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java @@ -25,11 +25,11 @@ package me.shedaniel.rei.api; import me.shedaniel.math.impl.PointHelper; import me.shedaniel.rei.api.widgets.Tooltip; +import me.shedaniel.rei.utils.FormattingUtils; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; import org.jetbrains.annotations.NotNull; @Environment(EnvType.CLIENT) @@ -46,6 +46,6 @@ public interface TextRepresentable { @NotNull default Text asFormatStrippedText() { - return new LiteralText(Formatting.strip(asFormattedText().getString())); + return new LiteralText(FormattingUtils.stripFormatting(asFormattedText().getString())); } } diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java index 7e46a05f6..a6d61ea29 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java @@ -25,9 +25,11 @@ package me.shedaniel.rei.utils; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.common.collect.UnmodifiableIterator; import me.shedaniel.rei.api.EntryStack; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.util.math.MathHelper; import java.util.*; import java.util.function.Function; @@ -275,4 +277,35 @@ public class CollectionUtils { } return sum; } + + public static <T> Iterable<Iterable<T>> partition(List<T> list, int size) { + return () -> new UnmodifiableIterator<Iterable<T>>() { + int i = 0; + int partitionSize = MathHelper.ceil(list.size() / (float) size); + + @Override + public boolean hasNext() { + return i < partitionSize; + } + + @Override + public Iterable<T> next() { + UnmodifiableIterator<T> iterator = new UnmodifiableIterator<T>() { + int cursor = i++ * size; + int curSize = cursor + Math.min(list.size() - cursor, size); + + @Override + public boolean hasNext() { + return cursor < curSize; + } + + @Override + public T next() { + return list.get(cursor++); + } + }; + return () -> iterator; + } + }; + } } diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/FormattingUtils.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/FormattingUtils.java new file mode 100644 index 000000000..411518b37 --- /dev/null +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/FormattingUtils.java @@ -0,0 +1,48 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.utils; + +public final class FormattingUtils { + public static String stripFormatting(String string) { + StringBuilder builder = new StringBuilder(); + boolean lastSpecial = false; + for (char c : string.toCharArray()) { + if (lastSpecial) { + lastSpecial = false; + if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || c == 'r' || (c >= 'A' && c <= 'F') || c == 'R')) { + builder.append('§'); + builder.append(c); + } + } else if (c == '§') { + lastSpecial = true; + } else { + builder.append(c); + } + } + if (lastSpecial) { + builder.append('§'); + } + return builder.toString(); + } +} diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java new file mode 100644 index 000000000..28a59a3f5 --- /dev/null +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java @@ -0,0 +1,84 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.utils; + +import net.minecraft.text.*; +import net.minecraft.util.Language; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public final class ImmutableLiteralText implements Text { + public static final ImmutableLiteralText EMPTY = new ImmutableLiteralText(""); + private final String content; + private OrderedText orderedText; + + public ImmutableLiteralText(String content) { + this.content = content; + } + + @Override + public Style getStyle() { + return Style.EMPTY; + } + + @Override + public String asString() { + return content; + } + + @Override + public List<Text> getSiblings() { + return Collections.emptyList(); + } + + @Override + public MutableText copy() { + return new LiteralText(content); + } + + @Override + public MutableText shallowCopy() { + return copy(); + } + + @Override + public <T> Optional<T> visit(Visitor<T> visitor) { + return visitSelf(visitor); + } + + @Override + public <T> Optional<T> visit(StyledVisitor<T> styledVisitor, Style style) { + return visitSelf(styledVisitor, style); + } + + @Override + public OrderedText asOrderedText() { + if (orderedText != null) { + orderedText = Language.getInstance().reorder(this); + } + return orderedText; + } +} |
