mirror of
https://github.com/HibiscusMC/HibiscusCommons.git
synced 2025-12-19 15:09:26 +00:00
feat: improve activating AllHooksActive event to better handle late loading
This commit is contained in:
@@ -13,10 +13,12 @@ public abstract class Hook implements Listener {
|
||||
|
||||
private final String id;
|
||||
|
||||
private boolean detected = false;
|
||||
private boolean active = false;
|
||||
|
||||
private boolean itemHook = false;
|
||||
private boolean entityHook = false;
|
||||
private boolean lateLoadHook = false;
|
||||
|
||||
public Hook(@NotNull String id, HookFlag... flags) {
|
||||
this.id = id;
|
||||
@@ -28,6 +30,9 @@ public abstract class Hook implements Listener {
|
||||
case ENTITY_SUPPORT:
|
||||
setEnabledEntityHook(true);
|
||||
break;
|
||||
case LATE_LOAD:
|
||||
setEnabledLateLoadHook(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,9 +97,15 @@ public abstract class Hook implements Listener {
|
||||
public final void setActive(boolean active) {
|
||||
this.active = active;
|
||||
|
||||
if (active) {
|
||||
Hooks.checkHookLoadingStatus();
|
||||
}
|
||||
if (hasEnabledLateLoadHook()) Hooks.checkHookLoadingStatus();
|
||||
}
|
||||
|
||||
public final boolean isDetected() {
|
||||
return this.detected;
|
||||
}
|
||||
|
||||
public final void setDetected(boolean detected) {
|
||||
this.detected = detected;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +122,10 @@ public abstract class Hook implements Listener {
|
||||
return entityHook;
|
||||
}
|
||||
|
||||
public final boolean hasEnabledLateLoadHook() {
|
||||
return lateLoadHook;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the method {@link #getItem(String)} should return a non-null value
|
||||
*
|
||||
@@ -124,6 +139,10 @@ public abstract class Hook implements Listener {
|
||||
entityHook = enabled;
|
||||
}
|
||||
|
||||
public final void setEnabledLateLoadHook(boolean enabled) {
|
||||
lateLoadHook = enabled;
|
||||
}
|
||||
|
||||
public String getItemString(@NotNull ItemStack itemStack) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@ package me.lojosho.hibiscuscommons.hooks;
|
||||
public enum HookFlag {
|
||||
ITEM_SUPPORT,
|
||||
ENTITY_SUPPORT,
|
||||
LATE_LOAD
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import me.lojosho.hibiscuscommons.api.events.HibiscusHooksAllActiveEvent;
|
||||
import me.lojosho.hibiscuscommons.hooks.items.*;
|
||||
import me.lojosho.hibiscuscommons.hooks.misc.*;
|
||||
import me.lojosho.hibiscuscommons.hooks.placeholders.HookPlaceholderAPI;
|
||||
import me.lojosho.hibiscuscommons.util.MessagesUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@@ -16,12 +17,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Hooks {
|
||||
|
||||
private static final HashMap<String, Hook> HOOK_POOL = new HashMap<>();
|
||||
|
||||
private static final HookNexo NEXO_HOOK = new HookNexo();
|
||||
private static final HookOraxen ORAXEN_HOOK = new HookOraxen();
|
||||
private static final HookItemAdder ITEMADDER_HOOK = new HookItemAdder();
|
||||
@@ -40,7 +41,6 @@ public class Hooks {
|
||||
private static final HookCustomFishing CF_HOOK = new HookCustomFishing();
|
||||
private static final HookGSit GSIT_HOOK = new HookGSit();
|
||||
|
||||
private static final Map<String, Hook> HOOKED = new HashMap<>();
|
||||
private static boolean allHooksActive = false;
|
||||
|
||||
public static Hook getHook(@NotNull String id) {
|
||||
@@ -72,10 +72,9 @@ public class Hooks {
|
||||
for (Hook hook : HOOK_POOL.values()) {
|
||||
if (Bukkit.getPluginManager().getPlugin(hook.getId()) != null) {
|
||||
HibiscusCommonsPlugin.getInstance().getServer().getPluginManager().registerEvents(hook, HibiscusCommonsPlugin.getInstance());
|
||||
hook.setDetected(true);
|
||||
hook.load();
|
||||
|
||||
HOOKED.put(hook.getId(), hook);
|
||||
|
||||
HibiscusCommonsPlugin.getInstance().getLogger().info("Successfully hooked into " + hook.getId());
|
||||
}
|
||||
}
|
||||
@@ -87,16 +86,26 @@ public class Hooks {
|
||||
* This is an operation that occurs only once to allow plugins
|
||||
* load their stuff successfully when all hooks are active.
|
||||
*/
|
||||
static void checkHookLoadingStatus() {
|
||||
public static void checkHookLoadingStatus() {
|
||||
if (allHooksActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!HOOKED.values().stream()
|
||||
.allMatch(Hook::isActive)) {
|
||||
List<Hook> lateLoadHooks = HOOK_POOL.values().stream().filter(Hook::isDetected).filter(Hook::hasEnabledLateLoadHook).toList();
|
||||
if (lateLoadHooks.isEmpty()) {
|
||||
MessagesUtil.sendDebugMessages("Not awaiting anymore plugins... All hooks are now active.");
|
||||
setAllHooksActive();
|
||||
return;
|
||||
}
|
||||
|
||||
List<Hook> activeLateHooks = lateLoadHooks.stream().filter(Hook::isActive).toList();
|
||||
if (activeLateHooks.size() == lateLoadHooks.size()) {
|
||||
MessagesUtil.sendDebugMessages("Match Hook");
|
||||
setAllHooksActive();
|
||||
}
|
||||
}
|
||||
|
||||
private static void setAllHooksActive() {
|
||||
allHooksActive = true;
|
||||
Bukkit.getPluginManager().callEvent(new HibiscusHooksAllActiveEvent());
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class HookItemAdder extends Hook {
|
||||
|
||||
public HookItemAdder() {
|
||||
super("itemsadder", HookFlag.ITEM_SUPPORT);
|
||||
super("itemsadder", HookFlag.ITEM_SUPPORT, HookFlag.LATE_LOAD);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class HookNexo extends Hook {
|
||||
|
||||
public HookNexo() {
|
||||
super("nexo", HookFlag.ITEM_SUPPORT);
|
||||
super("nexo", HookFlag.ITEM_SUPPORT, HookFlag.LATE_LOAD);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user