Fix keepInventoryOnEscape

This commit is contained in:
Daniel Saukel 2016-04-06 22:05:03 +02:00
parent ea72029d26
commit 43fcee3c31
4 changed files with 85 additions and 71 deletions

View File

@ -108,21 +108,6 @@ public class PlayerListener implements Listener {
dPlayer.setLives(dPlayer.getLives() - dPlayerDeathEvent.getLostLives()); 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) { if (dPlayer.getLives() != -1) {
MessageUtil.sendMessage(player, messageConfig.getMessage(Messages.PLAYER_DEATH, String.valueOf(dPlayer.getLives()))); 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.setRespawnInventory(event.getEntity().getInventory().getContents());
dPlayer.setRespawnArmor(event.getEntity().getInventory().getArmorContents()); dPlayer.setRespawnArmor(event.getEntity().getInventory().getArmorContents());
// Delete all drops // Delete all drops
for (ItemStack istack : event.getDrops()) { for (ItemStack item : event.getDrops()) {
istack.setType(Material.AIR); 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) @EventHandler(priority = EventPriority.HIGHEST)
@ -348,6 +346,8 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onRespawn(PlayerRespawnEvent event) { public void onRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
plugin.getDPlayers().getByPlayer(player).applyRespawnInventory();
DPlayer dPlayer = DPlayer.getByPlayer(player); DPlayer dPlayer = DPlayer.getByPlayer(player);
if (dPlayer == null) { if (dPlayer == null) {
return; return;

View File

@ -19,6 +19,7 @@ package io.github.dre2n.dungeonsxl.player;
import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.global.DPortal; import io.github.dre2n.dungeonsxl.global.DPortal;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/** /**
* Represents a player in the non-DXL worlds of the server. * Represents a player in the non-DXL worlds of the server.
@ -33,12 +34,26 @@ public class DGlobalPlayer {
private boolean chatSpyMode; private boolean chatSpyMode;
private DPortal creatingPortal; private DPortal creatingPortal;
private ItemStack[] respawnInventory;
private ItemStack[] respawnArmor;
public DGlobalPlayer(Player player) { public DGlobalPlayer(Player player) {
this.player = player; this.player = player;
DungeonsXL.getInstance().getDPlayers().addPlayer(this); 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 * @return the Bukkit player
*/ */
@ -98,4 +113,48 @@ public class DGlobalPlayer {
creatingPortal = dPortal; 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;
}
} }

View File

@ -85,8 +85,6 @@ public class DPlayer extends DGlobalPlayer {
private Wolf wolf; private Wolf wolf;
private int wolfRespawnTime = 30; private int wolfRespawnTime = 30;
private long offlineTime; private long offlineTime;
private ItemStack[] respawnInventory;
private ItemStack[] respawnArmor;
private String[] linesCopy; private String[] linesCopy;
private Inventory treasureInv = plugin.getServer().createInventory(getPlayer(), 45, messageConfig.getMessage(Messages.PLAYER_TREASURES)); 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; 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 * @return the linesCopy
*/ */
@ -654,13 +622,7 @@ public class DPlayer extends DGlobalPlayer {
// Respawn Items // Respawn Items
if (GameWorld.getByWorld(world).getConfig().getKeepInventoryOnDeath()) { if (GameWorld.getByWorld(world).getConfig().getKeepInventoryOnDeath()) {
if (respawnInventory != null || respawnArmor != null) { applyRespawnInventory();
getPlayer().getInventory().setContents(respawnInventory);
getPlayer().getInventory().setArmorContents(respawnArmor);
respawnInventory = null;
respawnArmor = null;
}
// P.plugin.updateInventory(this.player);
} }
} }
@ -937,10 +899,7 @@ public class DPlayer extends DGlobalPlayer {
} }
if (respawnInventory) { if (respawnInventory) {
getPlayer().getInventory().setContents(getRespawnInventory()); applyRespawnInventory();
getPlayer().getInventory().setArmorContents(getRespawnArmor());
setRespawnInventory(null);
setRespawnArmor(null);
} }
if (kick) { if (kick) {
@ -961,7 +920,8 @@ public class DPlayer extends DGlobalPlayer {
*/ */
public void delete() { public void delete() {
if (player.isOnline()) { if (player.isOnline()) {
new DGlobalPlayer(player); // Create a new DGlobalPlayer (outside a dungeon)
new DGlobalPlayer(this);
} else { } else {
plugin.getDPlayers().removePlayer(this); plugin.getDPlayers().removePlayer(this);

View File

@ -17,7 +17,8 @@
package io.github.dre2n.dungeonsxl.task; package io.github.dre2n.dungeonsxl.task;
import io.github.dre2n.commons.util.playerutil.PlayerUtil; 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.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -41,19 +42,13 @@ public class RespawnTask extends BukkitRunnable {
PlayerUtil.secureTeleport(player, location); PlayerUtil.secureTeleport(player, location);
} }
DPlayer dPlayer = DPlayer.getByPlayer(player); DGlobalPlayer dPlayer = DungeonsXL.getInstance().getDPlayers().getByPlayer(player);
if (dPlayer == null) { if (dPlayer == null) {
return; return;
} }
// Respawn Items dPlayer.applyRespawnInventory();
if (dPlayer.getRespawnInventory() != null || dPlayer.getRespawnArmor() != null) {
player.getInventory().setContents(dPlayer.getRespawnInventory());
player.getInventory().setArmorContents(dPlayer.getRespawnArmor());
dPlayer.setRespawnInventory(null);
dPlayer.setRespawnArmor(null);
}
} }
} }