Implement MVWorldDeleteEvent event

This commit is contained in:
Ben Woo 2024-11-24 11:21:01 +08:00
parent 07ed9fbc48
commit e93e2aa5f4
3 changed files with 23 additions and 21 deletions

View File

@ -4,8 +4,8 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; 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. * 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 boolean cancelled = false;
private final LoadedMultiverseWorld world; private final LoadedMultiverseWorld world;
private final boolean removeFromConfig;
public MVWorldDeleteEvent(LoadedMultiverseWorld world, boolean removeFromConfig) { public MVWorldDeleteEvent(@NotNull LoadedMultiverseWorld world) {
if (world == null) {
throw new IllegalArgumentException("world can't be null!");
}
this.world = world; this.world = world;
this.removeFromConfig = removeFromConfig;
} }
private static final HandlerList HANDLERS = new HandlerList(); 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. * Gets the world that's about to be deleted.
* *
* @return That {@link MultiverseWorld}. * @return That {@link LoadedMultiverseWorld}.
*/ */
public LoadedMultiverseWorld getWorld() { public LoadedMultiverseWorld getWorld() {
return world; return world;
} }
/**
* Is the world about to be removed from the config?
*
* @return True if yes, false if no.
*/
public boolean removeWorldFromConfig() {
return removeFromConfig;
}
} }

View File

@ -20,13 +20,14 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.api.BlockSafety; import org.mvplugins.multiverse.core.api.BlockSafety;
import org.mvplugins.multiverse.core.api.LocationManipulation; 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.message.MessageReplacement;
import org.mvplugins.multiverse.core.utils.result.Attempt; import org.mvplugins.multiverse.core.utils.result.Attempt;
import org.mvplugins.multiverse.core.utils.result.FailureReason; import org.mvplugins.multiverse.core.utils.result.FailureReason;
@ -75,6 +76,7 @@ public class WorldManager {
private final FilesManipulator filesManipulator; private final FilesManipulator filesManipulator;
private final BlockSafety blockSafety; private final BlockSafety blockSafety;
private final LocationManipulation locationManipulation; private final LocationManipulation locationManipulation;
private final PluginManager pluginManager;
@Inject @Inject
WorldManager( WorldManager(
@ -84,7 +86,9 @@ public class WorldManager {
@NotNull PlayerWorldTeleporter playerWorldActions, @NotNull PlayerWorldTeleporter playerWorldActions,
@NotNull FilesManipulator filesManipulator, @NotNull FilesManipulator filesManipulator,
@NotNull BlockSafety blockSafety, @NotNull BlockSafety blockSafety,
@NotNull LocationManipulation locationManipulation) { @NotNull LocationManipulation locationManipulation,
@NotNull PluginManager pluginManager) {
this.pluginManager = pluginManager;
this.worldsMap = new HashMap<>(); this.worldsMap = new HashMap<>();
this.loadedWorldsMap = new HashMap<>(); this.loadedWorldsMap = new HashMap<>();
this.unloadTracker = new ArrayList<>(); this.unloadTracker = new ArrayList<>();
@ -476,6 +480,13 @@ public class WorldManager {
AtomicReference<File> worldFolder = new AtomicReference<>(); AtomicReference<File> worldFolder = new AtomicReference<>();
return validateWorldToDelete(world) return validateWorldToDelete(world)
.peek(worldFolder::set) .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(() -> removeWorld(world).transform(DeleteFailureReason.REMOVE_FAILED))
.mapAttempt(() -> filesManipulator.deleteFolder(worldFolder.get()).fold( .mapAttempt(() -> filesManipulator.deleteFolder(worldFolder.get()).fold(
exception -> worldActionResult(DeleteFailureReason.FAILED_TO_DELETE_FOLDER, exception -> worldActionResult(DeleteFailureReason.FAILED_TO_DELETE_FOLDER,

View File

@ -3,6 +3,7 @@ package org.mvplugins.multiverse.core.world.reasons;
import co.aikar.locales.MessageKey; import co.aikar.locales.MessageKey;
import co.aikar.locales.MessageKeyProvider; 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.MVCorei18n;
import org.mvplugins.multiverse.core.utils.result.FailureReason; import org.mvplugins.multiverse.core.utils.result.FailureReason;
@ -33,7 +34,12 @@ public enum DeleteFailureReason implements FailureReason {
/** /**
* The world folder could not be deleted. * 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; private final MessageKeyProvider message;