Small optimization for armor searching

This commit is contained in:
HugoDaBosss 2016-03-18 22:57:16 +01:00
parent 615128eba2
commit 9b6a9fca3d

View File

@ -1,81 +1,90 @@
package us.myles.ViaVersion.protocols.protocol1_9to1_8; package us.myles.ViaVersion.protocols.protocol1_9to1_8;
import lombok.Getter; import java.util.HashMap;
import lombok.RequiredArgsConstructor;
import org.bukkit.Material; import lombok.Getter;
import org.bukkit.inventory.ItemStack; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor import org.bukkit.Material;
@Getter import org.bukkit.inventory.ItemStack;
public enum ArmorType {
@RequiredArgsConstructor
LEATHER_HELMET(1, 298, Material.LEATHER_HELMET), @Getter
LEATHER_CHESTPLATE(3, 299, Material.LEATHER_CHESTPLATE), public enum ArmorType {
LEATHER_LEGGINGS(2, 300, Material.LEATHER_LEGGINGS),
LEATHER_BOOTS(1, 301, Material.LEATHER_BOOTS), LEATHER_HELMET(1, 298, Material.LEATHER_HELMET),
CHAINMAIL_HELMET(2, 302, Material.CHAINMAIL_HELMET), LEATHER_CHESTPLATE(3, 299, Material.LEATHER_CHESTPLATE),
CHAINMAIL_CHESTPLATE(5, 303, Material.CHAINMAIL_CHESTPLATE), LEATHER_LEGGINGS(2, 300, Material.LEATHER_LEGGINGS),
CHAINMAIL_LEGGINGS(4, 304, Material.CHAINMAIL_LEGGINGS), LEATHER_BOOTS(1, 301, Material.LEATHER_BOOTS),
CHAINMAIL_BOOTS(1, 305, Material.CHAINMAIL_BOOTS), CHAINMAIL_HELMET(2, 302, Material.CHAINMAIL_HELMET),
IRON_HELMET(2, 306, Material.IRON_HELMET), CHAINMAIL_CHESTPLATE(5, 303, Material.CHAINMAIL_CHESTPLATE),
IRON_CHESTPLATE(6, 307, Material.IRON_CHESTPLATE), CHAINMAIL_LEGGINGS(4, 304, Material.CHAINMAIL_LEGGINGS),
IRON_LEGGINGS(5, 308, Material.IRON_LEGGINGS), CHAINMAIL_BOOTS(1, 305, Material.CHAINMAIL_BOOTS),
IRON_BOOTS(2, 309, Material.IRON_BOOTS), IRON_HELMET(2, 306, Material.IRON_HELMET),
DIAMOND_HELMET(3, 310, Material.DIAMOND_HELMET), IRON_CHESTPLATE(6, 307, Material.IRON_CHESTPLATE),
DIAMOND_CHESTPLATE(8, 311, Material.DIAMOND_CHESTPLATE), IRON_LEGGINGS(5, 308, Material.IRON_LEGGINGS),
DIAMOND_LEGGINGS(6, 312, Material.DIAMOND_LEGGINGS), IRON_BOOTS(2, 309, Material.IRON_BOOTS),
DIAMOND_BOOTS(3, 313, Material.DIAMOND_BOOTS), DIAMOND_HELMET(3, 310, Material.DIAMOND_HELMET),
GOLD_HELMET(2, 314, Material.GOLD_HELMET), DIAMOND_CHESTPLATE(8, 311, Material.DIAMOND_CHESTPLATE),
GOLD_CHESTPLATE(5, 315, Material.GOLD_CHESTPLATE), DIAMOND_LEGGINGS(6, 312, Material.DIAMOND_LEGGINGS),
GOLD_LEGGINGS(3, 316, Material.GOLD_LEGGINGS), DIAMOND_BOOTS(3, 313, Material.DIAMOND_BOOTS),
GOLD_BOOTS(1, 317, Material.GOLD_BOOTS), GOLD_HELMET(2, 314, Material.GOLD_HELMET),
NONE(0, 0, Material.AIR); GOLD_CHESTPLATE(5, 315, Material.GOLD_CHESTPLATE),
GOLD_LEGGINGS(3, 316, Material.GOLD_LEGGINGS),
private final int armorPoints; GOLD_BOOTS(1, 317, Material.GOLD_BOOTS),
private final int id; NONE(0, 0, Material.AIR);
private final Material type;
private final int armorPoints;
public static ArmorType findByType(Material type) { private final int id;
for (ArmorType a : ArmorType.values()) private final Material type;
if (a.getType() == type)
return a; private static HashMap<Material, ArmorType> armor;
return ArmorType.NONE; static {
} armor = new HashMap<Material, ArmorType>();
for(ArmorType a : ArmorType.values()) {
public static int calculateArmorPoints(ItemStack[] armor) { armor.put(a.getType(), a);
int total = 0; }
for (ItemStack anArmor : armor) { }
if (anArmor != null)
total += findByType(anArmor.getType()).getArmorPoints(); public static ArmorType findByType(Material type) {
} ArmorType t = armor.get(type);
return total; return t == null ? ArmorType.NONE : t;
} }
public static ArmorType findById(int id) { public static int calculateArmorPoints(ItemStack[] armor) {
for (ArmorType a : ArmorType.values()) int total = 0;
if (a.getId() == id) for (ItemStack anArmor : armor) {
return a; if (anArmor != null)
return ArmorType.NONE; total += findByType(anArmor.getType()).getArmorPoints();
} }
return total;
public static boolean isArmor(Material material) { }
for (ArmorType a : ArmorType.values())
if (a.getType() == material) public static ArmorType findById(int id) {
return true; for (ArmorType a : ArmorType.values())
return false; if (a.getId() == id)
} return a;
return ArmorType.NONE;
public static int calculateArmorPoints(int[] armor) { }
int total = 0;
for (int anArmor : armor) { public static boolean isArmor(Material material) {
if (anArmor != -1) for (ArmorType a : ArmorType.values())
total += findById(anArmor).getArmorPoints(); if (a.getType() == material)
} return true;
return total; return false;
} }
public Material getType() { public static int calculateArmorPoints(int[] armor) {
return this.type; int total = 0;
} for (int anArmor : armor) {
if (anArmor != -1)
total += findById(anArmor).getArmorPoints();
}
return total;
}
public Material getType() {
return this.type;
}
} }