Skip to content

Commit

Permalink
Use RegistryFriendlyByteBuf for extended menus
Browse files Browse the repository at this point in the history
  • Loading branch information
Urkaz committed Jul 7, 2024
1 parent 4e5f1ee commit 4f853bd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
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

0 comments on commit 4f853bd

Please sign in to comment.