Package ch.njol.skript.lang
Interface KeyProviderExpression<T>
- All Superinterfaces:
Debuggable
,Expression<T>
,Loopable<T>
,SyntaxElement
- All Known Implementing Classes:
Variable
Represents an expression that is able to return a set of keys linked to its values.
This can be used to return index-linked values to store in a list variable,
using the
Index-linking is not (currently) used with other change modes.
Changer.ChangeMode.SET
Changer
.
An expression can provide a set of keys to use, rather than numerical indices.
Index-linking is not (currently) used with other change modes.
Contract
- Neither
getArrayKeys(Event)
norgetAllKeys(Event)
should ever be called without a correspondingExpression.getArray(Event)
orExpression.getAll(Event)
call. - A caller may ask only for values and does not have to invoke either
getArrayKeys(Event)
orgetAllKeys(Event)
. getArrayKeys(Event)
might be called after the correspondingExpression.getArray(Event)
getAllKeys(Event)
might be called after the correspondingExpression.getAll(Event)
Advice on Caching
As long as callers are built sensibly and follow API advice, it should be safe to cache a key-list during a values call. E.g. if an expression is returning data from a map, it could request the whole entry-set duringExpression.getArray(Event)
and return the keys during getArrayKeys(Event)
(provided the cache is weak, safe and event-linked).
This is not necessary, but it may be helpful for some expressions where the set of keys could potentially change
between repeated calls, or is expensive to access.
Caveats
- The caller may never ask for
getArrayKeys(Event)
. The cache should be disposed of in a timely manner. - It is (theoretically) possible for two separate calls to occur simultaneously (asking for the value/key sets separately) so it is recommended to link any cache system to the event instance .
getArrayKeys(Event)
and so the cache should be disposed of
in a timely manner.
Map<Event, Collection<String>> cache = new WeakHashMap<>();
public Object[] getArray(Event event) {
Set<Entry<String, T>> entries = something.entrySet();
cache.put(event, List.copyOf(something.keySet()));
return something.values().toArray(...);
}
public String[] getArrayKeys(Event event) {
if (!cache.containsKey(event))
throw new IllegalStateException();
return cache.remove(event).toArray(new String[0]);
// this should never be absent/null
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
While all keyed expressions offer their keys, some may prefer that they are not used unless strictly required (e.g.default @NotNull String @NotNull []
getAllKeys
(org.bukkit.event.Event event) A set of keys, matching the length and order of the immediately-previousExpression.getAll(Event)
values array.@NotNull String @NotNull []
getArrayKeys
(org.bukkit.event.Event event) A set of keys, matching the length and order of the immediately-previousExpression.getArray(Event)
values array.default boolean
isSingle()
Keyed expressions should never be single.Methods inherited from interface ch.njol.skript.lang.Debuggable
toString, toString
Methods inherited from interface ch.njol.skript.lang.Expression
acceptChange, beforeChange, canBeSingle, canReturn, change, changeInPlace, changeInPlace, check, check, getAcceptedChangeModes, getAll, getAnd, getArray, getConvertedExpression, getOptionalSingle, getReturnType, getSingle, getSource, getSyntaxTypeName, getTime, isDefault, possibleReturnTypes, setTime, simplify, stream
Methods inherited from interface ch.njol.skript.lang.Loopable
isLoopOf, iterator, supportsLoopPeeking
Methods inherited from interface ch.njol.skript.lang.SyntaxElement
getParser, init
-
Method Details
-
getArrayKeys
@NotNull @NotNull String @NotNull [] getArrayKeys(org.bukkit.event.Event event) throws IllegalStateException A set of keys, matching the length and order of the immediately-previousExpression.getArray(Event)
values array.
This should only be called immediately after aExpression.getArray(Event)
invocation. If it is called without a matching values request (or after a delay) then the behaviour is undefined, in which case:- the method may throw an error,
- the method may return keys not matching any previous values,
- or the method may return nothing at all.
- Parameters:
event
- The event context- Returns:
- A set of keys, of the same length as
Expression.getArray(Event)
- Throws:
IllegalStateException
- If this was not called directly after aExpression.getArray(Event)
call
-
getAllKeys
A set of keys, matching the length and order of the immediately-previousExpression.getAll(Event)
values array.
This should only be called immediately after aExpression.getAll(Event)
invocation. If it is called without a matching values request (or after a delay) then the behaviour is undefined, in which case:- the method may throw an error,
- the method may return keys not matching any previous values,
- or the method may return nothing at all.
- Parameters:
event
- The event context- Returns:
- A set of keys, of the same length as
Expression.getAll(Event)
- Throws:
IllegalStateException
- If this was not called directly after aExpression.getAll(Event)
call
-
isSingle
default boolean isSingle()Keyed expressions should never be single.- Specified by:
isSingle
in interfaceExpression<T>
- Returns:
- true if this expression will ever only return one value at most, false if it can return multiple values.
-
areKeysRecommended
default boolean areKeysRecommended()While all keyed expressions offer their keys, some may prefer that they are not used unless strictly required (e.g. variables).- Returns:
- Whether the caller is recommended to ask for keys after asking for values
-