Skip to content

Commit

Permalink
fix: reload
Browse files Browse the repository at this point in the history
  • Loading branch information
ProdPreva1l committed Mar 10, 2025
1 parent ed26ed2 commit 5b9e59e
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions src/main/java/info/preva1l/hooker/Hooker.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,51 @@ private void onDisable(PluginDisableEvent event) {

private void reloadHooks() {
for (Object hook : loadedHooks.values()) {
Method tempMethod = null;
Method tempMethod2 = null;
Reloadable reloadable = hook.getClass().getAnnotation(Reloadable.class);
if (reloadable == null) continue;

for (Method method : hook.getClass().getDeclaredMethods()) {
if (!method.isAnnotationPresent(OnStop.class)) continue;
Reloadable reloadable = hook.getClass().getAnnotation(Reloadable.class);
if (reloadable == null) continue;
method.setAccessible(true);
if (reloadable.async()) {
MultiLib.getAsyncScheduler().runNow(owningPlugin, t -> {
try {
method.invoke(hook);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
if (method.isAnnotationPresent(OnStart.class)) {
tempMethod = method;
}
if (method.isAnnotationPresent(OnStop.class)) {
tempMethod2 = method;
}
}

assert tempMethod != null;
Method startMethod = tempMethod;
startMethod.setAccessible(true);

Method stopMethod = tempMethod2;
if (stopMethod != null) {
stopMethod.setAccessible(true);
}

if (reloadable.async()) {
MultiLib.getAsyncScheduler().runNow(owningPlugin, t -> {
try {
if (stopMethod != null) {
stopMethod.invoke(hook);
}
});
} else {
MultiLib.getGlobalRegionScheduler().run(owningPlugin, t -> {
try {
method.invoke(hook);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
startMethod.invoke(hook);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
} else {
MultiLib.getGlobalRegionScheduler().run(owningPlugin, t -> {
try {
if (stopMethod != null) {
stopMethod.invoke(hook);
}
});
}
break;
startMethod.invoke(hook);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
}
loadedHooks.remove(hook.getClass());
}
Expand Down

0 comments on commit 5b9e59e

Please sign in to comment.