diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 55250e12c1e..aeae8c4a0f9 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -496,7 +496,7 @@ public void onEnable() { } // initialize the modern Skript instance - skript = org.skriptlang.skript.Skript.of(this.getName()); + skript = org.skriptlang.skript.Skript.of(getClass(), getName()); unmodifiableSkript = skript.unmodifiableView(); skript.localizer().setSourceDirectories( getClass(), @@ -1387,10 +1387,7 @@ public static Collection getAddons() { Set addons = new HashSet<>(Skript.addons); addons.addAll(instance().addons().stream() .filter(addon -> addons.stream().noneMatch(oldAddon -> oldAddon.name().equals(addon.name()))) - .flatMap(addon -> { - SkriptAddon old = SkriptAddon.fromModern(addon); - return old == null ? Stream.empty() : Stream.of(old); - }) + .map(SkriptAddon::fromModern) .collect(Collectors.toSet()) ); return Collections.unmodifiableCollection(addons); @@ -1406,7 +1403,6 @@ public static Collection getAddons() { public static SkriptAddon getAddonInstance() { if (addon == null) { addon = SkriptAddon.fromModern(instance()); - assert addon != null; // should never be null for Skript } return addon; } diff --git a/src/main/java/ch/njol/skript/SkriptAddon.java b/src/main/java/ch/njol/skript/SkriptAddon.java index cc972bff94f..ec7e66ec6e5 100644 --- a/src/main/java/ch/njol/skript/SkriptAddon.java +++ b/src/main/java/ch/njol/skript/SkriptAddon.java @@ -51,7 +51,7 @@ public class SkriptAddon implements org.skriptlang.skript.addon.SkriptAddon { * Package-private constructor. Use {@link Skript#registerAddon(JavaPlugin)} to get a SkriptAddon for your plugin. */ SkriptAddon(JavaPlugin plugin) { - this(plugin, Skript.instance().registerAddon(plugin.getName())); + this(plugin, Skript.instance().registerAddon(plugin.getClass(), plugin.getName())); } SkriptAddon(JavaPlugin plugin, org.skriptlang.skript.addon.SkriptAddon addon) { @@ -133,14 +133,14 @@ public File getFile() { // @ApiStatus.Experimental - static @Nullable SkriptAddon fromModern(org.skriptlang.skript.addon.SkriptAddon addon) { - Class source = addon.localizer().source(); - if (source != null) // using source would be most accurate - return new SkriptAddon(JavaPlugin.getProvidingPlugin(source), addon); - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(addon.name()); - if (!(plugin instanceof JavaPlugin)) - return null; - return new SkriptAddon((JavaPlugin) plugin, addon); + static SkriptAddon fromModern(org.skriptlang.skript.addon.SkriptAddon addon) { + return new SkriptAddon(JavaPlugin.getProvidingPlugin(addon.source()), addon); + } + + @Override + @ApiStatus.Experimental + public Class source() { + return addon.source(); } @Override diff --git a/src/main/java/org/skriptlang/skript/Skript.java b/src/main/java/org/skriptlang/skript/Skript.java index f103546bb77..25604cf5cbf 100644 --- a/src/main/java/org/skriptlang/skript/Skript.java +++ b/src/main/java/org/skriptlang/skript/Skript.java @@ -16,20 +16,24 @@ public interface Skript extends SkriptAddon { /** * Constructs a default implementation of a Skript. * It makes use of the default implementations of required components. + * @param source The main class of the application creating this Skript. + * Typically, this can be the class invoking this method. * @param name The name for the Skript to use. * @return A Skript. */ - @Contract("_ -> new") - static Skript of(String name) { - return new SkriptImpl(name); + @Contract("_, _ -> new") + static Skript of(Class source, String name) { + return new SkriptImpl(source, name); } /** * Registers the provided addon with this Skript and loads the provided modules. + * @param source The main class of the application registering this addon. + * Typically, this can be the class invoking this method. * @param name The name of the addon to register. */ - @Contract("_ -> new") - SkriptAddon registerAddon(String name); + @Contract("_, _ -> new") + SkriptAddon registerAddon(Class source, String name); /** * @return An unmodifiable snapshot of addons currently registered with this Skript. diff --git a/src/main/java/org/skriptlang/skript/SkriptImpl.java b/src/main/java/org/skriptlang/skript/SkriptImpl.java index d6b145dd2d1..e7165edfc62 100644 --- a/src/main/java/org/skriptlang/skript/SkriptImpl.java +++ b/src/main/java/org/skriptlang/skript/SkriptImpl.java @@ -24,8 +24,8 @@ final class SkriptImpl implements Skript { */ private final SkriptAddon addon; - SkriptImpl(String name) { - addon = new SkriptAddonImpl(this, name, Localizer.of(this)); + SkriptImpl(Class source, String name) { + addon = new SkriptAddonImpl(this, source, name, Localizer.of(this)); storeRegistry(SyntaxRegistry.class, SyntaxRegistry.empty()); } @@ -72,17 +72,17 @@ public > R registry(Class registryClass, Supplier pu private static final Set addons = new HashSet<>(); @Override - public SkriptAddon registerAddon(String name) { + public SkriptAddon registerAddon(Class source, String name) { // make sure an addon is not already registered with this name for (SkriptAddon addon : addons) { if (name.equals(addon.name())) { throw new SkriptAPIException( - "An addon (provided by '" + addon.getClass().getName() + "') with the name '" + name + "' is already registered" + "An addon (provided by '" + addon.source().getName() + "') with the name '" + name + "' is already registered" ); } } - SkriptAddon addon = new SkriptAddonImpl(this, name, null); + SkriptAddon addon = new SkriptAddonImpl(this, source, name, null); addons.add(addon); return addon; } @@ -96,6 +96,11 @@ public SkriptAddon registerAddon(String name) { * SkriptAddon Implementation */ + @Override + public Class source() { + return addon.source(); + } + @Override public String name() { return addon.name(); @@ -119,15 +124,22 @@ public void loadModules(AddonModule... modules) { private static final class SkriptAddonImpl implements SkriptAddon { private final Skript skript; + private final Class source; private final String name; private final Localizer localizer; - SkriptAddonImpl(Skript skript, String name, @Nullable Localizer localizer) { + SkriptAddonImpl(Skript skript, Class source, String name, @Nullable Localizer localizer) { this.skript = skript; + this.source = source; this.name = name; this.localizer = localizer == null ? Localizer.of(this) : localizer; } + @Override + public Class source() { + return source; + } + @Override public String name() { return name; @@ -185,7 +197,7 @@ static final class UnmodifiableSkript implements Skript { } @Override - public SkriptAddon registerAddon(String name) { + public SkriptAddon registerAddon(Class source, String name) { throw new UnsupportedOperationException("Cannot register addons using an unmodifiable Skript"); } @@ -198,6 +210,11 @@ public SkriptAddon registerAddon(String name) { return addons.build(); } + @Override + public Class source() { + return skript.source(); + } + @Override public String name() { return skript.name(); diff --git a/src/main/java/org/skriptlang/skript/addon/AddonModule.java b/src/main/java/org/skriptlang/skript/addon/AddonModule.java index 96b15e2ca84..808a0b08eed 100644 --- a/src/main/java/org/skriptlang/skript/addon/AddonModule.java +++ b/src/main/java/org/skriptlang/skript/addon/AddonModule.java @@ -12,7 +12,8 @@ public interface AddonModule { /** - * + * Used for loading the components (e.g. syntax) of this module. + * Typically triggered through {@link SkriptAddon#loadModules(AddonModule...)}. * @param addon The addon this module belongs to. */ void load(SkriptAddon addon); diff --git a/src/main/java/org/skriptlang/skript/addon/SkriptAddon.java b/src/main/java/org/skriptlang/skript/addon/SkriptAddon.java index 4e925dd5fd5..4b7b6813a05 100644 --- a/src/main/java/org/skriptlang/skript/addon/SkriptAddon.java +++ b/src/main/java/org/skriptlang/skript/addon/SkriptAddon.java @@ -12,11 +12,17 @@ /** * A Skript addon is an extension to Skript that expands its features. - * Typically, an addon instance may be obtained through {@link Skript#registerAddon(String)}. + * Typically, an addon instance may be obtained through {@link Skript#registerAddon(Class, String)}. */ @ApiStatus.Experimental public interface SkriptAddon extends ViewProvider { + /** + * @return A class from the application that registered this addon. + * Typically, this is the main class or the specific class in which registration occurred. + */ + Class source(); + /** * @return The name of this addon. */ diff --git a/src/main/java/org/skriptlang/skript/addon/SkriptAddonImpl.java b/src/main/java/org/skriptlang/skript/addon/SkriptAddonImpl.java index 435e3f9e794..33af5c65b74 100644 --- a/src/main/java/org/skriptlang/skript/addon/SkriptAddonImpl.java +++ b/src/main/java/org/skriptlang/skript/addon/SkriptAddonImpl.java @@ -19,6 +19,11 @@ static class UnmodifiableAddon implements SkriptAddon { this.unmodifiableLocalizer = addon.localizer().unmodifiableView(); } + @Override + public Class source() { + return addon.source(); + } + @Override public String name() { return addon.name();