Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Module System #4573

Closed
Closed
Show file tree
Hide file tree
Changes from 8 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
Expand Up @@ -65,13 +65,8 @@ protected boolean init() {
supportsUUIDs = Skript.methodExists(DefaultDomain.class, "getUniqueIds");

// Manually load syntaxes for regions, because we're in module package
try {
Skript.getAddonInstance().loadClasses("ch.njol.skript.hooks.regions");
} catch (IOException e) {
Skript.exception(e);
return false;
}
return super.init();
Skript.getAddonInstance().loadClasses("ch.njol.skript.hooks.regions");
return super.init();
}

@Override
Expand Down
59 changes: 20 additions & 39 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -503,14 +504,9 @@ public void onEnable() {
new DefaultFunctions();

ChatMessages.registerListeners();

try {
getAddonInstance().loadClasses("ch.njol.skript", "conditions", "effects", "events", "expressions", "entity", "sections");
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
setEnabled(false);
return;
}

getAddonInstance().loadClasses("ch.njol.skript", "conditions", "effects", "events", "expressions", "entity", "sections")
.loadModules("org.skriptlang.skript");

Commands.registerListeners();

Expand All @@ -519,48 +515,29 @@ public void onEnable() {

final long tick = testing() ? Bukkit.getWorlds().get(0).getFullTime() : 0;
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
@SuppressWarnings("synthetic-access")
public void run() {
assert Bukkit.getWorlds().get(0).getFullTime() == tick;
Copy link
Member

Choose a reason for hiding this comment

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

What is this actually for? It's a test-only action but I can't see a comment explaining it.


// Load hooks from Skript jar
try {
try (JarFile jar = new JarFile(getFile())) {
for (final JarEntry e : new EnumerationIterable<>(jar.entries())) {
if (e.getName().startsWith("ch/njol/skript/hooks/") && e.getName().endsWith("Hook.class") && StringUtils.count("" + e.getName(), '/') <= 5) {
final String c = e.getName().replace('/', '.').substring(0, e.getName().length() - ".class".length());
try {
final Class<?> hook = Class.forName(c, true, getClassLoader());
if (hook != null && Hook.class.isAssignableFrom(hook) && !hook.isInterface() && Hook.class != hook && isHookEnabled((Class<? extends Hook<?>>) hook)) {
hook.getDeclaredConstructor().setAccessible(true);
hook.getDeclaredConstructor().newInstance();
}
} catch (final ClassNotFoundException ex) {
Skript.exception(ex, "Cannot load class " + c);
} catch (final ExceptionInInitializerError err) {
Skript.exception(err.getCause(), "Class " + c + " generated an exception while loading");
}
}
getAddonInstance().loadClasses(c -> {
if (Hook.class.isAssignableFrom(c) && !c.isInterface() && !Modifier.isAbstract(c.getModifiers())) {
try {
c.getDeclaredConstructor().newInstance();
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to move away from using newInstance here - it's the slowest and least-reliable way of creating instances.

} catch (Exception ex) {
Skript.exception(ex, "Failed to load hook class " + c);
}
}
} catch (final Exception e) {
error("Error while loading plugin hooks" + (e.getLocalizedMessage() == null ? "" : ": " + e.getLocalizedMessage()));
Skript.exception(e);
}
}, false, "ch.njol.skript.hooks", false);
finishedLoadingHooks = true;

if (TestMode.ENABLED) {
info("Preparing Skript for testing...");
tainted = true;
try {
getAddonInstance().loadClasses("ch.njol.skript", "tests");
} catch (IOException e) {
Skript.exception("Failed to load testing environment.");
Bukkit.getServer().shutdown();
}
getAddonInstance().loadClasses("ch.njol.skript", "tests");
}

stopAcceptingRegistrations();


Expand Down Expand Up @@ -1224,10 +1201,14 @@ public static void checkAcceptRegistrations() {

private static void stopAcceptingRegistrations() {
acceptRegistrations = false;

Converters.createMissingConverters();

Classes.onRegistrationsStop();

// Clear each cache
getAddonInstance().resetEntryCache(); // The SkriptAddon representing Skript is not part of the addons list
getAddons().forEach(SkriptAddon::resetEntryCache);
}

// ================ ADDONS ================
Expand Down
Loading