|
13 | 13 | import org.broken.arrow.library.visualization.BlockVisualize; |
14 | 14 | import org.broken.arrow.utility.library.chunk.tracker.ChunkRelevanceTrackerWrapper; |
15 | 15 | import org.broken.arrow.utility.library.listner.UtilityListener; |
| 16 | +import org.broken.arrow.utility.library.utility.UtilityServices; |
16 | 17 | import org.bukkit.Bukkit; |
17 | | -import org.bukkit.plugin.Plugin; |
| 18 | +import org.bukkit.plugin.ServicePriority; |
18 | 19 | import org.bukkit.plugin.java.JavaPlugin; |
19 | 20 |
|
| 21 | +import javax.annotation.Nonnull; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.function.Consumer; |
20 | 25 | import java.util.logging.Level; |
21 | 26 |
|
22 | 27 | /** |
|
26 | 31 | * compile every module into your plugin. |
27 | 32 | */ |
28 | 33 | public final class UtilityLibrary extends JavaPlugin { |
29 | | - private static UtilityLibrary instance; |
30 | | - private RegisterMenuAPI menuAPI; |
31 | | - private ChunkRelevanceTrackerWrapper chunkRelevanceTracker; |
32 | | - |
33 | | - @Override |
34 | | - public void onLoad() { |
35 | | - instance = this; |
36 | | - UpdateTitle.update(null, ""); |
37 | | - } |
38 | | - |
39 | | - @Override |
40 | | - public void onEnable() { |
41 | | - this.chunkRelevanceTracker = new ChunkRelevanceTrackerWrapper(this); |
42 | | - this.menuAPI = new RegisterMenuAPI(this); |
43 | | - Bukkit.getPluginManager().registerEvents(new UtilityListener(this.chunkRelevanceTracker),this); |
44 | | - getLogger().log(Level.INFO, "Has started API " + getDescription().getName() + " version= " + getDescription().getVersion()); |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * Retrieves the instance of the UtilityLibrary plugin. |
49 | | - * |
50 | | - * @return The instance of UtilityLibrary. |
51 | | - */ |
52 | | - public static UtilityLibrary getInstance() { |
53 | | - return instance; |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Get the RegisterMenuAPI instance for the given plugin. |
58 | | - * |
59 | | - * @return The RegisterMenuAPI instance. |
60 | | - */ |
61 | | - public RegisterMenuAPI getMenuApi() { |
62 | | - return menuAPI; |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * Creates a new ItemCreator instance for the given plugin. |
67 | | - * |
68 | | - * @return The ItemCreator instance. |
69 | | - */ |
70 | | - public ItemCreator getItemCreator() { |
71 | | - return new ItemCreator(this); |
72 | | - } |
73 | | - |
74 | | - /** |
75 | | - * Creates a new MySQL instance with the given MySQL preferences. |
76 | | - * |
77 | | - * @param mysqlPreference The MySQL preferences. |
78 | | - * @return The MySQL instance. |
79 | | - */ |
80 | | - public MySQL createMySQLInstance(ConnectionSettings mysqlPreference) { |
81 | | - return new MySQL(mysqlPreference); |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * Creates a new SQLite instance with the given parent and child paths. |
86 | | - * |
87 | | - * @param parent The parent path where file is located. |
88 | | - * @param child The child path where file is located. |
89 | | - * @return The SQLite instance. |
90 | | - */ |
91 | | - public SQLite createSQLiteInstance(String parent, String child) { |
92 | | - return new SQLite(parent, child); |
93 | | - } |
94 | | - |
95 | | - /** |
96 | | - * Retrieves a new CommandRegistering instance. |
97 | | - * |
98 | | - * @return The CommandRegistering instance. |
99 | | - */ |
100 | | - public CommandRegistering getCommandRegistry() { |
101 | | - return new CommandRegister(); |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * Retrieves a new BlockVisualize instance for the given plugin. |
106 | | - * |
107 | | - * @return The BlockVisualize instance. |
108 | | - */ |
109 | | - public BlockVisualize getVisualizer() { |
110 | | - return new BlockVisualize(this); |
111 | | - } |
112 | | - |
113 | | - /** |
114 | | - * Provides access to the {@link ChunkRelevanceTracker} used by this plugin. |
115 | | - * |
116 | | - * <p>This tracker exposes the public API for interacting with chunk relevance, |
117 | | - * including checking whether a chunk has players present or is force-loaded.</p> |
118 | | - * |
119 | | - * @return the {@link ChunkRelevanceTracker} instance |
120 | | - */ |
121 | | - public ChunkRelevanceTracker getChunkRelevanceTracker() { |
122 | | - return chunkRelevanceTracker; |
123 | | - } |
| 34 | + private static UtilityLibrary instance; |
| 35 | + |
| 36 | + private final List<Consumer<UtilityServices>> callbacks = new ArrayList<>(); |
| 37 | + private UtilityServices utilityServices; |
| 38 | + private volatile boolean ready; |
| 39 | + |
| 40 | + private RegisterMenuAPI menuAPI; |
| 41 | + private ChunkRelevanceTrackerWrapper chunkRelevanceTracker; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onLoad() { |
| 45 | + instance = this; |
| 46 | + UpdateTitle.update(null, ""); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onEnable() { |
| 51 | + this.chunkRelevanceTracker = new ChunkRelevanceTrackerWrapper(this); |
| 52 | + this.menuAPI = new RegisterMenuAPI(this); |
| 53 | + this.utilityServices = new UtilityServices(menuAPI, chunkRelevanceTracker); |
| 54 | + Bukkit.getPluginManager().registerEvents(new UtilityListener(this.chunkRelevanceTracker), this); |
| 55 | + getLogger().log(Level.INFO, "Has started API " + getDescription().getName() + " version= " + getDescription().getVersion()); |
| 56 | +runnable |
| 57 | + Bukkit.getServicesManager().register(UtilityServices.class, this.utilityServices, this, ServicePriority.Normal); |
| 58 | + Bukkit.getScheduler().runTaskLater(this, () -> { |
| 59 | + ready = true; |
| 60 | + for (Consumer<UtilityServices> callback : callbacks) { |
| 61 | + callback.accept(utilityServices); |
| 62 | + } |
| 63 | + callbacks.clear(); |
| 64 | + }, 20L); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Registers a callback that will be executed once all services are fully initialized. |
| 69 | + * |
| 70 | + * <p>If the UtilityLibrary is already ready, the callback is executed immediately. |
| 71 | + * Otherwise, it will be invoked once initialization has completed.</p> |
| 72 | + * |
| 73 | + * <p>This is the recommended way to access {@link UtilityServices}, as it guarantees |
| 74 | + * that all services are safe to use.</p> |
| 75 | + * |
| 76 | + * @param callback the callback to execute when services are ready. |
| 77 | + */ |
| 78 | + public void whenReady(@Nonnull final Consumer<UtilityServices> callback) { |
| 79 | + if (ready) { |
| 80 | + callback.accept(utilityServices); |
| 81 | + } else { |
| 82 | + callbacks.add(callback); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Retrieves the instance of the UtilityLibrary plugin. |
| 88 | + * |
| 89 | + * @return The instance of UtilityLibrary. |
| 90 | + */ |
| 91 | + public static UtilityLibrary getInstance() { |
| 92 | + return instance; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the RegisterMenuAPI instance for the given plugin. |
| 97 | + * |
| 98 | + * @return The RegisterMenuAPI instance. |
| 99 | + */ |
| 100 | + public RegisterMenuAPI getMenuApi() { |
| 101 | + return menuAPI; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Creates a new ItemCreator instance for the given plugin. |
| 106 | + * |
| 107 | + * @return The ItemCreator instance. |
| 108 | + */ |
| 109 | + public ItemCreator getItemCreator() { |
| 110 | + return new ItemCreator(this); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Creates a new MySQL instance with the given MySQL preferences. |
| 115 | + * |
| 116 | + * @param mysqlPreference The MySQL preferences. |
| 117 | + * @return The MySQL instance. |
| 118 | + */ |
| 119 | + public MySQL createMySQLInstance(ConnectionSettings mysqlPreference) { |
| 120 | + return new MySQL(mysqlPreference); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Creates a new SQLite instance with the given parent and child paths. |
| 125 | + * |
| 126 | + * @param parent The parent path where file is located. |
| 127 | + * @param child The child path where file is located. |
| 128 | + * @return The SQLite instance. |
| 129 | + */ |
| 130 | + public SQLite createSQLiteInstance(String parent, String child) { |
| 131 | + return new SQLite(parent, child); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Retrieves a new CommandRegistering instance. |
| 136 | + * |
| 137 | + * @return The CommandRegistering instance. |
| 138 | + */ |
| 139 | + public CommandRegistering getCommandRegistry() { |
| 140 | + return new CommandRegister(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Retrieves a new BlockVisualize instance for the given plugin. |
| 145 | + * |
| 146 | + * @return The BlockVisualize instance. |
| 147 | + */ |
| 148 | + public BlockVisualize getVisualizer() { |
| 149 | + return new BlockVisualize(this); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Provides access to the {@link ChunkRelevanceTracker} used by this plugin. |
| 154 | + * |
| 155 | + * <p>This tracker exposes the public API for interacting with chunk relevance, |
| 156 | + * including checking whether a chunk has players present or is force-loaded.</p> |
| 157 | + * |
| 158 | + * @return the {@link ChunkRelevanceTracker} instance |
| 159 | + */ |
| 160 | + public ChunkRelevanceTracker getChunkRelevanceTracker() { |
| 161 | + return chunkRelevanceTracker; |
| 162 | + } |
124 | 163 | } |
0 commit comments