From e93e2aa5f4b75ac05fc967c7a3e9b18c7fd7fcda Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 24 Nov 2024 11:21:01 +0800 Subject: [PATCH] Implement MVWorldDeleteEvent event --- .../core/event/MVWorldDeleteEvent.java | 21 +++---------------- .../multiverse/core/world/WorldManager.java | 15 +++++++++++-- .../world/reasons/DeleteFailureReason.java | 8 ++++++- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/event/MVWorldDeleteEvent.java b/src/main/java/org/mvplugins/multiverse/core/event/MVWorldDeleteEvent.java index 0438d84a..56610c47 100644 --- a/src/main/java/org/mvplugins/multiverse/core/event/MVWorldDeleteEvent.java +++ b/src/main/java/org/mvplugins/multiverse/core/event/MVWorldDeleteEvent.java @@ -4,8 +4,8 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; -import org.mvplugins.multiverse.core.world.MultiverseWorld; /** * Called when a world is about to be deleted by Multiverse. @@ -14,14 +14,9 @@ public class MVWorldDeleteEvent extends Event implements Cancellable { private boolean cancelled = false; private final LoadedMultiverseWorld world; - private final boolean removeFromConfig; - public MVWorldDeleteEvent(LoadedMultiverseWorld world, boolean removeFromConfig) { - if (world == null) { - throw new IllegalArgumentException("world can't be null!"); - } + public MVWorldDeleteEvent(@NotNull LoadedMultiverseWorld world) { this.world = world; - this.removeFromConfig = removeFromConfig; } private static final HandlerList HANDLERS = new HandlerList(); @@ -61,19 +56,9 @@ public class MVWorldDeleteEvent extends Event implements Cancellable { /** * Gets the world that's about to be deleted. * - * @return That {@link MultiverseWorld}. + * @return That {@link LoadedMultiverseWorld}. */ public LoadedMultiverseWorld getWorld() { return world; } - - /** - * Is the world about to be removed from the config? - * - * @return True if yes, false if no. - */ - public boolean removeWorldFromConfig() { - return removeFromConfig; - } - } diff --git a/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java b/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java index de522455..b130d074 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/WorldManager.java @@ -20,13 +20,14 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.WorldCreator; import org.bukkit.WorldType; -import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jvnet.hk2.annotations.Service; import org.mvplugins.multiverse.core.api.BlockSafety; import org.mvplugins.multiverse.core.api.LocationManipulation; +import org.mvplugins.multiverse.core.event.MVWorldDeleteEvent; import org.mvplugins.multiverse.core.utils.message.MessageReplacement; import org.mvplugins.multiverse.core.utils.result.Attempt; import org.mvplugins.multiverse.core.utils.result.FailureReason; @@ -75,6 +76,7 @@ public class WorldManager { private final FilesManipulator filesManipulator; private final BlockSafety blockSafety; private final LocationManipulation locationManipulation; + private final PluginManager pluginManager; @Inject WorldManager( @@ -84,7 +86,9 @@ public class WorldManager { @NotNull PlayerWorldTeleporter playerWorldActions, @NotNull FilesManipulator filesManipulator, @NotNull BlockSafety blockSafety, - @NotNull LocationManipulation locationManipulation) { + @NotNull LocationManipulation locationManipulation, + @NotNull PluginManager pluginManager) { + this.pluginManager = pluginManager; this.worldsMap = new HashMap<>(); this.loadedWorldsMap = new HashMap<>(); this.unloadTracker = new ArrayList<>(); @@ -476,6 +480,13 @@ public class WorldManager { AtomicReference worldFolder = new AtomicReference<>(); return validateWorldToDelete(world) .peek(worldFolder::set) + .mapAttempt(() -> { + MVWorldDeleteEvent event = new MVWorldDeleteEvent(world); + pluginManager.callEvent(event); + return event.isCancelled() + ? Attempt.failure(DeleteFailureReason.EVENT_CANCELLED) + : Attempt.success(null); + }) .mapAttempt(() -> removeWorld(world).transform(DeleteFailureReason.REMOVE_FAILED)) .mapAttempt(() -> filesManipulator.deleteFolder(worldFolder.get()).fold( exception -> worldActionResult(DeleteFailureReason.FAILED_TO_DELETE_FOLDER, diff --git a/src/main/java/org/mvplugins/multiverse/core/world/reasons/DeleteFailureReason.java b/src/main/java/org/mvplugins/multiverse/core/world/reasons/DeleteFailureReason.java index 454f57de..31ad2698 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/reasons/DeleteFailureReason.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/reasons/DeleteFailureReason.java @@ -3,6 +3,7 @@ package org.mvplugins.multiverse.core.world.reasons; import co.aikar.locales.MessageKey; import co.aikar.locales.MessageKeyProvider; +import org.mvplugins.multiverse.core.event.MVWorldDeleteEvent; import org.mvplugins.multiverse.core.utils.MVCorei18n; import org.mvplugins.multiverse.core.utils.result.FailureReason; @@ -33,7 +34,12 @@ public enum DeleteFailureReason implements FailureReason { /** * The world folder could not be deleted. */ - FAILED_TO_DELETE_FOLDER(MVCorei18n.DELETEWORLD_FAILEDTODELETEFOLDER); + FAILED_TO_DELETE_FOLDER(MVCorei18n.DELETEWORLD_FAILEDTODELETEFOLDER), + + /** + * The {@link MVWorldDeleteEvent} was cancelled. + */ + EVENT_CANCELLED(MVCorei18n.GENERIC_FAILURE); // todo: messaging private final MessageKeyProvider message;