From 43fcee3c311b4a571c1382069a2f44f269792506 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Wed, 6 Apr 2016 22:05:03 +0200 Subject: [PATCH] Fix keepInventoryOnEscape --- .../dungeonsxl/listener/PlayerListener.java | 34 +++++------ .../dungeonsxl/player/DGlobalPlayer.java | 59 +++++++++++++++++++ .../dre2n/dungeonsxl/player/DPlayer.java | 50 ++-------------- .../dre2n/dungeonsxl/task/RespawnTask.java | 13 ++-- 4 files changed, 85 insertions(+), 71 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java index 1140bc19..663ce9c1 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java @@ -108,21 +108,6 @@ public class PlayerListener implements Listener { dPlayer.setLives(dPlayer.getLives() - dPlayerDeathEvent.getLostLives()); - if (dPlayer.getLives() == 0 && dPlayer.isReady()) { - DPlayerKickEvent dPlayerKickEvent = new DPlayerKickEvent(dPlayer, DPlayerKickEvent.Cause.DEATH); - - if (!dPlayerKickEvent.isCancelled()) { - MessageUtil.broadcastMessage(messageConfig.getMessage(Messages.PLAYER_DEATH_KICK, player.getName())); - - // TODO: This Runnable is a workaround for a bug I couldn't find, yet... - new org.bukkit.scheduler.BukkitRunnable() { - public void run() { - dPlayer.leave(); - } - }.runTaskLater(plugin, 1L); - } - } - if (dPlayer.getLives() != -1) { MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_DEATH, String.valueOf(dPlayer.getLives()))); @@ -131,12 +116,25 @@ public class PlayerListener implements Listener { dPlayer.setRespawnInventory(event.getEntity().getInventory().getContents()); dPlayer.setRespawnArmor(event.getEntity().getInventory().getArmorContents()); // Delete all drops - for (ItemStack istack : event.getDrops()) { - istack.setType(Material.AIR); + for (ItemStack item : event.getDrops()) { + item.setType(Material.AIR); } } } } + + if (dPlayer.getLives() == 0 && dPlayer.isReady()) { + DPlayerKickEvent dPlayerKickEvent = new DPlayerKickEvent(dPlayer, DPlayerKickEvent.Cause.DEATH); + + if (!dPlayerKickEvent.isCancelled()) { + MessageUtil.broadcastMessage(messageConfig.getMessage(Messages.PLAYER_DEATH_KICK, player.getName())); + dPlayer.leave(); + if (gameWorld.getConfig().getKeepInventoryOnEscape()) { + /*new org.bukkit.scheduler.BukkitRunnable() {public void run(){*/ + dPlayer.applyRespawnInventory();/*}}.runTaskLater(plugin, 1);*/ + } + } + } } @EventHandler(priority = EventPriority.HIGHEST) @@ -348,6 +346,8 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onRespawn(PlayerRespawnEvent event) { Player player = event.getPlayer(); + plugin.getDPlayers().getByPlayer(player).applyRespawnInventory(); + DPlayer dPlayer = DPlayer.getByPlayer(player); if (dPlayer == null) { return; diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java index c43ab87c..d64a26c3 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DGlobalPlayer.java @@ -19,6 +19,7 @@ package io.github.dre2n.dungeonsxl.player; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.global.DPortal; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; /** * Represents a player in the non-DXL worlds of the server. @@ -33,12 +34,26 @@ public class DGlobalPlayer { private boolean chatSpyMode; private DPortal creatingPortal; + private ItemStack[] respawnInventory; + private ItemStack[] respawnArmor; + public DGlobalPlayer(Player player) { this.player = player; DungeonsXL.getInstance().getDPlayers().addPlayer(this); } + public DGlobalPlayer(DGlobalPlayer dPlayer) { + player = dPlayer.getPlayer(); + breakMode = dPlayer.isInBreakMode(); + chatSpyMode = dPlayer.isInChatSpyMode(); + creatingPortal = dPlayer.getPortal(); + respawnInventory = dPlayer.getRespawnInventory(); + respawnArmor = dPlayer.getRespawnArmor(); + + DungeonsXL.getInstance().getDPlayers().addPlayer(this); + } + /** * @return the Bukkit player */ @@ -98,4 +113,48 @@ public class DGlobalPlayer { creatingPortal = dPortal; } + /** + * @return the respawnInventory + */ + public ItemStack[] getRespawnInventory() { + return respawnInventory; + } + + /** + * @param respawnInventory + * the respawnInventory to set + */ + public void setRespawnInventory(ItemStack[] respawnInventory) { + this.respawnInventory = respawnInventory; + } + + /** + * Give the saved respawn inventory to the player + */ + public void applyRespawnInventory() { + if (respawnInventory == null || respawnArmor == null) { + return; + } + + player.getInventory().setContents(respawnInventory); + player.getInventory().setArmorContents(respawnArmor); + respawnInventory = null; + respawnArmor = null; + } + + /** + * @return the respawnArmor + */ + public ItemStack[] getRespawnArmor() { + return respawnArmor; + } + + /** + * @param respawnArmor + * the respawnArmor to set + */ + public void setRespawnArmor(ItemStack[] respawnArmor) { + this.respawnArmor = respawnArmor; + } + } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java index 18d4f32e..d1865607 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -85,8 +85,6 @@ public class DPlayer extends DGlobalPlayer { private Wolf wolf; private int wolfRespawnTime = 30; private long offlineTime; - private ItemStack[] respawnInventory; - private ItemStack[] respawnArmor; private String[] linesCopy; private Inventory treasureInv = plugin.getServer().createInventory(getPlayer(), 45, messageConfig.getMessage(Messages.PLAYER_TREASURES)); @@ -402,36 +400,6 @@ public class DPlayer extends DGlobalPlayer { this.offlineTime = offlineTime; } - /** - * @return the respawnInventory - */ - public ItemStack[] getRespawnInventory() { - return respawnInventory; - } - - /** - * @param respawnInventory - * the respawnInventory to set - */ - public void setRespawnInventory(ItemStack[] respawnInventory) { - this.respawnInventory = respawnInventory; - } - - /** - * @return the respawnArmor - */ - public ItemStack[] getRespawnArmor() { - return respawnArmor; - } - - /** - * @param respawnArmor - * the respawnArmor to set - */ - public void setRespawnArmor(ItemStack[] respawnArmor) { - this.respawnArmor = respawnArmor; - } - /** * @return the linesCopy */ @@ -500,7 +468,7 @@ public class DPlayer extends DGlobalPlayer { public void leave() { delete(); - + if (!editing) { WorldConfig dConfig = GameWorld.getByWorld(world).getConfig(); if (finished) { @@ -654,13 +622,7 @@ public class DPlayer extends DGlobalPlayer { // Respawn Items if (GameWorld.getByWorld(world).getConfig().getKeepInventoryOnDeath()) { - if (respawnInventory != null || respawnArmor != null) { - getPlayer().getInventory().setContents(respawnInventory); - getPlayer().getInventory().setArmorContents(respawnArmor); - respawnInventory = null; - respawnArmor = null; - } - // P.plugin.updateInventory(this.player); + applyRespawnInventory(); } } @@ -937,10 +899,7 @@ public class DPlayer extends DGlobalPlayer { } if (respawnInventory) { - getPlayer().getInventory().setContents(getRespawnInventory()); - getPlayer().getInventory().setArmorContents(getRespawnArmor()); - setRespawnInventory(null); - setRespawnArmor(null); + applyRespawnInventory(); } if (kick) { @@ -961,7 +920,8 @@ public class DPlayer extends DGlobalPlayer { */ public void delete() { if (player.isOnline()) { - new DGlobalPlayer(player); + // Create a new DGlobalPlayer (outside a dungeon) + new DGlobalPlayer(this); } else { plugin.getDPlayers().removePlayer(this); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java index 8f344626..a497ad33 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/RespawnTask.java @@ -17,7 +17,8 @@ package io.github.dre2n.dungeonsxl.task; import io.github.dre2n.commons.util.playerutil.PlayerUtil; -import io.github.dre2n.dungeonsxl.player.DPlayer; +import io.github.dre2n.dungeonsxl.DungeonsXL; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -41,19 +42,13 @@ public class RespawnTask extends BukkitRunnable { PlayerUtil.secureTeleport(player, location); } - DPlayer dPlayer = DPlayer.getByPlayer(player); + DGlobalPlayer dPlayer = DungeonsXL.getInstance().getDPlayers().getByPlayer(player); if (dPlayer == null) { return; } - // Respawn Items - if (dPlayer.getRespawnInventory() != null || dPlayer.getRespawnArmor() != null) { - player.getInventory().setContents(dPlayer.getRespawnInventory()); - player.getInventory().setArmorContents(dPlayer.getRespawnArmor()); - dPlayer.setRespawnInventory(null); - dPlayer.setRespawnArmor(null); - } + dPlayer.applyRespawnInventory(); } }