/TreeFeller/src/me/TheWiseman/TreeFeller/TreeFellerBlockListener.java

https://bitbucket.org/Waizujin/treefeller · Java · 303 lines · 233 code · 66 blank · 4 comment · 83 complexity · 9317098dcf81fc3b4ee9323e962d5e8d MD5 · raw file

  1. package me.TheWiseman.TreeFeller;
  2. import com.gmail.nossr50.Users;
  3. import com.gmail.nossr50.config.LoadProperties;
  4. import com.gmail.nossr50.datatypes.PlayerProfile;
  5. import com.gmail.nossr50.datatypes.SkillType;
  6. import com.gmail.nossr50.m;
  7. import com.gmail.nossr50.skills.Skills;
  8. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.logging.Logger;
  12. import org.bukkit.Material;
  13. import org.bukkit.World;
  14. import org.bukkit.block.Block;
  15. import org.bukkit.block.BlockFace;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.EventHandler;
  18. import org.bukkit.event.EventPriority;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.BlockBreakEvent;
  21. import org.bukkit.inventory.ItemStack;
  22. import org.bukkit.plugin.Plugin;
  23. public class TreeFellerBlockListener implements Listener {
  24. public static final Logger log = Logger.getLogger("Minecraft");
  25. public static TreeFeller plugin;
  26. public TreeFellerBlockListener(TreeFeller instance) {
  27. plugin = instance;
  28. }
  29. @EventHandler(priority = EventPriority.NORMAL)
  30. public void onBlockBreak(BlockBreakEvent event) {
  31. Player player = event.getPlayer();
  32. Block block = event.getBlock();
  33. String playerName = player.getDisplayName();
  34. if(plugin.getPlayerSettings().getInt("player.settings." + playerName, -1) == 0) {
  35. return;
  36. }
  37. if(event.isCancelled()) {
  38. return;
  39. }
  40. if((player.hasPermission("TreeFeller.Chop") == false) && (plugin.getConfig().getBoolean("CanChop") == true)) {
  41. return;
  42. }
  43. if(plugin.getConfig().getBoolean("CanChop") == false) {
  44. return;
  45. }
  46. if(mcMMOTreeFeller(event.getPlayer())) {
  47. return;
  48. }
  49. if(block.getType() == Material.LOG) {
  50. if(Chop(event.getBlock(), event.getPlayer(), event.getBlock().getWorld())) {
  51. } else {
  52. event.setCancelled(false);
  53. }
  54. }
  55. }
  56. public boolean Chop(Block block, Player player, World world) {
  57. List<Block> blocks = new LinkedList<Block>();
  58. Block highest = getHighestLog(block);
  59. if(isTree(highest, player, block)) {
  60. getBlocksToChop(block, highest, blocks, player);
  61. popLogs(block, blocks, world, player);
  62. } else {
  63. return false;
  64. }
  65. return true;
  66. }
  67. public void getBlocksToChop(Block block, Block highest, List<Block> blocks, Player player) {
  68. while(block.getY() <= highest.getY()) {
  69. if(isProtected(player, block) == false) {
  70. break;
  71. }
  72. if(!blocks.contains(block)) {
  73. blocks.add(block);
  74. }
  75. getBranches(block, blocks, block.getRelative(BlockFace.NORTH), player);
  76. getBranches(block, blocks, block.getRelative(BlockFace.NORTH_EAST), player);
  77. getBranches(block, blocks, block.getRelative(BlockFace.EAST), player);
  78. getBranches(block, blocks, block.getRelative(BlockFace.SOUTH_EAST), player);
  79. getBranches(block, blocks, block.getRelative(BlockFace.SOUTH), player);
  80. getBranches(block, blocks, block.getRelative(BlockFace.SOUTH_WEST), player);
  81. getBranches(block, blocks, block.getRelative(BlockFace.WEST), player);
  82. getBranches(block, blocks, block.getRelative(BlockFace.NORTH_WEST), player);
  83. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH))) {
  84. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH), player);
  85. }
  86. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_EAST))) {
  87. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_EAST), player);
  88. }
  89. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.EAST))) {
  90. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.EAST), player);
  91. }
  92. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_EAST))) {
  93. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_EAST), player);
  94. }
  95. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH))) {
  96. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH), player);
  97. }
  98. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_WEST))) {
  99. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_WEST), player);
  100. }
  101. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.WEST))) {
  102. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.WEST), player);
  103. }
  104. if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_WEST))) {
  105. getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_WEST), player);
  106. }
  107. if((blocks.contains(block.getRelative(BlockFace.UP))) || (block.getRelative(BlockFace.UP).getType() != Material.LOG)) {
  108. break;
  109. }
  110. block = block.getRelative(BlockFace.UP);
  111. }
  112. }
  113. public void getBranches(Block block, List<Block> blocks, Block other, Player player) {
  114. if((!blocks.contains(other)) && (other.getType() == Material.LOG)){
  115. getBlocksToChop(other, getHighestLog(other), blocks, player);
  116. }
  117. }
  118. public Block getHighestLog(Block block) {
  119. while(block.getRelative(BlockFace.UP).getType() == Material.LOG) {
  120. block = block.getRelative(BlockFace.UP);
  121. }
  122. return block;
  123. }
  124. public boolean isTree(Block block, Player player, Block first) {
  125. int counter = 0;
  126. if(block.getRelative(BlockFace.UP).getType() == Material.LEAVES) {
  127. counter++;
  128. }
  129. if(block.getRelative(BlockFace.DOWN).getType() == Material.LEAVES) {
  130. counter++;
  131. }
  132. if(block.getRelative(BlockFace.NORTH).getType() == Material.LEAVES) {
  133. counter++;
  134. }
  135. if(block.getRelative(BlockFace.EAST).getType() == Material.LEAVES) {
  136. counter++;
  137. }
  138. if(block.getRelative(BlockFace.SOUTH).getType() == Material.LEAVES) {
  139. counter++;
  140. }
  141. if(block.getRelative(BlockFace.WEST).getType() == Material.LEAVES) {
  142. counter++;
  143. }
  144. if(counter >= 2) {
  145. return true;
  146. }
  147. if(block.getData() == 1) {
  148. block = block.getRelative(BlockFace.UP);
  149. if(block.getRelative(BlockFace.UP).getType() == Material.LEAVES) {
  150. counter++;
  151. }
  152. if(block.getRelative(BlockFace.NORTH).getType() == Material.LEAVES) {
  153. counter++;
  154. }
  155. if(block.getRelative(BlockFace.EAST).getType() == Material.LEAVES) {
  156. counter++;
  157. }
  158. if(block.getRelative(BlockFace.SOUTH).getType() == Material.LEAVES) {
  159. counter++;
  160. }
  161. if(block.getRelative(BlockFace.WEST).getType() == Material.LEAVES) {
  162. counter++;
  163. }
  164. if(counter >= 2) {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. public void popLogs(Block block, List<Block> blocks, World world, Player player) {
  171. ItemStack item = new ItemStack(1, 1);
  172. item.setAmount(1);
  173. for(int counter = 0; counter < blocks.size(); counter++) {
  174. block = (Block)blocks.get(counter);
  175. if(plugin.logBlockEnabled()) {
  176. plugin.lbconsumer.queueBlockBreak(player.getName(), block.getState());
  177. }
  178. item.setType(block.getType());
  179. item.setDurability(block.getData());
  180. block.setType(Material.AIR);
  181. world.dropItem(block.getLocation(), item);
  182. if(plugin.activemcMMO()) {
  183. mcMMOFake(player, block);
  184. }
  185. }
  186. }
  187. public boolean mcMMOTreeFeller(Player player) {
  188. boolean isMcMMOEnabled = plugin.getServer().getPluginManager().isPluginEnabled("mcMMO");
  189. if(!isMcMMOEnabled) {
  190. return false;
  191. }
  192. PlayerProfile pp = Users.getProfile(player);
  193. if(pp.getTreeFellerMode()) {
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. public void mcMMOFake(Player player, Block block) {
  200. PlayerProfile pp = Users.getProfile(player);
  201. ItemStack inhand = player.getItemInHand();
  202. Skills.monitorSkill(player, pp, 0, SkillType.WOODCUTTING);
  203. if(LoadProperties.woodcuttingrequiresaxe.booleanValue() && m.getTier(inhand) != 0) {
  204. /* Oak */
  205. if(block.getData() == 0) {
  206. pp.addXP(SkillType.WOODCUTTING, LoadProperties.moak, player);
  207. }
  208. /* Pine / Spruce */
  209. if(block.getData() == 1) {
  210. pp.addXP(SkillType.WOODCUTTING, LoadProperties.mspruce, player);
  211. }
  212. /* Birch */
  213. if(block.getData() == 2) {
  214. pp.addXP(SkillType.WOODCUTTING, LoadProperties.mbirch, player);
  215. }
  216. /* Jungle */
  217. if(block.getData() == 3) {
  218. pp.addXP(SkillType.WOODCUTTING, LoadProperties.mjungle, player);
  219. }
  220. }
  221. Skills.XpCheckAll(player);
  222. }
  223. public boolean isProtected(Player player, Block block) {
  224. if(getWorldGuard() == null) {
  225. return false;
  226. } else {
  227. return getWorldGuard().canBuild(player, block);
  228. }
  229. }
  230. private WorldGuardPlugin getWorldGuard() {
  231. Plugin WorldGuard = plugin.getServer().getPluginManager().getPlugin("WorldGuard");
  232. if(WorldGuard == null || !(WorldGuard instanceof WorldGuardPlugin)) {
  233. return null;
  234. }
  235. return (WorldGuardPlugin) WorldGuard;
  236. }
  237. }