Combat.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package com.gmail.nossr50.util;
  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.mcMMO;
  17. import com.gmail.nossr50.config.Config;
  18. import com.gmail.nossr50.datatypes.AbilityType;
  19. import com.gmail.nossr50.datatypes.PlayerProfile;
  20. import com.gmail.nossr50.datatypes.SkillType;
  21. import com.gmail.nossr50.datatypes.ToolType;
  22. import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
  23. import com.gmail.nossr50.events.fake.FakeEntityDamageEvent;
  24. import com.gmail.nossr50.locale.LocaleLoader;
  25. import com.gmail.nossr50.party.Party;
  26. import com.gmail.nossr50.runnables.GainXp;
  27. import com.gmail.nossr50.runnables.BleedTimer;
  28. import com.gmail.nossr50.skills.Acrobatics;
  29. import com.gmail.nossr50.skills.Archery;
  30. import com.gmail.nossr50.skills.Axes;
  31. import com.gmail.nossr50.skills.Skills;
  32. import com.gmail.nossr50.skills.Swords;
  33. import com.gmail.nossr50.skills.Taming;
  34. import com.gmail.nossr50.skills.Unarmed;
  35. public class Combat {
  36. /**
  37. * Apply combat modifiers and process and XP gain.
  38. *
  39. * @param event The event to run the combat checks on.
  40. * @param plugin mcMMO plugin instance
  41. */
  42. public static void combatChecks(EntityDamageByEntityEvent event, mcMMO plugin) {
  43. if (event.getDamage() == 0 || event.getEntity().isDead()) {
  44. return;
  45. }
  46. Entity damager = event.getDamager();
  47. LivingEntity target = (LivingEntity) event.getEntity();
  48. EntityType damagerType = damager.getType();
  49. EntityType targetType = target.getType();
  50. switch (damagerType) {
  51. case PLAYER:
  52. Player attacker = (Player) event.getDamager();
  53. ItemStack itemInHand = attacker.getItemInHand();
  54. PlayerProfile PPa = Users.getProfile(attacker);
  55. combatAbilityChecks(attacker);
  56. if (ItemChecks.isSword(itemInHand) && Permissions.getInstance().swords(attacker)) {
  57. if (Permissions.getInstance().swordsBleed(attacker)) {
  58. Swords.bleedCheck(attacker, target, plugin);
  59. }
  60. if (PPa.getAbilityMode(AbilityType.SERRATED_STRIKES) && Permissions.getInstance().serratedStrikes(attacker)) {
  61. applyAbilityAoE(attacker, target, event.getDamage() / 4, plugin, SkillType.SWORDS);
  62. BleedTimer.add(target, 5);
  63. }
  64. startGainXp(attacker, PPa, target, SkillType.SWORDS, plugin);
  65. }
  66. else if (ItemChecks.isAxe(itemInHand) && Permissions.getInstance().axes(attacker)) {
  67. if (Permissions.getInstance().axeBonus(attacker)) {
  68. Axes.axesBonus(attacker, event);
  69. }
  70. if (Permissions.getInstance().criticalHit(attacker)) {
  71. Axes.axeCriticalCheck(attacker, event);
  72. }
  73. if (Permissions.getInstance().impact(attacker)) {
  74. Axes.impact(attacker, target, event);
  75. }
  76. if (PPa.getAbilityMode(AbilityType.SKULL_SPLIITER) && Permissions.getInstance().skullSplitter(attacker)) {
  77. applyAbilityAoE(attacker, target, event.getDamage() / 2, plugin, SkillType.AXES);
  78. }
  79. startGainXp(attacker, PPa, target, SkillType.AXES, plugin);
  80. }
  81. else if (itemInHand.getType().equals(Material.AIR) && Permissions.getInstance().unarmed(attacker)) {
  82. if (Permissions.getInstance().unarmedBonus(attacker)) {
  83. Unarmed.unarmedBonus(PPa, event);
  84. }
  85. if (PPa.getAbilityMode(AbilityType.BERSERK) && Permissions.getInstance().berserk(attacker)) {
  86. event.setDamage((int) (event.getDamage() * 1.5));
  87. }
  88. if (targetType.equals(EntityType.PLAYER) && Permissions.getInstance().disarm(attacker)) {
  89. Unarmed.disarmProcCheck(attacker, (Player) target);
  90. }
  91. startGainXp(attacker, PPa, target, SkillType.UNARMED, plugin);
  92. }
  93. else if (itemInHand.getType().equals(Material.BONE) && Permissions.getInstance().beastLore(attacker)) {
  94. Taming.beastLore(event, target, attacker);
  95. }
  96. break;
  97. case WOLF:
  98. Wolf wolf = (Wolf) damager;
  99. if (wolf.isTamed() && wolf.getOwner() instanceof Player) {
  100. Player master = (Player) wolf.getOwner();
  101. PlayerProfile PPo = Users.getProfile(master);
  102. if (Permissions.getInstance().taming(master)) {
  103. if (Permissions.getInstance().fastFoodService(master)) {
  104. Taming.fastFoodService(PPo, wolf, event);
  105. }
  106. if (Permissions.getInstance().sharpenedclaws(master)) {
  107. Taming.sharpenedClaws(PPo, event);
  108. }
  109. if (Permissions.getInstance().gore(master)) {
  110. Taming.gore(PPo, event, master, plugin);
  111. }
  112. startGainXp(master, PPo, target, SkillType.TAMING, plugin);
  113. }
  114. }
  115. break;
  116. case ARROW:
  117. archeryCheck((EntityDamageByEntityEvent) event, plugin);
  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 (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 (Permissions.getInstance().archery(attacker) && damage > 0) {
  163. if (Permissions.getInstance().archeryBonus(attacker)) {
  164. /*Archery needs a damage bonus to be viable in PVP*/
  165. int skillLvl = Users.getProfile(attacker).getSkillLevel(SkillType.ARCHERY);
  166. double dmgBonusPercent = ((skillLvl / 50) * 0.1D);
  167. /* Cap maximum bonus at 200% */
  168. if (dmgBonusPercent > 2) {
  169. dmgBonusPercent = 2;
  170. }
  171. /* Every 100 skill levels Archery gains 20% damage bonus, set that here */
  172. //TODO: Work in progress for balancing out Archery, will work on it more later...
  173. //TODO: Right now this is calculating a 10% bonus every 50 levels, not 20% every 100. Is this intended?
  174. int archeryBonus = (int)(event.getDamage() * dmgBonusPercent);
  175. event.setDamage(event.getDamage() + archeryBonus);
  176. }
  177. if (Permissions.getInstance().trackArrows(attacker)) {
  178. Archery.trackArrows(pluginx, target, PPa);
  179. }
  180. startGainXp(attacker, PPa, target, SkillType.ARCHERY, pluginx);
  181. if (target instanceof Player) {
  182. Player defender = (Player) target;
  183. PlayerProfile PPd = Users.getProfile(defender);
  184. if (PPa.inParty() && PPd.inParty() && Party.getInstance().inSameParty(defender, attacker)) {
  185. event.setCancelled(true);
  186. return;
  187. }
  188. if (Permissions.getInstance().daze(attacker)) {
  189. Archery.dazeCheck(defender, attacker);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * Attempt to damage target for value dmg with reason CUSTOM
  197. *
  198. * @param target LivingEntity which to attempt to damage
  199. * @param dmg Amount of damage to attempt to do
  200. */
  201. public static void dealDamage(LivingEntity target, int dmg) {
  202. dealDamage(target, dmg, EntityDamageEvent.DamageCause.CUSTOM);
  203. }
  204. /**
  205. * Attempt to damage target for value dmg with reason cause
  206. *
  207. * @param target LivingEntity which to attempt to damage
  208. * @param dmg Amount of damage to attempt to do
  209. * @param cause DamageCause to pass to damage event
  210. */
  211. private static void dealDamage(LivingEntity target, int dmg, DamageCause cause) {
  212. if (Config.getInstance().getEventCallbackEnabled()) {
  213. EntityDamageEvent ede = (EntityDamageEvent) new FakeEntityDamageEvent(target, cause, dmg);
  214. mcMMO.p.getServer().getPluginManager().callEvent(ede);
  215. if (ede.isCancelled()) {
  216. return;
  217. }
  218. target.damage(ede.getDamage());
  219. }
  220. else {
  221. target.damage(dmg);
  222. }
  223. }
  224. /**
  225. * Attempt to damage target for value dmg with reason ENTITY_ATTACK with damager attacker
  226. *
  227. * @param target LivingEntity which to attempt to damage
  228. * @param dmg Amount of damage to attempt to do
  229. * @param attacker Player to pass to event as damager
  230. */
  231. private static void dealDamage(LivingEntity target, int dmg, Player attacker) {
  232. if (Config.getInstance().getEventCallbackEnabled()) {
  233. EntityDamageEvent ede = (EntityDamageByEntityEvent) new FakeEntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, dmg);
  234. mcMMO.p.getServer().getPluginManager().callEvent(ede);
  235. if (ede.isCancelled()) {
  236. return;
  237. }
  238. target.damage(ede.getDamage());
  239. }
  240. else {
  241. target.damage(dmg);
  242. }
  243. }
  244. /**
  245. * Apply Area-of-Effect ability actions.
  246. *
  247. * @param attacker The attacking player
  248. * @param target The defending entity
  249. * @param damage The initial damage amount
  250. * @param plugin mcMMO plugin instance
  251. * @param type The type of skill being used
  252. */
  253. private static void applyAbilityAoE(Player attacker, LivingEntity target, int damage, mcMMO plugin, SkillType type) {
  254. int numberOfTargets = Misc.getTier(attacker.getItemInHand()); //The higher the weapon tier, the more targets you hit
  255. int damageAmount = damage;
  256. if (damageAmount < 1) {
  257. damageAmount = 1;
  258. }
  259. for (Entity entity : target.getNearbyEntities(2.5, 2.5, 2.5)) {
  260. if (!(entity instanceof LivingEntity)) {
  261. continue;
  262. }
  263. if (numberOfTargets <= 0) {
  264. break;
  265. }
  266. switch (entity.getType()) {
  267. case WOLF:
  268. AnimalTamer tamer = ((Wolf) entity).getOwner();
  269. if (tamer instanceof Player) {
  270. if (tamer.equals(attacker) || Party.getInstance().inSameParty(attacker, (Player) tamer)) {
  271. continue;
  272. }
  273. }
  274. break;
  275. case PLAYER:
  276. Player defender = (Player) entity;
  277. if (!target.getWorld().getPVP()) {
  278. continue;
  279. }
  280. if (defender.getName().equals(attacker.getName())) {
  281. continue;
  282. }
  283. if (Party.getInstance().inSameParty(attacker, defender)) {
  284. continue;
  285. }
  286. PlayerProfile playerProfile = Users.getProfile((Player) entity);
  287. if (playerProfile.getGodMode()) {
  288. continue;
  289. }
  290. break;
  291. }
  292. switch (type) {
  293. case SWORDS:
  294. if (entity instanceof Player) {
  295. ((Player) entity).sendMessage(LocaleLoader.getString("Swords.Combat.SS.Struck"));
  296. }
  297. BleedTimer.add((LivingEntity) entity, 5);
  298. break;
  299. case AXES:
  300. if (entity instanceof Player) {
  301. ((Player) entity).sendMessage(LocaleLoader.getString("Axes.Combat.Cleave.Struck"));
  302. }
  303. break;
  304. }
  305. dealDamage((LivingEntity) entity, damageAmount, attacker);
  306. numberOfTargets--;
  307. }
  308. }
  309. /**
  310. * Start the task that gives combat XP.
  311. *
  312. * @param attacker The attacking player
  313. * @param PP The player's PlayerProfile
  314. * @param target The defending entity
  315. * @param skillType The skill being used
  316. * @param plugin mcMMO plugin instance
  317. */
  318. public static void startGainXp(Player attacker, PlayerProfile PP, LivingEntity target, SkillType skillType, mcMMO pluginx) {
  319. double baseXP = 0;
  320. if (target instanceof Player) {
  321. if (!Config.getInstance().getExperienceGainsPlayerVersusPlayerEnabled()) {
  322. return;
  323. }
  324. Player defender = (Player) target;
  325. PlayerProfile PPd = Users.getProfile(defender);
  326. if (System.currentTimeMillis() >= (PPd.getRespawnATS() * 1000) + 5000 && ((PPd.getLastLogin() + 5) * 1000) < System.currentTimeMillis() && defender.getHealth() >= 1) {
  327. baseXP = 20 * Config.getInstance().getPlayerVersusPlayerXP();
  328. }
  329. }
  330. else if (!target.hasMetadata("mcmmoFromMobSpawner")) {
  331. if (target instanceof Animals && !target.hasMetadata("mcmmoSummoned")) {
  332. baseXP = Config.getInstance().getAnimalsXP();
  333. }
  334. else {
  335. EntityType type = target.getType();
  336. switch (type) {
  337. case BLAZE:
  338. baseXP = Config.getInstance().getBlazeXP();
  339. break;
  340. case CAVE_SPIDER:
  341. baseXP = Config.getInstance().getCaveSpiderXP();
  342. break;
  343. case CREEPER:
  344. baseXP = Config.getInstance().getCreeperXP();
  345. break;
  346. case ENDER_DRAGON:
  347. baseXP = Config.getInstance().getEnderDragonXP();
  348. break;
  349. case ENDERMAN:
  350. baseXP = Config.getInstance().getEndermanXP();
  351. break;
  352. case GHAST:
  353. baseXP = Config.getInstance().getGhastXP();
  354. break;
  355. case MAGMA_CUBE:
  356. baseXP = Config.getInstance().getMagmaCubeXP();
  357. break;
  358. case IRON_GOLEM:
  359. if (!((IronGolem) target).isPlayerCreated())
  360. baseXP = Config.getInstance().getIronGolemXP();
  361. break;
  362. case PIG_ZOMBIE:
  363. baseXP = Config.getInstance().getPigZombieXP();
  364. break;
  365. case SILVERFISH:
  366. baseXP = Config.getInstance().getSilverfishXP();
  367. break;
  368. case SKELETON:
  369. baseXP = Config.getInstance().getSkeletonXP();
  370. break;
  371. case SLIME:
  372. baseXP = Config.getInstance().getSlimeXP();
  373. break;
  374. case SPIDER:
  375. baseXP = Config.getInstance().getSpiderXP();
  376. break;
  377. case ZOMBIE:
  378. baseXP = Config.getInstance().getZombieXP();
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. baseXP *= 10;
  385. }
  386. if (baseXP != 0) {
  387. mcMMO.p.getServer().getScheduler().scheduleSyncDelayedTask(pluginx, new GainXp(attacker, PP, skillType, baseXP, target), 0);
  388. }
  389. }
  390. }