From 6157df970332bd5c02287762b7f5c015fa8cf1f2 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Tue, 12 Jan 2016 23:55:20 +0100 Subject: [PATCH] Implement DPlayer events --- .../dungeonsxl/command/LeaveCommand.java | 7 ++ .../event/dplayer/DPlayerJoinDGroupEvent.java | 12 ++- .../event/dplayer/DPlayerUpdateEvent.java | 19 +++- .../dungeonsxl/listener/PlayerListener.java | 9 +- .../dre2n/dungeonsxl/player/DPlayer.java | 97 +++++++++++++++---- .../dre2n/dungeonsxl/sign/LeaveSign.java | 16 ++- 6 files changed, 135 insertions(+), 25 deletions(-) diff --git a/src/io/github/dre2n/dungeonsxl/command/LeaveCommand.java b/src/io/github/dre2n/dungeonsxl/command/LeaveCommand.java index 05bbc4bb..cf32a5e5 100644 --- a/src/io/github/dre2n/dungeonsxl/command/LeaveCommand.java +++ b/src/io/github/dre2n/dungeonsxl/command/LeaveCommand.java @@ -1,6 +1,7 @@ package io.github.dre2n.dungeonsxl.command; import io.github.dre2n.dungeonsxl.dungeon.game.GameWorld; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerEscapeEvent; import io.github.dre2n.dungeonsxl.file.DMessages.Messages; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; @@ -33,6 +34,12 @@ public class LeaveCommand extends DCommand { } if (dPlayer != null) { + DPlayerEscapeEvent event = new DPlayerEscapeEvent(dPlayer); + + if (event.isCancelled()) { + return; + } + dPlayer.leave(); MessageUtil.sendMessage(player, dMessages.getMessage(Messages.CMD_LEAVE_SUCCESS)); diff --git a/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerJoinDGroupEvent.java b/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerJoinDGroupEvent.java index 8ad022b7..6477521d 100644 --- a/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerJoinDGroupEvent.java +++ b/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerJoinDGroupEvent.java @@ -1,5 +1,6 @@ package io.github.dre2n.dungeonsxl.event.dplayer; +import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.player.DPlayer; import org.bukkit.event.Cancellable; @@ -11,10 +12,12 @@ public class DPlayerJoinDGroupEvent extends DPlayerEvent implements Cancellable private boolean cancelled; private boolean creator; + private DGroup dGroup; - public DPlayerJoinDGroupEvent(DPlayer dPlayer, boolean creator) { + public DPlayerJoinDGroupEvent(DPlayer dPlayer, boolean creator, DGroup dGroup) { super(dPlayer); this.creator = creator; + this.dGroup = dGroup; } /** @@ -24,6 +27,13 @@ public class DPlayerJoinDGroupEvent extends DPlayerEvent implements Cancellable return creator; } + /** + * @return the dGroup + */ + public DGroup getDGroup() { + return dGroup; + } + @Override public HandlerList getHandlers() { return handlers; diff --git a/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerUpdateEvent.java b/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerUpdateEvent.java index 670a4821..ec76efc5 100644 --- a/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerUpdateEvent.java +++ b/src/io/github/dre2n/dungeonsxl/event/dplayer/DPlayerUpdateEvent.java @@ -15,14 +15,16 @@ public class DPlayerUpdateEvent extends DPlayerEvent implements Cancellable { private boolean respawnInventory; private boolean offline; private boolean kick; + private boolean triggerAllInDistance; - public DPlayerUpdateEvent(DPlayer dPlayer, boolean locationValid, boolean teleportWolf, boolean respawnInventory, boolean offline, boolean kick) { + public DPlayerUpdateEvent(DPlayer dPlayer, boolean locationValid, boolean teleportWolf, boolean respawnInventory, boolean offline, boolean kick, boolean triggerAllInDistance) { super(dPlayer); this.locationValid = locationValid; this.teleportWolf = teleportWolf; this.respawnInventory = respawnInventory; this.offline = offline; this.kick = kick; + this.triggerAllInDistance = triggerAllInDistance; } /** @@ -92,6 +94,21 @@ public class DPlayerUpdateEvent extends DPlayerEvent implements Cancellable { this.kick = kick; } + /** + * @return the triggerAllInDistance + */ + public boolean getTriggerAllInDistance() { + return triggerAllInDistance; + } + + /** + * @param triggerAllInDistance + * the triggerAllInDistance to set + */ + public void setTriggerAllInDistance(boolean triggerAllInDistance) { + this.triggerAllInDistance = triggerAllInDistance; + } + @Override public HandlerList getHandlers() { return handlers; diff --git a/src/io/github/dre2n/dungeonsxl/listener/PlayerListener.java b/src/io/github/dre2n/dungeonsxl/listener/PlayerListener.java index 0fc1f541..5149a940 100644 --- a/src/io/github/dre2n/dungeonsxl/listener/PlayerListener.java +++ b/src/io/github/dre2n/dungeonsxl/listener/PlayerListener.java @@ -7,6 +7,7 @@ import io.github.dre2n.dungeonsxl.dungeon.EditWorld; import io.github.dre2n.dungeonsxl.dungeon.game.GameChest; import io.github.dre2n.dungeonsxl.dungeon.game.GameWorld; import io.github.dre2n.dungeonsxl.event.dgroup.DGroupCreateEvent; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerDeathEvent; import io.github.dre2n.dungeonsxl.file.DMessages; import io.github.dre2n.dungeonsxl.file.DMessages.Messages; import io.github.dre2n.dungeonsxl.global.DPortal; @@ -67,7 +68,13 @@ public class PlayerListener implements Listener { return; } - dPlayer.setLives(dPlayer.getLives() - 1); + DPlayerDeathEvent dPlayerDeathEvent = new DPlayerDeathEvent(dPlayer, event, 1); + + if (dPlayerDeathEvent.isCancelled()) { + return; + } + + dPlayer.setLives(dPlayer.getLives() - dPlayerDeathEvent.getLostLives()); if (dPlayer.getLives() == 0 && dPlayer.isReady()) { MessageUtil.broadcastMessage(dMessages.getMessage(Messages.PLAYER_DEATH_KICK, player.getName())); diff --git a/src/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/io/github/dre2n/dungeonsxl/player/DPlayer.java index 28689ceb..b3706b35 100644 --- a/src/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -8,6 +8,9 @@ import io.github.dre2n.dungeonsxl.dungeon.EditWorld; import io.github.dre2n.dungeonsxl.dungeon.game.GameWorld; import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishDungeonEvent; import io.github.dre2n.dungeonsxl.event.dgroup.DGroupFinishFloorEvent; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerFinishEvent; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerKickEvent; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerUpdateEvent; import io.github.dre2n.dungeonsxl.file.DMessages; import io.github.dre2n.dungeonsxl.file.DMessages.Messages; import io.github.dre2n.dungeonsxl.reward.Reward; @@ -64,7 +67,7 @@ public class DPlayer { private ItemStack[] respawnArmor; private String[] linesCopy; - private Inventory treasureInv = DungeonsXL.getPlugin().getServer().createInventory(getPlayer(), 45, dMessages.getMessage(Messages.PLAYER_TREASURES)); + private Inventory treasureInv = plugin.getServer().createInventory(getPlayer(), 45, dMessages.getMessage(Messages.PLAYER_TREASURES)); private int initialLives = -1; private int lives; @@ -343,17 +346,30 @@ public class DPlayer { return; } + boolean first = true; + boolean hasToWait = false; + for (Player player : dGroup.getPlayers()) { DPlayer dPlayer = getByPlayer(player); if ( !dPlayer.finished) { MessageUtil.sendMessage(this.getPlayer(), dMessages.getMessage(Messages.PLAYER_WAIT_FOR_OTHER_PLAYERS)); - return; + hasToWait = true; + + } else if (dPlayer != this) { + first = false; } } - DGroupFinishDungeonEvent event = new DGroupFinishDungeonEvent(dGroup); + DPlayerFinishEvent dPlayerFinishEvent = new DPlayerFinishEvent(this, first, hasToWait); - if (event.isCancelled()) { + if (dPlayerFinishEvent.isCancelled()) { + finished = false; + return; + } + + DGroupFinishDungeonEvent dGroupFinishDungeonEvent = new DGroupFinishDungeonEvent(dGroup); + + if (dGroupFinishDungeonEvent.isCancelled()) { return; } @@ -813,46 +829,53 @@ public class DPlayer { public static void update(boolean updateSecond) { for (DPlayer dPlayer : plugin.getDPlayers()) { + boolean locationValid = true; + Location teleportLocation = dPlayer.getPlayer().getLocation(); + boolean teleportWolf = false; + boolean respawnInventory = false; + boolean offline = false; + boolean kick = false; + boolean triggerAllInDistance = false; + + GameWorld gameWorld = GameWorld.getByWorld(dPlayer.world); + EditWorld editWorld = EditWorld.getByWorld(dPlayer.world); + if ( !updateSecond) { if ( !dPlayer.getPlayer().getWorld().equals(dPlayer.world)) { + locationValid = false; + if (dPlayer.editing) { - EditWorld editWorld = EditWorld.getByWorld(dPlayer.world); if (editWorld != null) { if (editWorld.getLobby() == null) { - MiscUtil.secureTeleport(dPlayer.getPlayer(), editWorld.getWorld().getSpawnLocation()); + teleportLocation = editWorld.getWorld().getSpawnLocation(); } else { - MiscUtil.secureTeleport(dPlayer.getPlayer(), editWorld.getLobby()); + teleportLocation = editWorld.getLobby(); } } } else { - GameWorld gameWorld = GameWorld.getByWorld(dPlayer.world); if (gameWorld != null) { DGroup dGroup = DGroup.getByPlayer(dPlayer.getPlayer()); if (dPlayer.checkpoint == null) { - MiscUtil.secureTeleport(dPlayer.getPlayer(), dGroup.getGameWorld().getLocStart()); + teleportLocation = dGroup.getGameWorld().getLocStart(); if (dPlayer.wolf != null) { dPlayer.wolf.teleport(dGroup.getGameWorld().getLocStart()); } } else { - MiscUtil.secureTeleport(dPlayer.getPlayer(), dPlayer.checkpoint); + teleportLocation = dPlayer.getCheckpoint(); if (dPlayer.wolf != null) { - dPlayer.wolf.teleport(dPlayer.checkpoint); + teleportWolf = true; } } // Respawn Items if (dPlayer.respawnInventory != null || dPlayer.respawnArmor != null) { - dPlayer.getPlayer().getInventory().setContents(dPlayer.respawnInventory); - dPlayer.getPlayer().getInventory().setArmorContents(dPlayer.respawnArmor); - dPlayer.respawnInventory = null; - dPlayer.respawnArmor = null; + respawnInventory = true; } } } } - } else { - GameWorld gameWorld = GameWorld.getByWorld(dPlayer.world); + } else { if (gameWorld != null) { // Update Wolf if (dPlayer.wolf != null) { @@ -869,15 +892,49 @@ public class DPlayer { // Kick offline plugin.getDPlayers() if (dPlayer.offlineTime > 0) { + offline = true; + if (dPlayer.offlineTime < System.currentTimeMillis()) { - dPlayer.leave(); + kick = true; } } - // Check Distance Trigger Signs - DistanceTrigger.triggerAllInDistance(dPlayer.getPlayer(), gameWorld); + triggerAllInDistance = true; } } + + DPlayerUpdateEvent event = new DPlayerUpdateEvent(dPlayer, locationValid, teleportWolf, respawnInventory, offline, kick, triggerAllInDistance); + + if (event.isCancelled()) { + return; + } + + if ( !locationValid) { + MiscUtil.secureTeleport(dPlayer.getPlayer(), teleportLocation); + } + + if (teleportWolf) { + dPlayer.wolf.teleport(dPlayer.checkpoint); + } + + if (respawnInventory) { + dPlayer.getPlayer().getInventory().setContents(dPlayer.respawnInventory); + dPlayer.getPlayer().getInventory().setArmorContents(dPlayer.respawnArmor); + dPlayer.respawnInventory = null; + dPlayer.respawnArmor = null; + } + + if (kick) { + DPlayerKickEvent dPlayerKickEvent = new DPlayerKickEvent(dPlayer); + + if ( !dPlayerKickEvent.isCancelled()) { + dPlayer.leave(); + } + } + + if (triggerAllInDistance) { + DistanceTrigger.triggerAllInDistance(dPlayer.getPlayer(), gameWorld); + } } } diff --git a/src/io/github/dre2n/dungeonsxl/sign/LeaveSign.java b/src/io/github/dre2n/dungeonsxl/sign/LeaveSign.java index 3948aeac..09e648fc 100644 --- a/src/io/github/dre2n/dungeonsxl/sign/LeaveSign.java +++ b/src/io/github/dre2n/dungeonsxl/sign/LeaveSign.java @@ -1,7 +1,7 @@ package io.github.dre2n.dungeonsxl.sign; -import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.dungeon.game.GameWorld; +import io.github.dre2n.dungeonsxl.event.dplayer.DPlayerEscapeEvent; import io.github.dre2n.dungeonsxl.player.DPlayer; import io.github.dre2n.dungeonsxl.trigger.InteractTrigger; @@ -46,6 +46,12 @@ public class LeaveSign extends DSign { public boolean onPlayerTrigger(Player player) { DPlayer dPlayer = DPlayer.getByPlayer(player); if (dPlayer != null) { + DPlayerEscapeEvent event = new DPlayerEscapeEvent(dPlayer); + + if (event.isCancelled()) { + return false; + } + dPlayer.leave(); } @@ -54,7 +60,13 @@ public class LeaveSign extends DSign { @Override public void onTrigger() { - for (DPlayer dPlayer : DungeonsXL.getPlugin().getDPlayers()) { + for (DPlayer dPlayer : plugin.getDPlayers()) { + DPlayerEscapeEvent event = new DPlayerEscapeEvent(dPlayer); + + if (event.isCancelled()) { + return; + } + dPlayer.leave(); } }