Shared Contexts - DevNatan/inventory-framework GitHub Wiki

Shared Contexts consists of multiple players sharing the same context, therefore, the same inventory.

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.

What means "Sharing the same Context"?

When you open a view for a player, IF creates a context that contains data related to that player and the inventory he is viewing this is exclusive to him. Each player sees their own inventory created during context creation, with its own items, title, size, etc.

Shared Contexts explores the concept that the created context is not exclusively tied to a single player, but several, thus sharing everything I mentioned above. It's useful for things like an exchange system: share a single inventory with players making the exchange.

Creating a Shared Context

First, Shared Context is a concept and not a type of context thus it's a regular context like any other so a context becomes shared from the moment there is more than one player present in it.

The easiest and fastest way to create shared contexts is to open a view for more than one player.

viewFrame.open(SomeView.class, [...]);

When using ViewFrame#open(...) you receive a String type which is the id of the generated context.

String contextId = viewFrame.open(SomeView.class, [...]);

You can save this id to later open an already active context for another player.

viewFrame.openActive(SomeView.class, contextId, [...]);

closeForPlayer and closeForEveryone

Anything you do in a Shared Context will be propagated to all players linked to it, everything.

Some methods in IF have the suffix "forEveryone" and "forPlayer", this helps you work with shared contexts:

  • ...forPlayer Closes only to the player that is included in a given event e.g.: if a player clicks in some item and you use this method then will affect only the player who clicked
  • ...forEveryone Closes to everyone in the context e.g.: if a player e.g.: if a player clicks in some item and you use this method then will afftect all players in the context

closeForPlayer and closeForEveryone are a good example.

click.closeForEveryone();