Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ lombokVersion = 1.18.46
# Test Dependencies
slf4jVersion = 2.1.0-alpha1
junitVersion = 6.1.1
mockbukkitVersion = 4.114.0
mockbukkitVersion = 4.116.1
h2DatabaseVersion = 2.4.240
61 changes: 24 additions & 37 deletions src/main/java/fr/openmc/core/ListenersManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,49 @@

import fr.openmc.api.input.ChatInput;
import fr.openmc.api.input.location.ItemInteraction;
import fr.openmc.core.bootstrap.listeners.ListenerFactory;
import fr.openmc.core.features.itemsadder.SpawnerExtractorListener;
import fr.openmc.core.hooks.itemsadder.ItemsAdderHook;
import fr.openmc.core.listeners.*;
import fr.openmc.core.utils.nms.entity.EntityGlowNMS;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

/**
* Centralise l'enregistrement des listeners Bukkit du plugin.
*/
public class ListenersManager {
/**
* Enregistre les listeners de base, puis ceux conditionnels (tests, hooks).
* Enregistre les listeners.
*/
public static void init() {
// () -> : nécessaire si y'a un package d'api externe (ex com.comphenix.protocol)
registerEvents(
new OMCPlayerCacheListener(),
new HappyGhastListener(),
new SessionsListener(),
new JoinQuitMessageListener(),
new ClockInfos(),
new ChronometerListener(),
new ItemInteraction(),
new ChatInput(),
new SleepListener(),
new PlayerDeathListener(),
new AsyncChatListener(OMCPlugin.getInstance()),
new NoMoreRabbit(),
new ArmorListener(),
new EntityGlowNMS(),
new RegionTrackingListener()
OMCPlayerCacheListener::new,
HappyGhastListener::new,
SessionsListener::new,
JoinQuitMessageListener::new,
ClockInfos::new,
ChronometerListener::new,
ItemInteraction::new,
ChatInput::new,
SleepListener::new,
PlayerDeathListener::new,
() -> new AsyncChatListener(OMCPlugin.getInstance()),
NoMoreRabbit::new,
ArmorListener::new,
() -> new EntityGlowNMS(),
() -> new RegionTrackingListener(),
() -> new SpawnerExtractorListener(),
() -> new ItemsAddersListener()
);

if (!OMCPlugin.isUnitTestVersion()) {
registerEvents(
new SpawnerExtractorListener()
);
}

if (ItemsAdderHook.isEnable()) {
registerEvents(new ItemsAddersListener());
}
}

/**
* Enregistre une liste de listeners sur le plugin courant.
*
* @param args Listeners a enregistrer
* @param listeners Listeners a enregistrer
*/
private static void registerEvents(Listener... args) {
Server server = Bukkit.getServer();
JavaPlugin plugin = OMCPlugin.getInstance();
for (Listener listener : args) {
server.getPluginManager().registerEvents(listener, plugin);
public static void registerEvents(ListenerFactory... listeners) {
for (ListenerFactory listenerFactory : listeners) {
listenerFactory.create(true);
}
}
}
16 changes: 6 additions & 10 deletions src/main/java/fr/openmc/core/OMCPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fr.openmc.core.bootstrap.integration.DatabaseManager;
import fr.openmc.core.bootstrap.integration.ErrorReporter;
import fr.openmc.core.bootstrap.integration.OMCLogger;
import fr.openmc.core.bootstrap.listeners.ListenerFactory;
import fr.openmc.core.commands.admin.freeze.FreezeManager;
import fr.openmc.core.commands.utils.SpawnManager;
import fr.openmc.core.features.adminshop.AdminShopManager;
Expand Down Expand Up @@ -67,7 +68,6 @@
import org.bukkit.Particle;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
Expand Down Expand Up @@ -135,7 +135,7 @@ public class OMCPlugin extends JavaPlugin {
ShopManager::new,
HomeIconCacheManager::new,
DimensionOpenerManager::new,
ElevatorManager::new
() -> new ElevatorManager()
));

public static final List<Feature> loadedFeature = new ArrayList<>();
Expand Down Expand Up @@ -269,21 +269,17 @@ public void onDisable() {
*
* @param listeners Listeners à enregistrer
*/
public static void registerEvents(Listener... listeners) {
for (Listener listener : listeners) {
instance.getServer().getPluginManager().registerEvents(listener, instance);
}
public static void registerEvents(ListenerFactory... listeners) {
ListenersManager.registerEvents(listeners);
}

/**
* Enregistre une liste de listeners Bukkit sur l'instance du plugin.
*
* @param listeners Listeners à enregistrer
*/
public static void registerEvents(Collection<Listener> listeners) {
for (Listener listener : listeners) {
instance.getServer().getPluginManager().registerEvents(listener, instance);
}
public static void registerEvents(Collection<ListenerFactory> listeners) {
registerEvents(listeners.toArray(new ListenerFactory[0]));
}

/**
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/fr/openmc/core/OMCRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class OMCRegistry {
RegistryLoadingType.AFTER_IA),
new RegistryContext(
() -> CUSTOM_AMBIENTS = new CustomAmbientRegistry(),
RegistryLoadingType.BOOTSTRAP, RegistryLoadingType.RUNTIME),
RegistryLoadingType.BOOTSTRAP, RegistryLoadingType.RUNTIME, RegistryLoadingType.NOT_LOADED_UNIT_TEST),
new RegistryContext(
() -> CUSTOM_LOOTBOXES = new CustomLootboxRegistry(),
RegistryLoadingType.AFTER_IA),
Expand All @@ -54,8 +54,8 @@ private OMCRegistry() {}

public static void bootstrapAll(BootstrapContext context) {
for (RegistryContext ctx : OMCRegistry.ALL) {
if (Arrays.stream(ctx.loadingTypes())
.noneMatch(t -> t == RegistryLoadingType.BOOTSTRAP)) continue;
if (isTyped(ctx, RegistryLoadingType.NOT_LOADED_UNIT_TEST) && OMCPlugin.isUnitTestVersion()) continue;
if (isNotTyped(ctx, RegistryLoadingType.BOOTSTRAP)) continue;

LifecycleRegistry r = load(ctx);
try {
Expand All @@ -70,8 +70,8 @@ public static void bootstrapAll(BootstrapContext context) {

public static void initAll() {
for (RegistryContext ctx : OMCRegistry.ALL) {
if (Arrays.stream(ctx.loadingTypes())
.noneMatch(t -> t == RegistryLoadingType.RUNTIME)) continue;
if (isTyped(ctx, RegistryLoadingType.NOT_LOADED_UNIT_TEST) && OMCPlugin.isUnitTestVersion()) continue;
if (isNotTyped(ctx, RegistryLoadingType.RUNTIME)) continue;

LifecycleRegistry r = load(ctx);

Expand All @@ -85,8 +85,8 @@ public static void initAll() {

public static void postInitAll() {
for (RegistryContext ctx : OMCRegistry.ALL) {
if (Arrays.stream(ctx.loadingTypes())
.noneMatch(t -> t == RegistryLoadingType.AFTER_IA)) continue;
if (isTyped(ctx, RegistryLoadingType.NOT_LOADED_UNIT_TEST) && OMCPlugin.isUnitTestVersion()) continue;
if (isNotTyped(ctx, RegistryLoadingType.AFTER_IA)) continue;

LifecycleRegistry r = load(ctx);

Expand All @@ -111,4 +111,12 @@ private static LifecycleRegistry load(RegistryContext ctx) {
LOADED.add(registry);
return registry;
}

private static boolean isNotTyped(RegistryContext ctx, RegistryLoadingType type) {
return Arrays.stream(ctx.loadingTypes()).noneMatch(t -> t == type);
}

private static boolean isTyped(RegistryContext ctx, RegistryLoadingType type) {
return Arrays.stream(ctx.loadingTypes()).anyMatch(t -> t == type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.openmc.core.bootstrap.features;

public class DisableFeatureException extends RuntimeException {
public DisableFeatureException(String message) {
super(message);
}
public DisableFeatureException(String message, Throwable cause) {
super(message, cause);
}
}
23 changes: 8 additions & 15 deletions src/main/java/fr/openmc/core/bootstrap/features/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fr.openmc.core.OMCPlugin;
import fr.openmc.core.bootstrap.features.types.*;
import fr.openmc.core.bootstrap.integration.OMCLogger;
import org.bukkit.event.Listener;
import fr.openmc.core.bootstrap.listeners.ListenerFactory;

import java.sql.SQLException;

Expand All @@ -21,7 +21,7 @@ public abstract class Feature {
*/
public final void startInit() {
// Condition d'initialisation (si feature ne doit pas être lancée dans les tests ou qu'elle nécessite un hook)
if (this instanceof NotInUnitTest && OMCPlugin.isUnitTestVersion()) {
if (this instanceof NotLoadInUnitTest && OMCPlugin.isUnitTestVersion()) {
OMCLogger.errorFormatted("Feature " + this.getClass().getSimpleName() + " non initialisée dans les Unit Tests");
return;
}
Expand All @@ -35,17 +35,7 @@ public final void startInit() {

// Enregistre les listeners
if (this instanceof HasListeners hasListeners) {
for (Listener listener : hasListeners.getListeners()) {
if (this instanceof NotInUnitTest && OMCPlugin.isUnitTestVersion()) {
OMCLogger.errorFormatted("Listener " + listener.getClass().getSimpleName() + " de Feature " + this.getClass().getSimpleName() + " non chargée dans les Unit Tests");
continue;
}

if (this instanceof LoadIfEnable<?> loadIfEnable && !loadIfEnable.shouldLoad()) {
OMCLogger.errorFormatted("Listener " + listener.getClass().getSimpleName() + " de Feature " + this.getClass().getSimpleName() + " non initialisée car le hook associé n'est pas activé");
continue;
}

for (ListenerFactory listener : hasListeners.getListeners()) {
OMCPlugin.registerEvents(listener);
}
}
Expand All @@ -58,6 +48,9 @@ public final void startInit() {

initialize = true;
OMCLogger.successFormatted("Feature " + this.getClass().getSimpleName() + " initialisée correctement.");
} catch (DisableFeatureException e) {
OMCLogger.errorFormatted("Feature " + this.getClass().getSimpleName() + " non initialisée.");
OMCLogger.error(e.getMessage(), e);
} catch (Exception e) {
initialize = false;
OMCLogger.errorFormatted("Feature " + this.getClass().getSimpleName() + " non initialisée.");
Expand All @@ -74,7 +67,7 @@ public final void startInit() {
* @throws SQLException Si l'initialisation DB échoue
*/
public final void startDB(ConnectionSource connectionSource) throws SQLException {
if (this instanceof NotInUnitTest && OMCPlugin.isUnitTestVersion()) return;
if (this instanceof NotLoadInUnitTest && OMCPlugin.isUnitTestVersion()) return;
if (this instanceof HasDatabase dbF) {
dbF.initDB(connectionSource);
}
Expand All @@ -85,7 +78,7 @@ public final void startDB(ConnectionSource connectionSource) throws SQLException
*/
public final void startSave() {
if (!initialize) return;
if (this instanceof NotInUnitTest && OMCPlugin.isUnitTestVersion()) return;
if (this instanceof NotLoadInUnitTest && OMCPlugin.isUnitTestVersion()) return;

try {
save();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.openmc.core.bootstrap.features.types;

import org.bukkit.event.Listener;
import fr.openmc.core.bootstrap.listeners.ListenerFactory;

import java.util.Set;

Expand All @@ -11,6 +11,8 @@
public interface HasListeners {
/**
* Listeners à initialiser
* () -> : nécessaire si y'a un package d'api externe (ex com.comphenix.protocol)
*
*/
Set<Listener> getListeners();
Set<ListenerFactory> getListeners();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/**
* Charge uniquement la feature ou le listener hors des tests unitaires
*/
public interface NotInUnitTest {}
public interface NotLoadInUnitTest {}

4 changes: 2 additions & 2 deletions src/main/java/fr/openmc/core/bootstrap/hooks/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.j256.ormlite.support.ConnectionSource;
import fr.openmc.core.OMCPlugin;
import fr.openmc.core.bootstrap.features.types.HasDatabase;
import fr.openmc.core.bootstrap.features.types.NotInUnitTest;
import fr.openmc.core.bootstrap.features.types.NotLoadInUnitTest;
import fr.openmc.core.bootstrap.integration.OMCLogger;
import org.bukkit.plugin.PluginManager;

Expand Down Expand Up @@ -85,7 +85,7 @@ public void startSave() {
* @throws SQLException Si l'initialisation DB échoue
*/
public final void startDB(ConnectionSource connectionSource) throws SQLException {
if (this instanceof NotInUnitTest && OMCPlugin.isUnitTestVersion()) return;
if (this instanceof NotLoadInUnitTest && OMCPlugin.isUnitTestVersion()) return;
if (this instanceof HasDatabase dbHook) {
dbHook.initDB(connectionSource);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.openmc.core.bootstrap.listeners;

import fr.openmc.core.OMCPlugin;
import fr.openmc.core.bootstrap.integration.OMCLogger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

@FunctionalInterface
public interface ListenerFactory {
Comment thread
iambibi marked this conversation as resolved.
Listener create() throws NoClassDefFoundError;

default Listener create(boolean register) {
Server server = Bukkit.getServer();
JavaPlugin plugin = OMCPlugin.getInstance();
Listener listener = null;

try {
listener = this.create();
if (listener != null && register) {
server.getPluginManager().registerEvents(listener, plugin);
}
} catch (NoClassDefFoundError e) {
if (listener == null) return null;
OMCLogger.error("Erreur lors de l'enregistrement du listener " + listener.getClass().getSimpleName());
OMCLogger.error(e.getMessage());
}
return listener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* - BOOTSTRAP, registre a charger avant le chargement du monde et des datapacks
* - RUNTIME, registre a charger pendant le chargement du serveur
* - AFTER_IA, registre à charger après l'initialisation complete d'ItemsAdder {@link dev.lone.itemsadder.api.Events.ItemsAdderLoadDataEvent}
* - NOT_LOADED_UNIT_TEST, registre non chargé pendant les tests unitaires
*/
public enum RegistryLoadingType {
BOOTSTRAP,
RUNTIME,
AFTER_IA
AFTER_IA,
NOT_LOADED_UNIT_TEST
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.openmc.core.bootstrap.features.Feature;
import fr.openmc.core.bootstrap.features.types.HasCommands;
import fr.openmc.core.bootstrap.features.types.HasListeners;
import fr.openmc.core.bootstrap.listeners.ListenerFactory;
import fr.openmc.core.utils.text.messages.MessageType;
import fr.openmc.core.utils.text.messages.MessagesManager;
import fr.openmc.core.utils.text.messages.Prefix;
Expand All @@ -12,7 +13,6 @@
import net.kyori.adventure.title.TitlePart;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashSet;
Expand All @@ -31,10 +31,8 @@ public Set<Object> getCommands() {
}

@Override
public Set<Listener> getListeners() {
return Set.of(
new FreezeListener()
);
public Set<ListenerFactory> getListeners() {
return Set.of(FreezeListener::new);
}

/**
Expand Down
Loading
Loading