ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

Back to src/main/java/com/scriptslab/core/event
J

EventBusImpl.java

Java · 184 lines · 5.8 KB

src/main/java/com/scriptslab/core/event/EventBusImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.scriptslab.core.event;

import com.scriptslab.api.event.EventBus;
import com.scriptslab.api.event.EventSubscription;
import com.scriptslab.api.event.PluginEvent;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Thread-safe implementation of EventBus.
 * Uses ConcurrentHashMap and CopyOnWriteArrayList for thread safety.
 */
public final class EventBusImpl implements EventBus {
    
    private final Logger logger;
    private final Map<Class<? extends PluginEvent>, List<SubscriptionImpl<?>>> subscribers;
    
    public EventBusImpl() {
        this.logger = Logger.getLogger("EventBus");
        this.subscribers = new ConcurrentHashMap<>();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public <T extends PluginEvent> CompletableFuture<T> post(T event) {
        return CompletableFuture.supplyAsync(() -> {
            Class<? extends PluginEvent> eventType = event.getClass();
            List<SubscriptionImpl<?>> subs = subscribers.get(eventType);
            
            if (subs == null || subs.isEmpty()) {
                return event;
            }
            
            // Sort by priority
            List<SubscriptionImpl<?>> sortedSubs = new ArrayList<>(subs);
            sortedSubs.sort(Comparator.comparingInt(s -> s.priority.getLevel()));
            
            for (SubscriptionImpl<?> sub : sortedSubs) {
                if (!sub.isActive()) {
                    continue;
                }
                
                try {
                    ((Consumer<T>) sub.handler).accept(event);
                    
                    // Check if event was cancelled
                    if (event.isCancellable() && 
                        event instanceof PluginEvent.Cancellable cancellable &&
                        cancellable.isCancelled()) {
                        break;
                    }
                } catch (Exception e) {
                    logger.log(Level.SEVERE, "Error handling event " + eventType.getSimpleName(), e);
                }
            }
            
            return event;
        });
    }
    
    @Override
    public <T extends PluginEvent> CompletableFuture<T> postAsync(T event) {
        return post(event); // Already async
    }
    
    @Override
    public <T extends PluginEvent> EventSubscription subscribe(
            Class<T> eventType,
            Consumer<T> handler,
            EventPriority priority) {
        
        if (eventType == null || handler == null || priority == null) {
            throw new IllegalArgumentException("Arguments cannot be null");
        }
        
        SubscriptionImpl<T> subscription = new SubscriptionImpl<>(
                eventType, handler, priority, handler);
        
        subscribers.computeIfAbsent(eventType, k -> new CopyOnWriteArrayList<>())
                .add(subscription);
        
        logger.fine("Subscribed to " + eventType.getSimpleName() + 
                   " with priority " + priority);
        
        return subscription;
    }
    
    @Override
    public void unsubscribe(EventSubscription subscription) {
        if (subscription == null) {
            return;
        }
        
        subscription.cancel();
        
        List<SubscriptionImpl<?>> subs = subscribers.get(subscription.getEventType());
        if (subs != null) {
            subs.remove(subscription);
        }
        
        logger.fine("Unsubscribed from " + subscription.getEventType().getSimpleName());
    }
    
    @Override
    public void unsubscribeAll(Object subscriber) {
        if (subscriber == null) {
            return;
        }
        
        int count = 0;
        for (List<SubscriptionImpl<?>> subs : subscribers.values()) {
            Iterator<SubscriptionImpl<?>> iterator = subs.iterator();
            while (iterator.hasNext()) {
                SubscriptionImpl<?> sub = iterator.next();
                if (sub.subscriber == subscriber) {
                    sub.cancel();
                    iterator.remove();
                    count++;
                }
            }
        }
        
        logger.fine("Unsubscribed " + count + " handlers for " + subscriber);
    }
    
    @Override
    public int getSubscriberCount(Class<? extends PluginEvent> eventType) {
        List<SubscriptionImpl<?>> subs = subscribers.get(eventType);
        return subs != null ? subs.size() : 0;
    }
    
    /**
     * Internal subscription implementation.
     */
    private static class SubscriptionImpl<T extends PluginEvent> implements EventSubscription {
        
        private final Class<T> eventType;
        private final Consumer<T> handler;
        private final EventPriority priority;
        private final Object subscriber;
        private volatile boolean active;
        
        SubscriptionImpl(Class<T> eventType, Consumer<T> handler, 
                        EventPriority priority, Object subscriber) {
            this.eventType = eventType;
            this.handler = handler;
            this.priority = priority;
            this.subscriber = subscriber;
            this.active = true;
        }
        
        @Override
        public Class<? extends PluginEvent> getEventType() {
            return eventType;
        }
        
        @Override
        public EventPriority getPriority() {
            return priority;
        }
        
        @Override
        public boolean isActive() {
            return active;
        }
        
        @Override
        public void cancel() {
            active = false;
        }
        
        @Override
        public Object getSubscriber() {
            return subscriber;
        }
    }
}