From 9f53f0265e94bdfb4dfd37f8587674acbc019a59 Mon Sep 17 00:00:00 2001 From: taoneill Date: Mon, 10 Jan 2011 01:21:04 -0500 Subject: [PATCH] Brought back inventory code. --- .../com/tommytony/war/WarBlockListener.java | 4 +- .../main/java/com/tommytony/war/Warzone.java | 62 +++++++------------ .../com/tommytony/war/mappers/WarMapper.java | 38 +++++++----- 3 files changed, 47 insertions(+), 57 deletions(-) diff --git a/war/src/main/java/com/tommytony/war/WarBlockListener.java b/war/src/main/java/com/tommytony/war/WarBlockListener.java index d99c5cd..38670f8 100644 --- a/war/src/main/java/com/tommytony/war/WarBlockListener.java +++ b/war/src/main/java/com/tommytony/war/WarBlockListener.java @@ -5,8 +5,8 @@ import java.util.List; import org.bukkit.Block; import org.bukkit.Material; import org.bukkit.Player; -import org.bukkit.event.block.BlockBrokenEvent; import org.bukkit.event.block.BlockCanBuildEvent; +import org.bukkit.event.block.BlockDamagedEvent; import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockListener; @@ -45,7 +45,7 @@ public class WarBlockListener extends BlockListener { } - public void onBlockBroken(BlockBrokenEvent event) { + public void onBlockDamaged(BlockDamagedEvent event) { Player player = event.getPlayer(); Block block = event.getBlock(); if(player != null && block != null) { diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index 5467539..edf5d6d 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -8,6 +8,7 @@ import org.bukkit.ItemStack; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Player; +import org.bukkit.PlayerInventory; import org.bukkit.World; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; @@ -29,7 +30,7 @@ public class Warzone { private int lifePool; private HashMap loadout; - private HashMap> inventories = new HashMap>(); + private HashMap> inventories = new HashMap>(); private World world; public Warzone(War war, World world, String name) { @@ -208,18 +209,16 @@ public class Warzone { } public void respawnPlayer(Team team, Player player) { -// BUKKIT -// Inventory playerInv = player.getInventory(); -// playerInv.getContents().clear(); -// for(Integer slot : loadout.keySet()) { -// playerInv.getContents().add(loadout.get(slot)); -// // TODO set the proper slot index -// } + // Reset inventory to loadout + PlayerInventory playerInv = player.getInventory(); + for(int i = 0; i < playerInv.getSize(); i++){ + playerInv.setItem(i, new ItemStack(Material.Air)); + } + for(Integer slot : loadout.keySet()) { + playerInv.setItem(slot, loadout.get(slot)); + } player.setHealth(20); - -// BUKKIT -// player.setFireTicks(0); player.teleportTo(team.getTeamSpawn()); } @@ -256,23 +255,11 @@ public class Warzone { return false; } -// public void removeSpawnArea(Team team) { -// // Reset spawn to what it was before the gold blocks -// team.getVolume().resetBlocks(); -// } - -// public void addSpawnArea(Team team, Location location) { -// // Save the spawn state -// team.setTeamSpawn(location); -// } - - public List getMonuments() { return monuments; } public boolean getFriendlyFire() { - // TODO Auto-generated method stub return this.friendlyFire; } @@ -301,26 +288,26 @@ public class Warzone { } public void keepPlayerInventory(Player player) { - // BUKKIT -// Inventory inventory = player.getInventory(); -// -// inventories.put(player.getName(), inventory.getContents()); + PlayerInventory inventory = player.getInventory(); + List invToStore = new ArrayList(); + for(int i=0; i < inventory.getSize(); i++) { + invToStore.set(i, inventory.getItem(i)); + } + inventories.put(player.getName(), invToStore); } public void restorePlayerInventory(Player player) { -// HashMap originalContents = inventories.remove((player.getName()); -// Inventory playerInv = player.getInventory(); -// playerInv.clearContents(); -// playerInv.update(); -// for(Item item : originalContents) { -// playerInv.addItem(item); -// } -// playerInv.update(); -// player.getInventory().update(); + List originalContents = inventories.remove(player.getName()); + PlayerInventory playerInv = player.getInventory(); + for(int i = 0; i < playerInv.getSize(); i++) { + playerInv.setItem(i, new ItemStack(Material.Air)); + } + for(int i = 0; i < playerInv.getSize(); i++) { + playerInv.setItem(i, originalContents.get(i)); + } } public boolean hasMonument(String monumentName) { - boolean hasIt = false; for(Monument monument: monuments) { if(monument.getName().equals(monumentName)) { return true; @@ -330,7 +317,6 @@ public class Warzone { } public Monument getMonument(String monumentName) { - boolean hasIt = false; for(Monument monument: monuments) { if(monument.getName().equals(monumentName)) { return monument; diff --git a/war/src/main/java/com/tommytony/war/mappers/WarMapper.java b/war/src/main/java/com/tommytony/war/mappers/WarMapper.java index fcbb484..bf0508b 100644 --- a/war/src/main/java/com/tommytony/war/mappers/WarMapper.java +++ b/war/src/main/java/com/tommytony/war/mappers/WarMapper.java @@ -1,6 +1,9 @@ package com.tommytony.war.mappers; import java.io.File; import java.io.IOException; +import java.util.HashMap; + +import org.bukkit.ItemStack; import com.tommytony.war.War; import com.tommytony.war.Warzone; @@ -46,17 +49,17 @@ public class WarMapper { } // defaultLoadout -// String defaultLoadoutStr = warConfig.getString("defaultLoadout"); -// String[] defaultLoadoutSplit = defaultLoadoutStr.split(";"); -// war.getDefaultLoadout().clear(); -// for(String itemStr : defaultLoadoutSplit) { -// if(itemStr != null && !itemStr.equals("")) { -// String[] itemStrSplit = itemStr.split(","); -// ItemStack item = new ItemStack(Integer.parseInt(itemStrSplit[0]), -// Integer.parseInt(itemStrSplit[1])); -// war.getDefaultLoadout().add(item); -// } -// } + String defaultLoadoutStr = warConfig.getString("defaultLoadout"); + String[] defaultLoadoutSplit = defaultLoadoutStr.split(";"); + war.getDefaultLoadout().clear(); + for(String itemStr : defaultLoadoutSplit) { + if(itemStr != null && !itemStr.equals("")) { + String[] itemStrSplit = itemStr.split(","); + ItemStack item = new ItemStack(Integer.parseInt(itemStrSplit[0]), + Integer.parseInt(itemStrSplit[1])); + war.getDefaultLoadout().put(Integer.parseInt(itemStrSplit[2]), item); + } + } // defaultLifePool war.setDefaultLifepool(warConfig.getInt("defaultLifePool")); @@ -80,12 +83,13 @@ public class WarMapper { warConfig.setString("warzones", warzonesStr); // defaultLoadout -// String defaultLoadoutStr = ""; -// List items = war.getDefaultLoadout(); -// for(Item item : items) { -// defaultLoadoutStr += item.getItemId() + "," + item.getAmount() + "," + item.getSlot() + ";"; -// } -// warConfig.setString("defaultLoadout", defaultLoadoutStr); + String defaultLoadoutStr = ""; + HashMap items = war.getDefaultLoadout(); + for(Integer slot : items.keySet()) { + ItemStack item = items.get(slot); + defaultLoadoutStr += item.getTypeID() + "," + item.getAmount() + "," + slot + ";"; + } + warConfig.setString("defaultLoadout", defaultLoadoutStr); // defaultLifepool warConfig.setInt("defaultLifePool", war.getDefaultLifepool());