Combat.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package com.gmail.nossr50;
  2. import org.bukkit.Material;
  3. import org.bukkit.entity.AnimalTamer;
  4. import org.bukkit.entity.Animals;
  5. import org.bukkit.entity.Arrow;
  6. import org.bukkit.entity.Entity;
  7. import org.bukkit.entity.EntityType;
  8. import org.bukkit.entity.IronGolem;
  9. import org.bukkit.entity.LivingEntity;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.entity.Wolf;
  12. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  13. import org.bukkit.event.entity.EntityDamageEvent;
  14. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  15. import org.bukkit.inventory.ItemStack;
  16. import com.gmail.nossr50.config.LoadProperties;
  17. import com.gmail.nossr50.datatypes.AbilityType;
  18. import com.gmail.nossr50.datatypes.PlayerProfile;
  19. import com.gmail.nossr50.datatypes.SkillType;
  20. import com.gmail.nossr50.datatypes.ToolType;
  21. import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
  22. import com.gmail.nossr50.events.fake.FakeEntityDamageEvent;
  23. import com.gmail.nossr50.locale.mcLocale;
  24. import com.gmail.nossr50.party.Party;
  25. import com.gmail.nossr50.runnables.GainXp;
  26. import com.gmail.nossr50.runnables.mcBleedTimer;
  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. * Apply combat modifiers and process and XP gain.
  37. *
  38. * @param event The event to run the combat checks on.
  39. * @param plugin mcMMO plugin instance
  40. */
  41. public static void combatChecks(EntityDamageByEntityEvent event, mcMMO plugin) {
  42. if (event.getDamage() == 0 || event.getEntity().isDead()) {
  43. return;
  44. }
  45. Entity damager = event.getDamager();
  46. LivingEntity target = (LivingEntity) event.getEntity();
  47. EntityType damagerType = damager.getType();
  48. EntityType targetType = target.getType();
  49. switch (damagerType) {
  50. case PLAYER:
  51. Player attacker = (Player) event.getDamager();
  52. ItemStack itemInHand = attacker.getItemInHand();
  53. PlayerProfile PPa = Users.getProfile(attacker);
  54. combatAbilityChecks(attacker);
  55. if (ItemChecks.isSword(itemInHand) && mcPermissions.getInstance().swords(attacker)) {
  56. if (!mcBleedTimer.contains(target) && mcPermissions.getInstance().swordsBleed(attacker)) {
  57. Swords.bleedCheck(attacker, target, plugin);
  58. }
  59. if (PPa.getAbilityMode(AbilityType.SERRATED_STRIKES) && mcPermissions.getInstance().serratedStrikes(attacker)) {
  60. applyAbilityAoE(attacker, target, event.getDamage(), plugin, SkillType.SWORDS);
  61. }
  62. startGainXp(attacker, PPa, target, SkillType.SWORDS, plugin);
  63. }
  64. else if (ItemChecks.isAxe(itemInHand) && mcPermissions.getInstance().axes(attacker)) {
  65. if (mcPermissions.getInstance().axeBonus(attacker)) {
  66. Axes.axesBonus(attacker, event);
  67. }
  68. if (mcPermissions.getInstance().criticalHit(attacker)) {
  69. Axes.axeCriticalCheck(attacker, event);
  70. }
  71. if (mcPermissions.getInstance().impact(attacker)) {
  72. Axes.impact(attacker, target, event);
  73. }
  74. if (PPa.getAbilityMode(AbilityType.SKULL_SPLIITER) && mcPermissions.getInstance().skullSplitter(attacker)) {
  75. applyAbilityAoE(attacker, target, event.getDamage(), plugin, SkillType.AXES);
  76. }
  77. startGainXp(attacker, PPa, target, SkillType.AXES, plugin);
  78. }
  79. else if (itemInHand.getType().equals(Material.AIR) && mcPermissions.getInstance().unarmed(attacker)) {
  80. if (mcPermissions.getInstance().unarmedBonus(attacker)) {
  81. Unarmed.unarmedBonus(PPa, event);
  82. }
  83. if (PPa.getAbilityMode(AbilityType.BERSERK) && mcPermissions.getInstance().berserk(attacker)) {
  84. event.setDamage((int) (event.getDamage() * 1.5));
  85. }
  86. if (targetType.equals(EntityType.PLAYER) && mcPermissions.getInstance().disarm(attacker)) {
  87. Unarmed.disarmProcCheck(attacker, (Player) target);
  88. }
  89. startGainXp(attacker, PPa, target, SkillType.UNARMED, plugin);
  90. }
  91. else if (itemInHand.getType().equals(Material.BONE) && mcPermissions.getInstance().beastLore(attacker)) {
  92. Taming.beastLore(event, target, attacker);
  93. }
  94. break;
  95. case WOLF:
  96. Wolf wolf = (Wolf) damager;
  97. if (wolf.isTamed() && wolf.getOwner() instanceof Player) {
  98. Player master = (Player) wolf.getOwner();
  99. PlayerProfile PPo = Users.getProfile(master);
  100. if (mcPermissions.getInstance().taming(master)) {
  101. if (mcPermissions.getInstance().fastFoodService(master)) {
  102. Taming.fastFoodService(PPo, wolf, event);
  103. }
  104. if (mcPermissions.getInstance().sharpenedclaws(master)) {
  105. Taming.sharpenedClaws(PPo, event);
  106. }
  107. if (mcPermissions.getInstance().gore(master)) {
  108. Taming.gore(PPo, event, master, plugin);
  109. }
  110. startGainXp(master, PPo, target, SkillType.TAMING, plugin);
  111. }
  112. }
  113. break;
  114. case ARROW:
  115. archeryCheck((EntityDamageByEntityEvent) event, plugin);
  116. break;
  117. default:
  118. break;
  119. }
  120. if (targetType.equals(EntityType.PLAYER)) {
  121. Swords.counterAttackChecks(event);
  122. Acrobatics.dodgeChecks(event);
  123. }
  124. }
  125. /**
  126. * Process combat abilities based on weapon preparation modes.
  127. *
  128. * @param attacker The player attacking
  129. */
  130. public static void combatAbilityChecks(Player attacker) {
  131. PlayerProfile PPa = Users.getProfile(attacker);
  132. if (PPa.getToolPreparationMode(ToolType.AXE)) {
  133. Skills.abilityCheck(attacker, SkillType.AXES);
  134. }
  135. else if (PPa.getToolPreparationMode(ToolType.SWORD)) {
  136. Skills.abilityCheck(attacker, SkillType.SWORDS);
  137. }
  138. else if (PPa.getToolPreparationMode(ToolType.FISTS)) {
  139. Skills.abilityCheck(attacker, SkillType.UNARMED);
  140. }
  141. }
  142. /**
  143. * Process archery abilities.
  144. *
  145. * @param event The event to run the archery checks on.
  146. * @param pluginx mcMMO plugin instance
  147. */
  148. public static void archeryCheck(EntityDamageByEntityEvent event, mcMMO pluginx) {
  149. Arrow arrow = (Arrow) event.getDamager();
  150. LivingEntity shooter = arrow.getShooter();
  151. LivingEntity target = (LivingEntity) event.getEntity();
  152. if (target instanceof Player) {
  153. Player defender = (Player) target;
  154. if (mcPermissions.getInstance().unarmed(defender) && defender.getItemInHand().getType().equals(Material.AIR)) {
  155. Unarmed.deflectCheck(defender, event);
  156. }
  157. }
  158. if (shooter instanceof Player) {
  159. Player attacker = (Player) shooter;
  160. PlayerProfile PPa = Users.getProfile(attacker);
  161. int damage = event.getDamage();
  162. if (mcPermissions.getInstance().archery(attacker) && damage > 0) {
  163. /*Archery needs a damage bonus to be viable in PVP*/
  164. int skillLvl = Users.getProfile(attacker).getSkillLevel(SkillType.ARCHERY);
  165. double dmgBonusPercent = ((skillLvl / 50) * 0.1D);
  166. /* Cap maximum bonus at 200% */
  167. if(dmgBonusPercent > 2)
  168. dmgBonusPercent = 2;
  169. /* Every 100 skill levels Archery gains 20% damage bonus, set that here */
  170. //TODO: Work in progress for balancing out Archery, will work on it more later...
  171. //TODO: Right now this is calculating a 10% bonus every 50 levels, not 20% every 100. Is this intended?
  172. int archeryBonus = (int)(event.getDamage() * dmgBonusPercent);
  173. event.setDamage(event.getDamage() + archeryBonus);
  174. if (mcPermissions.getInstance().trackArrows(attacker)) {
  175. Archery.trackArrows(pluginx, target, PPa);
  176. }
  177. startGainXp(attacker, PPa, target, SkillType.ARCHERY, pluginx);
  178. if (target instanceof Player) {
  179. Player defender = (Player) target;
  180. PlayerProfile PPd = Users.getProfile(defender);
  181. if (PPa.inParty() && PPd.inParty() && Party.getInstance().inSameParty(defender, attacker)) {
  182. event.setCancelled(true);
  183. return;
  184. }
  185. Archery.dazeCheck(defender, attacker);
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * Attempt to damage target for value dmg with reason CUSTOM
  192. *
  193. * @param target LivingEntity which to attempt to damage
  194. * @param dmg Amount of damage to attempt to do
  195. */
  196. public static void dealDamage(LivingEntity target, int dmg) {
  197. dealDamage(target, dmg, EntityDamageEvent.DamageCause.CUSTOM);
  198. }
  199. /**
  200. * Attempt to damage target for value dmg with reason cause
  201. *
  202. * @param target LivingEntity which to attempt to damage
  203. * @param dmg Amount of damage to attempt to do
  204. * @param cause DamageCause to pass to damage event
  205. */
  206. private static void dealDamage(LivingEntity target, int dmg, DamageCause cause) {
  207. if (LoadProperties.eventCallback) {
  208. EntityDamageEvent ede = (EntityDamageEvent) new FakeEntityDamageEvent(target, cause, dmg);
  209. mcMMO.p.getServer().getPluginManager().callEvent(ede);
  210. if (ede.isCancelled()) {
  211. return;
  212. }
  213. target.damage(ede.getDamage());
  214. }
  215. else {
  216. target.damage(dmg);
  217. }
  218. }
  219. /**
  220. * Attempt to damage target for value dmg with reason ENTITY_ATTACK with damager attacker
  221. *
  222. * @param target LivingEntity which to attempt to damage
  223. * @param dmg Amount of damage to attempt to do
  224. * @param attacker Player to pass to event as damager
  225. */
  226. private static void dealDamage(LivingEntity target, int dmg, Player attacker) {
  227. if (LoadProperties.eventCallback) {
  228. EntityDamageEvent ede = (EntityDamageByEntityEvent) new FakeEntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, dmg);
  229. mcMMO.p.getServer().getPluginManager().callEvent(ede);
  230. if (ede.isCancelled()) {
  231. return;
  232. }
  233. target.damage(ede.getDamage());
  234. }
  235. else {
  236. target.damage(dmg);
  237. }
  238. }
  239. /**
  240. * Apply Area-of-Effect ability actions.
  241. *
  242. * @param attacker The attacking player
  243. * @param target The defending entity
  244. * @param damage The initial damage amount
  245. * @param plugin mcMMO plugin instance
  246. * @param type The type of skill being used
  247. */
  248. private static void applyAbilityAoE(Player attacker, LivingEntity target, int damage, mcMMO plugin, SkillType type) {
  249. int numberOfTargets = m.getTier(attacker.getItemInHand()); //The higher the weapon tier, the more targets you hit
  250. int damageAmount = 0;
  251. if (type.equals(SkillType.AXES)) {
  252. damageAmount = damage / 2;
  253. }
  254. else if (type.equals(SkillType.SWORDS)) {
  255. damageAmount = damage / 4;
  256. }
  257. if (damageAmount < 1) {
  258. damageAmount = 1;
  259. }
  260. for (Entity entity : target.getNearbyEntities(2.5, 2.5, 2.5)) {
  261. EntityType entityType = entity.getType();
  262. if (entityType.equals(EntityType.WOLF)) {
  263. Wolf wolf = (Wolf) entity;
  264. AnimalTamer tamer = wolf.getOwner();
  265. if (tamer instanceof Player) {
  266. Player owner = (Player) tamer;
  267. if (owner.equals(attacker) || Party.getInstance().inSameParty(attacker, owner)) {
  268. continue;
  269. }
  270. }
  271. }
  272. if (entity instanceof LivingEntity && numberOfTargets >= 1) {
  273. if (entityType.equals(EntityType.PLAYER)) {
  274. Player defender = (Player) entity;
  275. PlayerProfile PP = Users.getProfile(defender);
  276. //Reasons why the target shouldn't be hit
  277. if (PP.getGodMode()) {
  278. continue;
  279. }
  280. if (defender.getName().equals(attacker.getName())) { //Is this even possible?
  281. continue;
  282. }
  283. if (Party.getInstance().inSameParty(attacker, defender)) {
  284. continue;
  285. }
  286. if (defender.isDead()) {
  287. continue;
  288. }
  289. //Apply effect to players only if PVP is enabled
  290. if (target.getWorld().getPVP()) {
  291. String message = "";
  292. if (type.equals(SkillType.AXES)) {
  293. message = mcLocale.getString("Axes.Combat.Cleave.Struck");
  294. }
  295. else if (type.equals(SkillType.SWORDS)) {
  296. message = mcLocale.getString("Swords.Combat.SS.Struck");
  297. }
  298. dealDamage(defender, damageAmount, attacker);
  299. defender.sendMessage(message);
  300. if (type.equals(SkillType.SWORDS)) {
  301. PP.addBleedTicks(5);
  302. }
  303. numberOfTargets--;
  304. }
  305. }
  306. else {
  307. LivingEntity livingEntity = (LivingEntity) entity;
  308. if (type.equals(SkillType.SWORDS)) {
  309. mcBleedTimer.add(livingEntity);
  310. }
  311. dealDamage(livingEntity, damageAmount, attacker);
  312. numberOfTargets--;
  313. }
  314. }
  315. }
  316. }
  317. /**
  318. * Start the task that gives combat XP.
  319. *
  320. * @param attacker The attacking player
  321. * @param PP The player's PlayerProfile
  322. * @param target The defending entity
  323. * @param skillType The skill being used
  324. * @param plugin mcMMO plugin instance
  325. */
  326. public static void startGainXp(Player attacker, PlayerProfile PP, LivingEntity target, SkillType skillType, mcMMO pluginx) {
  327. double baseXP = 0;
  328. if (target instanceof Player) {
  329. if (!LoadProperties.pvpxp) {
  330. return;
  331. }
  332. Player defender = (Player) target;
  333. PlayerProfile PPd = Users.getProfile(defender);
  334. if (System.currentTimeMillis() >= (PPd.getRespawnATS() * 1000) + 5000 && ((PPd.getLastLogin() + 5) * 1000) < System.currentTimeMillis() && defender.getHealth() >= 1) {
  335. baseXP = 20 * LoadProperties.pvpxprewardmodifier;
  336. }
  337. }
  338. else if (!target.hasMetadata("mcmmoFromMobSpawner")) {
  339. if (target instanceof Animals && !target.hasMetadata("mcmmoSummoned")) {
  340. baseXP = LoadProperties.animalXP;
  341. }
  342. else {
  343. EntityType type = target.getType();
  344. switch (type) {
  345. case BLAZE:
  346. baseXP = LoadProperties.blazeXP;
  347. break;
  348. case CAVE_SPIDER:
  349. baseXP = LoadProperties.cavespiderXP;
  350. break;
  351. case CREEPER:
  352. baseXP = LoadProperties.creeperXP;
  353. break;
  354. case ENDER_DRAGON:
  355. baseXP = LoadProperties.enderdragonXP;
  356. break;
  357. case ENDERMAN:
  358. baseXP = LoadProperties.endermanXP;
  359. break;
  360. case GHAST:
  361. baseXP = LoadProperties.ghastXP;
  362. break;
  363. case MAGMA_CUBE:
  364. baseXP = LoadProperties.magmacubeXP;
  365. break;
  366. case IRON_GOLEM:
  367. if (!((IronGolem) target).isPlayerCreated())
  368. baseXP = LoadProperties.irongolemXP;
  369. break;
  370. case PIG_ZOMBIE:
  371. baseXP = LoadProperties.pigzombieXP;
  372. break;
  373. case SILVERFISH:
  374. baseXP = LoadProperties.silverfishXP;
  375. break;
  376. case SKELETON:
  377. baseXP = LoadProperties.skeletonXP;
  378. break;
  379. case SLIME:
  380. baseXP = LoadProperties.slimeXP;
  381. break;
  382. case SPIDER:
  383. baseXP = LoadProperties.spiderXP;
  384. break;
  385. case ZOMBIE:
  386. baseXP = LoadProperties.zombieXP;
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. baseXP *= 10;
  393. }
  394. if (baseXP != 0) {
  395. mcMMO.p.getServer().getScheduler().scheduleSyncDelayedTask(pluginx, new GainXp(attacker, PP, skillType, baseXP, target), 0);
  396. }
  397. }
  398. }