Creating a `Context`

I was busy implementing the Economy API when I noticed Set<Context>, but how do I create a global context?

What is the point of REMOTE_IP_KEY, LOCAL_HOST_KEY, LOCAL_IP_KEY and LOCAL_PORT_KEY?
Contexts are poorly explained in the Javadocs.

A global context is just an empty Set<Context>.

A word on the different keys: You don’t have to implement all existing keys, and you can also add your own custom keys.

For example a context key “time” with possible values “day” and “night” (people can not access their bank account at night, something like that).

To do that, just implement a TimeContextCalculator:

public class TimeContextCalculator extends ContextCalculator<Player> {

    public void accumulateContexts(Player subject, Set<Context> accumulator) {
        Context timeContext = new Context("time", isInDaytimeDimension(subject) ? "day" : "night");
        accumulator.add(timeContext);
    }

    public boolean matches(Context context, Player subject) {
        if(context.getKey().equals("time")) {
            return (isInDaytimeDimension(subject) == context.getValue().equals("day"));
        }
        return false;
    }

    private boolean isInDaytimeDimension(Player player) {
        return ...;
    }
}

That’s also how you would implement the default context keys.

That cleared things up.

When you know what contexts are for, they sound very powerful for imposing any given condition.

Economies can also exploit these for per world or per region economies.