Closes gh-7. Now saving, clearing and restoring player armor when joining/leaving/disconnecting.

This commit is contained in:
taoneill 2011-02-05 22:41:49 -05:00
parent 7dd4572d77
commit 9a58dad30f
3 changed files with 127 additions and 20 deletions

View File

@ -12,6 +12,7 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import com.tommytony.war.InventoryStash;
import com.tommytony.war.Team;
import com.tommytony.war.TeamMaterials;
import com.tommytony.war.WarHub;
@ -28,7 +29,7 @@ public class WarPlayerListener extends PlayerListener {
private final War war;
private Random random = null;
private HashMap<String, ItemStack[]> disconnected = new HashMap<String, ItemStack[]>();
private HashMap<String, InventoryStash> disconnected = new HashMap<String, InventoryStash>();
public WarPlayerListener(War war) {
this.war = war;
@ -41,18 +42,35 @@ public class WarPlayerListener extends PlayerListener {
// Disconnected
if(disconnected.containsKey(player.getName())) {
// restore the disconnected player's inventory
ItemStack[] originalContents = disconnected.remove(player.getName());
InventoryStash originalContents = disconnected.remove(player.getName());
PlayerInventory playerInv = player.getInventory();
playerInv.clear(playerInv.getSize() + 0);
playerInv.clear(playerInv.getSize() + 1);
playerInv.clear(playerInv.getSize() + 2);
playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
if(originalContents != null) {
playerInv.clear();
for(ItemStack item : originalContents) {
for(ItemStack item : originalContents.getContents()) {
if(item.getTypeId() != 0) {
playerInv.addItem(item);
}
}
if(originalContents.getHelmet() != null) {
playerInv.setHelmet(originalContents.getHelmet());
}
if(originalContents.getChest() != null) {
playerInv.setChestplate(originalContents.getChest());
}
if(originalContents.getLegs() != null) {
playerInv.setLeggings(originalContents.getLegs());
}
if(originalContents.getFeet() != null) {
playerInv.setBoots(originalContents.getFeet());
}
}
playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
player.sendMessage(war.str("You were disconnected. Here's your inventory from last time."));
}
}

View File

@ -0,0 +1,67 @@
package com.tommytony.war;
import org.bukkit.inventory.ItemStack;
public class InventoryStash {
private ItemStack[] contents;
private ItemStack helmet;
private ItemStack chest;
private ItemStack legs;
private ItemStack feet;
public InventoryStash(ItemStack[] contents) {
this.setContents(contents);
}
public InventoryStash(ItemStack[] contents, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet) {
this.setContents(contents);
this.setHelmet(helmet);
this.setChest(chest);
this.setLegs(legs);
this.setFeet(feet);
}
public void setContents(ItemStack[] contents) {
this.contents = contents;
}
public ItemStack[] getContents() {
return contents;
}
public void setHelmet(ItemStack helmet) {
this.helmet = helmet;
}
public ItemStack getHelmet() {
return helmet;
}
public void setChest(ItemStack chest) {
this.chest = chest;
}
public ItemStack getChest() {
return chest;
}
public void setLegs(ItemStack legs) {
this.legs = legs;
}
public ItemStack getLegs() {
return legs;
}
public void setFeet(ItemStack feet) {
this.feet = feet;
}
public ItemStack getFeet() {
return feet;
}
}

View File

@ -38,7 +38,7 @@ public class Warzone {
private int teamCap = 5;
private int scoreCap = 5;
private HashMap<String, ItemStack[]> inventories = new HashMap<String, ItemStack[]>();
private HashMap<String, InventoryStash> inventories = new HashMap<String, InventoryStash>();
private HashMap<String, Team> flagThieves = new HashMap<String, Team>();
private World world;
private Material[] originalSoutheastBlocks;
@ -371,17 +371,23 @@ public class Warzone {
private void handleRespawn(Team team, Player player){
// Reset inventory to loadout
PlayerInventory playerInv = player.getInventory();
playerInv.clear();
playerInv.clear();
playerInv.clear(playerInv.getSize() + 0);
playerInv.clear(playerInv.getSize() + 1);
playerInv.clear(playerInv.getSize() + 2);
playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
for(Integer slot : loadout.keySet()) {
// if(slot == 101) {
// playerInv.setLeggings(loadout.get(slot));
// } else if(slot == 102) {
// playerInv.setChestplate(loadout.get(slot));
// } else if(slot == 103) {
// playerInv.setHelmet(loadout.get(slot));
// } else {
if(slot == 100) {
playerInv.setBoots(loadout.get(slot));
} else if(slot == 101) {
playerInv.setLeggings(loadout.get(slot));
} else if(slot == 102) {
playerInv.setChestplate(loadout.get(slot));
} else if(slot == 103) {
playerInv.setHelmet(loadout.get(slot));
} else {
playerInv.setItem(slot, loadout.get(slot));
//}
}
}
if(isBlockHeads()) {
playerInv.setHelmet(new ItemStack(team.getMaterial()));
@ -465,25 +471,41 @@ public class Warzone {
public void keepPlayerInventory(Player player) {
PlayerInventory inventory = player.getInventory();
ItemStack[] contents = inventory.getContents();
inventories.put(player.getName(), contents);
inventories.put(player.getName(), new InventoryStash(contents, inventory.getHelmet(), inventory.getChestplate(),
inventory.getLeggings(), inventory.getBoots()));
}
public void restorePlayerInventory(Player player) {
ItemStack[] originalContents = inventories.remove(player.getName());
InventoryStash originalContents = inventories.remove(player.getName());
PlayerInventory playerInv = player.getInventory();
if(originalContents != null) {
playerInv.clear();
for(ItemStack item : originalContents) {
playerInv.clear(playerInv.getSize() + 0);
playerInv.clear(playerInv.getSize() + 1);
playerInv.clear(playerInv.getSize() + 2);
playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
for(ItemStack item : originalContents.getContents()) {
if(item.getTypeId() != 0) {
playerInv.addItem(item);
}
}
if(originalContents.getHelmet() != null) {
playerInv.setHelmet(originalContents.getHelmet());
}
if(originalContents.getChest() != null) {
playerInv.setChestplate(originalContents.getChest());
}
if(originalContents.getLegs() != null) {
playerInv.setLeggings(originalContents.getLegs());
}
if(originalContents.getFeet() != null) {
playerInv.setBoots(originalContents.getFeet());
}
}
playerInv.clear(playerInv.getSize() + 3);
}
public ItemStack[] getPlayerInventory(String playerName) {
public InventoryStash getPlayerInventory(String playerName) {
if(inventories.containsKey(playerName)) return inventories.get(playerName);
return null;
}