From 9a58dad30fb675d0fc07a2ccd6199c2f2407e2ff Mon Sep 17 00:00:00 2001 From: taoneill Date: Sat, 5 Feb 2011 22:41:49 -0500 Subject: [PATCH] Closes gh-7. Now saving, clearing and restoring player armor when joining/leaving/disconnecting. --- .../tommytony/war/WarPlayerListener.java | 26 +++++-- .../com/tommytony/war/InventoryStash.java | 67 +++++++++++++++++++ .../main/java/com/tommytony/war/Warzone.java | 54 ++++++++++----- 3 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 war/src/main/java/com/tommytony/war/InventoryStash.java diff --git a/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java b/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java index af9701d..adffb90 100644 --- a/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java +++ b/war/src/main/java/bukkit/tommytony/war/WarPlayerListener.java @@ -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 disconnected = new HashMap(); + private HashMap disconnected = new HashMap(); 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.")); } } diff --git a/war/src/main/java/com/tommytony/war/InventoryStash.java b/war/src/main/java/com/tommytony/war/InventoryStash.java new file mode 100644 index 0000000..beec46c --- /dev/null +++ b/war/src/main/java/com/tommytony/war/InventoryStash.java @@ -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; + } + + +} diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index eb34a87..45e0fec 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -38,7 +38,7 @@ public class Warzone { private int teamCap = 5; private int scoreCap = 5; - private HashMap inventories = new HashMap(); + private HashMap inventories = new HashMap(); private HashMap flagThieves = new HashMap(); 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; }