Create new equations for Protection Enchantment functionality to make it work properly (Further fix for PC-311)

This commit is contained in:
AlexTheCoder 2016-06-29 19:42:08 -04:00 committed by cnr
parent 37e50df948
commit b0303a8870
2 changed files with 113 additions and 30 deletions

View File

@ -3,6 +3,9 @@ package mineplex.minecraft.game.core.damage;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import mineplex.core.MiniPlugin; import mineplex.core.MiniPlugin;
@ -57,6 +60,9 @@ public class DamageManager extends MiniPlugin
public boolean DisableDamageChanges = false; public boolean DisableDamageChanges = false;
private boolean _enabled = true; private boolean _enabled = true;
private final HashMap<String, Integer> _protectionTypeModifiers = new HashMap<String, Integer>();
private final HashMap<String, DamageCause[]> _protectionCauses = new HashMap<String, DamageCause[]>();
public DamageManager(JavaPlugin plugin, CombatManager combatManager, NpcManager npcManager, DisguiseManager disguiseManager, ConditionManager conditionManager) public DamageManager(JavaPlugin plugin, CombatManager combatManager, NpcManager npcManager, DisguiseManager disguiseManager, ConditionManager conditionManager)
{ {
@ -79,6 +85,102 @@ public class DamageManager extends MiniPlugin
} }
registerEvents(new NpcProtectListener(npcManager)); registerEvents(new NpcProtectListener(npcManager));
_protectionTypeModifiers.put(Enchantment.PROTECTION_ENVIRONMENTAL.getName(), 1);
_protectionTypeModifiers.put(Enchantment.PROTECTION_FIRE.getName(), 2);
_protectionTypeModifiers.put(Enchantment.PROTECTION_EXPLOSIONS.getName(), 2);
_protectionTypeModifiers.put(Enchantment.PROTECTION_PROJECTILE.getName(), 2);
_protectionTypeModifiers.put(Enchantment.PROTECTION_FALL.getName(), 3);
_protectionCauses.put(Enchantment.PROTECTION_ENVIRONMENTAL.getName(), new DamageCause[] {DamageCause.BLOCK_EXPLOSION, DamageCause.CONTACT, DamageCause.CUSTOM, DamageCause.DROWNING, DamageCause.ENTITY_ATTACK, DamageCause.ENTITY_EXPLOSION, DamageCause.FALL, DamageCause.FALLING_BLOCK, DamageCause.FIRE, DamageCause.FIRE_TICK, DamageCause.LAVA, DamageCause.LIGHTNING, DamageCause.PROJECTILE, DamageCause.SUFFOCATION, DamageCause.THORNS});
_protectionCauses.put(Enchantment.PROTECTION_FIRE.getName(), new DamageCause[] {DamageCause.FIRE, DamageCause.FIRE_TICK, DamageCause.LAVA});
_protectionCauses.put(Enchantment.PROTECTION_EXPLOSIONS.getName(), new DamageCause[] {DamageCause.BLOCK_EXPLOSION, DamageCause.ENTITY_EXPLOSION});
_protectionCauses.put(Enchantment.PROTECTION_PROJECTILE.getName(), new DamageCause[] {DamageCause.PROJECTILE});
_protectionCauses.put(Enchantment.PROTECTION_FALL.getName(), new DamageCause[] {DamageCause.FALL});
}
private int getHighestLevel(Enchantment ench, ItemStack[] items)
{
int level = 0;
for (ItemStack item : items)
{
if (item == null && item.getType() == Material.AIR)
{
continue;
}
if (!item.containsEnchantment(ench))
{
continue;
}
if (item.getEnchantmentLevel(ench) <= level)
{
continue;
}
level = item.getEnchantmentLevel(ench);
}
return level;
}
private int getTotalEPF(Enchantment ench, ItemStack[] items)
{
if (!_protectionTypeModifiers.containsKey(ench.getName()))
{
return 0;
}
if (!_protectionCauses.containsKey(ench.getName()))
{
return 0;
}
int epf = 0;
for (ItemStack item : items)
{
if (item == null || item.getType() == Material.AIR)
{
continue;
}
if (!item.containsEnchantment(ench))
{
continue;
}
if (item.getEnchantmentLevel(ench) <= 0)
{
continue;
}
epf += (item.getEnchantmentLevel(ench) * _protectionTypeModifiers.get(ench.getName()));
}
return Math.min(20, epf);
}
private double getTotalEnchantReduction(ItemStack[] armor, DamageCause cause)
{
int epf = 0;
for (Enchantment ench : Enchantment.values())
{
if (!_protectionTypeModifiers.containsKey(ench.getName()))
{
continue;
}
if (!_protectionCauses.containsKey(ench.getName()))
{
continue;
}
if (!Arrays.asList(_protectionCauses.get(ench.getName())).contains(cause))
{
continue;
}
epf += getTotalEPF(ench, armor);
}
epf = Math.max(0, Math.min(20, epf));
return new BigDecimal(1).subtract(new BigDecimal(epf).divide(new BigDecimal(25))).doubleValue();
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
@ -273,35 +375,9 @@ public class DamageManager extends MiniPlugin
Player damagee = event.GetDamageePlayer(); Player damagee = event.GetDamageePlayer();
if (damagee != null) if (damagee != null)
{ {
for (ItemStack stack : damagee.getInventory().getArmorContents()) if (getTotalEnchantReduction(damagee.getInventory().getArmorContents(), event.GetCause()) > 0)
{ {
if (stack == null) event.AddMult("Ench Prot", damagee.getName(), getTotalEnchantReduction(damagee.getInventory().getArmorContents(), event.GetCause()), false);
continue;
Map<Enchantment, Integer> enchants = stack.getEnchantments();
for (Enchantment e : enchants.keySet())
{
if (e.equals(Enchantment.PROTECTION_ENVIRONMENTAL))
event.AddMod("Ench Prot", damagee.getName(), -0.5 * (double)enchants.get(e), false);
else if (e.equals(Enchantment.PROTECTION_FIRE) &&
event.GetCause() == DamageCause.FIRE &&
event.GetCause() == DamageCause.FIRE_TICK &&
event.GetCause() == DamageCause.LAVA)
event.AddMod("Ench Prot", damagee.getName(), -0.5 * (double)enchants.get(e), false);
else if (e.equals(Enchantment.PROTECTION_FALL) &&
event.GetCause() == DamageCause.FALL)
event.AddMod("Ench Prot", damagee.getName(), -0.5 * (double)enchants.get(e), false);
else if (e.equals(Enchantment.PROTECTION_EXPLOSIONS) &&
event.GetCause() == DamageCause.ENTITY_EXPLOSION)
event.AddMod("Ench Prot", damagee.getName(), -0.5 * (double)enchants.get(e), false);
else if (e.equals(Enchantment.PROTECTION_PROJECTILE) &&
event.GetCause() == DamageCause.PROJECTILE)
event.AddMod("Ench Prot", damagee.getName(), -0.5 * (double)enchants.get(e), false);
}
} }
} }
@ -324,8 +400,15 @@ public class DamageManager extends MiniPlugin
else if (e.equals(Enchantment.FIRE_ASPECT)) else if (e.equals(Enchantment.FIRE_ASPECT))
if (_conditionManager != null) if (_conditionManager != null)
{
double reduce = 0;
if (damagee != null)
{
reduce = (15 * getHighestLevel(Enchantment.PROTECTION_FIRE, damagee.getInventory().getArmorContents())) * (4 * (double)enchants.get(e));
}
_conditionManager.Factory().Ignite("Ench Fire", event.GetDamageeEntity(), damager, _conditionManager.Factory().Ignite("Ench Fire", event.GetDamageeEntity(), damager,
4 * (double)enchants.get(e), false, false); (4 * (double)enchants.get(e)) - reduce, false, false);
}
} }
} }

View File

@ -331,7 +331,7 @@ public class Gun extends StrikeItem
{ {
new BukkitRunnable() { new BukkitRunnable() {
public void run() { public void run() {
PlayerStats remoteStats = game.getArcadeManager().GetStatsManager().Get(player);; PlayerStats remoteStats = game.getArcadeManager().GetStatsManager().Get(player);
_kills = (int) remoteStats.getStat(game.GetName() + "." + getStatNameKills(true)); _kills = (int) remoteStats.getStat(game.GetName() + "." + getStatNameKills(true));
Player owner = UtilPlayer.searchExact(getOwnerName()); Player owner = UtilPlayer.searchExact(getOwnerName());