Combat.java 13 KB

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