Search in sources :

Example 21 with FriendlyByteBuf

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));
}
Also used : FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) PlayerContainerEvent(net.minecraftforge.event.entity.player.PlayerContainerEvent)

Aggregations

FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)21 ResourceLocation (net.minecraft.resources.ResourceLocation)7 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)2 ArrayList (java.util.ArrayList)2 BlockPos (net.minecraft.core.BlockPos)2 AttributeInstance (net.minecraft.world.entity.ai.attributes.AttributeInstance)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 BitSet (java.util.BitSet)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 Packet (net.minecraft.network.protocol.Packet)1 ClientboundLevelChunkPacket (net.minecraft.network.protocol.game.ClientboundLevelChunkPacket)1 ClientboundLevelChunkPacketData (net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData)1 ClientboundLevelChunkWithLightPacket (net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket)1 BitStorage (net.minecraft.util.BitStorage)1 AbstractContainerMenu (net.minecraft.world.inventory.AbstractContainerMenu)1 Biome (net.minecraft.world.level.biome.Biome)1 PalettedContainer (net.minecraft.world.level.chunk.PalettedContainer)1