mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Single module setup
This commit is contained in:
1 parent
ad9a794156
commit
66217f6534
70 files changed
+92
-138
No files matched your search
@@ -0,0 +1,43 @@
|
||||
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<String, Function<MessageContext, Boolean>> handlers = new HashMap<>();
|
||||
|
||||
public boolean register(String key, Function<MessageContext, Boolean> 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<String, Function<MessageContext, Boolean>> 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user