Interface ObjectShare


public interface ObjectShare
Object share for inter-mod communication, obtainable through FabricLoader.getObjectShare().

The share allows mods to exchange data without directly referencing each other. This makes simple interaction easier by eliminating any compile- or run-time dependencies if the shared value type is independent of the mod (only Java/game/Fabric types like collections, primitives, String, Consumer, Function, ...).

Active interaction is possible as well since the shared values can be arbitrary Java objects. For example exposing a Runnable or Function allows the "API" user to directly invoke some program logic.

It is required to prefix the share key with the mod id like mymod:someProperty. Mods should not modify entries by other mods. The share is thread safe.

  • Method Details

    • get

      Object get(String key)
      Get the value for a specific key.

      Java 16 introduced a convenient syntax for type safe queries that combines null check, type check and cast:

      if (FabricLoader.getInstance().getObjectShare().get("someMod:someValue") instanceof String value) {
        // use value here
      }
      

      A generic type still needs a second unchecked cast due to erasure:

      if (FabricLoader.getInstance().getObjectShare().get("mymod:fuel") instanceof Consumer<?> c) {
        ((Consumer<ItemStack>) c).accept(someStack);
      }
      

      Consider using whenAvailable(String, BiConsumer) instead if the value may not be available yet. The mod load order is undefined, so entries that are added during the same load phase should be queried in a later phase or be handled through whenAvailable(java.lang.String,java.util.function.BiConsumer<java.lang.String,java.lang.Object>).

      Parameters:
      key - key to query, format modid:subkey
      Returns:
      value associated with the key or null if none
    • whenAvailable

      void whenAvailable(String key, BiConsumer<String,Object> consumer)
      Request being notified when a key/value becomes available.

      This is primarily intended to resolve load order issues, when there is no good time to call get(java.lang.String).

      If there is already a value associated with the key, the consumer will be invoked directly, otherwise when one of the put methods adds a value for the key. The invocation happens on the thread calling whenAvailable(String, BiConsumer) or on whichever thread calls put with the same key.

      The request will only act once, not if the value changes again.

      Example use:

      FabricLoader.getInstance().getObjectShare().whenAvailable("someMod:someValue", (k, v) -> {
        if (v instanceof String value) {
          // use value
        }
      });
      
      Parameters:
      key - key to react upon, format modid:subkey
    • put

      Object put(String key, Object value)
      Set the value for a specific key.
      Parameters:
      key - key to add a value for, format modid:subkey
      value - value to add, must not be null
      Returns:
      previous value associated with the key, null if none
    • putIfAbsent

      Object putIfAbsent(String key, Object value)
      Set the value for a specific key if there isn't one yet.

      This is an atomic operation, thus thread safe contrary to using get+put.

      Parameters:
      key - key to add a value for, format modid:subkey
      value - value to add, must not be null
      Returns:
      previous value associated with the key, null if none and thus the entry changed
    • remove

      Object remove(String key)
      Remove the value for a specific key.
      Parameters:
      key - key to remove the value for, format modid:subkey
      Returns:
      previous value associated with the key, null if none