@@ -0,0 +1,99 @@
/*
* This file is part of Vanilla.
*
* Copyright (c) 2011-2012, SpoutDev <http://www.spout.org/>
* Vanilla is licensed under the SpoutDev License Version 1.
*
* Vanilla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, 180 days after any changes are published, you can use the
* software, incorporating those changes, under the terms of the MIT license,
* as described in the SpoutDev License Version 1.
*
* Vanilla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License,
* the MIT license and the SpoutDev License Version 1 along with this program.
* If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
* License and see <http://www.spout.org/SpoutDevLicenseV1.txt> for the full license,
* including the MIT license.
*/
package org.spout.vanilla.material.item.armor;

import org.spout.api.inventory.ItemStack;
import org.spout.vanilla.controller.source.DamageCause;
import org.spout.vanilla.enchantment.Enchantments;
import org.spout.vanilla.material.item.Enchantable;
import org.spout.vanilla.material.item.VanillaItemMaterial;
import org.spout.vanilla.util.EnchantmentUtil;

public abstract class Armor extends VanillaItemMaterial implements Enchantable {
private int protection;
private int enchantability;
private short durability;

protected Armor(String name, int id, short durability) {
super(name, id);
this.durability = durability;
}

public short getMaxDurability() {
return durability;
}

public void setMaxDurability(short durability) {
this.durability = durability;
}

public int getBaseProtection() {
return protection;
}

public int getProtection(ItemStack item, DamageCause cause) {
int amount = 0;
int level = 0;
// Armor can only have one of these protection enchantments
if (EnchantmentUtil.hasEnchantment(item, Enchantments.PROTECTION) && !cause.equals(DamageCause.COMMAND, DamageCause.VOID)) {
level = EnchantmentUtil.getEnchantmentLevel(item, Enchantments.PROTECTION);
} else if (EnchantmentUtil.hasEnchantment(item, Enchantments.BLAST_PROTECTION) && cause.equals(DamageCause.EXPLOSION)) {
level = EnchantmentUtil.getEnchantmentLevel(item, Enchantments.BLAST_PROTECTION);
} else if (EnchantmentUtil.hasEnchantment(item, Enchantments.FIRE_PROTECTION) && cause.equals(DamageCause.BURN, DamageCause.FIRE_CONTACT)) {
level = EnchantmentUtil.getEnchantmentLevel(item, Enchantments.FIRE_PROTECTION);
} else if (EnchantmentUtil.hasEnchantment(item, Enchantments.PROJECTILE_PROTECTION) && cause.equals(DamageCause.ARROW, DamageCause.FIREBALL)) {
level = EnchantmentUtil.getEnchantmentLevel(item, Enchantments.PROJECTILE_PROTECTION);
}
amount += level * 3;

// Feather Falling is compatible with all of the above enchantments
if (EnchantmentUtil.hasEnchantment(item, Enchantments.FEATHER_FALLING) && cause.equals(DamageCause.FALL)) {
amount += EnchantmentUtil.getEnchantmentLevel(item, Enchantments.FEATHER_FALLING);
}

return amount;
}

public void setBaseProtection(int protection) {
this.protection = protection;
}

@Override
public boolean getNBTData() {
return true;
}

@Override
public int getEnchantability() {
return enchantability;
}

@Override
public void setEnchantability(int enchantability) {
this.enchantability = enchantability;
}
}
@@ -26,11 +26,11 @@
*/
package org.spout.vanilla.material.item.armor.chain;

import org.spout.vanilla.material.item.Armor;
import org.spout.vanilla.material.item.armor.Armor;

