HerbalismManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.gmail.nossr50.skills.herbalism;
  2. import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig;
  3. import com.gmail.nossr50.core.MetadataConstants;
  4. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  5. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  6. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  7. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  8. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  9. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  10. import com.gmail.nossr50.datatypes.skills.ToolType;
  11. import com.gmail.nossr50.datatypes.treasure.HylianTreasure;
  12. import com.gmail.nossr50.runnables.skills.HerbalismBlockUpdaterTask;
  13. import com.gmail.nossr50.skills.SkillManager;
  14. import com.gmail.nossr50.util.*;
  15. import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
  16. import com.gmail.nossr50.util.random.RandomChanceUtil;
  17. import com.gmail.nossr50.util.skills.RankUtils;
  18. import com.gmail.nossr50.util.skills.SkillActivationType;
  19. import com.gmail.nossr50.util.skills.SkillUtils;
  20. import org.bukkit.Location;
  21. import org.bukkit.Material;
  22. import org.bukkit.block.BlockState;
  23. import org.bukkit.block.data.Ageable;
  24. import org.bukkit.entity.Player;
  25. import org.bukkit.inventory.ItemStack;
  26. import org.bukkit.inventory.PlayerInventory;
  27. import org.bukkit.metadata.FixedMetadataValue;
  28. import java.util.List;
  29. public class HerbalismManager extends SkillManager {
  30. public HerbalismManager(McMMOPlayer mcMMOPlayer) {
  31. super(mcMMOPlayer, PrimarySkillType.HERBALISM);
  32. }
  33. public boolean canBlockCheck() {
  34. return !(pluginRef.getConfigManager().getConfigExploitPrevention().isPreventVehicleAutoFarming() && getPlayer().isInsideVehicle());
  35. }
  36. public boolean canGreenThumbBlock(BlockState blockState) {
  37. if (!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.HERBALISM_GREEN_THUMB))
  38. return false;
  39. Player player = getPlayer();
  40. ItemStack item = player.getInventory().getItemInMainHand();
  41. return item.getAmount() > 0 && item.getType() == Material.WHEAT_SEEDS && BlockUtils.canMakeMossy(blockState) && Permissions.greenThumbBlock(player, blockState.getType());
  42. }
  43. public boolean canUseShroomThumb(BlockState blockState) {
  44. if (!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.HERBALISM_SHROOM_THUMB))
  45. return false;
  46. Player player = getPlayer();
  47. PlayerInventory inventory = player.getInventory();
  48. Material itemType = inventory.getItemInMainHand().getType();
  49. return (itemType == Material.BROWN_MUSHROOM || itemType == Material.RED_MUSHROOM) && inventory.contains(Material.BROWN_MUSHROOM, 1) && inventory.contains(Material.RED_MUSHROOM, 1) && BlockUtils.canMakeShroomy(blockState) && Permissions.isSubSkillEnabled(player, SubSkillType.HERBALISM_SHROOM_THUMB);
  50. }
  51. public boolean canUseHylianLuck() {
  52. if (!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.HERBALISM_HYLIAN_LUCK))
  53. return false;
  54. return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.HERBALISM_HYLIAN_LUCK);
  55. }
  56. public boolean canGreenTerraBlock(BlockState blockState) {
  57. return mcMMOPlayer.getAbilityMode(SuperAbilityType.GREEN_TERRA) && BlockUtils.canMakeMossy(blockState);
  58. }
  59. public boolean canActivateAbility() {
  60. return mcMMOPlayer.getToolPreparationMode(ToolType.HOE) && Permissions.greenTerra(getPlayer());
  61. }
  62. public boolean canGreenTerraPlant() {
  63. return mcMMOPlayer.getAbilityMode(SuperAbilityType.GREEN_TERRA);
  64. }
  65. /**
  66. * Handle the Farmer's Diet ability
  67. *
  68. * @param eventFoodLevel The initial change in hunger from the event
  69. * @return the modified change in hunger for the event
  70. */
  71. public int farmersDiet(int eventFoodLevel) {
  72. return SkillUtils.handleFoodSkills(getPlayer(), eventFoodLevel, SubSkillType.HERBALISM_FARMERS_DIET);
  73. }
  74. /**
  75. * Process the Green Terra ability.
  76. *
  77. * @param blockState The {@link BlockState} to check ability activation for
  78. * @return true if the ability was successful, false otherwise
  79. */
  80. public boolean processGreenTerra(BlockState blockState) {
  81. Player player = getPlayer();
  82. if (!Permissions.greenThumbBlock(player, blockState.getType())) {
  83. return false;
  84. }
  85. PlayerInventory playerInventory = player.getInventory();
  86. ItemStack seed = new ItemStack(Material.WHEAT_SEEDS);
  87. if (!playerInventory.containsAtLeast(seed, 1)) {
  88. pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Herbalism.Ability.GTe.NeedMore");
  89. return false;
  90. }
  91. playerInventory.removeItem(seed);
  92. player.updateInventory(); // Needed until replacement available
  93. return Herbalism.convertGreenTerraBlocks(blockState);
  94. }
  95. /**
  96. * @param blockState The {@link BlockState} to check ability activation for
  97. */
  98. public void herbalismBlockCheck(BlockState blockState) {
  99. Player player = getPlayer();
  100. Material material = blockState.getType();
  101. boolean oneBlockPlant = isOneBlockPlant(material);
  102. // Prevents placing and immediately breaking blocks for exp
  103. if (oneBlockPlant && pluginRef.getPlaceStore().isTrue(blockState)) {
  104. return;
  105. }
  106. if (!canBlockCheck()) {
  107. return;
  108. }
  109. int amount;
  110. int xp;
  111. boolean greenTerra = mcMMOPlayer.getAbilityMode(skill.getSuperAbility());
  112. // if (mcMMO.getModManager().isCustomHerbalismBlock(blockState)) {
  113. // CustomBlock customBlock = mcMMO.getModManager().getBlock(blockState);
  114. // xp = customBlock.getXpGain();
  115. //
  116. // if (Permissions.isSubSkillEnabled(player, SubSkillType.HERBALISM_DOUBLE_DROPS) && customBlock.isDoubleDropEnabled()) {
  117. // drops = blockState.getBlock().getDrops();
  118. // }
  119. // }
  120. // else {
  121. xp = pluginRef.getDynamicSettingsManager().getExperienceManager().getHerbalismXp(blockState.getType());
  122. if (!oneBlockPlant) {
  123. //Kelp is actually two blocks mixed together
  124. if (material == Material.KELP_PLANT || material == Material.KELP) {
  125. amount = Herbalism.countAndMarkDoubleDropsKelp(blockState, greenTerra, this);
  126. } else {
  127. amount = Herbalism.countAndMarkDoubleDropsMultiBlockPlant(blockState, greenTerra, this);
  128. }
  129. xp *= amount;
  130. } else {
  131. /* MARK SINGLE BLOCK CROP FOR DOUBLE DROP */
  132. if (checkDoubleDrop(blockState))
  133. BlockUtils.markDropsAsBonus(blockState, greenTerra);
  134. }
  135. if (Permissions.greenThumbPlant(player, material)) {
  136. processGreenThumbPlants(blockState, greenTerra);
  137. }
  138. //} mod config close
  139. applyXpGain(xp, XPGainReason.PVE);
  140. }
  141. public boolean isOneBlockPlant(Material material) {
  142. return !pluginRef.getMaterialMapStore().isMultiBlock(material);
  143. }
  144. /**
  145. * Check for success on herbalism double drops
  146. *
  147. * @param blockState target block state
  148. * @return true if double drop succeeds
  149. */
  150. public boolean checkDoubleDrop(BlockState blockState) {
  151. return BlockUtils.checkDoubleDrops(getPlayer(), blockState, SubSkillType.HERBALISM_DOUBLE_DROPS);
  152. }
  153. /**
  154. * Process the Green Thumb ability for blocks.
  155. *
  156. * @param blockState The {@link BlockState} to check ability activation for
  157. * @return true if the ability was successful, false otherwise
  158. */
  159. public boolean processGreenThumbBlocks(BlockState blockState) {
  160. if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.HERBALISM_GREEN_THUMB, getPlayer())) {
  161. pluginRef.getNotificationManager().sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE_FAILED, "Herbalism.Ability.GTh.Fail");
  162. return false;
  163. }
  164. return Herbalism.convertGreenTerraBlocks(blockState);
  165. }
  166. /**
  167. * Process the Hylian Luck ability.
  168. *
  169. * @param blockState The {@link BlockState} to check ability activation for
  170. * @return true if the ability was successful, false otherwise
  171. */
  172. public boolean processHylianLuck(BlockState blockState) {
  173. if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.HERBALISM_HYLIAN_LUCK, getPlayer())) {
  174. return false;
  175. }
  176. String friendly = StringUtils.getFriendlyConfigBlockDataString(blockState.getBlockData());
  177. if (!HerbalismTreasureConfig.getInstance().hylianMap.containsKey(friendly))
  178. return false;
  179. List<HylianTreasure> treasures = HerbalismTreasureConfig.getInstance().hylianMap.get(friendly);
  180. Player player = getPlayer();
  181. if (treasures.isEmpty()) {
  182. return false;
  183. }
  184. int skillLevel = getSkillLevel();
  185. Location location = Misc.getBlockCenter(blockState);
  186. for (HylianTreasure treasure : treasures) {
  187. if (skillLevel >= treasure.getDropLevel()
  188. && RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(treasure.getDropChance(), getPlayer(), SubSkillType.HERBALISM_HYLIAN_LUCK))) {
  189. if (!pluginRef.getEventManager().simulateBlockBreak(blockState.getBlock(), player, false)) {
  190. return false;
  191. }
  192. blockState.setType(Material.AIR);
  193. Misc.dropItem(location, treasure.getDrop());
  194. pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Herbalism.HylianLuck");
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. /**
  201. * Process the Shroom Thumb ability.
  202. *
  203. * @param blockState The {@link BlockState} to check ability activation for
  204. * @return true if the ability was successful, false otherwise
  205. */
  206. public boolean processShroomThumb(BlockState blockState) {
  207. Player player = getPlayer();
  208. PlayerInventory playerInventory = player.getInventory();
  209. if (!playerInventory.contains(Material.BROWN_MUSHROOM, 1)) {
  210. pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getPrettyItemString(Material.BROWN_MUSHROOM));
  211. return false;
  212. }
  213. if (!playerInventory.contains(Material.RED_MUSHROOM, 1)) {
  214. pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getPrettyItemString(Material.RED_MUSHROOM));
  215. return false;
  216. }
  217. playerInventory.removeItem(new ItemStack(Material.BROWN_MUSHROOM));
  218. playerInventory.removeItem(new ItemStack(Material.RED_MUSHROOM));
  219. player.updateInventory();
  220. if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.HERBALISM_SHROOM_THUMB, player)) {
  221. pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Herbalism.Ability.ShroomThumb.Fail");
  222. return false;
  223. }
  224. return Herbalism.convertShroomThumb(blockState);
  225. }
  226. /**
  227. * Process the Green Thumb ability for plants.
  228. *
  229. * @param blockState The {@link BlockState} to check ability activation for
  230. * @param greenTerra boolean to determine if greenTerra is active or not
  231. */
  232. private void processGreenThumbPlants(BlockState blockState, boolean greenTerra) {
  233. if (!BlockUtils.isFullyGrown(blockState))
  234. return;
  235. Player player = getPlayer();
  236. PlayerInventory playerInventory = player.getInventory();
  237. Material seed;
  238. switch (blockState.getType()) {
  239. case CARROTS:
  240. seed = Material.CARROT;
  241. break;
  242. case WHEAT:
  243. seed = Material.WHEAT_SEEDS;
  244. break;
  245. case NETHER_WART:
  246. seed = Material.NETHER_WART;
  247. break;
  248. case POTATOES:
  249. seed = Material.POTATO;
  250. break;
  251. case BEETROOTS:
  252. seed = Material.BEETROOT_SEEDS;
  253. break;
  254. case COCOA:
  255. seed = Material.COCOA_BEANS;
  256. break;
  257. default:
  258. return;
  259. }
  260. ItemStack seedStack = new ItemStack(seed);
  261. if (!greenTerra && !RandomChanceUtil.checkRandomChanceExecutionSuccess(player, SubSkillType.HERBALISM_GREEN_THUMB)) {
  262. return;
  263. }
  264. if (!handleBlockState(blockState, greenTerra)) {
  265. return;
  266. }
  267. if (!ItemUtils.isHoe(getPlayer().getInventory().getItemInMainHand())) {
  268. if (!playerInventory.containsAtLeast(seedStack, 1)) {
  269. return;
  270. }
  271. playerInventory.removeItem(seedStack);
  272. player.updateInventory(); // Needed until replacement available
  273. }
  274. new HerbalismBlockUpdaterTask(blockState).runTaskLater(pluginRef, 0);
  275. }
  276. private boolean handleBlockState(BlockState blockState, boolean greenTerra) {
  277. int greenThumbStage = getGreenThumbStage();
  278. blockState.setMetadata(MetadataConstants.GREEN_THUMB_METAKEY, new FixedMetadataValue(pluginRef, (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)));
  279. Ageable crops = (Ageable) blockState.getBlockData();
  280. switch (blockState.getType()) {
  281. case POTATOES:
  282. case CARROTS:
  283. case WHEAT:
  284. if (greenTerra) {
  285. crops.setAge(3);
  286. } else {
  287. crops.setAge(greenThumbStage);
  288. }
  289. break;
  290. case BEETROOTS:
  291. case NETHER_WART:
  292. if (greenTerra || greenThumbStage > 2) {
  293. crops.setAge(2);
  294. } else if (greenThumbStage == 2) {
  295. crops.setAge(1);
  296. } else {
  297. crops.setAge(0);
  298. }
  299. break;
  300. case COCOA:
  301. if (greenTerra || getGreenThumbStage() > 1) {
  302. crops.setAge(1);
  303. } else {
  304. crops.setAge(0);
  305. }
  306. break;
  307. default:
  308. return false;
  309. }
  310. blockState.setBlockData(crops);
  311. return true;
  312. }
  313. private int getGreenThumbStage() {
  314. return RankUtils.getRank(getPlayer(), SubSkillType.HERBALISM_GREEN_THUMB);
  315. }
  316. }