Combat.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. if(Users.getProfile(attacker).getSkillLevel(SkillType.AXES) >= 500)
  69. event.setDamage(damage + 4);
  70. Axes.axeCriticalCheck(attacker, event, pluginx); //Critical hit
  71. if (!(event instanceof FakeEntityDamageByEntityEvent) && PPa.getSkullSplitterMode())
  72. Axes.applyAoeDamage(attacker, event, pluginx);
  73. if(target instanceof Player)
  74. PvPExperienceGain(attacker, PPa, (Player) target, event.getDamage(), SkillType.AXES);
  75. else if(!pluginx.misc.mobSpawnerList.contains(target.getEntityId()))
  76. PvEExperienceGain(attacker, PPa, target, event.getDamage(), SkillType.AXES);
  77. }
  78. else if(itemInHand.getTypeId() == 0 && mcPermissions.getInstance().unarmed(attacker)) //Unarmed
  79. {
  80. Unarmed.unarmedBonus(attacker, event);
  81. if(PPa.getBerserkMode())
  82. event.setDamage(event.getDamage() + (event.getDamage() / 2));
  83. if(target instanceof Player)
  84. Unarmed.disarmProcCheck(attacker, (Player) target); //Disarm
  85. if(target instanceof Player)
  86. PvPExperienceGain(attacker, PPa, (Player) target, event.getDamage(), SkillType.UNARMED);
  87. else if(!pluginx.misc.mobSpawnerList.contains(target.getEntityId()))
  88. PvEExperienceGain(attacker, PPa, target, event.getDamage(), SkillType.UNARMED);
  89. }
  90. //Player use bone on wolf.
  91. else if(target instanceof Wolf)
  92. {
  93. Wolf wolf = (Wolf) target;
  94. if(itemInHand.getTypeId() == 352 && mcPermissions.getInstance().taming(attacker))
  95. {
  96. event.setCancelled(true);
  97. if(wolf.isTamed())
  98. attacker.sendMessage(mcLocale.getString("Combat.BeastLore")+" "+
  99. mcLocale.getString("Combat.BeastLoreOwner", new Object[] {Taming.getOwnerName(wolf)})+" "+
  100. mcLocale.getString("Combat.BeastLoreHealthWolfTamed", new Object[] {wolf.getHealth()}));
  101. else
  102. attacker.sendMessage(mcLocale.getString("Combat.BeastLore")+" "+
  103. mcLocale.getString("Combat.BeastLoreHealthWolf", new Object[] {wolf.getHealth()}));
  104. }
  105. }
  106. }
  107. /*
  108. * TAMING (WOLVES VERSUS ENTITIES)
  109. */
  110. else if(damager instanceof Wolf)
  111. {
  112. Wolf wolf = (Wolf) damager;
  113. if (wolf.isTamed() && Taming.ownerOnline(wolf, pluginx))
  114. {
  115. Player master = Taming.getOwner(wolf, pluginx);
  116. if (master == null) //Can it really happen?
  117. return;
  118. PlayerProfile PPo = Users.getProfile(master);
  119. if(mcPermissions.getInstance().taming(master))
  120. {
  121. //Fast Food Service
  122. Taming.fastFoodService(PPo, wolf, event);
  123. //Sharpened Claws
  124. Taming.sharpenedClaws(PPo, event);
  125. //Gore
  126. Taming.gore(PPo, event, master, pluginx);
  127. //Reward XP
  128. Taming.rewardXp(event, pluginx, master);
  129. }
  130. }
  131. }
  132. //Another offensive check for Archery
  133. else if(damager instanceof Arrow)
  134. archeryCheck((EntityDamageByEntityEvent)event, pluginx);
  135. /*
  136. * DEFENSIVE CHECKS
  137. */
  138. if(target instanceof Player)
  139. {
  140. Swords.counterAttackChecks(event);
  141. Acrobatics.dodgeChecks(event);
  142. }
  143. /*
  144. * DEFENSIVE CHECKS FOR WOLVES
  145. */
  146. else if(target instanceof Wolf)
  147. {
  148. Wolf wolf = (Wolf) target;
  149. if(wolf.isTamed() && Taming.ownerOnline(wolf, pluginx))
  150. Taming.preventDamage(event, pluginx);
  151. }
  152. }
  153. public static void combatAbilityChecks(Player attacker, PlayerProfile PPa)
  154. {
  155. //Check to see if any abilities need to be activated
  156. if(PPa.getAxePreparationMode())
  157. Skills.abilityCheck(attacker, SkillType.AXES);
  158. if(PPa.getSwordsPreparationMode())
  159. Skills.abilityCheck(attacker, SkillType.SWORDS);
  160. if(PPa.getFistsPreparationMode())
  161. Skills.abilityCheck(attacker, SkillType.UNARMED);
  162. }
  163. public static void archeryCheck(EntityDamageByEntityEvent event, mcMMO pluginx)
  164. {
  165. Arrow arrow = (Arrow)event.getDamager();
  166. Entity y = arrow.getShooter();
  167. Entity x = event.getEntity();
  168. if(x instanceof Player)
  169. {
  170. Player defender = (Player)x;
  171. PlayerProfile PPd = Users.getProfile(defender);
  172. if(PPd == null)
  173. Users.addUser(defender);
  174. if(mcPermissions.getInstance().unarmed(defender) && defender.getItemInHand().getTypeId() == 0)
  175. {
  176. if(defender != null && PPd.getSkillLevel(SkillType.UNARMED) >= 1000)
  177. {
  178. if(Math.random() * 1000 <= 500)
  179. {
  180. event.setCancelled(true);
  181. defender.sendMessage(mcLocale.getString("Combat.ArrowDeflect")); //$NON-NLS-1$
  182. return;
  183. }
  184. } else if(defender != null && Math.random() * 1000 <= (PPd.getSkillLevel(SkillType.UNARMED) / 2))
  185. {
  186. event.setCancelled(true);
  187. defender.sendMessage(mcLocale.getString("Combat.ArrowDeflect")); //$NON-NLS-1$
  188. return;
  189. }
  190. }
  191. }
  192. /*
  193. * If attacker is player
  194. */
  195. if(y instanceof Player)
  196. {
  197. Player attacker = (Player)y;
  198. PlayerProfile PPa = Users.getProfile(attacker);
  199. int damage = event.getDamage();
  200. if(mcPermissions.getInstance().archery(attacker) && damage > 0)
  201. {
  202. Archery.trackArrows(pluginx, x, PPa);
  203. /*
  204. * IGNITION
  205. */
  206. Archery.ignitionCheck(x, attacker);
  207. /*
  208. * Defender is Monster
  209. */
  210. if(!pluginx.misc.mobSpawnerList.contains(x.getEntityId()))
  211. {
  212. int xp = getXp(x, damage);
  213. PPa.addXP(SkillType.ARCHERY, xp*10, attacker);
  214. }
  215. /*
  216. * Attacker is Player
  217. */
  218. if(x instanceof Player){
  219. Player defender = (Player)x;
  220. PlayerProfile PPd = Users.getProfile(defender);
  221. /*
  222. * Stuff for the daze proc
  223. */
  224. if(PPa.inParty() && PPd.inParty())
  225. {
  226. if(Party.getInstance().inSameParty(defender, attacker))
  227. {
  228. event.setCancelled(true);
  229. return;
  230. }
  231. }
  232. /*
  233. * PVP XP
  234. */
  235. if(LoadProperties.pvpxp && ((PPd.getLastLogin()+5)*1000) < System.currentTimeMillis() && !attacker.getName().equals(defender.getName()))
  236. {
  237. int xp = (int) ((damage * 2) * 10);
  238. PPa.addXP(SkillType.ARCHERY, xp, attacker);
  239. }
  240. /*
  241. * DAZE PROC
  242. */
  243. Archery.dazeCheck(defender, attacker);
  244. }
  245. }
  246. Skills.XpCheckSkill(SkillType.ARCHERY, attacker);
  247. }
  248. }
  249. /**
  250. * Attempt to damage target for value dmg with reason CUSTOM
  251. *
  252. * @param target LivingEntity which to attempt to damage
  253. * @param dmg Amount of damage to attempt to do
  254. */
  255. public static void dealDamage(LivingEntity target, int dmg){
  256. dealDamage(target, dmg, EntityDamageEvent.DamageCause.CUSTOM);
  257. }
  258. /**
  259. * Attempt to damage target for value dmg with reason cause
  260. *
  261. * @param target LivingEntity which to attempt to damage
  262. * @param dmg Amount of damage to attempt to do
  263. * @param cause DamageCause to pass to damage event
  264. */
  265. public static void dealDamage(LivingEntity target, int dmg, DamageCause cause) {
  266. if(LoadProperties.eventCallback) {
  267. EntityDamageEvent ede = new EntityDamageEvent(target, cause, dmg);
  268. Bukkit.getPluginManager().callEvent(ede);
  269. if(ede.isCancelled()) return;
  270. target.damage(ede.getDamage());
  271. } else {
  272. target.damage(dmg);
  273. }
  274. }
  275. /**
  276. * Attempt to damage target for value dmg with reason ENTITY_ATTACK with damager attacker
  277. *
  278. * @param target LivingEntity which to attempt to damage
  279. * @param dmg Amount of damage to attempt to do
  280. * @param attacker Player to pass to event as damager
  281. */
  282. public static void dealDamage(LivingEntity target, int dmg, Player attacker) {
  283. if(LoadProperties.eventCallback) {
  284. EntityDamageEvent ede = (EntityDamageByEntityEvent) new FakeEntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, dmg);
  285. Bukkit.getPluginManager().callEvent(ede);
  286. if(ede.isCancelled()) return;
  287. target.damage(ede.getDamage());
  288. } else {
  289. target.damage(dmg);
  290. }
  291. }
  292. private static void PvPExperienceGain(Player attacker, PlayerProfile PPa, Player defender, int damage, SkillType skillType)
  293. {
  294. if (!LoadProperties.pvpxp)
  295. return;
  296. PlayerProfile PPd = Users.getProfile(defender);
  297. if(System.currentTimeMillis() >= (PPd.getRespawnATS()*1000) + 5000
  298. && ((PPd.getLastLogin()+5)*1000) < System.currentTimeMillis()
  299. && defender.getHealth() >= 1)
  300. {
  301. //Prevent a ridiculous amount of XP being granted by capping it at the remaining health of the mob
  302. int hpLeft = defender.getHealth(), xpinc = 0;
  303. if(hpLeft < damage)
  304. {
  305. if(hpLeft > 0)
  306. xpinc = hpLeft;
  307. else
  308. xpinc = 0;
  309. } else
  310. xpinc = damage;
  311. int xp = (int) (xpinc * 2 * LoadProperties.pvpxprewardmodifier);
  312. PPa.addXP(skillType, xp*10, attacker);
  313. Skills.XpCheckSkill(skillType, attacker);
  314. }
  315. }
  316. private static void PvEExperienceGain(Player attacker, PlayerProfile PPa, LivingEntity target, int damage, SkillType skillType)
  317. {
  318. int xp = getXp(target, damage);
  319. PPa.addXP(skillType, xp*10, attacker);
  320. }
  321. public static int getXp(Entity entity, int damage)
  322. {
  323. int xp = 0;
  324. if(entity instanceof LivingEntity)
  325. {
  326. LivingEntity le = (LivingEntity) entity;
  327. //Prevent a ridiculous amount of XP being granted by capping it at the remaining health of the entity
  328. int hpLeft = le.getHealth();
  329. int xpinc = 0;
  330. if(hpLeft < damage)
  331. {
  332. if(hpLeft > 0)
  333. xpinc = hpLeft;
  334. else
  335. xpinc = 0;
  336. }
  337. else
  338. xpinc = damage;
  339. if(entity instanceof Animals)
  340. xp = (int) (xpinc * LoadProperties.animalXP);
  341. else
  342. {
  343. EntityType type = entity.getType();
  344. switch(type){
  345. case BLAZE:
  346. xp = (int) (xpinc * LoadProperties.blazeXP);
  347. break;
  348. case CAVE_SPIDER:
  349. xp = (int) (xpinc * LoadProperties.cavespiderXP);
  350. break;
  351. case CREEPER:
  352. xp = (int) (xpinc * LoadProperties.creeperXP);
  353. break;
  354. case ENDER_DRAGON:
  355. xp = (int) (xpinc * LoadProperties.enderdragonXP);
  356. break;
  357. case ENDERMAN:
  358. xp = (int) (xpinc * LoadProperties.endermanXP);
  359. break;
  360. case GHAST:
  361. xp = (int) (xpinc * LoadProperties.ghastXP);
  362. break;
  363. case MAGMA_CUBE:
  364. xp = (int) (xpinc * LoadProperties.magmacubeXP);
  365. break;
  366. case PIG_ZOMBIE:
  367. xp = (int) (xpinc * LoadProperties.pigzombieXP);
  368. break;
  369. case SILVERFISH:
  370. xp = (int) (xpinc * LoadProperties.silverfishXP);
  371. break;
  372. case SKELETON:
  373. xp = (int) (xpinc * LoadProperties.skeletonXP);
  374. break;
  375. case SLIME:
  376. xp = (int) (xpinc * LoadProperties.slimeXP);
  377. break;
  378. case SPIDER:
  379. xp = (int) (xpinc * LoadProperties.spiderXP);
  380. break;
  381. case ZOMBIE:
  382. xp = (int) (xpinc * LoadProperties.zombieXP);
  383. break;
  384. }
  385. }
  386. }
  387. return xp;
  388. }
  389. }