Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21] Use RegistryFriendlyByteBuf for extended menus #534

Open
wants to merge 1 commit into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

package dev.architectury.registry.menu;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.world.MenuProvider;

public interface ExtendedMenuProvider extends MenuProvider {
void saveExtraData(FriendlyByteBuf buf);
void saveExtraData(RegistryFriendlyByteBuf buf);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
Expand All @@ -50,10 +51,10 @@ private MenuRegistry() {
* @param provider The {@link MenuProvider} that provides the menu
* @param bufWriter That writer that sends extra data for {@link MenuType} created with {@link MenuRegistry#ofExtended(ExtendedMenuTypeFactory)}
*/
public static void openExtendedMenu(ServerPlayer player, MenuProvider provider, Consumer<FriendlyByteBuf> bufWriter) {
public static void openExtendedMenu(ServerPlayer player, MenuProvider provider, Consumer<RegistryFriendlyByteBuf> bufWriter) {
openExtendedMenu(player, new ExtendedMenuProvider() {
@Override
public void saveExtraData(FriendlyByteBuf buf) {
public void saveExtraData(RegistryFriendlyByteBuf buf) {
bufWriter.accept(buf);
}

Expand Down Expand Up @@ -183,6 +184,6 @@ public interface ExtendedMenuTypeFactory<T extends AbstractContainerMenu> {
* @param buf The {@link FriendlyByteBuf} for the menu to provide extra data
* @return A new {@link T} that extends {@link AbstractContainerMenu}
*/
T create(int id, Inventory inventory, FriendlyByteBuf buf);
T create(int id, Inventory inventory, RegistryFriendlyByteBuf buf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -51,7 +51,8 @@ public static void openExtendedMenu(ServerPlayer player, ExtendedMenuProvider pr
player.openMenu(new ExtendedScreenHandlerFactory<byte[]>() {
@Override
public byte[] getScreenOpeningData(ServerPlayer player) {
FriendlyByteBuf buf = PacketByteBufs.create();
RegistryAccess access = player.registryAccess();
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), access);
provider.saveExtraData(buf);
byte[] bytes = ByteBufUtil.getBytes(buf);
buf.release();
Expand All @@ -77,7 +78,8 @@ public static <T extends AbstractContainerMenu> MenuType<T> of(SimpleMenuTypeFac

public static <T extends AbstractContainerMenu> MenuType<T> ofExtended(ExtendedMenuTypeFactory<T> factory) {
return new ExtendedScreenHandlerType<>((syncId, inventory, data) -> {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
RegistryAccess access = inventory.player.registryAccess();
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(data), access);
T menu = factory.create(syncId, inventory, buf);
buf.release();
return menu;
Expand Down
Loading