Skip to content

Commit 25e94ed

Browse files
committed
Moved logic to another class
This is to better have control over the live cycle than just have to guess or using own runnable.
1 parent e8f6ad1 commit 25e94ed

2 files changed

Lines changed: 251 additions & 96 deletions

File tree

Utility Library/src/main/java/org/broken/arrow/utility/library/UtilityLibrary.java

Lines changed: 135 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
import org.broken.arrow.library.visualization.BlockVisualize;
1414
import org.broken.arrow.utility.library.chunk.tracker.ChunkRelevanceTrackerWrapper;
1515
import org.broken.arrow.utility.library.listner.UtilityListener;
16+
import org.broken.arrow.utility.library.utility.UtilityServices;
1617
import org.bukkit.Bukkit;
17-
import org.bukkit.plugin.Plugin;
18+
import org.bukkit.plugin.ServicePriority;
1819
import org.bukkit.plugin.java.JavaPlugin;
1920

21+
import javax.annotation.Nonnull;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.function.Consumer;
2025
import java.util.logging.Level;
2126

2227
/**
@@ -26,99 +31,133 @@
2631
* compile every module into your plugin.
2732
*/
2833
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+
}
124163
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package org.broken.arrow.utility.library.utility;
2+
3+
import org.broken.arrow.library.chunk.tracking.ChunkRelevanceTracker;
4+
import org.broken.arrow.library.command.CommandRegister;
5+
import org.broken.arrow.library.command.commandhandler.CommandRegistering;
6+
import org.broken.arrow.library.database.builders.ConnectionSettings;
7+
import org.broken.arrow.library.database.core.databases.MySQL;
8+
import org.broken.arrow.library.database.core.databases.SQLite;
9+
import org.broken.arrow.library.itemcreator.ItemCreator;
10+
import org.broken.arrow.library.menu.RegisterMenuAPI;
11+
import org.broken.arrow.library.visualization.BlockVisualize;
12+
import org.broken.arrow.utility.library.UtilityLibrary;
13+
import org.broken.arrow.utility.library.chunk.tracker.ChunkRelevanceTrackerWrapper;
14+
15+
import javax.annotation.Nonnull;
16+
import java.util.function.Consumer;
17+
18+
/**
19+
* Central access point for all services provided by the UtilityLibrary plugin.
20+
*
21+
* <p>This class can be obtained through Bukkit's ServicesManager, but is more safely
22+
* accessed via {@link UtilityLibrary#whenReady(Consumer)}, which guarantees that
23+
* all services are fully initialized before use.</p>
24+
*/
25+
public class UtilityServices {
26+
private final UtilityLibrary utilityLibrary;
27+
private final RegisterMenuAPI menuAPI;
28+
private final ChunkRelevanceTrackerWrapper chunkRelevanceTracker;
29+
30+
/**
31+
* Constructs a new UtilityServices instance.
32+
*
33+
* <p>This constructor is intended for internal use by the UtilityLibrary plugin.
34+
* Consumers should not create instances manually, but instead obtain them through
35+
* the Bukkit ServicesManager or the provided readiness mechanisms.</p>
36+
*
37+
* @param menuAPI the menu API implementation
38+
* @param chunkRelevanceTracker the chunk relevance tracker implementation
39+
*/
40+
public UtilityServices( @Nonnull final RegisterMenuAPI menuAPI, @Nonnull final ChunkRelevanceTrackerWrapper chunkRelevanceTracker) {
41+
this.utilityLibrary = UtilityLibrary.getInstance();
42+
this.menuAPI = menuAPI;
43+
this.chunkRelevanceTracker = chunkRelevanceTracker;
44+
}
45+
46+
/**
47+
* Provides access to the menu API.
48+
*
49+
* @return the menu API instance.
50+
*/
51+
public RegisterMenuAPI getMenuApi() {
52+
return menuAPI;
53+
}
54+
55+
/**
56+
* Creates a new {@link ItemCreator} instance.
57+
*
58+
* <p>Each invocation returns a new instance bound to the current UtilityLibrary context.</p>
59+
*
60+
* @return a new ItemCreator instance
61+
*/
62+
public ItemCreator getItemCreator() {
63+
return new ItemCreator(utilityLibrary);
64+
}
65+
66+
/**
67+
* Creates a new MySQL connection wrapper using the provided settings.
68+
*
69+
* @param mysqlPreference the connection settings
70+
* @return a new MySQL instance
71+
*/
72+
public MySQL createMySQLInstance(ConnectionSettings mysqlPreference) {
73+
return new MySQL(mysqlPreference);
74+
}
75+
76+
/**
77+
* Creates a new SQLite connection wrapper.
78+
*
79+
* @param parent the parent directory path
80+
* @param child the database file name or relative path
81+
* @return a new SQLite instance
82+
*/
83+
public SQLite createSQLiteInstance(String parent, String child) {
84+
return new SQLite(parent, child);
85+
}
86+
87+
/**
88+
* Creates a new command registration helper.
89+
*
90+
* @return a new CommandRegistering instance
91+
*/
92+
public CommandRegistering getCommandRegistry() {
93+
return new CommandRegister();
94+
}
95+
96+
/**
97+
* Creates a new block visualization helper.
98+
*
99+
* @return a new BlockVisualize instance
100+
*/
101+
public BlockVisualize getVisualizer() {
102+
return new BlockVisualize(utilityLibrary);
103+
}
104+
105+
/**
106+
* Provides access to the chunk relevance tracker.
107+
*
108+
* <p>This tracker allows checking whether chunks are considered relevant,
109+
* such as having players present or being force-loaded.</p>
110+
*
111+
* @return the chunk relevance tracker instance
112+
*/
113+
public ChunkRelevanceTracker getChunkRelevanceTracker() {
114+
return chunkRelevanceTracker;
115+
}
116+
}

0 commit comments

Comments
 (0)