Skip to content

Commit f5d08b4

Browse files
committed
Multiple improvements with listeners and their documentation.
- Added a method to remove registered listeners again - Fixed a bug where a listener that gets registered with onEnable when bluemap is already enabled is not being called - Improved the documentation for the onEnable and onDisable functions
1 parent 7635c0c commit f5d08b4

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/main/java/de/bluecolored/bluemap/api/BlueMapAPI.java

+27-17
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,16 @@
2424
*/
2525
package de.bluecolored.bluemap.api;
2626

27+
import de.bluecolored.bluemap.api.marker.Marker;
28+
import de.bluecolored.bluemap.api.marker.MarkerAPI;
29+
import de.bluecolored.bluemap.api.renderer.RenderAPI;
30+
2731
import java.awt.image.BufferedImage;
2832
import java.io.IOException;
29-
import java.util.ArrayList;
30-
import java.util.Collection;
31-
import java.util.List;
32-
import java.util.Optional;
33-
import java.util.UUID;
33+
import java.util.*;
3434
import java.util.concurrent.ExecutionException;
3535
import java.util.function.Consumer;
3636

37-
import de.bluecolored.bluemap.api.marker.Marker;
38-
import de.bluecolored.bluemap.api.marker.MarkerAPI;
39-
import de.bluecolored.bluemap.api.renderer.RenderAPI;
40-
4137
/**
4238
* An API to control the running instance of BlueMap.
4339
* <p>This API is thread-save, so you <b>can</b> use it async, off the main-server-thread, to save performance!</p>
@@ -46,10 +42,10 @@ public abstract class BlueMapAPI {
4642
private static BlueMapAPI instance;
4743

4844
@Deprecated
49-
private static final Collection<BlueMapAPIListener> listener = new ArrayList<>(2);
45+
private static final Collection<BlueMapAPIListener> listener = new HashSet<>(2);
5046

51-
private static final Collection<Consumer<BlueMapAPI>> onEnableConsumers = new ArrayList<>(2);
52-
private static final Collection<Consumer<BlueMapAPI>> onDisableConsumers = new ArrayList<>(2);
47+
private static final Collection<Consumer<BlueMapAPI>> onEnableConsumers = new HashSet<>(2);
48+
private static final Collection<Consumer<BlueMapAPI>> onDisableConsumers = new HashSet<>(2);
5349

5450
/**
5551
* Getter for the {@link RenderAPI}.
@@ -136,7 +132,7 @@ public static synchronized void registerListener(BlueMapAPIListener listener) {
136132
* @return <code>true</code> if a listener was removed as a result of this call
137133
*
138134
* @deprecated Implementing {@link BlueMapAPIListener} can cause a ClassNotFoundException when you soft-depend on BlueMap and your plugin/mod gets used without BlueMap.
139-
* Use {@link BlueMapAPI#onEnable(Consumer)} and {@link BlueMapAPI#onDisable(Consumer)} instead.
135+
* Use {@link BlueMapAPI#onEnable(Consumer)} and {@link BlueMapAPI#onDisable(Consumer)} instead.
140136
*/
141137
@Deprecated
142138
public static synchronized boolean unregisterListener(BlueMapAPIListener listener) {
@@ -152,25 +148,39 @@ public static synchronized Optional<BlueMapAPI> getInstance() {
152148
}
153149

154150
/**
155-
* Registers a {@link Consumer} that will be called when BlueMap has been loaded and started and the API is ready to use.<br>
156-
* If {@link BlueMapAPI} is already enabled when this listener is registered the consumer will be called immediately <i>(on the same thread)</i>!
151+
* Registers a {@link Consumer} that will be called every time BlueMap has just been loaded and started and the API is ready to use.<br>
152+
* If {@link BlueMapAPI} is already enabled when this listener is registered the consumer will be called immediately <i>(once, on the same thread)</i>!
153+
* <p><b>The {@link Consumer} can be called multiple times if BlueMap disables and enables again, e.g. if BlueMap gets reloaded!</b></p>
157154
* <p><i>(Note: The consumer will likely be called asynchronously, <b>not</b> on the server-thread!)</i></p>
155+
* <p>Remember to unregister the consumer when you no longer need it using {@link #unregisterListener(Consumer)}.</p>
158156
* @param consumer the {@link Consumer}
159157
*/
160158
public static synchronized void onEnable(Consumer<BlueMapAPI> consumer) {
161159
onEnableConsumers.add(consumer);
160+
if (BlueMapAPI.instance != null) consumer.accept(BlueMapAPI.instance);
162161
}
163162

164163
/**
165-
* Registers a {@link Consumer} that will be called <b>before</b> BlueMap is being unloaded and stopped, after the consumer returns the API is no longer usable!<br>
164+
* Registers a {@link Consumer} that will be called every time <b>before</b> BlueMap is being unloaded and stopped, after the consumer returns the API is no longer usable!<br>
166165
* Unlike with {@link BlueMapAPI#onEnable(Consumer)}, if {@link BlueMapAPI} is not enabled when this listener is registered the consumer will <b>not</b> be called.
166+
* <p><b>The {@link Consumer} can be called multiple times if BlueMap disables and enables again, e.g. if BlueMap gets reloaded!</b></p>
167167
* <p><i>(Note: The consumer will likely be called asynchronously, <b>not</b> on the server-thread!)</i></p>
168+
* <p>Remember to unregister the consumer when you no longer need it using {@link #unregisterListener(Consumer)}.</p>
168169
* @param consumer the {@link Consumer}
169170
*/
170171
public static synchronized void onDisable(Consumer<BlueMapAPI> consumer) {
171172
onDisableConsumers.add(consumer);
172173
}
173-
174+
175+
/**
176+
* Removes a {@link Consumer} that has been registered using {@link #onEnable(Consumer)} or {@link #onDisable(Consumer)}.
177+
* @param consumer the {@link Consumer} instance that has been registered previously
178+
* @return <code>true</code> if a listener was removed as a result of this call
179+
*/
180+
public static synchronized boolean unregisterListener(Consumer<BlueMapAPI> consumer) {
181+
return onEnableConsumers.remove(consumer) | onDisableConsumers.remove(consumer);
182+
}
183+
174184
/**
175185
* Used by BlueMap to register the API and call the listeners properly.
176186
* @param instance the {@link BlueMapAPI}-instance

0 commit comments

Comments
 (0)