Herbalism.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50.skills;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.Location;
  17. import org.bukkit.Material;
  18. import org.bukkit.World;
  19. import org.bukkit.block.Block;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.event.block.BlockBreakEvent;
  22. import org.bukkit.inventory.ItemStack;
  23. import com.gmail.nossr50.Users;
  24. import com.gmail.nossr50.m;
  25. import com.gmail.nossr50.mcMMO;
  26. import com.gmail.nossr50.config.LoadProperties;
  27. import com.gmail.nossr50.datatypes.AbilityType;
  28. import com.gmail.nossr50.datatypes.PlayerProfile;
  29. import com.gmail.nossr50.datatypes.SkillType;
  30. import com.gmail.nossr50.locale.mcLocale;
  31. public class Herbalism
  32. {
  33. public static void greenTerraCheck(Player player, Block block)
  34. {
  35. PlayerProfile PP = Users.getProfile(player);
  36. if(m.isHoe(player.getItemInHand()))
  37. {
  38. if(block != null)
  39. {
  40. if(!m.abilityBlockCheck(block))
  41. return;
  42. }
  43. if(PP.getHoePreparationMode())
  44. {
  45. PP.setHoePreparationMode(false);
  46. }
  47. int ticks = 2;
  48. int x = PP.getSkillLevel(SkillType.HERBALISM);
  49. while(x >= 50)
  50. {
  51. x-=50;
  52. ticks++;
  53. }
  54. if(!PP.getGreenTerraMode() && Skills.cooldownOver(player, PP.getSkillDATS(AbilityType.GREEN_TERRA), LoadProperties.greenTerraCooldown))
  55. {
  56. player.sendMessage(mcLocale.getString("Skills.GreenTerraOn"));
  57. for(Player y : player.getWorld().getPlayers())
  58. {
  59. if(y != null && y != player && m.getDistance(player.getLocation(), y.getLocation()) < 10)
  60. y.sendMessage(mcLocale.getString("Skills.GreenTerraPlayer", new Object[] {player.getName()}));
  61. }
  62. PP.setSkillDATS(AbilityType.GREEN_TERRA, System.currentTimeMillis()+(ticks*1000));
  63. PP.setGreenTerraMode(true);
  64. }
  65. }
  66. }
  67. public static void greenTerra(Player player, Block block){
  68. if(block.getType() == Material.COBBLESTONE || block.getType() == Material.DIRT){
  69. if(!hasSeeds(player))
  70. player.sendMessage("You need more seeds to spread Green Terra");
  71. if(hasSeeds(player) && block.getType() != Material.WHEAT)
  72. {
  73. removeSeeds(player);
  74. if(LoadProperties.enableSmoothToMossy && block.getType() == Material.SMOOTH_BRICK)
  75. block.setData((byte)1);
  76. if(LoadProperties.enableDirtToGrass && block.getType() == Material.DIRT)
  77. block.setType(Material.GRASS);
  78. if(LoadProperties.enableCobbleToMossy && block.getType() == Material.COBBLESTONE)
  79. block.setType(Material.MOSSY_COBBLESTONE);
  80. }
  81. }
  82. }
  83. public static Boolean canBeGreenTerra(Block block){
  84. int t = block.getTypeId();
  85. return t == 103 || t == 4 || t == 3 || t == 59 || t == 81 || t == 83 || t == 91 || t == 86 || t == 39 || t == 46 || t == 37 || t == 38;
  86. }
  87. public static boolean hasSeeds(Player player){
  88. ItemStack[] inventory = player.getInventory().getContents();
  89. for(ItemStack x : inventory){
  90. if(x != null && x.getTypeId() == 295){
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. public static void removeSeeds(Player player){
  97. ItemStack[] inventory = player.getInventory().getContents();
  98. for(ItemStack x : inventory){
  99. if(x != null && x.getTypeId() == 295){
  100. if(x.getAmount() == 1){
  101. x.setTypeId(0);
  102. x.setAmount(0);
  103. player.getInventory().setContents(inventory);
  104. } else{
  105. x.setAmount(x.getAmount() - 1);
  106. player.getInventory().setContents(inventory);
  107. }
  108. return;
  109. }
  110. }
  111. }
  112. public static void herbalismProcCheck(final Block block, Player player, BlockBreakEvent event, mcMMO plugin)
  113. {
  114. final PlayerProfile PP = Users.getProfile(player);
  115. int herbLevel = PP.getSkillLevel(SkillType.HERBALISM);
  116. int type = block.getTypeId();
  117. Location loc = block.getLocation();
  118. ItemStack is = null;
  119. Material mat = null;
  120. if(plugin.misc.blockWatchList.contains(block))
  121. {
  122. return;
  123. }
  124. //Wheat
  125. if(type == 59 && block.getData() == (byte) 0x7)
  126. {
  127. mat = Material.getMaterial(296);
  128. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  129. PP.addXP(SkillType.HERBALISM, LoadProperties.mwheat, player);
  130. if(player != null)
  131. {
  132. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  133. m.mcDropItem(loc, is);
  134. }
  135. //GREEN THUMB
  136. if(PP.getGreenTerraMode() || (herbLevel >= 1500 || (Math.random() * 1500 <= herbLevel)))
  137. {
  138. event.setCancelled(true);
  139. m.mcDropItem(loc, is);
  140. //DROP SOME SEEDS
  141. mat = Material.SEEDS;
  142. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  143. m.mcDropItem(loc, is);
  144. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  145. public void run() {
  146. block.setType(Material.CROPS);
  147. //This replants the wheat at a certain stage in development based on Herbalism Skill
  148. if(!PP.getGreenTerraMode())
  149. {
  150. if (PP.getSkillLevel(SkillType.HERBALISM) >= 600)
  151. block.setData((byte) 0x4);
  152. else if (PP.getSkillLevel(SkillType.HERBALISM) >= 400)
  153. block.setData((byte) 0x3);
  154. else if (PP.getSkillLevel(SkillType.HERBALISM) >= 200)
  155. block.setData((byte) 0x2);
  156. else
  157. block.setData((byte) 0x1);
  158. } else
  159. block.setData((byte) 0x4);
  160. }
  161. }, 1);
  162. }
  163. }
  164. //Nether Wart
  165. if(type == 115 && block.getData() == (byte) 0x3)
  166. {
  167. mat = Material.getMaterial(372);
  168. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  169. PP.addXP(SkillType.HERBALISM, LoadProperties.mnetherwart, player);
  170. if(player != null)
  171. {
  172. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  173. {
  174. m.mcDropItem(loc, is);
  175. m.mcDropItem(loc, is);
  176. if(Math.random() * 10 > 5)
  177. m.mcDropItem(loc, is);
  178. if(Math.random() * 10 > 5)
  179. m.mcDropItem(loc, is);
  180. if(Math.random() * 10 > 5)
  181. m.mcDropItem(loc, is);
  182. }
  183. }
  184. }
  185. /*
  186. * We need to check not-wheat and not-netherwart stuff for if it was placed by the player or not
  187. */
  188. if(block.getData() != (byte) 5)
  189. {
  190. //Cactus
  191. if(type == 81){
  192. //Setup the loop
  193. World world = block.getWorld();
  194. Block[] blockArray = new Block[3];
  195. blockArray[0] = block;
  196. blockArray[1] = world.getBlockAt(block.getX(), block.getY()+1, block.getZ());
  197. blockArray[2] = world.getBlockAt(block.getX(), block.getY()+2, block.getZ());
  198. Material[] materialArray = new Material[3];
  199. materialArray[0] = blockArray[0].getType();
  200. materialArray[1] = blockArray[1].getType();
  201. materialArray[2] = blockArray[2].getType();
  202. byte[] byteArray = new byte[3];
  203. byteArray[0] = blockArray[0].getData();
  204. byteArray[1] = blockArray[0].getData();
  205. byteArray[2] = blockArray[0].getData();
  206. int x = 0;
  207. for(Block target : blockArray)
  208. {
  209. if(materialArray[x] == Material.CACTUS)
  210. {
  211. is = new ItemStack(Material.CACTUS, 1, (byte)0, (byte)0);
  212. if(byteArray[x] != (byte) 5)
  213. {
  214. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  215. {
  216. m.mcDropItem(target.getLocation(), is);
  217. }
  218. PP.addXP(SkillType.HERBALISM, LoadProperties.mcactus, player);
  219. }
  220. }
  221. x++;
  222. }
  223. }
  224. //Sugar Canes
  225. if(type == 83)
  226. {
  227. //Setup the loop
  228. World world = block.getWorld();
  229. Block[] blockArray = new Block[3];
  230. blockArray[0] = block;
  231. blockArray[1] = world.getBlockAt(block.getX(), block.getY()+1, block.getZ());
  232. blockArray[2] = world.getBlockAt(block.getX(), block.getY()+2, block.getZ());
  233. Material[] materialArray = new Material[3];
  234. materialArray[0] = blockArray[0].getType();
  235. materialArray[1] = blockArray[1].getType();
  236. materialArray[2] = blockArray[2].getType();
  237. byte[] byteArray = new byte[3];
  238. byteArray[0] = blockArray[0].getData();
  239. byteArray[1] = blockArray[0].getData();
  240. byteArray[2] = blockArray[0].getData();
  241. int x = 0;
  242. for(Block target : blockArray)
  243. {
  244. if(materialArray[x] == Material.SUGAR_CANE_BLOCK)
  245. {
  246. is = new ItemStack(Material.SUGAR_CANE, 1, (byte)0, (byte)0);
  247. //Check for being placed by the player
  248. if(byteArray[x] != (byte) 5)
  249. {
  250. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  251. {
  252. m.mcDropItem(target.getLocation(), is);
  253. }
  254. PP.addXP(SkillType.HERBALISM, LoadProperties.msugar, player);
  255. }
  256. }
  257. x++;
  258. }
  259. }
  260. //Pumpkins
  261. if((type == 91 || type == 86))
  262. {
  263. mat = Material.getMaterial(block.getTypeId());
  264. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  265. if(player != null)
  266. {
  267. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  268. {
  269. m.mcDropItem(loc, is);
  270. }
  271. }
  272. PP.addXP(SkillType.HERBALISM, LoadProperties.mpumpkin, player);
  273. }
  274. //Melon
  275. if(type == 103)
  276. {
  277. mat = Material.getMaterial(360);
  278. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  279. if(player != null)
  280. {
  281. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  282. {
  283. m.mcDropItem(loc, is);
  284. m.mcDropItem(loc, is);
  285. m.mcDropItem(loc, is);
  286. if(Math.random() * 10 > 5)
  287. m.mcDropItem(loc, is);
  288. if(Math.random() * 10 > 5)
  289. m.mcDropItem(loc, is);
  290. if(Math.random() * 10 > 5)
  291. m.mcDropItem(loc, is);
  292. if(Math.random() * 10 > 5)
  293. m.mcDropItem(loc, is);
  294. }
  295. }
  296. PP.addXP(SkillType.HERBALISM, LoadProperties.mmelon, player);
  297. }
  298. //Mushroom
  299. if(type == 39 || type == 40)
  300. {
  301. mat = Material.getMaterial(block.getTypeId());
  302. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  303. if(player != null)
  304. {
  305. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  306. {
  307. m.mcDropItem(loc, is);
  308. }
  309. }
  310. PP.addXP(SkillType.HERBALISM, LoadProperties.mmushroom, player);
  311. }
  312. //Flower
  313. if(type == 37 || type == 38){
  314. mat = Material.getMaterial(block.getTypeId());
  315. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  316. if(player != null){
  317. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  318. m.mcDropItem(loc, is);
  319. }
  320. PP.addXP(SkillType.HERBALISM, LoadProperties.mflower, player);
  321. }
  322. //Lily Pads
  323. if(type == 111)
  324. {
  325. mat = Material.getMaterial(block.getTypeId());
  326. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  327. if(player != null){
  328. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  329. m.mcDropItem(loc, is);
  330. }
  331. PP.addXP(SkillType.HERBALISM, LoadProperties.mlilypad, player);
  332. }
  333. //Vines
  334. if(type == 106){
  335. mat = Material.getMaterial(block.getTypeId());
  336. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  337. if(player != null){
  338. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  339. m.mcDropItem(loc, is);
  340. }
  341. PP.addXP(SkillType.HERBALISM, LoadProperties.mvines, player);
  342. }
  343. }
  344. Skills.XpCheckSkill(SkillType.HERBALISM, player);
  345. }
  346. }