Search in sources :

Example 1 with ServerPlayerEntityAccessor

use of net.fabricmc.fabric.mixin.container.ServerPlayerEntityAccessor in project fabric by FabricMC.

the class ContainerProviderImpl method openContainer.

@Override
public void openContainer(Identifier identifier, ServerPlayerEntity player, Consumer<PacketByteBuf> writer) {
    int syncId;
    if (player instanceof ServerPlayerEntitySyncHook) {
        ServerPlayerEntitySyncHook serverPlayerEntitySyncHook = (ServerPlayerEntitySyncHook) player;
        syncId = serverPlayerEntitySyncHook.fabric_incrementSyncId();
    } else if (player instanceof ServerPlayerEntityAccessor) {
        if (!emittedNoSyncHookWarning) {
            LOGGER.warn("ServerPlayerEntitySyncHook could not be applied - fabric-containers is using a hack!");
            emittedNoSyncHookWarning = true;
        }
        syncId = (((ServerPlayerEntityAccessor) player).getContainerSyncId() + 1) % 100;
        ((ServerPlayerEntityAccessor) player).setContainerSyncId(syncId);
    } else {
        throw new RuntimeException("Neither ServerPlayerEntitySyncHook nor Accessor present! This should not happen!");
    }
    PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
    buf.writeIdentifier(identifier);
    buf.writeByte(syncId);
    writer.accept(buf);
    player.networkHandler.sendPacket(new CustomPayloadS2CPacket(PacketTypes.OPEN_CONTAINER, buf));
    PacketByteBuf clonedBuf = new PacketByteBuf(buf.duplicate());
    clonedBuf.readIdentifier();
    clonedBuf.readUnsignedByte();
    Container container = createContainer(syncId, identifier, player, clonedBuf);
    if (container == null) {
        return;
    }
    player.container = container;
    player.container.addListener(player);
}
Also used : Container(net.minecraft.container.Container) CustomPayloadS2CPacket(net.minecraft.client.network.packet.CustomPayloadS2CPacket) PacketByteBuf(net.minecraft.util.PacketByteBuf) ServerPlayerEntityAccessor(net.fabricmc.fabric.mixin.container.ServerPlayerEntityAccessor)

Aggregations

ServerPlayerEntityAccessor (net.fabricmc.fabric.mixin.container.ServerPlayerEntityAccessor)1 CustomPayloadS2CPacket (net.minecraft.client.network.packet.CustomPayloadS2CPacket)1 Container (net.minecraft.container.Container)1 PacketByteBuf (net.minecraft.util.PacketByteBuf)1