Interface ObjectShare
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 Summary
Modifier and TypeMethodDescriptionGet the value for a specific key.Set the value for a specific key.putIfAbsent
(String key, Object value) Set the value for a specific key if there isn't one yet.Remove the value for a specific key.void
whenAvailable
(String key, BiConsumer<String, Object> consumer) Request being notified when a key/value becomes available.
-
Method Details
-
get
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(java.lang.String, java.util.function.BiConsumer<java.lang.String, java.lang.Object>)
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 throughwhenAvailable(java.lang.String,java.util.function.BiConsumer<java.lang.String,java.lang.Object>)
.- Parameters:
key
- key to query, formatmodid:subkey
- Returns:
- value associated with the key or null if none
-
whenAvailable
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 theput
methods adds a value for the key. The invocation happens on the thread callingwhenAvailable(java.lang.String, java.util.function.BiConsumer<java.lang.String, java.lang.Object>)
or on whichever thread callsput
with the samekey
.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, formatmodid:subkey
-
put
Set the value for a specific key.- Parameters:
key
- key to add a value for, formatmodid:subkey
value
- value to add, must not be null- Returns:
- previous value associated with the key, null if none
-
putIfAbsent
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, formatmodid: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
Remove the value for a specific key.- Parameters:
key
- key to remove the value for, formatmodid:subkey
- Returns:
- previous value associated with the key, null if none
-