Skip to content

Commit

Permalink
Add source requirement to SkriptAddon
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed Jun 30, 2024
1 parent 82d1978 commit 036d0b5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
8 changes: 2 additions & 6 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -1387,10 +1387,7 @@ public static Collection<SkriptAddon> getAddons() {
Set<SkriptAddon> 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);
Expand All @@ -1406,7 +1403,6 @@ public static Collection<SkriptAddon> getAddons() {
public static SkriptAddon getAddonInstance() {
if (addon == null) {
addon = SkriptAddon.fromModern(instance());
assert addon != null; // should never be null for Skript
}
return addon;
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/ch/njol/skript/SkriptAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/skriptlang/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/org/skriptlang/skript/SkriptImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -72,17 +72,17 @@ public <R extends Registry<?>> R registry(Class<R> registryClass, Supplier<R> pu
private static final Set<SkriptAddon> 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;
}
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}

Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/skriptlang/skript/addon/AddonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/skriptlang/skript/addon/SkriptAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SkriptAddon> {

/**
* @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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 036d0b5

Please sign in to comment.