From a37766d31b91b85b1299a415e5719f8ce1ebb0b6 Mon Sep 17 00:00:00 2001 From: taoneill Date: Mon, 19 Dec 2011 16:01:33 -0500 Subject: [PATCH] Closes gh-254. Closes gh-321. Closes gh-312. Closes gh-322. Truns out I wasn't saving inventory item damge or data values, which led to potions not working and items sometimes breaking on first use randomly. Instant damage (splash harming) potions now work properly when you are hitting yourself at the same time with splash damage. Self-inflicted damage is now properly recorded. Fixed one or two NPEs. --- .../main/java/bukkit/tommytony/war/War.java | 14 ++- .../tommytony/war/WarEntityListener.java | 54 ++++++----- .../main/java/com/tommytony/war/WarHub.java | 39 ++++---- .../main/java/com/tommytony/war/Warzone.java | 14 ++- .../tommytony/war/mappers/WarzoneMapper.java | 90 ++++++++----------- .../com/tommytony/war/utils/SignHelper.java | 29 +++--- 6 files changed, 126 insertions(+), 114 deletions(-) diff --git a/war/src/main/java/bukkit/tommytony/war/War.java b/war/src/main/java/bukkit/tommytony/war/War.java index 8643a48..d02b020 100644 --- a/war/src/main/java/bukkit/tommytony/war/War.java +++ b/war/src/main/java/bukkit/tommytony/war/War.java @@ -242,21 +242,27 @@ public class War extends JavaPlugin { int i = 0; for (ItemStack stack : inv.getContents()) { if (stack != null && stack.getType() != Material.AIR) { - loadout.put(i, stack); + loadout.put(i, this.copyStack(stack)); i++; } } if (inv.getBoots() != null && inv.getBoots().getType() != Material.AIR) { - loadout.put(100, inv.getBoots()); + loadout.put(100, this.copyStack(inv.getBoots())); } if (inv.getLeggings() != null && inv.getLeggings().getType() != Material.AIR) { - loadout.put(101, inv.getLeggings()); + loadout.put(101, this.copyStack(inv.getLeggings())); } if (inv.getChestplate() != null && inv.getChestplate().getType() != Material.AIR) { - loadout.put(102, inv.getChestplate()); + loadout.put(102, this.copyStack(inv.getChestplate())); } } + + private ItemStack copyStack(ItemStack originalStack) { + ItemStack copiedStack = new ItemStack(originalStack.getType(), originalStack.getAmount(), originalStack.getDurability(), new Byte(originalStack.getData().getData())); + copiedStack.setDurability(originalStack.getDurability()); + return copiedStack; + } /** * Converts the player-inventory to a loadout hashmap diff --git a/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java b/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java index 1ff745a..14df564 100644 --- a/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java +++ b/war/src/main/java/bukkit/tommytony/war/WarEntityListener.java @@ -20,6 +20,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.entity.TNTPrimed; +import org.bukkit.entity.ThrownPotion; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EntityCombustEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -57,6 +58,8 @@ public class WarEntityListener extends EntityListener { Entity attacker = event.getDamager(); Entity defender = event.getEntity(); + //DamageCause cause = event.getCause(); + //War.war.log(cause.toString(), Level.INFO); // Maybe an arrow was thrown if (attacker != null && event.getDamager() instanceof Projectile && ((Projectile)event.getDamager()).getShooter() instanceof Player){ attacker = ((Player)((Projectile)event.getDamager()).getShooter()); @@ -70,8 +73,9 @@ public class WarEntityListener extends EntityListener { Team attackerTeam = Team.getTeamByPlayerName(a.getName()); Warzone defenderWarzone = Warzone.getZoneByPlayerName(d.getName()); Team defenderTeam = Team.getTeamByPlayerName(d.getName()); - - if (attackerTeam != null && defenderTeam != null && attackerTeam != defenderTeam && attackerWarzone == defenderWarzone) { + + if ((attackerTeam != null && defenderTeam != null && attackerTeam != defenderTeam && attackerWarzone == defenderWarzone) + || (attackerTeam != null && defenderTeam != null && attacker.getEntityId() == defender.getEntityId())) { // Make sure one of the players isn't in the spawn if (defenderTeam.getSpawnVolume().contains(d.getLocation())) { // attacking person in spawn if (!defenderWarzone.isFlagThief(d.getName())) { // thieves can always be attacked @@ -93,6 +97,10 @@ public class WarEntityListener extends EntityListener { event.setCancelled(true); return; } + + if (attackerTeam != null && defenderTeam != null && attacker.getEntityId() == defender.getEntityId()) { + War.war.badMsg(a, "You hit yourself!"); + } // Detect death, prevent it and respawn the player if (event.getDamage() >= d.getHealth()) { @@ -100,30 +108,34 @@ public class WarEntityListener extends EntityListener { String attackerString = attackerTeam.getKind().getColor() + a.getDisplayName(); String defenderString = defenderTeam.getKind().getColor() + d.getDisplayName(); - Material killerWeapon = a.getItemInHand().getType(); - String weaponString = killerWeapon.toString(); - if (killerWeapon == Material.AIR) { - weaponString = "fist"; - } else if (killerWeapon == Material.BOW || event.getDamager() instanceof Arrow) { - int rand = killSeed.nextInt(3); - if (rand == 0) { - weaponString = "arrow"; - } else if (rand == 1) { - weaponString = "bow"; - } else { + if (attacker.getEntityId() != defender.getEntityId()) { + Material killerWeapon = a.getItemInHand().getType(); + String weaponString = killerWeapon.toString(); + if (killerWeapon == Material.AIR) { + weaponString = "hand"; + } else if (killerWeapon == Material.BOW || event.getDamager() instanceof Arrow) { + int rand = killSeed.nextInt(3); + if (rand == 0) { + weaponString = "arrow"; + } else if (rand == 1) { + weaponString = "bow"; + } else { + weaponString = "aim"; + } + + } else if (event.getDamager() instanceof Projectile) { weaponString = "aim"; } - } else if (event.getDamager() instanceof Projectile) { - weaponString = "aim"; + String adjectiveString = War.war.getDeadlyAdjectives().get(this.killSeed.nextInt(War.war.getDeadlyAdjectives().size())); + String verbString = War.war.getKillerVerbs().get(this.killSeed.nextInt(War.war.getKillerVerbs().size())); + + killMessage = attackerString + ChatColor.WHITE + "'s " + adjectiveString + weaponString.toLowerCase().replace('_', ' ') + + " " + verbString + " " + defenderString; + } else { + killMessage = defenderString + ChatColor.WHITE + " committed accidental suicide"; } - String adjectiveString = War.war.getDeadlyAdjectives().get(this.killSeed.nextInt(War.war.getDeadlyAdjectives().size())); - String verbString = War.war.getKillerVerbs().get(this.killSeed.nextInt(War.war.getKillerVerbs().size())); - - killMessage = attackerString + ChatColor.WHITE + "'s " + adjectiveString + weaponString.toLowerCase().replace('_', ' ') - + " " + verbString + " " + defenderString; - for (Team team : defenderWarzone.getTeams()) { team.teamcast(killMessage); } diff --git a/war/src/main/java/com/tommytony/war/WarHub.java b/war/src/main/java/com/tommytony/war/WarHub.java index aed5e51..974033a 100644 --- a/war/src/main/java/com/tommytony/war/WarHub.java +++ b/war/src/main/java/com/tommytony/war/WarHub.java @@ -2,6 +2,7 @@ package com.tommytony.war; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; import org.bukkit.Location; import org.bukkit.Material; @@ -220,24 +221,28 @@ public class WarHub { } Block zoneGate = this.zoneGateBlocks.get(zone.getName()); - Block block = zoneGate.getFace(left).getFace(back, 1); - if (block.getType() != Material.SIGN_POST) { - block.setType(Material.SIGN_POST); + if (zoneGate != null) { + Block block = zoneGate.getFace(left).getFace(back, 1); + if (block.getType() != Material.SIGN_POST) { + block.setType(Material.SIGN_POST); + } + block.setData(data); + + int zoneCap = 0; + int zonePlayers = 0; + for (Team t : zone.getTeams()) { + zonePlayers += t.getPlayers().size(); + zoneCap += zone.getTeamCap(); + } + String[] lines = new String[4]; + lines[0] = "Warzone"; + lines[1] = zone.getName(); + lines[2] = zonePlayers + "/" + zoneCap + " players"; + lines[3] = zone.getTeams().size() + " teams"; + SignHelper.setToSign(War.war, block, data, lines); + } else { + War.war.log("Failed to find warhub gate for " + zone.getName() + " warzone.", Level.WARNING); } - block.setData(data); - - int zoneCap = 0; - int zonePlayers = 0; - for (Team t : zone.getTeams()) { - zonePlayers += t.getPlayers().size(); - zoneCap += zone.getTeamCap(); - } - String[] lines = new String[4]; - lines[0] = "Warzone"; - lines[1] = zone.getName(); - lines[2] = zonePlayers + "/" + zoneCap + " players"; - lines[3] = zone.getTeams().size() + " teams"; - SignHelper.setToSign(War.war, block, data, lines); } public void setVolume(Volume vol) { diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index 44c1b35..f22c2be 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -356,15 +356,15 @@ public class Warzone { playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead for (Integer slot : loadout.keySet()) { if (slot == 100) { - playerInv.setBoots(loadout.get(slot)); + playerInv.setBoots(this.copyStack(loadout.get(slot))); } else if (slot == 101) { - playerInv.setLeggings(loadout.get(slot)); + playerInv.setLeggings(this.copyStack(loadout.get(slot))); } else if (slot == 102) { - playerInv.setChestplate(loadout.get(slot)); + playerInv.setChestplate(this.copyStack(loadout.get(slot))); } else { ItemStack item = loadout.get(slot); if (item != null) { - playerInv.addItem(item); + playerInv.addItem(this.copyStack(item)); } } } @@ -382,6 +382,12 @@ public class Warzone { } } } + + private ItemStack copyStack(ItemStack originalStack) { + ItemStack copiedStack = new ItemStack(originalStack.getType(), originalStack.getAmount(), originalStack.getDurability(), new Byte(originalStack.getData().getData())); + copiedStack.setDurability(originalStack.getDurability()); + return copiedStack; + } public boolean isMonumentCenterBlock(Block block) { for (Monument monument : this.monuments) { diff --git a/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java b/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java index 1d37849..afb00b8 100644 --- a/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java +++ b/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java @@ -132,15 +132,7 @@ public class WarzoneMapper { // loadout String loadoutStr = warzoneConfig.getString("loadout"); if (loadoutStr != null && !loadoutStr.equals("")) { - String[] loadoutStrSplit = loadoutStr.split(";"); - warzone.getLoadout().clear(); - for (String itemStr : loadoutStrSplit) { - if (itemStr != null && !itemStr.equals("")) { - String[] itemStrSplit = itemStr.split(","); - ItemStack item = new ItemStack(Integer.parseInt(itemStrSplit[0]), Integer.parseInt(itemStrSplit[1])); - warzone.getLoadout().put(Integer.parseInt(itemStrSplit[2]), item); - } - } + fromStringToLoadout(loadoutStr, warzone.getLoadout()); } // extraLoadouts @@ -155,16 +147,8 @@ public class WarzoneMapper { for (String extraName : warzone.getExtraLoadouts().keySet()) { String loadoutString = warzoneConfig.getString(extraName + "Loadout"); - String[] loadoutSplit = loadoutString.split(";"); HashMap loadout = warzone.getExtraLoadouts().get(extraName); - loadout.clear(); - for (String str : loadoutSplit) { - if (str != null && !str.equals("")) { - String[] strSplit = str.split(","); - ItemStack item = new ItemStack(Integer.parseInt(strSplit[0]), Integer.parseInt(strSplit[1])); - loadout.put(Integer.parseInt(strSplit[2]), item); - } - } + fromStringToLoadout(loadoutString, loadout); } // authors @@ -226,15 +210,7 @@ public class WarzoneMapper { // reward String rewardStr = warzoneConfig.getString("reward"); if (rewardStr != null && !rewardStr.equals("")) { - String[] rewardStrSplit = rewardStr.split(";"); - warzone.getReward().clear(); - for (String itemStr : rewardStrSplit) { - if (itemStr != null && !itemStr.equals("")) { - String[] itemStrSplit = itemStr.split(","); - ItemStack item = new ItemStack(Integer.parseInt(itemStrSplit[0]), Integer.parseInt(itemStrSplit[1])); - warzone.getReward().put(Integer.parseInt(itemStrSplit[2]), item); - } - } + fromStringToLoadout(rewardStr, warzone.getReward()); } // unbreakableZoneBlocks @@ -458,28 +434,15 @@ public class WarzoneMapper { // loadout String loadoutStr = ""; HashMap items = warzone.getLoadout(); - for (Integer slot : items.keySet()) { - ItemStack item = items.get(slot); - if (item != null) { - loadoutStr += item.getTypeId() + "," + item.getAmount() + "," + slot + ";"; - } - } - warzoneConfig.setString("loadout", loadoutStr); + warzoneConfig.setString("loadout", fromLoadoutToString(items)); // defaultExtraLoadouts String extraLoadoutsStr = ""; for (String name : warzone.getExtraLoadouts().keySet()) { extraLoadoutsStr += name + ","; - String str = ""; HashMap loadout = warzone.getExtraLoadouts().get(name); - for (Integer slot : loadout.keySet()) { - ItemStack item = loadout.get(slot); - if (item != null) { - str += item.getTypeId() + "," + item.getAmount() + "," + slot + ";"; - } - } - warzoneConfig.setString(name + "Loadout", str); + warzoneConfig.setString(name + "Loadout", fromLoadoutToString(loadout)); } warzoneConfig.setString("extraLoadouts", extraLoadoutsStr); @@ -516,13 +479,7 @@ public class WarzoneMapper { // reward String rewardStr = ""; HashMap rewardItems = warzone.getReward(); - for (Integer slot : rewardItems.keySet()) { - ItemStack item = rewardItems.get(slot); - if (item != null) { - rewardStr += item.getTypeId() + "," + item.getAmount() + "," + slot + ";"; - } - } - warzoneConfig.setString("reward", rewardStr); + warzoneConfig.setString("reward", fromLoadoutToString(rewardItems)); // unbreakableZoneBlocks warzoneConfig.setBoolean("unbreakableZoneBlocks", warzone.isUnbreakableZoneBlocks()); @@ -604,11 +561,6 @@ public class WarzoneMapper { warzoneConfig.save(); warzoneConfig.close(); - if (saveAllBlocks) { - // zone blocks - // VolumeMapper.save(warzone.getVolume(), warzone.getName(), war); - } - // monument blocks for (Monument monument : monuments) { VolumeMapper.save(monument.getVolume(), warzone.getName()); @@ -646,4 +598,34 @@ public class WarzoneMapper { War.war.log("Failed to delete file " + zoneFile.getName(), Level.WARNING); } } + + private static String fromLoadoutToString(HashMap loadout) { + String loadoutString = ""; + for (Integer slot : loadout.keySet()) { + ItemStack item = loadout.get(slot); + if (item != null) { + loadoutString += item.getTypeId() + "," + item.getAmount() + "," + slot + "," + item.getDurability() + "," + item.getData().getData() + ";"; + } + } + return loadoutString; + } + + private static void fromStringToLoadout(String loadoutString, HashMap destinationLoadout) { + String[] rewardStrSplit = loadoutString.split(";"); + destinationLoadout.clear(); + for (String itemStr : rewardStrSplit) { + if (itemStr != null && !itemStr.equals("")) { + String[] itemStrSplit = itemStr.split(","); + ItemStack item = null; + if (itemStrSplit.length == 3) { + item = new ItemStack(Integer.parseInt(itemStrSplit[0]), Integer.parseInt(itemStrSplit[1])); + } else if (itemStrSplit.length == 5) { + short durability = Short.parseShort(itemStrSplit[3]); + item = new ItemStack(Integer.parseInt(itemStrSplit[0]), Integer.parseInt(itemStrSplit[1]), durability, Byte.parseByte(itemStrSplit[4])); + item.setDurability(durability); + } + destinationLoadout.put(Integer.parseInt(itemStrSplit[2]), item); + } + } + } } diff --git a/war/src/main/java/com/tommytony/war/utils/SignHelper.java b/war/src/main/java/com/tommytony/war/utils/SignHelper.java index a6ff922..2b7841b 100644 --- a/war/src/main/java/com/tommytony/war/utils/SignHelper.java +++ b/war/src/main/java/com/tommytony/war/utils/SignHelper.java @@ -13,21 +13,22 @@ public class SignHelper { if (block.getType() != Material.SIGN_POST) { block.setType(Material.SIGN_POST); } - BlockState state = block.getState(); - state.setData(new org.bukkit.material.Sign(Material.SIGN_POST, data)); - if (state instanceof Sign) { - Sign sign = (Sign) state; - try { - if (sign.getLines() != null) { - sign.setLine(0, lines[0]); - sign.setLine(1, lines[1]); - sign.setLine(2, lines[2]); - sign.setLine(3, lines[3]); - sign.update(true); - } - } catch (Exception e) { - // just can't stand this anymore + try { + BlockState state = block.getState(); + state.setData(new org.bukkit.material.Sign(Material.SIGN_POST, data)); + if (state instanceof Sign) { + Sign sign = (Sign) state; + + if (sign.getLines() != null) { + sign.setLine(0, lines[0]); + sign.setLine(1, lines[1]); + sign.setLine(2, lines[2]); + sign.setLine(3, lines[3]); + sign.update(true); + } } + } catch (Exception e) { + // just can't stand this anymore } }