Repository navigation
Expand file tree
/
Copy pathSimplePluginModule.java
More file actions
71 lines (60 loc) · 2.26 KB
/
Copy pathSimplePluginModule.java
File metadata and controls
71 lines (60 loc) · 2.26 KB
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
package com.minecraftcitiesnetwork.pluginInfrastructure.modules;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
/**
* A {@link PluginModule} base that keeps the host plugin and data folder, and tracks listeners
* registered through {@link #registerListener}.
*
* <p>Tracking is not cleanup: neither this class's {@link #initialize} nor its {@link #shutdown}
* unregisters anything. Following {@link PluginModule#initialize}'s contract, a subclass whose
* {@code initialize} fails after {@code registerListener} must call {@link #unregisterListeners()}
* before rethrowing, and its {@code shutdown} must call {@code unregisterListeners()} before
* {@code super.shutdown(plugin)}.
*
* @param <T> the host plugin type
*/
public abstract class SimplePluginModule<T extends Plugin> implements PluginModule<T> {
private final Collection<Listener> listeners = new ArrayList<>();
private T plugin;
private Path dataFolder;
protected T plugin() {
if (this.plugin == null) {
throw new IllegalStateException("Module not yet initialized!");
}
return this.plugin;
}
@Override
public void initialize(@NotNull T plugin, @NotNull Path dataFolder) {
this.plugin = plugin;
this.dataFolder = dataFolder;
}
@Override
public void shutdown(@NotNull T plugin) {
this.plugin = null;
this.dataFolder = null;
}
@NotNull
protected Path dataFolder() {
if (this.dataFolder == null) {
throw new IllegalStateException("Module not initialized!");
}
return this.dataFolder;
}
protected void registerListener(@NotNull Listener listener) {
this.listeners.add(listener);
this.plugin.getServer().getPluginManager().registerEvents(listener, this.plugin);
}
protected void unregisterListeners(@NotNull Listener listener) {
this.listeners.remove(listener);
HandlerList.unregisterAll(listener);
}
protected void unregisterListeners() {
this.listeners.forEach(HandlerList::unregisterAll);
this.listeners.clear();
}
}