public abstract class ChainArmor extends Armor {
protected ChainArmor(String name, int id, int protection) {
super(name, id, protection);
protected ChainArmor(String name, int id, short durability) {
super(name, id, durability);
this.setEnchantability(12);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Boots;

public class ChainBoots extends ChainArmor implements Boots {
public ChainBoots(String name, int id, int protection) {
super(name, id, protection);
public ChainBoots(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(1);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Chestplate;

public class ChainChestplate extends ChainArmor implements Chestplate {
public ChainChestplate(String name, int id, int protection) {
super(name, id, protection);
public ChainChestplate(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(5);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Helmet;

public class ChainHelmet extends ChainArmor implements Helmet {
public ChainHelmet(String name, int id, int protection) {
super(name, id, protection);
public ChainHelmet(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(2);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Leggings;

public class ChainLeggings extends ChainArmor implements Leggings {
public ChainLeggings(String name, int id, int protection) {
super(name, id, protection);
public ChainLeggings(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(4);
}
}
@@ -26,11 +26,11 @@
*/
package org.spout.vanilla.material.item.armor.diamond;

import org.spout.vanilla.material.item.Armor;
import org.spout.vanilla.material.item.armor.Armor;

public abstract class DiamondArmor extends Armor {
protected DiamondArmor(String name, int id, int protection) {
super(name, id, protection);
protected DiamondArmor(String name, int id, short durability) {
super(name, id, durability);
this.setEnchantability(10);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Boots;

public class DiamondBoots extends DiamondArmor implements Boots {
public DiamondBoots(String name, int id, int protection) {
super(name, id, protection);
public DiamondBoots(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(3);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Chestplate;

public class DiamondChestplate extends DiamondArmor implements Chestplate {
public DiamondChestplate(String name, int id, int protection) {
super(name, id, protection);
public DiamondChestplate(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(8);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Helmet;

public class DiamondHelmet extends DiamondArmor implements Helmet {
public DiamondHelmet(String name, int id, int protection) {
super(name, id, protection);
public DiamondHelmet(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(3);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Leggings;

public class DiamondLeggings extends DiamondArmor implements Leggings {
public DiamondLeggings(String name, int id, int protection) {
super(name, id, protection);
public DiamondLeggings(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(6);
}
}
@@ -26,11 +26,11 @@
*/
package org.spout.vanilla.material.item.armor.gold;

import org.spout.vanilla.material.item.Armor;
import org.spout.vanilla.material.item.armor.Armor;

public abstract class GoldArmor extends Armor {
protected GoldArmor(String name, int id, int protection) {
super(name, id, protection);
protected GoldArmor(String name, int id, short durability) {
super(name, id, durability);
this.setEnchantability(25);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Boots;

public class GoldBoots extends GoldArmor implements Boots {
public GoldBoots(String name, int id, int protection) {
super(name, id, protection);
public GoldBoots(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(1);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Chestplate;

public class GoldChestplate extends GoldArmor implements Chestplate {
public GoldChestplate(String name, int id, int protection) {
super(name, id, protection);
public GoldChestplate(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(5);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Helmet;

public class GoldHelmet extends GoldArmor implements Helmet {
public GoldHelmet(String name, int id, int protection) {
super(name, id, protection);
public GoldHelmet(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(2);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Leggings;

public class GoldLeggings extends GoldArmor implements Leggings {
public GoldLeggings(String name, int id, int protection) {
super(name, id, protection);
public GoldLeggings(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(3);
}
}
@@ -26,11 +26,11 @@
*/
package org.spout.vanilla.material.item.armor.iron;

import org.spout.vanilla.material.item.Armor;
import org.spout.vanilla.material.item.armor.Armor;

public abstract class IronArmor extends Armor {
protected IronArmor(String name, int id, int protection) {
super(name, id, protection);
protected IronArmor(String name, int id, short durability) {
super(name, id, durability);
this.setEnchantability(9);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Boots;

public class IronBoots extends IronArmor implements Boots {
public IronBoots(String name, int id, int protection) {
super(name, id, protection);
public IronBoots(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(2);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Chestplate;

public class IronChestplate extends IronArmor implements Chestplate {
public IronChestplate(String name, int id, int protection) {
super(name, id, protection);
public IronChestplate(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(6);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Helmet;

public class IronHelmet extends IronArmor implements Helmet {
public IronHelmet(String name, int id, int protection) {
super(name, id, protection);
public IronHelmet(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(2);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Leggings;

public class IronLeggings extends IronArmor implements Leggings {
public IronLeggings(String name, int id, int protection) {
super(name, id, protection);
public IronLeggings(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(5);
}
}
@@ -26,11 +26,11 @@
*/
package org.spout.vanilla.material.item.armor.leather;

import org.spout.vanilla.material.item.Armor;
import org.spout.vanilla.material.item.armor.Armor;

public abstract class LeatherArmor extends Armor {
protected LeatherArmor(String name, int id, int protection) {
super(name, id, protection);
protected LeatherArmor(String name, int id, short durability) {
super(name, id, durability);
this.setEnchantability(15);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Boots;

public class LeatherBoots extends LeatherArmor implements Boots {
public LeatherBoots(String name, int id, int protection) {
super(name, id, protection);
public LeatherBoots(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(1);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Helmet;

public class LeatherCap extends LeatherArmor implements Helmet {
public LeatherCap(String name, int id, int protection) {
super(name, id, protection);
public LeatherCap(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(1);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Leggings;

public class LeatherPants extends LeatherArmor implements Leggings {
public LeatherPants(String name, int id, int protection) {
super(name, id, protection);
public LeatherPants(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(2);
}
}
@@ -29,7 +29,8 @@
import org.spout.vanilla.material.item.armor.Chestplate;

public class LeatherTunic extends LeatherArmor implements Chestplate {
public LeatherTunic(String name, int id, int protection) {
super(name, id, protection);
public LeatherTunic(String name, int id, short durability) {
super(name, id, durability);
this.setBaseProtection(3);
}
}
@@ -104,8 +104,7 @@ public VanillaHandlerLookupService() {
}

protected static <T extends Message> void bind(Class<T> clazz, Class<? extends MessageHandler<T>> handlerClass) throws InstantiationException, IllegalAccessException {
MessageHandler<T> handler = handlerClass.newInstance();
handlers.put(clazz, handler);
handlers.put(clazz, handlerClass.newInstance());
}

@Override
@@ -150,7 +150,8 @@ public void handleServer(Session session, Player player, PlayerDiggingMessage me
} else {
damageDone = ((int) diggingTicks * ((VanillaMaterial) heldItem.getMaterial()).getDamage());
}
// TODO: Take into account enchantments
// TODO: Take into account EFFICIENCY enchantment
// TODO: Digging is slower while under water, on ladders, etc. AQUA_AFFINITY enchantment speeds up underwater digging

totalDamage = ((int) blockMaterial.getHardness() - damageDone);
if (totalDamage <= 40) { // Yes, this is a very high allowance - this is because this is only over a single block, and this will spike due to varying latency.
@@ -0,0 +1,25 @@
package org.spout.vanilla.item;

import org.junit.Test;

import org.spout.api.inventory.ItemStack;
import org.spout.vanilla.controller.source.DamageCause;
import org.spout.vanilla.enchantment.Enchantments;
import org.spout.vanilla.material.VanillaMaterials;
import org.spout.vanilla.material.item.armor.Armor;
import org.spout.vanilla.util.EnchantmentUtil;

import static org.junit.Assert.assertTrue;

public class DamageTest {

@Test
public void testDamageModifier() {
ItemStack test = new ItemStack(VanillaMaterials.DIAMOND_CHESTPLATE, 1);
EnchantmentUtil.addEnchantment(test, Enchantments.PROTECTION, 4, false);
assertTrue(EnchantmentUtil.hasEnchantment(test, Enchantments.PROTECTION));

Armor armor = (Armor) test.getMaterial();
assertTrue((int) Math.ceil(.04 * (armor.getBaseProtection() + armor.getProtection(test, DamageCause.CACTUS))) == 1);
}
}