From af5e69100a0521747a4e17f3a3d4b7a9f066d6ba Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 2 Nov 2020 21:17:19 +0100 Subject: [PATCH] Implement remaining events --- .../api/event/player/EditPlayerEditEvent.java | 14 +------ .../event/world/EditWorldGenerateEvent.java | 16 +------- .../world/ResourceWorldInstantiateEvent.java | 29 ++++++++------- .../dungeonsxl/api/player/EditPlayer.java | 17 +++++++++ .../dungeonsxl/command/EditCommand.java | 5 ++- .../dungeonsxl/command/EscapeCommand.java | 10 ++++- .../dungeonsxl/command/LeaveCommand.java | 37 +++++++++++-------- .../dungeonsxl/player/DEditPlayer.java | 4 +- .../dungeonsxl/player/DGamePlayer.java | 6 +++ .../erethon/dungeonsxl/world/DGameWorld.java | 4 ++ .../dungeonsxl/world/DResourceWorld.java | 8 +++- 11 files changed, 89 insertions(+), 61 deletions(-) diff --git a/api/src/main/java/de/erethon/dungeonsxl/api/event/player/EditPlayerEditEvent.java b/api/src/main/java/de/erethon/dungeonsxl/api/event/player/EditPlayerEditEvent.java index 5382151c..bcc44cb5 100644 --- a/api/src/main/java/de/erethon/dungeonsxl/api/event/player/EditPlayerEditEvent.java +++ b/api/src/main/java/de/erethon/dungeonsxl/api/event/player/EditPlayerEditEvent.java @@ -15,7 +15,6 @@ package de.erethon.dungeonsxl.api.event.player; import de.erethon.dungeonsxl.api.player.EditPlayer; -import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; /** @@ -23,10 +22,9 @@ import org.bukkit.event.HandlerList; * * @author Daniel Saukel */ -public class EditPlayerEditEvent extends EditPlayerEvent implements Cancellable { +public class EditPlayerEditEvent extends EditPlayerEvent { private static final HandlerList handlers = new HandlerList(); - private boolean cancelled; private boolean newlyLoaded; @@ -53,14 +51,4 @@ public class EditPlayerEditEvent extends EditPlayerEvent implements Cancellable return handlers; } - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancelled) { - this.cancelled = cancelled; - } - } diff --git a/api/src/main/java/de/erethon/dungeonsxl/api/event/world/EditWorldGenerateEvent.java b/api/src/main/java/de/erethon/dungeonsxl/api/event/world/EditWorldGenerateEvent.java index cca95269..af99809b 100644 --- a/api/src/main/java/de/erethon/dungeonsxl/api/event/world/EditWorldGenerateEvent.java +++ b/api/src/main/java/de/erethon/dungeonsxl/api/event/world/EditWorldGenerateEvent.java @@ -15,18 +15,16 @@ package de.erethon.dungeonsxl.api.event.world; import de.erethon.dungeonsxl.api.world.EditWorld; -import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; /** - * Fired when a dungeon world is generated. + * Fired after a dungeon world is generated. * * @author Daniel Saukel */ -public class EditWorldGenerateEvent extends EditWorldEvent implements Cancellable { +public class EditWorldGenerateEvent extends EditWorldEvent { private static final HandlerList handlers = new HandlerList(); - private boolean cancelled; public EditWorldGenerateEvent(EditWorld editWorld) { super(editWorld); @@ -41,14 +39,4 @@ public class EditWorldGenerateEvent extends EditWorldEvent implements Cancellabl return handlers; } - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancelled) { - this.cancelled = cancelled; - } - } diff --git a/api/src/main/java/de/erethon/dungeonsxl/api/event/world/ResourceWorldInstantiateEvent.java b/api/src/main/java/de/erethon/dungeonsxl/api/event/world/ResourceWorldInstantiateEvent.java index a0708b0c..abf744e7 100644 --- a/api/src/main/java/de/erethon/dungeonsxl/api/event/world/ResourceWorldInstantiateEvent.java +++ b/api/src/main/java/de/erethon/dungeonsxl/api/event/world/ResourceWorldInstantiateEvent.java @@ -14,7 +14,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.HandlerList; @@ -29,20 +28,11 @@ public class ResourceWorldInstantiateEvent extends ResourceWorldEvent implements private static final HandlerList handlers = new HandlerList(); private boolean cancelled; - private Dungeon dungeon; + private String instanceWorldName; - public ResourceWorldInstantiateEvent(ResourceWorld resource, Dungeon dungeon) { + public ResourceWorldInstantiateEvent(ResourceWorld resource, String instanceWorldName) { super(resource); - this.dungeon = dungeon; - } - - /** - * Returns the dungeon as a part of which the instance is loaded. - * - * @return the dungeon as a part of which the instance is loaded - */ - public Dungeon getDungeon() { - return dungeon; + this.instanceWorldName = instanceWorldName; } /** @@ -51,7 +41,18 @@ public class ResourceWorldInstantiateEvent extends ResourceWorldEvent implements * @return if the loaded instance will be an edit world */ public boolean isEditInstance() { - return dungeon == null; + return instanceWorldName.startsWith("DXL_Edit_"); + } + + /** + * Returns the name the newly loaded Bukkit world is going to have. + *

+ * Note that at this point no Bukkit World object for this world exists. + * + * @return the name the newly loaded Bukkit world is going to have + */ + public String getInstanceWorldName() { + return instanceWorldName; } @Override diff --git a/api/src/main/java/de/erethon/dungeonsxl/api/player/EditPlayer.java b/api/src/main/java/de/erethon/dungeonsxl/api/player/EditPlayer.java index 924f10f1..d4998370 100644 --- a/api/src/main/java/de/erethon/dungeonsxl/api/player/EditPlayer.java +++ b/api/src/main/java/de/erethon/dungeonsxl/api/player/EditPlayer.java @@ -47,6 +47,23 @@ public interface EditPlayer extends InstancePlayer { */ void setCopiedLines(String[] copiedLines); + /** + * Makes the player leave his group and dungeon. + *

+ * This unloads the world if there are no editors left after this player leaves. + */ + @Override + default void leave() { + leave(true); + } + + /** + * Makes the player leave his group and dungeon. + * + * @param unloadIfEmpty whether the world is to be unloaded if, after this player leaves, no editors are left + */ + void leave(boolean unloadIfEmpty); + /** * Makes the player leave the edit world without saving the progress. */ diff --git a/core/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java b/core/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java index b82fdbe4..90350c94 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java +++ b/core/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java @@ -19,6 +19,7 @@ package de.erethon.dungeonsxl.command; import de.erethon.commons.chat.MessageUtil; import de.erethon.commons.config.CommonMessage; import de.erethon.dungeonsxl.DungeonsXL; +import de.erethon.dungeonsxl.api.event.player.EditPlayerEditEvent; import de.erethon.dungeonsxl.api.player.GlobalPlayer; import de.erethon.dungeonsxl.api.player.InstancePlayer; import de.erethon.dungeonsxl.api.player.PlayerGroup; @@ -27,6 +28,7 @@ import de.erethon.dungeonsxl.api.world.ResourceWorld; import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.player.DEditPlayer; import de.erethon.dungeonsxl.player.DPermission; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -59,6 +61,7 @@ public class EditCommand extends DCommand { return; } + boolean newlyLoaded = resource.getEditWorld() == null; EditWorld editWorld = resource.getOrInstantiateEditWorld(false); if (editWorld == null) { MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); @@ -78,7 +81,7 @@ public class EditCommand extends DCommand { return; } - new DEditPlayer(plugin, player, editWorld); + Bukkit.getPluginManager().callEvent(new EditPlayerEditEvent(new DEditPlayer(plugin, player, editWorld), newlyLoaded)); } } diff --git a/core/src/main/java/de/erethon/dungeonsxl/command/EscapeCommand.java b/core/src/main/java/de/erethon/dungeonsxl/command/EscapeCommand.java index 205d2f1c..7704220d 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/command/EscapeCommand.java +++ b/core/src/main/java/de/erethon/dungeonsxl/command/EscapeCommand.java @@ -18,11 +18,13 @@ package de.erethon.dungeonsxl.command; import de.erethon.commons.chat.MessageUtil; import de.erethon.dungeonsxl.DungeonsXL; +import de.erethon.dungeonsxl.api.event.player.EditPlayerLeaveEvent; import de.erethon.dungeonsxl.api.player.EditPlayer; import de.erethon.dungeonsxl.api.player.PlayerGroup; import de.erethon.dungeonsxl.api.world.EditWorld; import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.player.DPermission; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -50,6 +52,12 @@ public class EscapeCommand extends DCommand { MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_DUNGEON.getMessage()); } else if (editPlayer != null) { + EditPlayerLeaveEvent event = new EditPlayerLeaveEvent(editPlayer, true, true); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + editPlayer.escape(); EditWorld editWorld = editPlayer.getEditWorld(); @@ -57,7 +65,7 @@ public class EscapeCommand extends DCommand { return; } - if (editWorld.getWorld().getPlayers().isEmpty()) { + if (editWorld.getWorld().getPlayers().isEmpty() && event.getUnloadIfEmpty()) { editWorld.delete(false); } diff --git a/core/src/main/java/de/erethon/dungeonsxl/command/LeaveCommand.java b/core/src/main/java/de/erethon/dungeonsxl/command/LeaveCommand.java index fd42bca5..26a673ad 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/command/LeaveCommand.java +++ b/core/src/main/java/de/erethon/dungeonsxl/command/LeaveCommand.java @@ -20,11 +20,12 @@ import de.erethon.commons.chat.MessageUtil; import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.api.dungeon.Game; import de.erethon.dungeonsxl.api.event.group.GroupPlayerLeaveEvent; +import de.erethon.dungeonsxl.api.event.player.EditPlayerLeaveEvent; +import de.erethon.dungeonsxl.api.player.EditPlayer; +import de.erethon.dungeonsxl.api.player.GamePlayer; +import de.erethon.dungeonsxl.api.player.GlobalPlayer; import de.erethon.dungeonsxl.api.player.PlayerGroup; import de.erethon.dungeonsxl.config.DMessage; -import de.erethon.dungeonsxl.player.DEditPlayer; -import de.erethon.dungeonsxl.player.DGlobalPlayer; -import de.erethon.dungeonsxl.player.DInstancePlayer; import de.erethon.dungeonsxl.player.DPermission; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -48,7 +49,7 @@ public class LeaveCommand extends DCommand { @Override public void onExecute(String[] args, CommandSender sender) { Player player = (Player) sender; - DGlobalPlayer dPlayer = (DGlobalPlayer) dPlayers.get(player); + GlobalPlayer globalPlayer = dPlayers.get(player); Game game = plugin.getGame(player); if (game != null && game.isTutorial()) { @@ -56,23 +57,29 @@ public class LeaveCommand extends DCommand { return; } - PlayerGroup dGroup = dPlayer.getGroup(); + PlayerGroup group = globalPlayer.getGroup(); - if (dGroup == null && !(dPlayer instanceof DEditPlayer)) { + if (group == null && !(globalPlayer instanceof EditPlayer)) { MessageUtil.sendMessage(player, DMessage.ERROR_JOIN_GROUP.getMessage()); return; } - GroupPlayerLeaveEvent groupPlayerLeaveEvent = new GroupPlayerLeaveEvent(dGroup, dPlayer); - Bukkit.getPluginManager().callEvent(groupPlayerLeaveEvent); - if (groupPlayerLeaveEvent.isCancelled()) { - return; - } - - if (dPlayer instanceof DInstancePlayer) { - ((DInstancePlayer) dPlayer).leave(); + if (globalPlayer instanceof GamePlayer) { + GroupPlayerLeaveEvent event = new GroupPlayerLeaveEvent(group, globalPlayer); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + ((GamePlayer) globalPlayer).leave(); + } else if (globalPlayer instanceof EditPlayer) { + EditPlayerLeaveEvent event = new EditPlayerLeaveEvent((EditPlayer) globalPlayer, false, true); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + ((EditPlayer) globalPlayer).leave(event.getUnloadIfEmpty()); } else { - dGroup.removeMember(player); + group.removeMember(player); } MessageUtil.sendMessage(player, DMessage.CMD_LEAVE_SUCCESS.getMessage()); diff --git a/core/src/main/java/de/erethon/dungeonsxl/player/DEditPlayer.java b/core/src/main/java/de/erethon/dungeonsxl/player/DEditPlayer.java index 166e57b8..2301a835 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/player/DEditPlayer.java +++ b/core/src/main/java/de/erethon/dungeonsxl/player/DEditPlayer.java @@ -138,12 +138,12 @@ public class DEditPlayer extends DInstancePlayer implements EditPlayer { } @Override - public void leave() { + public void leave(boolean unloadIfEmpty) { delete(); reset(false); - if (!plugin.isLoadingWorld() && editWorld != null && editWorld.getPlayers().isEmpty()) { + if (unloadIfEmpty && !plugin.isLoadingWorld() && editWorld != null && editWorld.getPlayers().isEmpty()) { editWorld.delete(); new ProgressBar(player, config.getEditInstanceRemovalDelay()) { @Override diff --git a/core/src/main/java/de/erethon/dungeonsxl/player/DGamePlayer.java b/core/src/main/java/de/erethon/dungeonsxl/player/DGamePlayer.java index cb4d023f..ce0faa4d 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/player/DGamePlayer.java +++ b/core/src/main/java/de/erethon/dungeonsxl/player/DGamePlayer.java @@ -26,6 +26,7 @@ import de.erethon.dungeonsxl.api.dungeon.GameGoal; import de.erethon.dungeonsxl.api.dungeon.GameRule; import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer; import de.erethon.dungeonsxl.api.event.group.GroupPlayerKickEvent; +import de.erethon.dungeonsxl.api.event.group.GroupScoreEvent; import de.erethon.dungeonsxl.api.event.player.GamePlayerDeathEvent; import de.erethon.dungeonsxl.api.event.player.GamePlayerFinishEvent; import de.erethon.dungeonsxl.api.event.player.GlobalPlayerRewardPayOutEvent; @@ -332,6 +333,11 @@ public class DGamePlayer extends DInstancePlayer implements GamePlayer { return; } + GroupScoreEvent event = new GroupScoreEvent(getGroup(), this, stealing); + if (event.isCancelled()) { + return; + } + game.sendMessage(DMessage.GROUP_FLAG_CAPTURED.getMessage(getName(), stealing.getName())); GameRuleContainer rules = game.getRules(); diff --git a/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java b/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java index f4e4e8f7..c1ac0965 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java +++ b/core/src/main/java/de/erethon/dungeonsxl/world/DGameWorld.java @@ -387,6 +387,10 @@ public class DGameWorld extends DInstanceWorld implements GameWorld { @Override public Dungeon getDungeon() { + if (getGame() != null) { + return getGame().getDungeon(); + } + for (Dungeon dungeon : plugin.getDungeonRegistry()) { if (dungeon.containsFloor(getResource())) { return dungeon; diff --git a/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java b/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java index 9aa2611c..89108f81 100644 --- a/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java +++ b/core/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java @@ -24,6 +24,7 @@ import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.api.dungeon.Dungeon; import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer; import de.erethon.dungeonsxl.api.event.world.EditWorldGenerateEvent; +import de.erethon.dungeonsxl.api.event.world.ResourceWorldInstantiateEvent; import de.erethon.dungeonsxl.api.player.EditPlayer; import de.erethon.dungeonsxl.api.world.EditWorld; import de.erethon.dungeonsxl.api.world.GameWorld; @@ -199,6 +200,10 @@ public class DResourceWorld implements ResourceWorld { } DInstanceWorld instance = game ? new DGameWorld(plugin, this, instanceFolder) : new DEditWorld(plugin, this, instanceFolder); + ResourceWorldInstantiateEvent event = new ResourceWorldInstantiateEvent(this, name); + if (event.isCancelled()) { + return null; + } FileUtil.copyDir(folder, instanceFolder, DungeonsXL.EXCLUDED_FILES); instance.world = Bukkit.createWorld(WorldCreator.name(name).environment(getWorldEnvironment())).getName(); @@ -284,7 +289,7 @@ public class DResourceWorld implements ResourceWorld { DEditWorld editWorld = new DEditWorld(plugin, this, folder); this.editWorld = editWorld; - EditWorldGenerateEvent event = new EditWorldGenerateEvent(editWorld); + ResourceWorldInstantiateEvent event = new ResourceWorldInstantiateEvent(this, name); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { return null; @@ -298,6 +303,7 @@ public class DResourceWorld implements ResourceWorld { editWorld.world = creator.createWorld().getName(); editWorld.generateIdFile(); + Bukkit.getPluginManager().callEvent(new EditWorldGenerateEvent(editWorld)); return editWorld; }