WoodcuttingManager.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.gmail.nossr50.skills.woodcutting;
  2. import com.gmail.nossr50.config.Config;
  3. import com.gmail.nossr50.datatypes.mods.CustomBlock;
  4. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  5. import com.gmail.nossr50.datatypes.skills.AbilityType;
  6. import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
  7. import com.gmail.nossr50.datatypes.skills.SkillType;
  8. import com.gmail.nossr50.datatypes.skills.XPGainReason;
  9. import com.gmail.nossr50.locale.LocaleLoader;
  10. import com.gmail.nossr50.mcMMO;
  11. import com.gmail.nossr50.skills.SkillManager;
  12. import com.gmail.nossr50.skills.woodcutting.Woodcutting.ExperienceGainMethod;
  13. import com.gmail.nossr50.util.*;
  14. import com.gmail.nossr50.util.skills.CombatUtils;
  15. import com.gmail.nossr50.util.skills.SecondarySkillActivationType;
  16. import com.gmail.nossr50.util.skills.SkillUtils;
  17. import org.bukkit.Material;
  18. import org.bukkit.block.Block;
  19. import org.bukkit.block.BlockState;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.inventory.ItemStack;
  22. import java.util.HashSet;
  23. import java.util.Set;
  24. public class WoodcuttingManager extends SkillManager {
  25. public WoodcuttingManager(McMMOPlayer mcMMOPlayer) {
  26. super(mcMMOPlayer, SkillType.WOODCUTTING);
  27. }
  28. public boolean canUseLeafBlower(ItemStack heldItem) {
  29. return Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.WOODCUTTING_LEAF_BLOWER) && getSkillLevel() >= Woodcutting.leafBlowerUnlockLevel && ItemUtils.isAxe(heldItem);
  30. }
  31. public boolean canUseTreeFeller(ItemStack heldItem) {
  32. return mcMMOPlayer.getAbilityMode(AbilityType.TREE_FELLER) && Permissions.treeFeller(getPlayer()) && ItemUtils.isAxe(heldItem);
  33. }
  34. protected boolean canGetDoubleDrops() {
  35. return Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.WOODCUTTING_HARVEST) && SkillUtils.isActivationSuccessful(SecondarySkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SecondaryAbility.WOODCUTTING_HARVEST, getPlayer(), this.skill, getSkillLevel(), activationChance);
  36. }
  37. /**
  38. * Begins Woodcutting
  39. *
  40. * @param blockState Block being broken
  41. */
  42. public void woodcuttingBlockCheck(BlockState blockState) {
  43. int xp = Woodcutting.getExperienceFromLog(blockState, ExperienceGainMethod.DEFAULT);
  44. switch (blockState.getType()) {
  45. case BROWN_MUSHROOM_BLOCK:
  46. case RED_MUSHROOM_BLOCK:
  47. break;
  48. default:
  49. if (canGetDoubleDrops()) {
  50. Woodcutting.checkForDoubleDrop(blockState);
  51. }
  52. }
  53. applyXpGain(xp, XPGainReason.PVE);
  54. }
  55. /**
  56. * Begins Tree Feller
  57. *
  58. * @param blockState Block being broken
  59. */
  60. public void processTreeFeller(BlockState blockState) {
  61. Player player = getPlayer();
  62. Set<BlockState> treeFellerBlocks = new HashSet<BlockState>();
  63. Woodcutting.treeFellerReachedThreshold = false;
  64. Woodcutting.processTree(blockState, treeFellerBlocks);
  65. // If the player is trying to break too many blocks
  66. if (Woodcutting.treeFellerReachedThreshold) {
  67. Woodcutting.treeFellerReachedThreshold = false;
  68. player.sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFeller.Threshold"));
  69. return;
  70. }
  71. // If the tool can't sustain the durability loss
  72. if (!Woodcutting.handleDurabilityLoss(treeFellerBlocks, player.getInventory().getItemInMainHand())) {
  73. player.sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFeller.Splinter"));
  74. double health = player.getHealth();
  75. if (health > 1) {
  76. CombatUtils.dealDamage(player, Misc.getRandom().nextInt((int) (health - 1)));
  77. }
  78. return;
  79. }
  80. dropBlocks(treeFellerBlocks);
  81. Woodcutting.treeFellerReachedThreshold = false; // Reset the value after we're done with Tree Feller each time.
  82. }
  83. /**
  84. * Handles the dropping of blocks
  85. *
  86. * @param treeFellerBlocks List of blocks to be dropped
  87. */
  88. private void dropBlocks(Set<BlockState> treeFellerBlocks) {
  89. Player player = getPlayer();
  90. int xp = 0;
  91. for (BlockState blockState : treeFellerBlocks) {
  92. Block block = blockState.getBlock();
  93. if (!EventUtils.simulateBlockBreak(block, player, true)) {
  94. break; // TODO: Shouldn't we use continue instead?
  95. }
  96. Material material = blockState.getType();
  97. if (material == Material.BROWN_MUSHROOM_BLOCK || material == Material.RED_MUSHROOM_BLOCK) {
  98. xp += Woodcutting.getExperienceFromLog(blockState, ExperienceGainMethod.TREE_FELLER);
  99. Misc.dropItems(Misc.getBlockCenter(blockState), block.getDrops());
  100. }
  101. else if (mcMMO.getModManager().isCustomLog(blockState)) {
  102. if (canGetDoubleDrops()) {
  103. Woodcutting.checkForDoubleDrop(blockState);
  104. }
  105. CustomBlock customBlock = mcMMO.getModManager().getBlock(blockState);
  106. xp = customBlock.getXpGain();
  107. Misc.dropItems(Misc.getBlockCenter(blockState), block.getDrops());
  108. }
  109. else if (mcMMO.getModManager().isCustomLeaf(blockState)) {
  110. Misc.dropItems(Misc.getBlockCenter(blockState), block.getDrops());
  111. }
  112. else {
  113. if (BlockUtils.isLog(blockState)) {
  114. if (canGetDoubleDrops()) {
  115. Woodcutting.checkForDoubleDrop(blockState);
  116. }
  117. xp += Woodcutting.getExperienceFromLog(blockState, ExperienceGainMethod.TREE_FELLER);
  118. Misc.dropItems(Misc.getBlockCenter(blockState), block.getDrops());
  119. }
  120. if (BlockUtils.isLeaves(blockState)) {
  121. Misc.dropItems(Misc.getBlockCenter(blockState), block.getDrops());
  122. }
  123. }
  124. blockState.setType(Material.AIR);
  125. blockState.update(true);
  126. }
  127. applyXpGain(xp, XPGainReason.PVE);
  128. }
  129. }