Skip to content

Commit

Permalink
Add a method to disable a hook's registration (#4093)
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Jun 24, 2021
1 parent f812834 commit 45f58fd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,33 @@ private static boolean checkServerPlatform() {
// If nothing got triggered, everything is probably ok
return true;
}

private static final Set<Class<? extends Hook<?>>> disabledHookRegistrations = new HashSet<>();
private static boolean finishedLoadingHooks = false;

/**
* Checks whether a hook has been enabled.
* @param hook The hook to check.
* @return Whether the hook is enabled.
* @see #disableHookRegistration(Class[])
*/
public static boolean isHookEnabled(Class<? extends Hook<?>> hook) {
return !disabledHookRegistrations.contains(hook);
}

/**
* Disables the registration for the given hook classes. If Skript has been enabled, this method
* will throw an API exception. It should be used in something like {@link JavaPlugin#onLoad()}.
* @param hooks The hooks to disable the registration of.
* @see #isHookEnabled(Class)
*/
@SafeVarargs
public static void disableHookRegistration(Class<? extends Hook<?>>... hooks) {
if (finishedLoadingHooks) { // Hooks have been registered if Skript is enabled
throw new SkriptAPIException("Disabling hooks is not possible after Skript has been enabled!");
}
Collections.addAll(disabledHookRegistrations, hooks);
}

@Override
public void onEnable() {
Expand Down Expand Up @@ -468,7 +495,7 @@ public void run() {
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) {
if (hook != null && Hook.class.isAssignableFrom(hook) && !hook.isInterface() && Hook.class != hook && isHookEnabled((Class<? extends Hook<?>>) hook)) {
hook.getDeclaredConstructor().setAccessible(true);
hook.getDeclaredConstructor().newInstance();
}
Expand All @@ -477,14 +504,14 @@ public void run() {
} catch (final ExceptionInInitializerError err) {
Skript.exception(err.getCause(), "Class " + c + " generated an exception while loading");
}
continue;
}
}
}
} catch (final Exception e) {
error("Error while loading plugin hooks" + (e.getLocalizedMessage() == null ? "" : ": " + e.getLocalizedMessage()));
Skript.exception(e);
}
finishedLoadingHooks = true;

Language.setUseLocal(false);

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/ch/njol/skript/SkriptConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
import java.util.Collection;
import java.util.Locale;

import ch.njol.skript.hooks.VaultHook;
import ch.njol.skript.hooks.regions.GriefPreventionHook;
import ch.njol.skript.hooks.regions.PreciousStonesHook;
import ch.njol.skript.hooks.regions.ResidenceHook;
import ch.njol.skript.hooks.regions.WorldGuardHook;
import org.bukkit.event.EventPriority;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -332,6 +337,42 @@ public void set(Boolean t) {

});

public final static Option<Boolean> disableHookVault = new Option<>("disable hooks.vault", false)
.optional(true)
.setter(value -> {
if (value) {
Skript.disableHookRegistration(VaultHook.class);
}
});
public final static Option<Boolean> disableHookGriefPrevention = new Option<>("disable hooks.regions.grief prevention", false)
.optional(true)
.setter(value -> {
if (value) {
Skript.disableHookRegistration(GriefPreventionHook.class);
}
});
public final static Option<Boolean> disableHookPreciousStones = new Option<>("disable hooks.regions.precious stones", false)
.optional(true)
.setter(value -> {
if (value) {
Skript.disableHookRegistration(PreciousStonesHook.class);
}
});
public final static Option<Boolean> disableHookResidence = new Option<>("disable hooks.regions.residence", false)
.optional(true)
.setter(value -> {
if (value) {
Skript.disableHookRegistration(ResidenceHook.class);
}
});
public final static Option<Boolean> disableHookWorldGuard = new Option<>("disable hooks.regions.worldguard", false)
.optional(true)
.setter(value -> {
if (value) {
Skript.disableHookRegistration(WorldGuardHook.class);
}
});

/**
* This should only be used in special cases
*/
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/config.sk
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ script loader thread size: 0
# Do note that though, this option may cause issues with addons and possibly some scripts! Do NOT enable this option unless you have really long
# script load times AND you take the risk of lost data and full responsibility!

disable hooks:
vault: false
regions:
grief prevention: false
precious stones: false
residence: false
worldguard: false
# Controls whether Skript should attempt to hook into the plugins listed above.
# If you change a setting here while the server is running, a restart is required for that change to take effect.
# Do note that some addons may stop Skript from hooking into a plugin, even if the plugin's hook is not disabled here.

# ==== Variables ====

databases:
Expand Down

0 comments on commit 45f58fd

Please # to comment.