Skip to content

Commit

Permalink
fix: api not being accessible through TritonAPI
Browse files Browse the repository at this point in the history
Due to #285, the TritonAPI class in the core module was no longer
overshadowing the same class in the api module, effectively breaking the
TritonAPI#getInstance method.

This commit works around that by saving a reference to the Triton
instance in the TritonAPI class ifself.
  • Loading branch information
diogotcorreia committed Jul 14, 2024
1 parent aee7002 commit 9d5a140
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
22 changes: 19 additions & 3 deletions api/src/main/java/com/rexcantor64/triton/api/TritonAPI.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.rexcantor64.triton.api;

import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;

/**
* The entry point of the API
*
* @since 1.0.0
*/
public class TritonAPI {
public final class TritonAPI {
@Internal
private static Triton instance;

/**
* Get the instance of the {@link Triton plugin}.
Expand All @@ -16,8 +19,21 @@ public class TritonAPI {
* @since 1.0.0
*/
public static @NotNull Triton getInstance() {
// This class gets replaced with a proper implementation in Triton's build.
throw new UnsupportedOperationException("Triton is not running! If you're seeing this, it is because some plugin shadowed the TritonAPI (when it should not have!).");
if (instance == null) {
throw new UnsupportedOperationException("Triton is not running (yet?)! If you're seeing this, some plugin is trying to use the Triton API before Triton has loaded.");
}
return instance;
}

@SuppressWarnings("unused")
@Internal
private static void register(@NotNull Triton instance) {
TritonAPI.instance = instance;
}

@Internal
private TritonAPI() {
throw new UnsupportedOperationException("This class cannot be instantiated.");
}

}
4 changes: 4 additions & 0 deletions core/src/main/java/com/rexcantor64/triton/Triton.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.rexcantor64.triton.storage.MysqlStorage;
import com.rexcantor64.triton.storage.Storage;
import com.rexcantor64.triton.utils.FileUtils;
import com.rexcantor64.triton.utils.TritonAPIUtils;
import com.rexcantor64.triton.web.TwinManager;
import lombok.Getter;
import lombok.val;
Expand Down Expand Up @@ -86,6 +87,9 @@ public static boolean isSpigot() {
}

protected void onEnable() {
instance = this;
TritonAPIUtils.register(instance);

translationsFolder = new File(getDataFolder(), "translations");

logger = loader.getTritonLogger();
Expand Down
9 changes: 0 additions & 9 deletions core/src/main/java/com/rexcantor64/triton/api/TritonAPI.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.rexcantor64.triton.utils;

import com.rexcantor64.triton.api.Triton;
import com.rexcantor64.triton.api.TritonAPI;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Method;

/**
* Utility functions to easily register the {@link Triton} instance in
* {@link TritonAPI} for consumption by other plugins.
*
* @since 4.0.0
*/
public class TritonAPIUtils {
private static final Method REGISTER;

static {
try {
REGISTER = TritonAPI.class.getDeclaredMethod("register", Triton.class);
REGISTER.setAccessible(true);

} catch (NoSuchMethodException e) {
throw new RuntimeException("Failed to initialize Triton API", e);
}
}

public static void register(@NotNull Triton instance) {
try {
REGISTER.invoke(null, instance);
} catch (Exception e) {
com.rexcantor64.triton.Triton.get().getLogger().logError(e, "Failed to initialize Triton API");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public BungeePlugin getLoader() {

@Override
public void onEnable() {
instance = this;
super.onEnable();

Metrics metrics = new Metrics(getPlugin(), 5607);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public static SpigotTriton asSpigot() {

@Override
public void onEnable() {
instance = this;

super.onEnable();

if (!this.isProtocolLibAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public Object getPlugin() {

@Override
public void onEnable() {
instance = this;
super.onEnable();

// bStats
Expand Down

0 comments on commit 9d5a140

Please sign in to comment.