From ab5157b7b9637affad1b162808ddedefdee42e69 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 2 Nov 2021 16:06:10 +0800 Subject: Refactor MenuInfoProvider to give more concrete info --- .../rei/api/common/transfer/info/MenuInfo.java | 4 ++ .../api/common/transfer/info/MenuInfoProvider.java | 40 ++++++++++++++++++++ .../api/common/transfer/info/MenuInfoRegistry.java | 16 +++++++- .../transfer/info/MenuSerializationContext.java | 5 ++- .../info/MenuSerializationProviderContext.java | 43 ++++++++++++++++++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationProviderContext.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java index f7cfef1d4..3b61866f9 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java @@ -34,6 +34,8 @@ import me.shedaniel.rei.api.common.transfer.info.clean.InputCleanHandler; import me.shedaniel.rei.api.common.transfer.info.simple.SimplePlayerInventoryMenuInfo; import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import me.shedaniel.rei.api.common.util.CollectionUtils; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -112,6 +114,7 @@ public interface MenuInfo ex * @throws MenuTransferException the exception to throw if something is wrong, * this exception should be caught by the invoker */ + @Environment(EnvType.CLIENT) default void validate(MenuInfoContext context) throws MenuTransferException { } @@ -136,6 +139,7 @@ public interface MenuInfo ex * @return the {@link CompoundTag} serialized */ default CompoundTag save(MenuSerializationContext context, D display) { + // TODO Remove this, replace with getDisplay() return DisplaySerializerRegistry.getInstance().save(display, new CompoundTag()); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java index 393a6cab6..904c59dd4 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoProvider.java @@ -25,7 +25,12 @@ package me.shedaniel.rei.api.common.transfer.info; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; +import org.jetbrains.annotations.ApiStatus; import java.util.Optional; @@ -37,5 +42,40 @@ import java.util.Optional; */ @FunctionalInterface public interface MenuInfoProvider { + @Environment(EnvType.CLIENT) + default Optional> provideClient(D display, T menu) { + return provide((CategoryIdentifier) display.getCategoryIdentifier(), (Class) menu.getClass()); + } + + default Optional> provide(CategoryIdentifier display, T menu, MenuSerializationProviderContext context, CompoundTag networkTag) { + Optional> menuInfo = provide(display, (Class) menu.getClass()); + if (menuInfo.isPresent()) { + menuInfo.get().read(new MenuSerializationContext() { + @Override + public MenuInfo getContainerInfo() { + return menuInfo.get(); + } + + @Override + public T getMenu() { + return context.getMenu(); + } + + @Override + public Player getPlayerEntity() { + return context.getPlayerEntity(); + } + + @Override + public CategoryIdentifier getCategoryIdentifier() { + return context.getCategoryIdentifier(); + } + }, networkTag); + } + return menuInfo; + } + + @Deprecated + @ApiStatus.ScheduledForRemoval Optional> provide(CategoryIdentifier categoryId, Class menuClass); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java index bd1b14ff6..fdb83fb30 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfoRegistry.java @@ -28,7 +28,11 @@ import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.plugins.REIServerPlugin; import me.shedaniel.rei.api.common.registry.Reloadable; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.inventory.AbstractContainerMenu; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.function.Predicate; @@ -45,7 +49,17 @@ public interface MenuInfoRegistry extends Reloadable { void registerGeneric(Predicate> categoryPredicate, MenuInfoProvider menuInfo); - @Nullable MenuInfo get(CategoryIdentifier category, Class menuClass); + @Nullable + @ApiStatus.ScheduledForRemoval + @Deprecated + MenuInfo get(CategoryIdentifier category, Class menuClass); + + @Nullable + @Environment(EnvType.CLIENT) + MenuInfo getClient(D display, C menu); + + @Nullable + MenuInfo get(CategoryIdentifier category, C menu, MenuSerializationProviderContext context, CompoundTag tag); int infoSize(); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationContext.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationContext.java index 7a335c064..b3ed73d5f 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationContext.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationContext.java @@ -34,12 +34,15 @@ import net.minecraft.world.inventory.AbstractContainerMenu; * @param the type of {@link AbstractContainerMenu} * @param

the type of {@link Player}, server sided contexts may pass {@link net.minecraft.server.level.ServerPlayer} instead */ -public interface MenuSerializationContext { +public interface MenuSerializationContext extends MenuSerializationProviderContext { + @Override T getMenu(); + @Override P getPlayerEntity(); MenuInfo getContainerInfo(); + @Override CategoryIdentifier getCategoryIdentifier(); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationProviderContext.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationProviderContext.java new file mode 100644 index 000000000..19a8cb78a --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuSerializationProviderContext.java @@ -0,0 +1,43 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 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.common.transfer.info; + +import me.shedaniel.rei.api.common.category.CategoryIdentifier; +import me.shedaniel.rei.api.common.display.Display; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; + +/** + * Context for menu display serialization. + * + * @param the type of {@link AbstractContainerMenu} + * @param

the type of {@link Player}, server sided contexts may pass {@link net.minecraft.server.level.ServerPlayer} instead + */ +public interface MenuSerializationProviderContext { + T getMenu(); + + P getPlayerEntity(); + + CategoryIdentifier getCategoryIdentifier(); +} -- cgit