Add InstanceWorldPostUnloadEvent (#705)

This commit is contained in:
Daniel Saukel 2020-10-31 17:01:45 +01:00
parent ab514e4938
commit b28423fa58
6 changed files with 117 additions and 17 deletions

View File

@ -23,7 +23,7 @@ import org.bukkit.event.HandlerList;
* Fired when a group finishs a {@link Dungeon}, which means the end floor of a dungeon.
* <p>
* Do not confuse this with {@link de.erethon.dungeonsxl.api.event.player.GamePlayerFinishEvent}. GamePlayerFinishEvent is fired when a player triggers an end
* sign, while GroupFinishDungeonEvent is triggered when all group members have triggered the ready sign and the game actually ends.
* sign, while GroupFinishDungeonEvent is triggered when all group members have triggered the end sign and the game actually ends.
*
* @author Daniel Saukel
*/

View File

@ -0,0 +1,62 @@
/*
* Copyright (C) 2014-2020 Daniel Saukel
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.event.world;
import de.erethon.dungeonsxl.api.world.ResourceWorld;
import org.bukkit.event.HandlerList;
/**
* Fired after an instance world is unloaded.
*
* @author Daniel Saukel
*/
public class InstanceWorldPostUnloadEvent extends ResourceWorldEvent {
private static final HandlerList handlers = new HandlerList();
private String instanceWorldName;
public InstanceWorldPostUnloadEvent(ResourceWorld resource, String instanceWorldName) {
super(resource);
this.instanceWorldName = instanceWorldName;
}
/**
* Returns the name the instance world had.
*
* @return the name the instance world had
*/
public String getInstanceWorldName() {
return instanceWorldName;
}
/**
* Returns if the unloaded instance was an edit world.
*
* @return if the unloaded instance was an edit world
*/
public boolean wasEditInstance() {
return instanceWorldName.startsWith("DXL_Edit_");
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2014-2020 Daniel Saukel
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.event.world;
import de.erethon.dungeonsxl.api.world.ResourceWorld;
import org.bukkit.event.Event;
/**
* Superclass for events involving DungeonsXL resource worlds.
*
* @author Daniel Saukel
*/
public abstract class ResourceWorldEvent extends Event {
protected ResourceWorld resource;
protected ResourceWorldEvent(ResourceWorld resource) {
this.resource = resource;
}
/**
* Returns the resource world involved in this event.
*
* @return the resource world involved in this event.
*/
public ResourceWorld getResource() {
return resource;
}
}

View File

@ -17,7 +17,6 @@ package de.erethon.dungeonsxl.api.event.world;
import de.erethon.dungeonsxl.api.dungeon.Dungeon;
import de.erethon.dungeonsxl.api.world.ResourceWorld;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
@ -25,28 +24,18 @@ import org.bukkit.event.HandlerList;
*
* @author Daniel Saukel
*/
public class ResourceWorldInstantiateEvent extends Event implements Cancellable {
public class ResourceWorldInstantiateEvent extends ResourceWorldEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private ResourceWorld resourceWorld;
private Dungeon dungeon;
public ResourceWorldInstantiateEvent(ResourceWorld resourceWorld, Dungeon dungeon) {
this.resourceWorld = resourceWorld;
public ResourceWorldInstantiateEvent(ResourceWorld resource, Dungeon dungeon) {
super(resource);
this.dungeon = dungeon;
}
/**
* Returns the resource world that is to be instantiated.
*
* @return the resource world that is to be instantiated
*/
public ResourceWorld getResourceWorld() {
return resourceWorld;
}
/**
* Returns the dungeon as a part of which the instance is loaded.
*

View File

@ -22,6 +22,7 @@ import de.erethon.commons.misc.ProgressBar;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.event.world.EditWorldSaveEvent;
import de.erethon.dungeonsxl.api.event.world.EditWorldUnloadEvent;
import de.erethon.dungeonsxl.api.event.world.InstanceWorldPostUnloadEvent;
import de.erethon.dungeonsxl.api.world.EditWorld;
import de.erethon.dungeonsxl.mob.CitizensMobProvider;
import de.erethon.dungeonsxl.player.DEditPlayer;
@ -165,6 +166,7 @@ public class DEditWorld extends DInstanceWorld implements EditWorld {
kickAllPlayers();
String name = getWorld().getName();
if (save) {
getResource().getSignData().serializeSigns(signs.values());
Bukkit.unloadWorld(getWorld(), true);
@ -175,8 +177,9 @@ public class DEditWorld extends DInstanceWorld implements EditWorld {
FileUtil.copyDir(getFolder(), getResource().getFolder(), DungeonsXL.EXCLUDED_FILES);
DResourceWorld.deleteUnusedFiles(getResource().getFolder());
FileUtil.removeDir(getFolder());
Bukkit.getPluginManager().callEvent(new InstanceWorldPostUnloadEvent(getResource(), name));
}
}.runTaskLaterAsynchronously(plugin, plugin.getMainConfig().getEditInstanceRemovalDelay() * 20L);
}.runTaskLater(plugin, plugin.getMainConfig().getEditInstanceRemovalDelay() * 20L);
}
if (!save) {
Bukkit.unloadWorld(getWorld(), /* SPIGOT-5225 */ !Version.isAtLeast(Version.MC1_14_4));
@ -184,8 +187,9 @@ public class DEditWorld extends DInstanceWorld implements EditWorld {
@Override
public void run() {
FileUtil.removeDir(getFolder());
Bukkit.getPluginManager().callEvent(new InstanceWorldPostUnloadEvent(getResource(), name));
}
}.runTaskLaterAsynchronously(plugin, plugin.getMainConfig().getEditInstanceRemovalDelay() * 20L);
}.runTaskLater(plugin, plugin.getMainConfig().getEditInstanceRemovalDelay() * 20L);
}
getResource().editWorld = null;

View File

@ -29,6 +29,7 @@ import de.erethon.dungeonsxl.api.dungeon.Game;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.event.world.GameWorldStartGameEvent;
import de.erethon.dungeonsxl.api.event.world.InstanceWorldPostUnloadEvent;
import de.erethon.dungeonsxl.api.event.world.InstanceWorldUnloadEvent;
import de.erethon.dungeonsxl.api.mob.DungeonMob;
import de.erethon.dungeonsxl.api.player.PlayerGroup;
@ -474,9 +475,11 @@ public class DGameWorld extends DInstanceWorld implements GameWorld {
kickAllPlayers();
String name = getWorld().getName();
Bukkit.unloadWorld(getWorld(), /* SPIGOT-5225 */ !Version.isAtLeast(Version.MC1_14_4));
FileUtil.removeDir(getFolder());
plugin.getInstanceCache().remove(this);
Bukkit.getPluginManager().callEvent(new InstanceWorldPostUnloadEvent(getResource(), name));
}
private GameRuleContainer getRules() {