Skip to content

Shared Contexts

Natan Vieira edited this page Dec 28, 2022 · 36 revisions

Shared contexts are contexts whose container (inventory), its display, is shared among multiple players, any change to that context is propagated to all players that are linked to it.

Shared Contexts API is experimental and is not subject to the general compatibility guarantees such API may be changed or may be removed completely in any further release.

Creating a shared context

There are two ways to create a shared context:

  • Linking it directly to a specific group of players through open.
  • Preparing the context to receive a group of players before opening it, via prepare.

Confined Context

The difference between both is that: preparing and opening a context are different things, from the moment a context is opened for one or more players it becomes a confined context. That is, he cannot receive more players than he already has.

// `open` returns a MyView instance, and not a context so you won't have access to it anyway.
viewFrame.open(MyView.class, list of players);

Unconfined Context

Preparing a context means that it is an unconfined context and while it is unconfined, it can receive an unlimited number of players until it is displayed to one of those players and becomes a confined context.

// `prepare` returns a ViewContext instance, you can add multiple players before displaying it.
UnconfinedViewContext context = viewFrame.prepare(MyView.class);

context.addViewer(player1);
context.addViewer(player2);

// do something with context

viewFrame.open(context);

What happens in a shared context

Anything you do in a shared context will be propagated to all players linked to it, everything. For example adding an item, removing an item, an update timer, everything is shared up to the internal implementation of the inventory depending on the platform your view was created on.

Even closing the context.

// closes for EVERYONE and invalidates this context
context.close();

If you and to close only for the player of a interaction context use closeForPlayer instead.

// closes for the player and context is invalidated only if there's no more players on it
context.closeForPlayer();

Welcome to the Inventory Framework documentation.

▶️ Introduction

🧩 Core Topics

💡 Built-In Features

🧰 Extra Features

🤓 Advanced Usage

⚙️ Internal Mechanisms

🛠️ Tooling

You can find practical examples in the examples directory.

Clone this wiki locally