use of net.minecraft.network.FriendlyByteBuf in project MinecraftForge by MinecraftForge.
the class NetworkHooks method openGui.
/**
* Request to open a GUI on the client, from the server
*
* Refer to {@link ConfigGuiFactory} for how to provide a function to consume
* these GUI requests on the client.
*
* The maximum size for #extraDataWriter is 32600 bytes.
*
* @param player The player to open the GUI for
* @param containerSupplier A supplier of container properties including the registry name of the container
* @param extraDataWriter Consumer to write any additional data the GUI needs
*/
public static void openGui(ServerPlayer player, MenuProvider containerSupplier, Consumer<FriendlyByteBuf> extraDataWriter) {
if (player.level.isClientSide)
return;
player.doCloseContainer();
player.nextContainerCounter();
int openContainerId = player.containerCounter;
FriendlyByteBuf extraData = new FriendlyByteBuf(Unpooled.buffer());
extraDataWriter.accept(extraData);
// reset to beginning in case modders read for whatever reason
extraData.readerIndex(0);
FriendlyByteBuf output = new FriendlyByteBuf(Unpooled.buffer());
output.writeVarInt(extraData.readableBytes());
output.writeBytes(extraData);
if (output.readableBytes() > 32600 || output.readableBytes() < 1) {
throw new IllegalArgumentException("Invalid PacketBuffer for openGui, found " + output.readableBytes() + " bytes");
}
AbstractContainerMenu c = containerSupplier.createMenu(openContainerId, player.getInventory(), player);
MenuType<?> type = c.getType();
PlayMessages.OpenContainer msg = new PlayMessages.OpenContainer(type, openContainerId, containerSupplier.getDisplayName(), output);
NetworkConstants.playChannel.sendTo(msg, player.connection.getConnection(), NetworkDirection.PLAY_TO_CLIENT);
player.containerMenu = c;
player.initMenu(player.containerMenu);
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, c));
}
Aggregations