Combat.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2012 Matt 'The Yeti' Burnett & mcMMO Development
  3. * Copyright (C) 2010-2011 'nossr50'
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.gmail.nossr50;
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.Material;
  21. import org.bukkit.entity.*;
  22. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  23. import org.bukkit.event.entity.EntityDamageEvent;
  24. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  25. import org.bukkit.inventory.ItemStack;
  26. import com.gmail.nossr50.config.LoadProperties;
  27. import com.gmail.nossr50.datatypes.PlayerProfile;
  28. import com.gmail.nossr50.datatypes.SkillType;
  29. import com.gmail.nossr50.events.FakeEntityDamageByEntityEvent;
  30. import com.gmail.nossr50.events.FakeEntityDamageEvent;
  31. import com.gmail.nossr50.locale.mcLocale;
  32. import com.gmail.nossr50.party.Party;
  33. import com.gmail.nossr50.skills.Acrobatics;
  34. import com.gmail.nossr50.skills.Archery;
  35. import com.gmail.nossr50.skills.Axes;
  36. import com.gmail.nossr50.skills.Skills;
  37. import com.gmail.nossr50.skills.Swords;
  38. import com.gmail.nossr50.skills.Taming;
  39. import com.gmail.nossr50.skills.Unarmed;
  40. public class Combat
  41. {
  42. public static void combatChecks(EntityDamageByEntityEvent event, mcMMO pluginx)
  43. {
  44. if (event.getDamage() == 0 || event.getEntity().isDead())
  45. return;
  46. Entity damager = event.getDamager();
  47. LivingEntity target = (LivingEntity) event.getEntity();
  48. int damage = event.getDamage();
  49. EntityType damagerType = damager.getType();
  50. EntityType targetType = target.getType();
  51. switch (damagerType)
  52. {
  53. case PLAYER:
  54. Player attacker = (Player) event.getDamager();
  55. ItemStack itemInHand = attacker.getItemInHand();
  56. PlayerProfile PPa = Users.getProfile(attacker);
  57. combatAbilityChecks(attacker, PPa);
  58. if (m.isSwords(itemInHand) && mcPermissions.getInstance().swords(attacker))
  59. {
  60. if (!pluginx.misc.bleedTracker.contains(target))
  61. Swords.bleedCheck(attacker, target, pluginx);
  62. if (PPa.getSerratedStrikesMode())
  63. Swords.applySerratedStrikes(attacker, event, pluginx);
  64. if (targetType.equals(EntityType.PLAYER))
  65. PvPExperienceGain(attacker, PPa, (Player) target, damage, SkillType.SWORDS);
  66. else if (!pluginx.misc.mobSpawnerList.contains(target.getEntityId()))
  67. PvEExperienceGain(attacker, PPa, target, damage, SkillType.SWORDS);
  68. }
  69. else if (m.isAxes(itemInHand) && mcPermissions.getInstance().axes(attacker))
  70. {
  71. Axes.axesBonus(attacker, event);
  72. Axes.axeCriticalCheck(attacker, event, pluginx);
  73. Axes.impact(attacker, target, event);
  74. if (PPa.getSkullSplitterMode())
  75. Axes.applyAoeDamage(attacker, event, pluginx);
  76. if (targetType.equals(EntityType.PLAYER))
  77. PvPExperienceGain(attacker, PPa, (Player) target, event.getDamage(), SkillType.AXES); //use getDamage because damage is modified in earlier functions
  78. else if (!pluginx.misc.mobSpawnerList.contains(target.getEntityId()))
  79. PvEExperienceGain(attacker, PPa, target, event.getDamage(), SkillType.AXES); //use getDamage because damage is modified in earlier functions
  80. }
  81. else if (itemInHand.getType().equals(Material.AIR) && mcPermissions.getInstance().unarmed(attacker))
  82. {
  83. Unarmed.unarmedBonus(attacker, event);
  84. if (PPa.getBerserkMode())
  85. event.setDamage(damage + (damage / 2));
  86. if (targetType.equals(EntityType.PLAYER))
  87. {
  88. Unarmed.disarmProcCheck(attacker, (Player) target);
  89. PvPExperienceGain(attacker, PPa, (Player) target, event.getDamage(), SkillType.UNARMED); //use getDamage because damage is modified in earlier functions
  90. }
  91. else if (!pluginx.misc.mobSpawnerList.contains(target.getEntityId()))
  92. PvEExperienceGain(attacker, PPa, target, event.getDamage(), SkillType.UNARMED); //use getDamage because damage is modified in earlier functions
  93. }
  94. else if (itemInHand.getType().equals(Material.BONE) && mcPermissions.getInstance().taming(attacker) && targetType.equals(EntityType.WOLF))
  95. {
  96. Wolf wolf = (Wolf) target;
  97. String message = "Combat.BeastLore" + " ";
  98. int health = wolf.getHealth();
  99. event.setCancelled(true);
  100. if (wolf.isTamed())
  101. {
  102. message.concat(mcLocale.getString("Combat.BeastLoreOwner", new Object[] {Taming.getOwnerName(wolf)}) + " ");
  103. message.concat(mcLocale.getString("Combat.BeastLoreHealthWolfTamed", new Object[] {health}));
  104. }
  105. else
  106. message.concat(mcLocale.getString("Combat.BeastLoreHealthWolf", new Object[] {health}));
  107. attacker.sendMessage(message);
  108. }
  109. case WOLF:
  110. {
  111. Wolf wolf = (Wolf) damager;
  112. if (wolf.isTamed() && wolf.getOwner() instanceof Player)
  113. {
  114. Player master = (Player) wolf.getOwner();
  115. PlayerProfile PPo = Users.getProfile(master);
  116. if (mcPermissions.getInstance().taming(master))
  117. {
  118. Taming.fastFoodService(PPo, wolf, event);
  119. Taming.sharpenedClaws(PPo, event);
  120. Taming.gore(PPo, event, master, pluginx);
  121. Taming.rewardXp(event, pluginx, master);
  122. }
  123. }
  124. }
  125. case ARROW:
  126. archeryCheck((EntityDamageByEntityEvent)event, pluginx);
  127. }
  128. if (targetType.equals(EntityType.PLAYER))
  129. {
  130. Swords.counterAttackChecks(event);
  131. Acrobatics.dodgeChecks(event);
  132. }
  133. }
  134. public static void combatAbilityChecks(Player attacker, PlayerProfile PPa)
  135. {
  136. //Check to see if any abilities need to be activated
  137. if(PPa.getAxePreparationMode())
  138. Skills.abilityCheck(attacker, SkillType.AXES);
  139. if(PPa.getSwordsPreparationMode())
  140. Skills.abilityCheck(attacker, SkillType.SWORDS);
  141. if(PPa.getFistsPreparationMode())
  142. Skills.abilityCheck(attacker, SkillType.UNARMED);
  143. }
  144. public static void archeryCheck(EntityDamageByEntityEvent event, mcMMO pluginx)
  145. {
  146. Arrow arrow = (Arrow)event.getDamager();
  147. Entity y = arrow.getShooter();
  148. Entity x = event.getEntity();
  149. if(x instanceof Player)
  150. {
  151. Player defender = (Player)x;
  152. PlayerProfile PPd = Users.getProfile(defender);
  153. if(PPd == null)
  154. Users.addUser(defender);
  155. if(mcPermissions.getInstance().unarmed(defender) && defender.getItemInHand().getTypeId() == 0)
  156. {
  157. if(defender != null && PPd.getSkillLevel(SkillType.UNARMED) >= 1000)
  158. {
  159. if(Math.random() * 1000 <= 500)
  160. {
  161. event.setCancelled(true);
  162. defender.sendMessage(mcLocale.getString("Combat.ArrowDeflect")); //$NON-NLS-1$
  163. return;
  164. }
  165. } else if(defender != null && Math.random() * 1000 <= (PPd.getSkillLevel(SkillType.UNARMED) / 2))
  166. {
  167. event.setCancelled(true);
  168. defender.sendMessage(mcLocale.getString("Combat.ArrowDeflect")); //$NON-NLS-1$
  169. return;
  170. }
  171. }
  172. }
  173. /*
  174. * If attacker is player
  175. */
  176. if(y instanceof Player)
  177. {
  178. Player attacker = (Player)y;
  179. PlayerProfile PPa = Users.getProfile(attacker);
  180. int damage = event.getDamage();
  181. if(mcPermissions.getInstance().archery(attacker) && damage > 0)
  182. {
  183. Archery.trackArrows(pluginx, x, PPa);
  184. /*
  185. * IGNITION
  186. */
  187. Archery.ignitionCheck(x, attacker);
  188. /*
  189. * Defender is Monster
  190. */
  191. if(!pluginx.misc.mobSpawnerList.contains(x.getEntityId()))
  192. {
  193. int xp = getXp(x, damage);
  194. PPa.addXP(SkillType.ARCHERY, xp*10, attacker);
  195. }
  196. /*
  197. * Attacker is Player
  198. */
  199. if(x instanceof Player){
  200. Player defender = (Player)x;
  201. PlayerProfile PPd = Users.getProfile(defender);
  202. /*
  203. * Stuff for the daze proc
  204. */
  205. if(PPa.inParty() && PPd.inParty())
  206. {
  207. if(Party.getInstance().inSameParty(defender, attacker))
  208. {
  209. event.setCancelled(true);
  210. return;
  211. }
  212. }
  213. /*
  214. * PVP XP
  215. */
  216. if(LoadProperties.pvpxp && ((PPd.getLastLogin()+5)*1000) < System.currentTimeMillis() && !attacker.getName().equals(defender.getName()))
  217. {
  218. int xp = (int) ((damage * 2) * 10);
  219. PPa.addXP(SkillType.ARCHERY, xp, attacker);
  220. }
  221. /*
  222. * DAZE PROC
  223. */
  224. Archery.dazeCheck(defender, attacker);
  225. }
  226. }
  227. Skills.XpCheckSkill(SkillType.ARCHERY, attacker);
  228. }
  229. }
  230. /**
  231. * Attempt to damage target for value dmg with reason CUSTOM
  232. *
  233. * @param target LivingEntity which to attempt to damage
  234. * @param dmg Amount of damage to attempt to do
  235. */
  236. public static void dealDamage(LivingEntity target, int dmg){
  237. dealDamage(target, dmg, EntityDamageEvent.DamageCause.CUSTOM);
  238. }
  239. /**
  240. * Attempt to damage target for value dmg with reason cause
  241. *
  242. * @param target LivingEntity which to attempt to damage
  243. * @param dmg Amount of damage to attempt to do
  244. * @param cause DamageCause to pass to damage event
  245. */
  246. public static void dealDamage(LivingEntity target, int dmg, DamageCause cause) {
  247. if(LoadProperties.eventCallback) {
  248. EntityDamageEvent ede = (EntityDamageEvent) new FakeEntityDamageEvent(target, cause, dmg);
  249. Bukkit.getPluginManager().callEvent(ede);
  250. if(ede.isCancelled()) return;
  251. target.damage(ede.getDamage());
  252. } else {
  253. target.damage(dmg);
  254. }
  255. }
  256. /**
  257. * Attempt to damage target for value dmg with reason ENTITY_ATTACK with damager attacker
  258. *
  259. * @param target LivingEntity which to attempt to damage
  260. * @param dmg Amount of damage to attempt to do
  261. * @param attacker Player to pass to event as damager
  262. */
  263. public static void dealDamage(LivingEntity target, int dmg, Player attacker) {
  264. if(LoadProperties.eventCallback) {
  265. EntityDamageEvent ede = (EntityDamageByEntityEvent) new FakeEntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, dmg);
  266. Bukkit.getPluginManager().callEvent(ede);
  267. if(ede.isCancelled()) return;
  268. target.damage(ede.getDamage());
  269. } else {
  270. target.damage(dmg);
  271. }
  272. }
  273. private static void PvPExperienceGain(Player attacker, PlayerProfile PPa, Player defender, int damage, SkillType skillType)
  274. {
  275. if (!LoadProperties.pvpxp)
  276. return;
  277. PlayerProfile PPd = Users.getProfile(defender);
  278. if(System.currentTimeMillis() >= (PPd.getRespawnATS()*1000) + 5000
  279. && ((PPd.getLastLogin()+5)*1000) < System.currentTimeMillis()
  280. && defender.getHealth() >= 1)
  281. {
  282. //Prevent a ridiculous amount of XP being granted by capping it at the remaining health of the mob
  283. int hpLeft = defender.getHealth(), xpinc = 0;
  284. if(hpLeft < damage)
  285. {
  286. if(hpLeft > 0)
  287. xpinc = hpLeft;
  288. else
  289. xpinc = 0;
  290. } else
  291. xpinc = damage;
  292. int xp = (int) (xpinc * 2 * LoadProperties.pvpxprewardmodifier);
  293. PPa.addXP(skillType, xp*10, attacker);
  294. Skills.XpCheckSkill(skillType, attacker);
  295. }
  296. }
  297. private static void PvEExperienceGain(Player attacker, PlayerProfile PPa, LivingEntity target, int damage, SkillType skillType)
  298. {
  299. int xp = getXp(target, damage);
  300. PPa.addXP(skillType, xp*10, attacker);
  301. Skills.XpCheckSkill(skillType, attacker);
  302. }
  303. public static int getXp(Entity entity, int damage)
  304. {
  305. int xp = 0;
  306. if(entity instanceof LivingEntity)
  307. {
  308. LivingEntity le = (LivingEntity) entity;
  309. //Prevent a ridiculous amount of XP being granted by capping it at the remaining health of the entity
  310. int hpLeft = le.getHealth();
  311. int xpinc = 0;
  312. if(hpLeft < damage)
  313. {
  314. if(hpLeft > 0)
  315. xpinc = hpLeft;
  316. else
  317. xpinc = 0;
  318. }
  319. else
  320. xpinc = damage;
  321. if(entity instanceof Animals)
  322. xp = (int) (xpinc * LoadProperties.animalXP);
  323. else
  324. {
  325. EntityType type = entity.getType();
  326. switch(type){
  327. case BLAZE:
  328. xp = (int) (xpinc * LoadProperties.blazeXP);
  329. break;
  330. case CAVE_SPIDER:
  331. xp = (int) (xpinc * LoadProperties.cavespiderXP);
  332. break;
  333. case CREEPER:
  334. xp = (int) (xpinc * LoadProperties.creeperXP);
  335. break;
  336. case ENDER_DRAGON:
  337. xp = (int) (xpinc * LoadProperties.enderdragonXP);
  338. break;
  339. case ENDERMAN:
  340. xp = (int) (xpinc * LoadProperties.endermanXP);
  341. break;
  342. case GHAST:
  343. xp = (int) (xpinc * LoadProperties.ghastXP);
  344. break;
  345. case MAGMA_CUBE:
  346. xp = (int) (xpinc * LoadProperties.magmacubeXP);
  347. break;
  348. case PIG_ZOMBIE:
  349. xp = (int) (xpinc * LoadProperties.pigzombieXP);
  350. break;
  351. case SILVERFISH:
  352. xp = (int) (xpinc * LoadProperties.silverfishXP);
  353. break;
  354. case SKELETON:
  355. xp = (int) (xpinc * LoadProperties.skeletonXP);
  356. break;
  357. case SLIME:
  358. xp = (int) (xpinc * LoadProperties.slimeXP);
  359. break;
  360. case SPIDER:
  361. xp = (int) (xpinc * LoadProperties.spiderXP);
  362. break;
  363. case ZOMBIE:
  364. xp = (int) (xpinc * LoadProperties.zombieXP);
  365. break;
  366. }
  367. }
  368. }
  369. return xp;
  370. }
  371. }