/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
- package me.TheWiseman.TreeFeller;
-
- import com.gmail.nossr50.Users;
- import com.gmail.nossr50.config.LoadProperties;
- import com.gmail.nossr50.datatypes.PlayerProfile;
- import com.gmail.nossr50.datatypes.SkillType;
- import com.gmail.nossr50.m;
- import com.gmail.nossr50.skills.Skills;
- import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.logging.Logger;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockFace;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.plugin.Plugin;
-
- public class TreeFellerBlockListener implements Listener {
- public static final Logger log = Logger.getLogger("Minecraft");
- public static TreeFeller plugin;
-
- public TreeFellerBlockListener(TreeFeller instance) {
- plugin = instance;
- }
-
- @EventHandler(priority = EventPriority.NORMAL)
- public void onBlockBreak(BlockBreakEvent event) {
- Player player = event.getPlayer();
- Block block = event.getBlock();
- String playerName = player.getDisplayName();
-
- if(plugin.getPlayerSettings().getInt("player.settings." + playerName, -1) == 0) {
- return;
- }
-
- if(event.isCancelled()) {
- return;
- }
-
- if((player.hasPermission("TreeFeller.Chop") == false) && (plugin.getConfig().getBoolean("CanChop") == true)) {
- return;
- }
-
- if(plugin.getConfig().getBoolean("CanChop") == false) {
- return;
- }
-
- if(mcMMOTreeFeller(event.getPlayer())) {
- return;
- }
-
- if(block.getType() == Material.LOG) {
- if(Chop(event.getBlock(), event.getPlayer(), event.getBlock().getWorld())) {
-
- } else {
- event.setCancelled(false);
- }
- }
- }
-
- public boolean Chop(Block block, Player player, World world) {
- List<Block> blocks = new LinkedList<Block>();
- Block highest = getHighestLog(block);
-
- if(isTree(highest, player, block)) {
- getBlocksToChop(block, highest, blocks, player);
- popLogs(block, blocks, world, player);
- } else {
- return false;
- }
-
- return true;
- }
-
- public void getBlocksToChop(Block block, Block highest, List<Block> blocks, Player player) {
- while(block.getY() <= highest.getY()) {
- if(isProtected(player, block) == false) {
- break;
- }
-
- if(!blocks.contains(block)) {
- blocks.add(block);
- }
-
- getBranches(block, blocks, block.getRelative(BlockFace.NORTH), player);
- getBranches(block, blocks, block.getRelative(BlockFace.NORTH_EAST), player);
- getBranches(block, blocks, block.getRelative(BlockFace.EAST), player);
- getBranches(block, blocks, block.getRelative(BlockFace.SOUTH_EAST), player);
- getBranches(block, blocks, block.getRelative(BlockFace.SOUTH), player);
- getBranches(block, blocks, block.getRelative(BlockFace.SOUTH_WEST), player);
- getBranches(block, blocks, block.getRelative(BlockFace.WEST), player);
- getBranches(block, blocks, block.getRelative(BlockFace.NORTH_WEST), player);
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_EAST))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_EAST), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.EAST))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.EAST), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_EAST))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_EAST), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_WEST))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.SOUTH_WEST), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.WEST))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.WEST), player);
- }
-
- if(!blocks.contains(block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_WEST))) {
- getBranches(block, blocks, block.getRelative(BlockFace.UP).getRelative(BlockFace.NORTH_WEST), player);
- }
-
- if((blocks.contains(block.getRelative(BlockFace.UP))) || (block.getRelative(BlockFace.UP).getType() != Material.LOG)) {
- break;
- }
-
- block = block.getRelative(BlockFace.UP);
- }
- }
-
- public void getBranches(Block block, List<Block> blocks, Block other, Player player) {
- if((!blocks.contains(other)) && (other.getType() == Material.LOG)){
- getBlocksToChop(other, getHighestLog(other), blocks, player);
- }
- }
-
- public Block getHighestLog(Block block) {
- while(block.getRelative(BlockFace.UP).getType() == Material.LOG) {
- block = block.getRelative(BlockFace.UP);
- }
-
- return block;
- }
-
- public boolean isTree(Block block, Player player, Block first) {
- int counter = 0;
-
- if(block.getRelative(BlockFace.UP).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.DOWN).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.NORTH).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.EAST).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.SOUTH).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.WEST).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(counter >= 2) {
- return true;
- }
-
- if(block.getData() == 1) {
- block = block.getRelative(BlockFace.UP);
-
- if(block.getRelative(BlockFace.UP).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.NORTH).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.EAST).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.SOUTH).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(block.getRelative(BlockFace.WEST).getType() == Material.LEAVES) {
- counter++;
- }
-
- if(counter >= 2) {
- return true;
- }
- }
-
- return false;
- }
-
- public void popLogs(Block block, List<Block> blocks, World world, Player player) {
- ItemStack item = new ItemStack(1, 1);
- item.setAmount(1);
-
- for(int counter = 0; counter < blocks.size(); counter++) {
- block = (Block)blocks.get(counter);
-
- if(plugin.logBlockEnabled()) {
- plugin.lbconsumer.queueBlockBreak(player.getName(), block.getState());
- }
-
- item.setType(block.getType());
- item.setDurability(block.getData());
- block.setType(Material.AIR);
- world.dropItem(block.getLocation(), item);
-
- if(plugin.activemcMMO()) {
- mcMMOFake(player, block);
- }
-
- }
- }
-
-
- public boolean mcMMOTreeFeller(Player player) {
- boolean isMcMMOEnabled = plugin.getServer().getPluginManager().isPluginEnabled("mcMMO");
-
- if(!isMcMMOEnabled) {
- return false;
- }
-
- PlayerProfile pp = Users.getProfile(player);
- if(pp.getTreeFellerMode()) {
- return true;
- } else {
- return false;
- }
- }
-
- public void mcMMOFake(Player player, Block block) {
- PlayerProfile pp = Users.getProfile(player);
- ItemStack inhand = player.getItemInHand();
- Skills.monitorSkill(player, pp, 0, SkillType.WOODCUTTING);
-
- if(LoadProperties.woodcuttingrequiresaxe.booleanValue() && m.getTier(inhand) != 0) {
- /* Oak */
- if(block.getData() == 0) {
- pp.addXP(SkillType.WOODCUTTING, LoadProperties.moak, player);
- }
-
- /* Pine / Spruce */
- if(block.getData() == 1) {
- pp.addXP(SkillType.WOODCUTTING, LoadProperties.mspruce, player);
- }
-
- /* Birch */
- if(block.getData() == 2) {
- pp.addXP(SkillType.WOODCUTTING, LoadProperties.mbirch, player);
- }
-
- /* Jungle */
- if(block.getData() == 3) {
- pp.addXP(SkillType.WOODCUTTING, LoadProperties.mjungle, player);
- }
- }
-
- Skills.XpCheckAll(player);
- }
-
- public boolean isProtected(Player player, Block block) {
- if(getWorldGuard() == null) {
- return false;
- } else {
- return getWorldGuard().canBuild(player, block);
- }
- }
-
- private WorldGuardPlugin getWorldGuard() {
- Plugin WorldGuard = plugin.getServer().getPluginManager().getPlugin("WorldGuard");
-
- if(WorldGuard == null || !(WorldGuard instanceof WorldGuardPlugin)) {
- return null;
- }
-
- return (WorldGuardPlugin) WorldGuard;
- }
- }