Herbalism.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package com.gmail.nossr50.skills.gathering;
  2. import java.util.Random;
  3. import org.bukkit.CropState;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.block.BlockBreakEvent;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.inventory.PlayerInventory;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.config.Config;
  13. import com.gmail.nossr50.config.mods.CustomBlocksConfig;
  14. import com.gmail.nossr50.datatypes.AbilityType;
  15. import com.gmail.nossr50.datatypes.PlayerProfile;
  16. import com.gmail.nossr50.datatypes.SkillType;
  17. import com.gmail.nossr50.datatypes.mods.CustomBlock;
  18. import com.gmail.nossr50.locale.LocaleLoader;
  19. import com.gmail.nossr50.runnables.GreenThumbTimer;
  20. import com.gmail.nossr50.util.Misc;
  21. import com.gmail.nossr50.util.ModChecks;
  22. import com.gmail.nossr50.util.Permissions;
  23. import com.gmail.nossr50.util.Skills;
  24. import com.gmail.nossr50.util.Users;
  25. public class Herbalism {
  26. private static Random random = new Random();
  27. /**
  28. * Activate the Green Terra ability.
  29. *
  30. * @param player The player activating the ability
  31. * @param block The block to be changed by Green Terra
  32. */
  33. public static void greenTerra(Player player, Block block) {
  34. PlayerInventory inventory = player.getInventory();
  35. boolean hasSeeds = inventory.contains(Material.SEEDS);
  36. if (!hasSeeds) {
  37. player.sendMessage("You need more seeds to spread Green Terra"); //TODO: Needs more locale.
  38. }
  39. else if (hasSeeds && !block.getType().equals(Material.WHEAT)) {
  40. inventory.removeItem(new ItemStack(Material.SEEDS));
  41. player.updateInventory(); // Needed until replacement available
  42. greenTerraConvert(player, block);
  43. }
  44. }
  45. public static void greenTerraConvert(Player player, Block block) {
  46. Material type = block.getType();
  47. if (Misc.blockBreakSimulate(block, player, false)) {
  48. if (Config.getInstance().getHerbalismGreenThumbSmoothbrickToMossy() && type == Material.SMOOTH_BRICK && block.getData() == 0) {
  49. block.setTypeIdAndData(block.getTypeId(), (byte) 1, false); //Set type of the brick to mossy, force the client update
  50. }
  51. else if (Config.getInstance().getHerbalismGreenThumbDirtToGrass() && type == Material.DIRT) {
  52. block.setType(Material.GRASS);
  53. }
  54. else if (Config.getInstance().getHerbalismGreenThumbCobbleToMossy() && type == Material.COBBLESTONE) {
  55. block.setType(Material.MOSSY_COBBLESTONE);
  56. }
  57. }
  58. }
  59. /**
  60. * Check for extra Herbalism drops.
  61. *
  62. * @param block The block to check for extra drops
  63. * @param player The player getting extra drops
  64. * @param event The event to use for Green Thumb
  65. * @param plugin mcMMO plugin instance
  66. */
  67. public static void herbalismProcCheck(final Block block, Player player, BlockBreakEvent event, mcMMO plugin) {
  68. final PlayerProfile profile = Users.getProfile(player);
  69. final int MAX_BONUS_LEVEL = 1000;
  70. int herbLevel = profile.getSkillLevel(SkillType.HERBALISM);
  71. int id = block.getTypeId();
  72. Material type = block.getType();
  73. Byte data = block.getData();
  74. Location loc = block.getLocation();
  75. Material mat = null;
  76. int xp = 0;
  77. int catciDrops = 0;
  78. int caneDrops = 0;
  79. boolean customPlant = false;
  80. int randomChance = 1000;
  81. if (player.hasPermission("mcmmo.perks.lucky.herbalism")) {
  82. randomChance = (int) (randomChance * 0.75);
  83. }
  84. switch (type) {
  85. case BROWN_MUSHROOM:
  86. case RED_MUSHROOM:
  87. if (!mcMMO.placeStore.isTrue(block)) {
  88. mat = Material.getMaterial(id);
  89. xp = Config.getInstance().getHerbalismXPMushrooms();
  90. }
  91. break;
  92. case CACTUS:
  93. for (int y = 0; y <= 2; y++) {
  94. Block b = block.getRelative(0, y, 0);
  95. if (b.getType().equals(Material.CACTUS)) {
  96. mat = Material.CACTUS;
  97. if (!mcMMO.placeStore.isTrue(b)) {
  98. if (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel) {
  99. catciDrops++;
  100. }
  101. xp += Config.getInstance().getHerbalismXPCactus();
  102. }
  103. }
  104. }
  105. break;
  106. case CROPS:
  107. if (data == CropState.RIPE.getData()) {
  108. mat = Material.WHEAT;
  109. xp = Config.getInstance().getHerbalismXPWheat();
  110. if (Permissions.getInstance().greenThumbWheat(player)) {
  111. greenThumbWheat(block, player, event, plugin);
  112. }
  113. }
  114. break;
  115. case MELON_BLOCK:
  116. if (!mcMMO.placeStore.isTrue(block)) {
  117. mat = Material.MELON;
  118. xp = Config.getInstance().getHerbalismXPMelon();
  119. }
  120. break;
  121. case NETHER_WARTS:
  122. if (data == (byte) 0x3) {
  123. mat = Material.NETHER_STALK;
  124. xp = Config.getInstance().getHerbalismXPNetherWart();
  125. }
  126. break;
  127. case PUMPKIN:
  128. case JACK_O_LANTERN:
  129. if (!mcMMO.placeStore.isTrue(block)) {
  130. mat = Material.getMaterial(id);
  131. xp = Config.getInstance().getHerbalismXPPumpkin();
  132. }
  133. break;
  134. case RED_ROSE:
  135. case YELLOW_FLOWER:
  136. if (!mcMMO.placeStore.isTrue(block)) {
  137. mat = Material.getMaterial(id);
  138. xp = Config.getInstance().getHerbalismXPFlowers();
  139. }
  140. break;
  141. case SUGAR_CANE_BLOCK:
  142. for (int y = 0; y <= 2; y++) {
  143. Block b = block.getRelative(0, y, 0);
  144. if (b.getType().equals(Material.SUGAR_CANE_BLOCK)) {
  145. mat = Material.SUGAR_CANE;
  146. if (!mcMMO.placeStore.isTrue(b)) {
  147. if (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel) {
  148. caneDrops++;
  149. }
  150. xp += Config.getInstance().getHerbalismXPSugarCane();
  151. }
  152. }
  153. }
  154. break;
  155. case VINE:
  156. if (!mcMMO.placeStore.isTrue(block)) {
  157. mat = type;
  158. xp = Config.getInstance().getHerbalismXPVines();
  159. }
  160. break;
  161. case WATER_LILY:
  162. if (!mcMMO.placeStore.isTrue(block)) {
  163. mat = type;
  164. xp = Config.getInstance().getHerbalismXPLilyPads();
  165. }
  166. break;
  167. default:
  168. if (Config.getInstance().getBlockModsEnabled() && CustomBlocksConfig.getInstance().customHerbalismBlocks.contains(new ItemStack(block.getTypeId(), 1, (short) 0, block.getData()))) {
  169. customPlant = true;
  170. xp = ModChecks.getCustomBlock(block).getXpGain();
  171. }
  172. break;
  173. }
  174. if (mat == null && !customPlant) {
  175. return;
  176. }
  177. if (Permissions.getInstance().herbalismDoubleDrops(player)) {
  178. ItemStack is = null;
  179. if (customPlant) {
  180. is = new ItemStack(ModChecks.getCustomBlock(block).getItemDrop());
  181. }
  182. else {
  183. is = new ItemStack(mat);
  184. }
  185. if (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel) {
  186. Config configInstance = Config.getInstance();
  187. switch (type) {
  188. case BROWN_MUSHROOM:
  189. if (configInstance.getBrownMushroomsDoubleDropsEnabled()) {
  190. Misc.dropItem(loc, is);
  191. }
  192. break;
  193. case CACTUS:
  194. if (configInstance.getCactiDoubleDropsEnabled()) {
  195. Misc.dropItems(loc, is, catciDrops);
  196. }
  197. break;
  198. case CROPS:
  199. if (configInstance.getWheatDoubleDropsEnabled()) {
  200. Misc.dropItem(loc, is);
  201. }
  202. break;
  203. case MELON_BLOCK:
  204. if (configInstance.getMelonsDoubleDropsEnabled()) {
  205. Misc.dropItems(loc, is, 3);
  206. Misc.randomDropItems(loc, is, 50, 4);
  207. }
  208. break;
  209. case NETHER_WARTS:
  210. if (configInstance.getNetherWartsDoubleDropsEnabled()) {
  211. Misc.dropItems(loc, is, 2);
  212. Misc.randomDropItems(loc, is, 50, 3);
  213. }
  214. break;
  215. case PUMPKIN:
  216. if (configInstance.getPumpkinsDoubleDropsEnabled()) {
  217. Misc.dropItem(loc, is);
  218. }
  219. break;
  220. case RED_MUSHROOM:
  221. if (configInstance.getRedMushroomsDoubleDropsEnabled()) {
  222. Misc.dropItem(loc, is);
  223. }
  224. break;
  225. case SUGAR_CANE_BLOCK:
  226. if (configInstance.getSugarCaneDoubleDropsEnabled()) {
  227. Misc.dropItems(loc, is, caneDrops);
  228. }
  229. break;
  230. case VINE:
  231. if (configInstance.getVinesDoubleDropsEnabled()) {
  232. Misc.dropItem(loc, is);
  233. }
  234. break;
  235. case WATER_LILY:
  236. if (configInstance.getWaterLiliesDoubleDropsEnabled()) {
  237. Misc.dropItem(loc, is);
  238. }
  239. break;
  240. case YELLOW_FLOWER:
  241. if (configInstance.getYellowFlowersDoubleDropsEnabled()) {
  242. Misc.dropItem(loc, is);
  243. }
  244. break;
  245. default:
  246. if (customPlant) {
  247. CustomBlock customBlock = ModChecks.getCustomBlock(block);
  248. int minimumDropAmount = customBlock.getMinimumDropAmount();
  249. int maximumDropAmount = customBlock.getMaximumDropAmount();
  250. is = customBlock.getItemDrop();
  251. if (minimumDropAmount != maximumDropAmount) {
  252. Misc.dropItems(loc, is, minimumDropAmount);
  253. Misc.randomDropItems(loc, is, 50, maximumDropAmount - minimumDropAmount);
  254. }
  255. else {
  256. Misc.dropItems(loc, is, minimumDropAmount);
  257. }
  258. }
  259. break;
  260. }
  261. }
  262. }
  263. Skills.xpProcessing(player, profile, SkillType.HERBALISM, xp);
  264. }
  265. /**
  266. * Apply the Green Thumb ability to crops.
  267. *
  268. * @param block The block to apply the ability to
  269. * @param player The player using the ability
  270. * @param event The event triggering the ability
  271. * @param plugin mcMMO plugin instance
  272. */
  273. private static void greenThumbWheat(Block block, Player player, BlockBreakEvent event, mcMMO plugin) {
  274. final int MAX_BONUS_LEVEL = 1500;
  275. PlayerProfile profile = Users.getProfile(player);
  276. int herbLevel = profile.getSkillLevel(SkillType.HERBALISM);
  277. PlayerInventory inventory = player.getInventory();
  278. boolean hasSeeds = inventory.contains(Material.SEEDS);
  279. Location loc = block.getLocation();
  280. int randomChance = 1500;
  281. if (player.hasPermission("mcmmo.perks.lucky.herbalism")) {
  282. randomChance = (int) (randomChance * 0.75);
  283. }
  284. if (hasSeeds && profile.getAbilityMode(AbilityType.GREEN_TERRA) || hasSeeds && (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel)) {
  285. event.setCancelled(true);
  286. Misc.dropItem(loc, new ItemStack(Material.WHEAT));
  287. Misc.randomDropItems(loc, new ItemStack(Material.SEEDS), 50, 3);
  288. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new GreenThumbTimer(block, profile), 1);
  289. inventory.removeItem(new ItemStack(Material.SEEDS));
  290. player.updateInventory(); // Needed until replacement available
  291. }
  292. }
  293. /**
  294. * Apply the Green Thumb ability to blocks.
  295. *
  296. * @param is The item in the player's hand
  297. * @param player The player activating the ability
  298. * @param block The block being used in the ability
  299. */
  300. public static void greenThumbBlocks(ItemStack is, Player player, Block block) {
  301. final int MAX_BONUS_LEVEL = 1500;
  302. PlayerProfile profile = Users.getProfile(player);
  303. int skillLevel = profile.getSkillLevel(SkillType.HERBALISM);
  304. int seeds = is.getAmount();
  305. player.setItemInHand(new ItemStack(Material.SEEDS, seeds - 1));
  306. int randomChance = 1500;
  307. if (player.hasPermission("mcmmo.perks.lucky.herbalism")) {
  308. randomChance = (int) (randomChance * 0.75);
  309. }
  310. if (skillLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= skillLevel) {
  311. greenTerraConvert(player, block);
  312. }
  313. else {
  314. player.sendMessage(LocaleLoader.getString("Herbalism.Ability.GTh.Fail"));
  315. }
  316. }
  317. }