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());
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;

View File

@ -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;
}
}

View File

@ -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
*/
@ -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);

View File

@ -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();
}
}