package net.draycia.carbon.managers; import net.draycia.carbon.channels.ChatChannel; import net.draycia.carbon.channels.contexts.MessageContext; import net.draycia.carbon.channels.contexts.impl.SimpleMessageContext; import net.draycia.carbon.storage.ChatUser; import java.util.HashMap; import java.util.Map; import java.util.function.Function; public class ContextManager { private final Map> handlers = new HashMap<>(); public boolean register(String key, Function handler) { if (this.handlers.containsKey(key)) { return false; } this.handlers.put(key, handler); return true; } public boolean testContext(ChatUser user, ChatUser target, ChatChannel channel) { for (Map.Entry> handler : handlers.entrySet()) { String key = handler.getKey(); Object value = channel.getContext(key); if (value == null) { continue; } MessageContext context = new SimpleMessageContext(user, target, channel, value); return handler.getValue().apply(context); } return true; } }