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 0e0eea5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
21 changes: 18 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,20 @@ 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;
}

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

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

}
5 changes: 5 additions & 0 deletions core/src/main/java/com/rexcantor64/triton/Triton.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rexcantor64.triton;

import com.rexcantor64.triton.api.TritonAPI;
import com.rexcantor64.triton.api.TritonAPIUtils;
import com.rexcantor64.triton.api.language.LanguageParser;
import com.rexcantor64.triton.api.legacy.LegacyLanguageParser;
import com.rexcantor64.triton.bridge.BridgeManager;
Expand Down Expand Up @@ -86,6 +88,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.

12 changes: 12 additions & 0 deletions core/src/main/java/com/rexcantor64/triton/api/TritonAPIUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.rexcantor64.triton.api;

import org.jetbrains.annotations.NotNull;

// This class has to be in this package to have access to the methods in TritonAPI
public class TritonAPIUtils {

public static void register(@NotNull Triton instance) {
TritonAPI.register(instance);
}

}
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 0e0eea5

Please sign in to comment.