Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
package io.github.hyscript7.ascendancy.api;
import org.bukkit.Bukkit;
import org.bukkit.plugin.ServicePriority;
import java.util.Optional;
import org.bukkit.plugin.Plugin;

public class AscendancyAPI {
}
public interface AscendancyAPI {
class Holder {
private static AscendancyAPI INSTANCE;
}

static AscendancyAPI get(){
if (Holder.INSTANCE == null) {
throw new IllegalStateException("AscendancyAPI instance is not set. Please set it using AscendancyAPI.set() before calling AscendancyAPI.get().");
}
return Holder.INSTANCE;
}

static Optional<AscendancyAPI> fromServicesManager(){
if (Optional.ofNullable(Bukkit.getServer().getServicesManager().load(AscendancyAPI.class)).isPresent()) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd either add logging to justify this if statement or just simply return:

return Optional.ofNullable(Bukkit.getServer().getServicesManager().load(AscendancyAPI.class));

return Optional.of(Bukkit.getServer().getServicesManager().load(AscendancyAPI.class));
}
return Optional.empty();
}

static void set(AscendancyAPI instance, Plugin plugin) throws AscendancyAPIAlreadyInitializedException {
if (Holder.INSTANCE != null) {
throw new AscendancyAPIAlreadyInitializedException("AscendancyAPI instance is already set. Cannot set it again.");
}

Holder.INSTANCE = instance;
Bukkit.getServicesManager().register(AscendancyAPI.class, Holder.INSTANCE, plugin, ServicePriority.Normal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.hyscript7.ascendancy.api;

public class AscendancyAPIAlreadyInitializedException extends RuntimeException {
public AscendancyAPIAlreadyInitializedException(String message) {
super(message);
}
}