/* * 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; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.serialization.Dynamic; import com.mojang.serialization.JsonOps; import me.shedaniel.architectury.utils.Fraction; import me.shedaniel.rei.api.entry.ComparisonContext; import me.shedaniel.rei.api.entry.EntryDefinition; import me.shedaniel.rei.api.entry.EntryRenderer; import me.shedaniel.rei.api.entry.EntryType; import me.shedaniel.rei.impl.Internals; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.resources.language.I18n; import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.TagParser; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.GsonHelper; import net.minecraft.util.Unit; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; @Environment(EnvType.CLIENT) public interface EntryStack extends TextRepresentable, Renderer { static EntryStack empty() { return Internals.getEntryStackProvider().empty(); } static EntryStack of(EntryDefinition definition, T value) { return Internals.getEntryStackProvider().of(definition, value); } static EntryStack of(EntryType type, T value) { return of(type.getDefinition(), value); } @ApiStatus.Internal static EntryStack readFromJson(JsonElement jsonElement) { try { JsonObject obj = jsonElement.getAsJsonObject(); EntryType type = EntryType.deferred(new ResourceLocation(GsonHelper.getAsString(obj, "type"))); Object o = type.getDefinition().fromTag(TagParser.parseTag(obj.toString())); return EntryStack.of(type, o); } catch (Exception e) { e.printStackTrace(); 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; } catch (Exception e) { e.printStackTrace(); return null; } } @NotNull EntryDefinition getDefinition(); @NotNull default EntryType getType() { return getDefinition().getType(); } @NotNull default Class getValueType() { return getDefinition().getValueType(); } @NotNull default EntryRenderer getRenderer() { return getDefinition().getRenderer(); } Optional getIdentifier(); Fraction getAmount(); void setAmount(Fraction amount); boolean isEmpty(); EntryStack copy(); @ApiStatus.Internal default EntryStack rewrap() { return copy(); } @Deprecated int hashCode(); int hash(ComparisonContext context); boolean equals(EntryStack other, ComparisonContext context); @Deprecated boolean equals(Object o); T getValue(); EntryStack setting(Settings settings, R value); EntryStack removeSetting(Settings settings); EntryStack clearSettings(); R get(Settings settings); class Settings { @ApiStatus.Internal private static final List> SETTINGS = new ArrayList<>(); public static final Supplier TRUE = () -> true; public static final Supplier FALSE = () -> false; public static final Settings> RENDER = new Settings<>(TRUE); public static final Settings> CHECK_TAGS = new Settings<>(FALSE); public static final Settings> CHECK_AMOUNT = new Settings<>(FALSE); public static final Settings> TOOLTIP_ENABLED = new Settings<>(TRUE); public static final Settings> TOOLTIP_APPEND_MOD = new Settings<>(TRUE); public static final Settings> RENDER_COUNTS = new Settings<>(TRUE); public static final Settings, List>> TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); public static final Settings, String>> COUNTS = new Settings<>(stack -> null); private static short nextId; private R defaultValue; private short id; @ApiStatus.Internal public Settings(R defaultValue) { this.defaultValue = defaultValue; this.id = nextId++; SETTINGS.add(this); } @ApiStatus.Internal public static Settings getById(short id) { return (Settings) SETTINGS.get(id); } public R getDefaultValue() { return defaultValue; } @ApiStatus.Internal public short getId() { return id; } public static class Fluid { // Return null to disable public static final Settings, String>> AMOUNT_TOOLTIP = new Settings<>(stack -> I18n.get("tooltip.rei.fluid_amount", stack.simplifyAmount().getAmount())); private Fluid() { } } } default EntryStack simplifyAmount() { setAmount(getAmount().simplify()); return this; } }