Herbalism.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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(hasSeeds(player) && PP.getGreenTerraMode() || hasSeeds(player) && (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. removeSeeds(player);
  163. }
  164. }
  165. //Nether Wart
  166. if(type == 115 && block.getData() == (byte) 0x3)
  167. {
  168. mat = Material.getMaterial(372);
  169. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  170. PP.addXP(SkillType.HERBALISM, LoadProperties.mnetherwart, player);
  171. if(player != null)
  172. {
  173. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  174. {
  175. m.mcDropItems(loc, is, 2);
  176. m.mcRandomDropItems(loc, is, 50, 3);
  177. }
  178. }
  179. }
  180. /*
  181. * We need to check not-wheat and not-netherwart stuff for if it was placed by the player or not
  182. */
  183. if(block.getData() != (byte) 5)
  184. {
  185. //Cactus
  186. if(type == 81){
  187. //Setup the loop
  188. World world = block.getWorld();
  189. Block[] blockArray = new Block[3];
  190. blockArray[0] = block;
  191. blockArray[1] = world.getBlockAt(block.getX(), block.getY()+1, block.getZ());
  192. blockArray[2] = world.getBlockAt(block.getX(), block.getY()+2, block.getZ());
  193. Material[] materialArray = new Material[3];
  194. materialArray[0] = blockArray[0].getType();
  195. materialArray[1] = blockArray[1].getType();
  196. materialArray[2] = blockArray[2].getType();
  197. byte[] byteArray = new byte[3];
  198. byteArray[0] = blockArray[0].getData();
  199. byteArray[1] = blockArray[0].getData();
  200. byteArray[2] = blockArray[0].getData();
  201. int x = 0;
  202. for(Block target : blockArray)
  203. {
  204. if(materialArray[x] == Material.CACTUS)
  205. {
  206. is = new ItemStack(Material.CACTUS, 1, (byte)0, (byte)0);
  207. if(byteArray[x] != (byte) 5)
  208. {
  209. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  210. {
  211. m.mcDropItem(target.getLocation(), is);
  212. }
  213. PP.addXP(SkillType.HERBALISM, LoadProperties.mcactus, player);
  214. }
  215. }
  216. x++;
  217. }
  218. }
  219. //Sugar Canes
  220. if(type == 83)
  221. {
  222. //Setup the loop
  223. World world = block.getWorld();
  224. Block[] blockArray = new Block[3];
  225. blockArray[0] = block;
  226. blockArray[1] = world.getBlockAt(block.getX(), block.getY()+1, block.getZ());
  227. blockArray[2] = world.getBlockAt(block.getX(), block.getY()+2, block.getZ());
  228. Material[] materialArray = new Material[3];
  229. materialArray[0] = blockArray[0].getType();
  230. materialArray[1] = blockArray[1].getType();
  231. materialArray[2] = blockArray[2].getType();
  232. byte[] byteArray = new byte[3];
  233. byteArray[0] = blockArray[0].getData();
  234. byteArray[1] = blockArray[0].getData();
  235. byteArray[2] = blockArray[0].getData();
  236. int x = 0;
  237. for(Block target : blockArray)
  238. {
  239. if(materialArray[x] == Material.SUGAR_CANE_BLOCK)
  240. {
  241. is = new ItemStack(Material.SUGAR_CANE, 1, (byte)0, (byte)0);
  242. //Check for being placed by the player
  243. if(byteArray[x] != (byte) 5)
  244. {
  245. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  246. {
  247. m.mcDropItem(target.getLocation(), is);
  248. }
  249. PP.addXP(SkillType.HERBALISM, LoadProperties.msugar, player);
  250. }
  251. }
  252. x++;
  253. }
  254. }
  255. //Pumpkins
  256. if((type == 91 || type == 86))
  257. {
  258. mat = Material.getMaterial(block.getTypeId());
  259. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  260. if(player != null)
  261. {
  262. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  263. {
  264. m.mcDropItem(loc, is);
  265. }
  266. }
  267. PP.addXP(SkillType.HERBALISM, LoadProperties.mpumpkin, player);
  268. }
  269. //Melon
  270. if(type == 103)
  271. {
  272. mat = Material.getMaterial(360);
  273. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  274. if(player != null)
  275. {
  276. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  277. {
  278. m.mcDropItems(loc, is, 3);
  279. m.mcRandomDropItems(loc, is, 50, 4);
  280. }
  281. }
  282. PP.addXP(SkillType.HERBALISM, LoadProperties.mmelon, player);
  283. }
  284. //Mushroom
  285. if(type == 39 || type == 40)
  286. {
  287. mat = Material.getMaterial(block.getTypeId());
  288. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  289. if(player != null)
  290. {
  291. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  292. {
  293. m.mcDropItem(loc, is);
  294. }
  295. }
  296. PP.addXP(SkillType.HERBALISM, LoadProperties.mmushroom, player);
  297. }
  298. //Flower
  299. if(type == 37 || type == 38){
  300. mat = Material.getMaterial(block.getTypeId());
  301. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  302. if(player != null){
  303. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  304. m.mcDropItem(loc, is);
  305. }
  306. PP.addXP(SkillType.HERBALISM, LoadProperties.mflower, player);
  307. }
  308. //Lily Pads
  309. if(type == 111)
  310. {
  311. mat = Material.getMaterial(block.getTypeId());
  312. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  313. if(player != null){
  314. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  315. m.mcDropItem(loc, is);
  316. }
  317. PP.addXP(SkillType.HERBALISM, LoadProperties.mlilypad, player);
  318. }
  319. //Vines
  320. if(type == 106){
  321. mat = Material.getMaterial(block.getTypeId());
  322. is = new ItemStack(mat, 1, (byte)0, (byte)0);
  323. if(player != null){
  324. if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel))
  325. m.mcDropItem(loc, is);
  326. }
  327. PP.addXP(SkillType.HERBALISM, LoadProperties.mvines, player);
  328. }
  329. }
  330. Skills.XpCheckSkill(SkillType.HERBALISM, player);
  331. }
  332. }