From b11c3866aada638f0ad758ced4068c71e620ce23 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 27 Mar 2021 01:12:02 +0800 Subject: Make Recipe Transfer work Signed-off-by: shedaniel --- .../rei/api/client/search/SearchFilter.java | 5 ++ .../rei/api/common/entry/EntryIngredient.java | 8 ++- .../shedaniel/rei/api/common/entry/EntryStack.java | 9 ++- .../rei/api/common/entry/type/EntryDefinition.java | 3 +- .../rei/api/common/plugins/PluginManager.java | 2 +- .../rei/api/common/util/EntryIngredients.java | 2 +- .../shedaniel/rei/plugin/common/DefaultPlugin.java | 6 +- .../common/displays/DefaultInformationDisplay.java | 2 +- .../displays/brewing/DefaultBrewingDisplay.java | 37 ++++++++---- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 8 ++- .../impl/client/gui/widget/InternalWidgets.java | 7 ++- .../registry/display/DisplayRegistryImpl.java | 4 ++ .../rei/impl/client/search/argument/Argument.java | 6 +- .../search/argument/type/TagArgumentType.java | 3 +- .../display/DisplaySerializerRegistryImpl.java | 4 +- .../rei/impl/common/entry/AbstractEntryStack.java | 70 ++++++++++++++++++++++ .../rei/impl/common/entry/EmptyEntryStack.java | 45 ++++++++++++++ .../rei/impl/common/entry/EntryIngredientImpl.java | 45 ++++++++++++-- .../rei/impl/common/entry/TypedEntryStack.java | 63 ------------------- .../entry/type/types/BuiltinEntryDefinition.java | 3 +- .../common/registry/RecipeManagerContextImpl.java | 19 +++++- .../plugin/client/entry/FluidEntryDefinition.java | 10 +++- .../plugin/client/entry/ItemEntryDefinition.java | 5 +- 23 files changed, 253 insertions(+), 113 deletions(-) create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EmptyEntryStack.java diff --git a/api/src/main/java/me/shedaniel/rei/api/client/search/SearchFilter.java b/api/src/main/java/me/shedaniel/rei/api/client/search/SearchFilter.java index 735e5fddf..ce77fe569 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/search/SearchFilter.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/search/SearchFilter.java @@ -27,6 +27,11 @@ import me.shedaniel.rei.api.common.entry.EntryStack; import java.util.function.Predicate; +/** + * A search filter that respects different prefixes, matches a {@link EntryStack}. + * + * @see SearchProvider + */ public interface SearchFilter extends Predicate> { static SearchFilter matchAll() { return new SearchFilter() { diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java index 103586d35..cd179180f 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java @@ -28,6 +28,8 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import java.util.List; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; /** * An immutable representation of a list of {@link EntryStack}. @@ -59,7 +61,7 @@ public interface EntryIngredient extends List> { return Internals.getEntryIngredientProvider().builder(initialCapacity); } - static EntryIngredient from(ListTag tag) { + static EntryIngredient read(ListTag tag) { if (tag.isEmpty()) return empty(); EntryStack[] stacks = new EntryStack[tag.size()]; for (int i = 0; i < tag.size(); i++) { @@ -75,6 +77,10 @@ public interface EntryIngredient extends List> { return (List>) (List) this; } + EntryIngredient filter(Predicate> filter); + + EntryIngredient map(UnaryOperator> transformer); + interface Builder { Builder add(EntryStack stack); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java index 07cbe62f7..ce7201d51 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java @@ -50,10 +50,7 @@ import net.minecraft.util.Unit; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; @@ -108,7 +105,7 @@ public interface EntryStack extends TextRepresentable, Renderer { EntryDefinition definition = EntryTypeRegistry.getInstance().get(new ResourceLocation(tag.getString("type"))); EntrySerializer serializer = definition.getSerializer(); if (serializer != null && serializer.supportReading()) { - return EntryStack.of((EntryType) definition, serializer.read(tag)); + return EntryStack.of((EntryDefinition) definition, serializer.read(tag)); } throw new UnsupportedOperationException(definition.getType().getId() + " does not support deserialization!"); } @@ -163,6 +160,8 @@ public interface EntryStack extends TextRepresentable, Renderer { EntryStack normalize(); + Collection getTagsFor(); + @Deprecated int hashCode(); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java index f08480de1..04ea05d7f 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java @@ -31,6 +31,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagContainer; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; @@ -67,7 +68,7 @@ public interface EntryDefinition { Component asFormattedText(EntryStack entry, T value); - Collection getTagsFor(EntryStack entry, T value); + Collection getTagsFor(TagContainer tagContainer, EntryStack entry, T value); @ApiStatus.NonExtendable default EntryDefinition cast() { diff --git a/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginManager.java b/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginManager.java index 12c35f604..4cd223fa7 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginManager.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginManager.java @@ -55,7 +55,7 @@ public interface PluginManager

> extends ParentReloadable< } static List>> getActiveInstances() { - return EnvExecutor.getEnvSpecific(() -> () -> Arrays.asList(getInstance(), getClientInstance()), + return EnvExecutor.getEnvSpecific(() -> () -> Arrays.asList(getInstance(), getClientInstance(), getServerInstance()), () -> () -> Arrays.asList(getInstance(), getServerInstance())); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java b/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java index 9b022ff28..786a212fc 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java @@ -158,7 +158,7 @@ public final class EntryIngredients { } ImmutableList.Builder ingredients = ImmutableList.builder(); for (Tag tag : listTag) { - ingredients.add(EntryIngredient.from((ListTag) tag)); + ingredients.add(EntryIngredient.read((ListTag) tag)); } return ingredients.build(); } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java index 3e72b8710..1b303c869 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/DefaultPlugin.java @@ -38,6 +38,7 @@ import me.shedaniel.rei.plugin.common.displays.*; import me.shedaniel.rei.plugin.common.displays.beacon.DefaultBeaconBaseDisplay; import me.shedaniel.rei.plugin.common.displays.beacon.DefaultBeaconDisplay; import me.shedaniel.rei.plugin.common.displays.beacon.DefaultBeaconPaymentDisplay; +import me.shedaniel.rei.plugin.common.displays.brewing.DefaultBrewingDisplay; import me.shedaniel.rei.plugin.common.displays.cooking.DefaultBlastingDisplay; import me.shedaniel.rei.plugin.common.displays.cooking.DefaultCookingDisplay; import me.shedaniel.rei.plugin.common.displays.cooking.DefaultSmeltingDisplay; @@ -94,6 +95,7 @@ public class DefaultPlugin implements BuiltinPlugin, REIServerPlugin { registry.register(CAMPFIRE, DefaultCampfireDisplay.serializer()); registry.register(STONE_CUTTING, DefaultStoneCuttingDisplay.serializer()); registry.register(STRIPPING, DefaultStrippingDisplay.serializer()); + registry.register(BREWING, DefaultBrewingDisplay.serializer()); registry.register(COMPOSTING, DefaultCompostingDisplay.serializer()); registry.register(FUEL, DefaultFuelDisplay.serializer()); registry.register(SMITHING, DefaultSmithingDisplay.serializer()); @@ -106,13 +108,13 @@ public class DefaultPlugin implements BuiltinPlugin, REIServerPlugin { @Override public void registerMenuInfo(MenuInfoRegistry registry) { - registry.register(BuiltinPlugin.CRAFTING, CraftingMenu.class,new RecipeBookGridMenuInfo() { + registry.register(BuiltinPlugin.CRAFTING, CraftingMenu.class, new RecipeBookGridMenuInfo() { @Override public List> getDisplayInputs(MenuInfoContext context) { return context.getDisplay().getOrganisedInputEntries(this, context.getMenu()); } }); - registry.register(BuiltinPlugin.CRAFTING, InventoryMenu.class,new RecipeBookGridMenuInfo() { + registry.register(BuiltinPlugin.CRAFTING, InventoryMenu.class, new RecipeBookGridMenuInfo() { @Override public List> getDisplayInputs(MenuInfoContext context) { return context.getDisplay().getOrganisedInputEntries(this, context.getMenu()); diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultInformationDisplay.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultInformationDisplay.java index 370c072ac..519405a0e 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultInformationDisplay.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/DefaultInformationDisplay.java @@ -119,7 +119,7 @@ public class DefaultInformationDisplay implements Display { @Override public DefaultInformationDisplay read(CompoundTag tag) { - EntryIngredient stacks = EntryIngredient.from(tag.getList("stacks", NbtType.COMPOUND)); + EntryIngredient stacks = EntryIngredient.read(tag.getList("stacks", NbtType.COMPOUND)); Component name = Component.Serializer.fromJson(tag.getString("name")); List descriptions = new ArrayList<>(); for (Tag descriptionTag : tag.getList("descriptions", NbtType.STRING)) { diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/brewing/DefaultBrewingDisplay.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/brewing/DefaultBrewingDisplay.java index d7d6abc72..6abff9561 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/brewing/DefaultBrewingDisplay.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/brewing/DefaultBrewingDisplay.java @@ -24,8 +24,10 @@ package me.shedaniel.rei.plugin.common.displays.brewing; import com.google.common.collect.Lists; +import me.shedaniel.architectury.utils.NbtType; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.display.DisplaySerializer; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.EntryIngredients; @@ -34,6 +36,7 @@ import me.shedaniel.rei.plugin.common.BuiltinPlugin; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.ChatFormatting; +import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; @@ -54,17 +57,9 @@ public class DefaultBrewingDisplay implements Display { } @ApiStatus.Internal - public DefaultBrewingDisplay(EntryIngredient inputs, EntryIngredient reactants, EntryStack output) { - EntryIngredient.Builder inputBuilder = EntryIngredient.builder(inputs.size()); - for (EntryStack input : inputs) { - inputBuilder.add(input.copy().tooltip(new TranslatableComponent("category.rei.brewing.input").withStyle(ChatFormatting.YELLOW))); - } - this.input = inputBuilder.build(); - EntryIngredient.Builder reactantBuilder = EntryIngredient.builder(reactants.size()); - for (EntryStack reactant : reactants) { - reactantBuilder.add(reactant.copy().tooltip(new TranslatableComponent("category.rei.brewing.reactant").withStyle(ChatFormatting.YELLOW))); - } - this.reactant = reactantBuilder.build(); + public DefaultBrewingDisplay(EntryIngredient input, EntryIngredient reactant, EntryStack output) { + this.input = input.map(stack -> stack.copy().tooltip(new TranslatableComponent("category.rei.brewing.input").withStyle(ChatFormatting.YELLOW))); + this.reactant = reactant.map(stack -> stack.copy().tooltip(new TranslatableComponent("category.rei.brewing.reactant").withStyle(ChatFormatting.YELLOW))); this.output = output.copy().tooltip(new TranslatableComponent("category.rei.brewing.result").withStyle(ChatFormatting.YELLOW)); } @@ -91,4 +86,24 @@ public class DefaultBrewingDisplay implements Display { stack.add(output); return stack; } + + public static DisplaySerializer serializer() { + return new DisplaySerializer() { + @Override + public CompoundTag save(CompoundTag tag, DefaultBrewingDisplay display) { + tag.put("input", display.input.save()); + tag.put("reactant", display.reactant.save()); + tag.put("output", display.output.save()); + return tag; + } + + @Override + public DefaultBrewingDisplay read(CompoundTag tag) { + EntryIngredient input = EntryIngredient.read(tag.getList("input", NbtType.COMPOUND)); + EntryIngredient reactant = EntryIngredient.read(tag.getList("reactant", NbtType.COMPOUND)); + EntryStack output = EntryStack.read(tag.getCompound("output")); + return new DefaultBrewingDisplay(input, reactant, output); + } + }; + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 7bc63e0b3..f82444644 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -32,6 +32,7 @@ import me.shedaniel.architectury.event.events.client.ClientScreenInputEvent; import me.shedaniel.architectury.networking.NetworkManager; import me.shedaniel.architectury.platform.Platform; import me.shedaniel.architectury.utils.Env; +import me.shedaniel.architectury.utils.EnvExecutor; import me.shedaniel.math.Point; import me.shedaniel.rei.api.client.REIHelper; import me.shedaniel.rei.api.client.REIOverlay; @@ -74,6 +75,7 @@ import me.shedaniel.rei.impl.client.transfer.TransferHandlerRegistryImpl; import me.shedaniel.rei.impl.client.view.ViewsImpl; import me.shedaniel.rei.impl.common.category.CategoryIdentifierImpl; import me.shedaniel.rei.impl.common.display.DisplaySerializerRegistryImpl; +import me.shedaniel.rei.impl.common.entry.EmptyEntryStack; import me.shedaniel.rei.impl.common.entry.EntryIngredientImpl; import me.shedaniel.rei.impl.common.entry.TypedEntryStack; import me.shedaniel.rei.impl.common.entry.comparison.ItemComparatorRegistryImpl; @@ -84,6 +86,7 @@ import me.shedaniel.rei.impl.common.entry.type.EntryTypeRegistryImpl; import me.shedaniel.rei.impl.common.entry.type.types.EmptyEntryDefinition; import me.shedaniel.rei.impl.common.fluid.FluidSupportProviderImpl; import me.shedaniel.rei.impl.common.plugins.PluginManagerImpl; +import me.shedaniel.rei.impl.common.registry.RecipeManagerContextImpl; import me.shedaniel.rei.impl.common.transfer.MenuInfoRegistryImpl; import me.shedaniel.rei.impl.common.util.IssuesDetector; import me.shedaniel.rei.plugin.test.REITestPlugin; @@ -148,6 +151,7 @@ public class RoughlyEnoughItemsCore { REIPlugin.class, UnaryOperator.identity(), new EntryTypeRegistryImpl(), + new RecipeManagerContextImpl<>(RecipeManagerContextImpl.supplier()), new ItemComparatorRegistryImpl(), new DisplaySerializerRegistryImpl(), new FluidSupportProviderImpl()), "commonPluginManager"); @@ -210,7 +214,7 @@ public class RoughlyEnoughItemsCore { Internals.attachInstance(new Internals.EntryStackProvider() { @Override public EntryStack empty() { - return TypedEntryStack.EMPTY; + return EmptyEntryStack.EMPTY; } @Override @@ -428,7 +432,7 @@ public class RoughlyEnoughItemsCore { @Environment(EnvType.CLIENT) private void loadTestPlugins() { - if ( System.getProperty("rei.test", "false").equals("true")) { + if (System.getProperty("rei.test", "false").equals("true")) { PluginView.getClientInstance().registerPlugin(new REITestPlugin()); } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java index d3fc3b757..8db620c00 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java @@ -101,11 +101,12 @@ public final class InternalWidgets { visible[0] = false; IntList redSlots = null; TransferHandler.Context context = TransferHandler.Context.create(false, containerScreen, displaySupplier.get()); - for (TransferHandler autoTransferHandler : TransferHandlerRegistry.getInstance()) { + for (TransferHandler transferHandler : TransferHandlerRegistry.getInstance()) { try { - TransferHandler.Result result = autoTransferHandler.handle(context); - if (result.isApplicable()) + TransferHandler.Result result = transferHandler.handle(context); + if (result.isApplicable()) { visible[0] = true; + } if (result.isSuccessful()) { button.setEnabled(true); error = null; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java index 2635039fc..1fa56fe0b 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java @@ -51,6 +51,10 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl> fillers = new ArrayList<>(); private final MutableInt displayCount = new MutableInt(0); + public DisplayRegistryImpl() { + super(RecipeManagerContextImpl.supplier()); + } + @Override public void acceptPlugin(REIClientPlugin plugin) { plugin.registerDisplays(this); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java index f654ce076..67520b666 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/Argument.java @@ -51,7 +51,7 @@ import java.util.regex.Pattern; @Environment(EnvType.CLIENT) public class Argument { public static final String SPACE = " ", EMPTY = ""; - static final Argument ALWAYS = new Argument<>(AlwaysMatchingArgumentType.INSTANCE, EMPTY, true, -1, -1); + static final Argument ALWAYS = new Argument<>(AlwaysMatchingArgumentType.INSTANCE, EMPTY, true, -1, -1, true); private ArgumentType argumentType; private String text; private T filterData; @@ -60,10 +60,6 @@ public class Argument { private final int end; private static final Pattern SPLIT_PATTERN = Pattern.compile("(?:\"([^\"]*)\")|([^\\s]+)"); - public Argument(ArgumentType argumentType, String text, boolean regular, int start, int end) { - this(argumentType, text, regular, start, end, true); - } - public Argument(ArgumentType argumentType, String text, boolean regular, int start, int end, boolean lowercase) { this.argumentType = argumentType; this.text = lowercase ? text.toLowerCase(Locale.ROOT) : text; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/TagArgumentType.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/TagArgumentType.java index a46e07317..a6495fc41 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/TagArgumentType.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/TagArgumentType.java @@ -26,7 +26,6 @@ package me.shedaniel.rei.impl.client.search.argument.type; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.gui.config.SearchMode; import me.shedaniel.rei.api.common.entry.EntryStack; -import me.shedaniel.rei.api.common.entry.type.EntryDefinition; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.network.chat.Style; @@ -70,7 +69,7 @@ public final class TagArgumentType extends ArgumentType { @Override public boolean matches(Mutable data, EntryStack stack, String searchText, Unit filterData) { if (data.getValue() == null) { - Collection tags = ((EntryDefinition) stack.getDefinition()).getTagsFor((EntryStack) stack, stack.getValue()); + Collection tags = stack.getTagsFor(); if (tags.isEmpty()) { data.setValue(EMPTY_ARRAY); } else { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/display/DisplaySerializerRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/display/DisplaySerializerRegistryImpl.java index 7c8b8c127..fed8f7c79 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/display/DisplaySerializerRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/display/DisplaySerializerRegistryImpl.java @@ -61,12 +61,12 @@ public class DisplaySerializerRegistryImpl implements DisplaySerializerRegistry @Override public CompoundTag save(CategoryIdentifier categoryId, D display, CompoundTag tag) { - return null; + return ((DisplaySerializer) serializers.get(categoryId).serializer.get()).save(tag, display); } @Override public D read(CategoryIdentifier categoryId, CompoundTag tag) { - return null; + return ((DisplaySerializer) serializers.get(categoryId).serializer.get()).read(tag); } @Override diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java index 5a0a5db08..d0edb1602 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java @@ -27,6 +27,8 @@ import com.mojang.blaze3d.vertex.PoseStack; import it.unimi.dsi.fastutil.shorts.Short2ObjectMap; import it.unimi.dsi.fastutil.shorts.Short2ObjectMaps; import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap; +import me.shedaniel.architectury.platform.Platform; +import me.shedaniel.architectury.utils.Env; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.ClientHelper; @@ -35,12 +37,20 @@ import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; import me.shedaniel.rei.api.common.util.EntryStacks; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.SerializationTags; +import net.minecraft.tags.TagContainer; import org.apache.commons.lang3.mutable.Mutable; import org.apache.commons.lang3.mutable.MutableObject; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.Collection; + @ApiStatus.Internal public abstract class AbstractEntryStack extends AbstractRenderer implements EntryStack { private static final Short2ObjectMap EMPTY_SETTINGS = Short2ObjectMaps.emptyMap(); @@ -92,6 +102,40 @@ public abstract class AbstractEntryStack extends AbstractRenderer implements return this.settings == null ? EMPTY_SETTINGS : this.settings; } + @Override + @Nullable + public ResourceLocation getIdentifier() { + return getDefinition().getIdentifier(this, getValue()); + } + + @Override + public boolean isEmpty() { + return getDefinition().isEmpty(this, getValue()); + } + + @Override + public EntryStack copy() { + return wrap(getDefinition().copy(this, getValue())); + } + + @Override + public EntryStack rewrap() { + return wrap(getValue()); + } + + @Override + public EntryStack normalize() { + return wrap(getDefinition().normalize(this, getValue())); + } + + public EntryStack wrap(A value) { + TypedEntryStack stack = new TypedEntryStack<>(getDefinition(), value); + for (Short2ObjectMap.Entry entry : getSettings().short2ObjectEntrySet()) { + stack.setting(EntryStack.Settings.getById(entry.getShortKey()), entry.getValue()); + } + return stack; + } + @Override public T get(Settings settings) { Object o = this.settings == null ? null : this.settings.get(settings.getId()); @@ -131,8 +175,34 @@ public abstract class AbstractEntryStack extends AbstractRenderer implements return EntryStacks.equalsExact(this, that); } + @Override + public int hash(ComparisonContext context) { + return getDefinition().hash(this, getValue(), context); + } + @Override public int hashCode() { return hash(ComparisonContext.EXACT); } + + @Override + public Collection getTagsFor() { + TagContainer container; + if (Platform.getEnvironment() == Env.CLIENT) { + container = getClientTagContainer(); + } else { + container = SerializationTags.getInstance(); + } + return getDefinition().getTagsFor(container, this, getValue()); + } + + @Environment(EnvType.CLIENT) + private static TagContainer getClientTagContainer() { + return Minecraft.getInstance().getConnection().getTags(); + } + + @Override + public Component asFormattedText() { + return getDefinition().asFormattedText(this, getValue()); + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EmptyEntryStack.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EmptyEntryStack.java new file mode 100644 index 000000000..337f80fe6 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EmptyEntryStack.java @@ -0,0 +1,45 @@ +/* + * 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.impl.common.entry; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.BuiltinEntryTypes; +import me.shedaniel.rei.api.common.entry.type.EntryDefinition; +import net.minecraft.util.Unit; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class EmptyEntryStack extends AbstractEntryStack { + public static final EntryStack EMPTY = new EmptyEntryStack(); + + @Override + public EntryDefinition getDefinition() { + return BuiltinEntryTypes.EMPTY.getDefinition(); + } + + @Override + public Unit getValue() { + return Unit.INSTANCE; + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EntryIngredientImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EntryIngredientImpl.java index 759020cf4..892d34b0d 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EntryIngredientImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/EntryIngredientImpl.java @@ -37,11 +37,10 @@ import java.util.stream.StreamSupport; public class EntryIngredientImpl { public static Internals.EntryIngredientProvider provide() { - EmptyEntryIngredient empty = new EmptyEntryIngredient(); return new Internals.EntryIngredientProvider() { @Override public EntryIngredient empty() { - return empty; + return EmptyEntryIngredient.EMPTY; } @Override @@ -51,7 +50,7 @@ public class EntryIngredientImpl { @Override public EntryIngredient of(EntryStack... stacks) { - if (stacks.length == 0) return empty; + if (stacks.length == 0) return empty(); if (stacks.length == 1) return of(stacks[0]); return _of(stacks); } @@ -61,7 +60,7 @@ public class EntryIngredientImpl { if (stacks instanceof EntryIngredient) return (EntryIngredient) stacks; if (stacks instanceof Collection) { int size = ((Collection>) stacks).size(); - if (size == 0) return empty; + if (size == 0) return empty(); if (size == 1) return of(stacks.iterator().next()); return _of(((Collection>) stacks).toArray(new EntryStack[0])); } @@ -146,6 +145,7 @@ public class EntryIngredientImpl { } private static class EmptyEntryIngredient extends AbstractList> implements EntryIngredient, RandomAccess { + private static final EmptyEntryIngredient EMPTY = new EmptyEntryIngredient(); @Override public Iterator> iterator() { return Collections.emptyIterator(); @@ -232,6 +232,16 @@ public class EntryIngredientImpl { public ListTag save() { return new ListTag(); } + + @Override + public EntryIngredient filter(Predicate> filter) { + return this; + } + + @Override + public EntryIngredient map(UnaryOperator> transformer) { + return this; + } } private static class SingletonEntryIngredient extends AbstractList> implements EntryIngredient, RandomAccess { @@ -343,6 +353,19 @@ public class EntryIngredientImpl { listTag.add(stack.save()); return listTag; } + + @Override + public EntryIngredient filter(Predicate> filter) { + if (filter.test(stack)) { + return this; + } + return EmptyEntryIngredient.EMPTY; + } + + @Override + public EntryIngredient map(UnaryOperator> transformer) { + return new SingletonEntryIngredient(transformer.apply(stack)); + } } private static class ArrayIngredient extends AbstractList> implements EntryIngredient, RandomAccess { @@ -437,5 +460,19 @@ public class EntryIngredientImpl { } return listTag; } + + @Override + public EntryIngredient filter(Predicate> filter) { + return EntryIngredient.of(stream().filter(filter).toArray(EntryStack[]::new)); + } + + @Override + public EntryIngredient map(UnaryOperator> transformer) { + EntryStack[] out = new EntryStack[array.length]; + for (int i = 0; i < array.length; i++) { + out[i] = transformer.apply(array[i]); + } + return new ArrayIngredient(out); + } } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/TypedEntryStack.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/TypedEntryStack.java index 9eada5a65..90600718f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/TypedEntryStack.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/TypedEntryStack.java @@ -23,29 +23,14 @@ package me.shedaniel.rei.impl.common.entry; -import it.unimi.dsi.fastutil.shorts.Short2ObjectMap; -import me.shedaniel.rei.api.common.entry.EntryStack; -import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; -import me.shedaniel.rei.api.common.entry.type.BuiltinEntryTypes; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; -import me.shedaniel.rei.api.common.entry.type.EntryType; -import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.Unit; import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; @ApiStatus.Internal public class TypedEntryStack extends AbstractEntryStack { - public static final EntryStack EMPTY = new TypedEntryStack<>(BuiltinEntryTypes.EMPTY, Unit.INSTANCE); - private final EntryDefinition definition; private T value; - public TypedEntryStack(EntryType type, T value) { - this(type.getDefinition(), value); - } - public TypedEntryStack(EntryDefinition definition, T value) { this.definition = definition; this.value = value; @@ -60,52 +45,4 @@ public class TypedEntryStack extends AbstractEntryStack { public T getValue() { return value; } - - @Override - @Nullable - public ResourceLocation getIdentifier() { - return getDefinition().getIdentifier(this, value); - } - - @Override - public boolean isEmpty() { - return getDefinition().isEmpty(this, value); - } - - @Override - public EntryStack copy() { - TypedEntryStack stack = new TypedEntryStack<>(definition, getDefinition().copy(this, value)); - for (Short2ObjectMap.Entry entry : getSettings().short2ObjectEntrySet()) { - stack.setting(EntryStack.Settings.getById(entry.getShortKey()), entry.getValue()); - } - return stack; - } - - @Override - public EntryStack rewrap() { - TypedEntryStack stack = new TypedEntryStack<>(definition, value); - for (Short2ObjectMap.Entry entry : getSettings().short2ObjectEntrySet()) { - stack.setting(EntryStack.Settings.getById(entry.getShortKey()), entry.getValue()); - } - return stack; - } - - @Override - public EntryStack normalize() { - TypedEntryStack stack = new TypedEntryStack<>(definition, getDefinition().normalize(this, value)); - for (Short2ObjectMap.Entry entry : getSettings().short2ObjectEntrySet()) { - stack.setting(EntryStack.Settings.getById(entry.getShortKey()), entry.getValue()); - } - return stack; - } - - @Override - public int hash(ComparisonContext context) { - return getDefinition().hash(this, value, context); - } - - @Override - public Component asFormattedText() { - return getDefinition().asFormattedText(this, value); - } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/types/BuiltinEntryDefinition.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/types/BuiltinEntryDefinition.java index 2c14fb335..84eccfc5f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/types/BuiltinEntryDefinition.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/types/BuiltinEntryDefinition.java @@ -33,6 +33,7 @@ import me.shedaniel.rei.api.common.util.ImmutableTextComponent; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagContainer; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; @@ -115,7 +116,7 @@ public class BuiltinEntryDefinition implements EntryDefinition, EntrySeria } @Override - public Collection getTagsFor(EntryStack entry, T value) { + public Collection getTagsFor(TagContainer tagContainer, EntryStack entry, T value) { return Collections.emptyList(); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/registry/RecipeManagerContextImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/registry/RecipeManagerContextImpl.java index 14ce8aa47..c4081bc9c 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/registry/RecipeManagerContextImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/registry/RecipeManagerContextImpl.java @@ -23,7 +23,9 @@ package me.shedaniel.rei.impl.common.registry; -import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.architectury.utils.EnvExecutor; +import me.shedaniel.architectury.utils.GameInstance; +import me.shedaniel.rei.api.common.plugins.REIPlugin; import me.shedaniel.rei.api.common.registry.RecipeManagerContext; import net.minecraft.client.Minecraft; import net.minecraft.world.item.crafting.Recipe; @@ -32,12 +34,23 @@ import net.minecraft.world.item.crafting.RecipeManager; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.function.Supplier; import java.util.stream.Collectors; -public class RecipeManagerContextImpl

implements RecipeManagerContext

{ +public class RecipeManagerContextImpl

> implements RecipeManagerContext

{ private static final Comparator> RECIPE_COMPARATOR = Comparator.comparing((Recipe o) -> o.getId().getNamespace()).thenComparing(o -> o.getId().getPath()); + private final Supplier recipeManager; private List> sortedRecipes = null; + public RecipeManagerContextImpl(Supplier recipeManager) { + this.recipeManager = recipeManager; + } + + public static Supplier supplier() { + return () -> EnvExecutor.getEnvSpecific(() -> () -> Minecraft.getInstance().getConnection().getRecipeManager(), + () -> () -> GameInstance.getServer().getRecipeManager()); + } + @Override public List> getAllSortedRecipes() { if (sortedRecipes == null) { @@ -49,7 +62,7 @@ public class RecipeManagerContextImpl

implements Reci @Override public RecipeManager getRecipeManager() { - return Minecraft.getInstance().getConnection().getRecipeManager(); + return recipeManager.get(); } @Override 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 bc87ddf98..c1c2fdf1a 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 @@ -54,8 +54,11 @@ import net.minecraft.core.Registry; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TextComponent; +import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagCollection; +import net.minecraft.tags.TagContainer; +import net.minecraft.world.level.block.Block; import net.minecraft.world.level.material.Fluid; import org.jetbrains.annotations.Nullable; @@ -157,12 +160,13 @@ public class FluidEntryDefinition implements EntryDefinition, EntryS @Override public Component asFormattedText(EntryStack entry, FluidStack value) { - return value.getFluid().defaultFluidState().createLegacyBlock().getBlock().getName(); + Block block = value.getFluid().defaultFluidState().createLegacyBlock().getBlock(); + return new TranslatableComponent(block.getDescriptionId()); } @Override - public Collection getTagsFor(EntryStack entry, FluidStack value) { - TagCollection collection = Minecraft.getInstance().getConnection().getTags().getFluids(); + public Collection getTagsFor(TagContainer tagContainer, EntryStack entry, FluidStack value) { + TagCollection collection = tagContainer.getFluids(); return collection == null ? Collections.emptyList() : collection.getMatchingTags(value.getFluid()); } diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/ItemEntryDefinition.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/ItemEntryDefinition.java index 59129f9d1..13b09cf88 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/ItemEntryDefinition.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/ItemEntryDefinition.java @@ -56,6 +56,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagCollection; +import net.minecraft.tags.TagContainer; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; @@ -167,8 +168,8 @@ public class ItemEntryDefinition implements EntryDefinition, EntrySer } @Override - public Collection getTagsFor(EntryStack entry, ItemStack value) { - TagCollection collection = Minecraft.getInstance().getConnection().getTags().getItems(); + public Collection getTagsFor(TagContainer tagContainer, EntryStack entry, ItemStack value) { + TagCollection collection = tagContainer.getItems(); return collection == null ? Collections.emptyList() : collection.getMatchingTags(value.getItem()); } -- cgit