diff options
Diffstat (limited to 'api/src/main/java/me')
6 files changed, 92 insertions, 49 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java b/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java index 8bf538ad0..50d44339e 100644 --- a/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java @@ -23,11 +23,9 @@ package me.shedaniel.rei.api.gui; -import com.google.common.collect.Maps; import com.mojang.blaze3d.vertex.PoseStack; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; -import me.shedaniel.architectury.utils.Fraction; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.gui.widgets.Slot; @@ -43,10 +41,9 @@ import net.minecraft.util.Mth; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -64,22 +61,13 @@ public class SimpleDisplayRenderer extends DisplayRenderer { } private static List<EntryIngredient> simplify(List<EntryIngredient> original) { - Map<EntryIngredient, AtomicReference<Fraction>> inputCounter = Maps.newLinkedHashMap(); - original.stream().collect(Collectors.groupingBy(stacks -> CollectionUtils.mapAndMax(stacks, EntryStack::getAmount, Fraction::compareTo).orElse(Fraction.zero()))) - .forEach((fraction, value) -> { - if (!fraction.equals(Fraction.zero())) { - value.forEach(stackList -> { - EntryIngredient stacks = inputCounter.keySet().stream().filter(s -> equalsList(stackList, s)).findFirst().orElse(stackList); - AtomicReference<Fraction> reference = inputCounter.computeIfAbsent(stacks, s -> new AtomicReference<>(Fraction.zero())); - reference.set(reference.get().add(fraction)); - }); - } - }); - return inputCounter.entrySet().stream().map(entry -> EntryIngredient.of(CollectionUtils.map(entry.getKey(), stack -> { - EntryStack<?> s = stack.copy(); - s.setAmount(entry.getValue().get()); - return s; - }))).collect(Collectors.toList()); + List<EntryIngredient> out = new ArrayList<>(); + for (EntryIngredient ingredient : original) { + if (out.stream().noneMatch(s -> equalsList(ingredient, s))) { + out.add(ingredient); + } + } + return out; } public static DisplayRenderer from(Supplier<List<EntryIngredient>> input, Supplier<List<EntryIngredient>> output) { diff --git a/api/src/main/java/me/shedaniel/rei/api/gui/widgets/DelegateWidget.java b/api/src/main/java/me/shedaniel/rei/api/gui/widgets/DelegateWidget.java index ed4e4a3cc..bd22f7e78 100644 --- a/api/src/main/java/me/shedaniel/rei/api/gui/widgets/DelegateWidget.java +++ b/api/src/main/java/me/shedaniel/rei/api/gui/widgets/DelegateWidget.java @@ -1,3 +1,26 @@ +/* + * 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.api.gui.widgets; import com.mojang.blaze3d.vertex.PoseStack; diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java index dd2cbf0f4..eb8adb020 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java @@ -28,12 +28,9 @@ import com.google.gson.JsonObject; import com.mojang.serialization.Dynamic; import com.mojang.serialization.JsonOps; import me.shedaniel.architectury.platform.Platform; -import me.shedaniel.architectury.utils.Fraction; import me.shedaniel.rei.api.gui.Renderer; -import me.shedaniel.rei.api.ingredient.entry.ComparisonContext; -import me.shedaniel.rei.api.ingredient.entry.EntryDefinition; -import me.shedaniel.rei.api.ingredient.entry.EntryRenderer; -import me.shedaniel.rei.api.ingredient.entry.EntryType; +import me.shedaniel.rei.api.ingredient.entry.*; +import me.shedaniel.rei.api.ingredient.util.EntryStacks; import me.shedaniel.rei.api.util.TextRepresentable; import me.shedaniel.rei.impl.Internals; import net.fabricmc.api.EnvType; @@ -75,25 +72,31 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { try { JsonObject obj = jsonElement.getAsJsonObject(); EntryType<Object> type = EntryType.deferred(new ResourceLocation(GsonHelper.getAsString(obj, "type"))); - Object o = type.getDefinition().fromTag(TagParser.parseTag(obj.toString())); - return EntryStack.of(type, o); + EntrySerializer<Object> serializer = type.getDefinition().getSerializer(); + if (serializer != null && serializer.supportReading()) { + Object o = serializer.read(TagParser.parseTag(obj.toString())); + return EntryStack.of(type, o); + } } catch (Exception e) { e.printStackTrace(); - return EntryStack.empty(); } + return EntryStack.empty(); } @ApiStatus.Internal @Nullable default JsonElement toJson() { try { - JsonObject object = Dynamic.convert(NbtOps.INSTANCE, JsonOps.INSTANCE, getDefinition().toTag(this, getValue())).getAsJsonObject(); - object.addProperty("type", getType().getId().toString()); - return object; + EntrySerializer<T> serializer = getDefinition().getSerializer(); + if (serializer != null && serializer.supportSaving()) { + JsonObject object = Dynamic.convert(NbtOps.INSTANCE, JsonOps.INSTANCE, serializer.save(this, getValue())).getAsJsonObject(); + object.addProperty("type", getType().getId().toString()); + return object; + } } catch (Exception e) { e.printStackTrace(); - return null; } + return null; } @NotNull @@ -116,10 +119,6 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { Optional<ResourceLocation> getIdentifier(); - Fraction getAmount(); - - void setAmount(Fraction amount); - boolean isEmpty(); EntryStack<T> copy(); @@ -198,7 +197,7 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { public static class Fluid { private static final String FLUID_AMOUNT = Platform.isForge() ? "tooltip.rei.fluid_amount.forge" : "tooltip.rei.fluid_amount"; // Return null to disable - public static final Settings<Function<EntryStack<?>, String>> AMOUNT_TOOLTIP = new Settings<>(stack -> I18n.get(FLUID_AMOUNT, stack.simplifyAmount().getAmount())); + public static final Settings<Function<EntryStack<?>, String>> AMOUNT_TOOLTIP = new Settings<>(stack -> I18n.get(FLUID_AMOUNT, EntryStacks.simplifyAmount(stack.cast()).getValue().getAmount())); private Fluid() { } @@ -206,11 +205,6 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { } - default EntryStack<T> simplifyAmount() { - setAmount(getAmount().simplify()); - return this; - } - @ApiStatus.NonExtendable default <O> EntryStack<O> cast() { return (EntryStack<O>) this; diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntryDefinition.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntryDefinition.java index b1380a963..151cc986a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntryDefinition.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntryDefinition.java @@ -29,6 +29,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Optional; @@ -42,10 +43,6 @@ public interface EntryDefinition<T> { Optional<ResourceLocation> getIdentifier(EntryStack<T> entry, T value); - Fraction getAmount(EntryStack<T> entry, T value); - - void setAmount(EntryStack<T> entry, T value, Fraction amount); - boolean isEmpty(EntryStack<T> entry, T value); T copy(EntryStack<T> entry, T value); @@ -54,9 +51,8 @@ public interface EntryDefinition<T> { boolean equals(T o1, T o2, ComparisonContext context); - CompoundTag toTag(EntryStack<T> entry, T value); - - T fromTag(CompoundTag tag); + @Nullable + EntrySerializer<T> getSerializer(); Component asFormattedText(EntryStack<T> entry, T value); diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntrySerializer.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntrySerializer.java new file mode 100644 index 000000000..9bb8ca460 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntrySerializer.java @@ -0,0 +1,37 @@ +/* + * 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.api.ingredient.entry; + +import me.shedaniel.rei.api.ingredient.EntryStack; +import net.minecraft.nbt.CompoundTag; + +public interface EntrySerializer<T> { + boolean supportSaving(); + + boolean supportReading(); + + CompoundTag save(EntryStack<T> entry, T value); + + T read(CompoundTag tag); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/util/EntryStacks.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/util/EntryStacks.java index 30b6af005..51618ecc8 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/util/EntryStacks.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/util/EntryStacks.java @@ -196,4 +196,9 @@ public final class EntryStacks { public static <T> int hashIgnoreNbt(EntryStack<T> stack) { return stack.hash(ComparisonContext.IGNORE_NBT); } + + public static EntryStack<FluidStack> simplifyAmount(EntryStack<FluidStack> stack) { + stack.getValue().setAmount(stack.getValue().getAmount().simplify()); + return stack; + } } |
