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.

This commit is contained in:
taoneill 2011-12-19 16:01:33 -05:00
parent ccd6649d06
commit a37766d31b
6 changed files with 126 additions and 114 deletions

View File

@ -242,21 +242,27 @@ public class War extends JavaPlugin {
int i = 0; int i = 0;
for (ItemStack stack : inv.getContents()) { for (ItemStack stack : inv.getContents()) {
if (stack != null && stack.getType() != Material.AIR) { if (stack != null && stack.getType() != Material.AIR) {
loadout.put(i, stack); loadout.put(i, this.copyStack(stack));
i++; i++;
} }
} }
if (inv.getBoots() != null && inv.getBoots().getType() != Material.AIR) { 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) { 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) { 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 * Converts the player-inventory to a loadout hashmap

View File

@ -20,6 +20,7 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile; import org.bukkit.entity.Projectile;
import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.TNTPrimed;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityCombustEvent; import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
@ -57,6 +58,8 @@ public class WarEntityListener extends EntityListener {
Entity attacker = event.getDamager(); Entity attacker = event.getDamager();
Entity defender = event.getEntity(); Entity defender = event.getEntity();
//DamageCause cause = event.getCause();
//War.war.log(cause.toString(), Level.INFO);
// Maybe an arrow was thrown // Maybe an arrow was thrown
if (attacker != null && event.getDamager() instanceof Projectile && ((Projectile)event.getDamager()).getShooter() instanceof Player){ if (attacker != null && event.getDamager() instanceof Projectile && ((Projectile)event.getDamager()).getShooter() instanceof Player){
attacker = ((Player)((Projectile)event.getDamager()).getShooter()); attacker = ((Player)((Projectile)event.getDamager()).getShooter());
@ -70,8 +73,9 @@ public class WarEntityListener extends EntityListener {
Team attackerTeam = Team.getTeamByPlayerName(a.getName()); Team attackerTeam = Team.getTeamByPlayerName(a.getName());
Warzone defenderWarzone = Warzone.getZoneByPlayerName(d.getName()); Warzone defenderWarzone = Warzone.getZoneByPlayerName(d.getName());
Team defenderTeam = Team.getTeamByPlayerName(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 // Make sure one of the players isn't in the spawn
if (defenderTeam.getSpawnVolume().contains(d.getLocation())) { // attacking person in spawn if (defenderTeam.getSpawnVolume().contains(d.getLocation())) { // attacking person in spawn
if (!defenderWarzone.isFlagThief(d.getName())) { // thieves can always be attacked if (!defenderWarzone.isFlagThief(d.getName())) { // thieves can always be attacked
@ -93,6 +97,10 @@ public class WarEntityListener extends EntityListener {
event.setCancelled(true); event.setCancelled(true);
return; 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 // Detect death, prevent it and respawn the player
if (event.getDamage() >= d.getHealth()) { if (event.getDamage() >= d.getHealth()) {
@ -100,30 +108,34 @@ public class WarEntityListener extends EntityListener {
String attackerString = attackerTeam.getKind().getColor() + a.getDisplayName(); String attackerString = attackerTeam.getKind().getColor() + a.getDisplayName();
String defenderString = defenderTeam.getKind().getColor() + d.getDisplayName(); String defenderString = defenderTeam.getKind().getColor() + d.getDisplayName();
Material killerWeapon = a.getItemInHand().getType(); if (attacker.getEntityId() != defender.getEntityId()) {
String weaponString = killerWeapon.toString(); Material killerWeapon = a.getItemInHand().getType();
if (killerWeapon == Material.AIR) { String weaponString = killerWeapon.toString();
weaponString = "fist"; if (killerWeapon == Material.AIR) {
} else if (killerWeapon == Material.BOW || event.getDamager() instanceof Arrow) { weaponString = "hand";
int rand = killSeed.nextInt(3); } else if (killerWeapon == Material.BOW || event.getDamager() instanceof Arrow) {
if (rand == 0) { int rand = killSeed.nextInt(3);
weaponString = "arrow"; if (rand == 0) {
} else if (rand == 1) { weaponString = "arrow";
weaponString = "bow"; } else if (rand == 1) {
} else { weaponString = "bow";
} else {
weaponString = "aim";
}
} else if (event.getDamager() instanceof Projectile) {
weaponString = "aim"; weaponString = "aim";
} }
} else if (event.getDamager() instanceof Projectile) { String adjectiveString = War.war.getDeadlyAdjectives().get(this.killSeed.nextInt(War.war.getDeadlyAdjectives().size()));
weaponString = "aim"; 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()) { for (Team team : defenderWarzone.getTeams()) {
team.teamcast(killMessage); team.teamcast(killMessage);
} }

View File

@ -2,6 +2,7 @@ package com.tommytony.war;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -220,24 +221,28 @@ public class WarHub {
} }
Block zoneGate = this.zoneGateBlocks.get(zone.getName()); Block zoneGate = this.zoneGateBlocks.get(zone.getName());
Block block = zoneGate.getFace(left).getFace(back, 1); if (zoneGate != null) {
if (block.getType() != Material.SIGN_POST) { Block block = zoneGate.getFace(left).getFace(back, 1);
block.setType(Material.SIGN_POST); 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) { public void setVolume(Volume vol) {

View File

@ -356,15 +356,15 @@ public class Warzone {
playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
for (Integer slot : loadout.keySet()) { for (Integer slot : loadout.keySet()) {
if (slot == 100) { if (slot == 100) {
playerInv.setBoots(loadout.get(slot)); playerInv.setBoots(this.copyStack(loadout.get(slot)));
} else if (slot == 101) { } else if (slot == 101) {
playerInv.setLeggings(loadout.get(slot)); playerInv.setLeggings(this.copyStack(loadout.get(slot)));
} else if (slot == 102) { } else if (slot == 102) {
playerInv.setChestplate(loadout.get(slot)); playerInv.setChestplate(this.copyStack(loadout.get(slot)));
} else { } else {
ItemStack item = loadout.get(slot); ItemStack item = loadout.get(slot);
if (item != null) { 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) { public boolean isMonumentCenterBlock(Block block) {
for (Monument monument : this.monuments) { for (Monument monument : this.monuments) {

View File

@ -132,15 +132,7 @@ public class WarzoneMapper {
// loadout // loadout
String loadoutStr = warzoneConfig.getString("loadout"); String loadoutStr = warzoneConfig.getString("loadout");
if (loadoutStr != null && !loadoutStr.equals("")) { if (loadoutStr != null && !loadoutStr.equals("")) {
String[] loadoutStrSplit = loadoutStr.split(";"); fromStringToLoadout(loadoutStr, warzone.getLoadout());
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);
}
}
} }
// extraLoadouts // extraLoadouts
@ -155,16 +147,8 @@ public class WarzoneMapper {
for (String extraName : warzone.getExtraLoadouts().keySet()) { for (String extraName : warzone.getExtraLoadouts().keySet()) {
String loadoutString = warzoneConfig.getString(extraName + "Loadout"); String loadoutString = warzoneConfig.getString(extraName + "Loadout");
String[] loadoutSplit = loadoutString.split(";");
HashMap<Integer, ItemStack> loadout = warzone.getExtraLoadouts().get(extraName); HashMap<Integer, ItemStack> loadout = warzone.getExtraLoadouts().get(extraName);
loadout.clear(); fromStringToLoadout(loadoutString, loadout);
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);
}
}
} }
// authors // authors
@ -226,15 +210,7 @@ public class WarzoneMapper {
// reward // reward
String rewardStr = warzoneConfig.getString("reward"); String rewardStr = warzoneConfig.getString("reward");
if (rewardStr != null && !rewardStr.equals("")) { if (rewardStr != null && !rewardStr.equals("")) {
String[] rewardStrSplit = rewardStr.split(";"); fromStringToLoadout(rewardStr, warzone.getReward());
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);
}
}
} }
// unbreakableZoneBlocks // unbreakableZoneBlocks
@ -458,28 +434,15 @@ public class WarzoneMapper {
// loadout // loadout
String loadoutStr = ""; String loadoutStr = "";
HashMap<Integer, ItemStack> items = warzone.getLoadout(); HashMap<Integer, ItemStack> items = warzone.getLoadout();
for (Integer slot : items.keySet()) { warzoneConfig.setString("loadout", fromLoadoutToString(items));
ItemStack item = items.get(slot);
if (item != null) {
loadoutStr += item.getTypeId() + "," + item.getAmount() + "," + slot + ";";
}
}
warzoneConfig.setString("loadout", loadoutStr);
// defaultExtraLoadouts // defaultExtraLoadouts
String extraLoadoutsStr = ""; String extraLoadoutsStr = "";
for (String name : warzone.getExtraLoadouts().keySet()) { for (String name : warzone.getExtraLoadouts().keySet()) {
extraLoadoutsStr += name + ","; extraLoadoutsStr += name + ",";
String str = "";
HashMap<Integer, ItemStack> loadout = warzone.getExtraLoadouts().get(name); HashMap<Integer, ItemStack> loadout = warzone.getExtraLoadouts().get(name);
for (Integer slot : loadout.keySet()) { warzoneConfig.setString(name + "Loadout", fromLoadoutToString(loadout));
ItemStack item = loadout.get(slot);
if (item != null) {
str += item.getTypeId() + "," + item.getAmount() + "," + slot + ";";
}
}
warzoneConfig.setString(name + "Loadout", str);
} }
warzoneConfig.setString("extraLoadouts", extraLoadoutsStr); warzoneConfig.setString("extraLoadouts", extraLoadoutsStr);
@ -516,13 +479,7 @@ public class WarzoneMapper {
// reward // reward
String rewardStr = ""; String rewardStr = "";
HashMap<Integer, ItemStack> rewardItems = warzone.getReward(); HashMap<Integer, ItemStack> rewardItems = warzone.getReward();
for (Integer slot : rewardItems.keySet()) { warzoneConfig.setString("reward", fromLoadoutToString(rewardItems));
ItemStack item = rewardItems.get(slot);
if (item != null) {
rewardStr += item.getTypeId() + "," + item.getAmount() + "," + slot + ";";
}
}
warzoneConfig.setString("reward", rewardStr);
// unbreakableZoneBlocks // unbreakableZoneBlocks
warzoneConfig.setBoolean("unbreakableZoneBlocks", warzone.isUnbreakableZoneBlocks()); warzoneConfig.setBoolean("unbreakableZoneBlocks", warzone.isUnbreakableZoneBlocks());
@ -604,11 +561,6 @@ public class WarzoneMapper {
warzoneConfig.save(); warzoneConfig.save();
warzoneConfig.close(); warzoneConfig.close();
if (saveAllBlocks) {
// zone blocks
// VolumeMapper.save(warzone.getVolume(), warzone.getName(), war);
}
// monument blocks // monument blocks
for (Monument monument : monuments) { for (Monument monument : monuments) {
VolumeMapper.save(monument.getVolume(), warzone.getName()); VolumeMapper.save(monument.getVolume(), warzone.getName());
@ -646,4 +598,34 @@ public class WarzoneMapper {
War.war.log("Failed to delete file " + zoneFile.getName(), Level.WARNING); War.war.log("Failed to delete file " + zoneFile.getName(), Level.WARNING);
} }
} }
private static String fromLoadoutToString(HashMap<Integer, ItemStack> 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<Integer, ItemStack> 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);
}
}
}
} }

View File

@ -13,21 +13,22 @@ public class SignHelper {
if (block.getType() != Material.SIGN_POST) { if (block.getType() != Material.SIGN_POST) {
block.setType(Material.SIGN_POST); block.setType(Material.SIGN_POST);
} }
BlockState state = block.getState(); try {
state.setData(new org.bukkit.material.Sign(Material.SIGN_POST, data)); BlockState state = block.getState();
if (state instanceof Sign) { state.setData(new org.bukkit.material.Sign(Material.SIGN_POST, data));
Sign sign = (Sign) state; if (state instanceof Sign) {
try { Sign sign = (Sign) state;
if (sign.getLines() != null) {
sign.setLine(0, lines[0]); if (sign.getLines() != null) {
sign.setLine(1, lines[1]); sign.setLine(0, lines[0]);
sign.setLine(2, lines[2]); sign.setLine(1, lines[1]);
sign.setLine(3, lines[3]); sign.setLine(2, lines[2]);
sign.update(true); sign.setLine(3, lines[3]);
} sign.update(true);
} catch (Exception e) { }
// just can't stand this anymore
} }
} catch (Exception e) {
// just can't stand this anymore
} }
} }