Java Code Examples for org.bukkit.block.Block#breakNaturally()

The following examples show how to use org.bukkit.block.Block#breakNaturally() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimberListener.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
private void breakTree(Block block, int i) {
    if (UniversalMaterial.isLog(block.getType())){
        block.breakNaturally();
        i = 2;
    }else {
        i--;
    }
    if (i > 0){
        for (BlockFace face : BlockFace.values()) {
            if (face.equals(BlockFace.DOWN) || face.equals(BlockFace.UP) || face.equals(BlockFace.NORTH) ||
                    face.equals(BlockFace.EAST) || face.equals(BlockFace.SOUTH) || face.equals(BlockFace.WEST)) {
                breakTree(block.getRelative(face), i);
            }
        }
    }
}
 
Example 2
Source File: ProjectileSpleefListener.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void projectileHitEvent(ProjectileHitEvent e) {
	if (!(e.getEntity().getShooter() instanceof Player)) {
		return;
	}
	GameMap gMap = MatchManager.get().getPlayerMap((Player)e.getEntity().getShooter());
	if (gMap != null && gMap.isProjectileSpleefEnabled()) {
		Projectile projectile = e.getEntity();
		if(projectile instanceof EnderPearl){
			return;
		}
		Block block = SkyWarsReloaded.getNMS().getHitBlock(e);
		if(block == null) {
			return;
		}
		block.breakNaturally();
	}
}
 
Example 3
Source File: TreeVaporizer.java    From NBTEditor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onBlockBreak(BlockBreakEvent event, PlayerDetails details) {
	Block root = event.getBlock();
	if (isLog(root.getType())) {
		// Find the blocks
		Set<Block> blocks = getTreeBlocks(root);
		if (blocks.size() > 0) {
			root.getWorld().playSound(root.getLocation(), Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 0.5f, 1);
		}
		// Destroy them.
		for (Block block : blocks) {
			block.breakNaturally();
		}
		// Apply durability.
		if (event.getPlayer().getGameMode() != GameMode.CREATIVE) {
			UtilsMc.offsetItemStackDamage(details.getItem(), blocks.size());
		}
	}
}
 
Example 4
Source File: HuntEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
private Player hasValidSign(Location l, RegionType rt, UUID uuid) {
    Player player = Bukkit.getPlayer(uuid);
    Civilian civilian = CivilianManager.getInstance().getCivilian(uuid);
    Block block = l.getBlock().getRelative(BlockFace.UP);
    BlockState state = block.getState();
    if (!(state instanceof Sign)) {
        return null;
    }

    Sign sign = (Sign) state;

    Player targetPlayer = null;
    try {
        targetPlayer = Bukkit.getPlayer(sign.getLine(0));
    } catch (Exception e) {
        block.breakNaturally();
        player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslation(civilian.getLocale(),
                "invalid-name"));
        return null;
    }
    if (targetPlayer == null || !targetPlayer.isOnline()) {
        block.breakNaturally();
        player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslation(civilian.getLocale(),
                "invalid-name"));
        return null;
    }

    if (!targetPlayer.getWorld().equals(player.getWorld())) {
        block.breakNaturally();
        player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslation(civilian.getLocale(),
                "target-not-in-world"));
        return null;
    }

    return targetPlayer;
}
 
Example 5
Source File: EffBreakNaturally.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void execute(final Event e) {
	ItemType tool = this.tool != null ? this.tool.getSingle(e) : null;
	for (Block block : this.blocks.getArray(e)) {
		if (tool != null) {
			ItemStack is = tool.getRandom();
			if (is != null)
				block.breakNaturally(is);
			else
				block.breakNaturally();
		} else {
			block.breakNaturally();
		}
	}
}
 
Example 6
Source File: EncompassRegionBuilder.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
private void give(Block sign, Player p, List<Block> blocks) {
    HashMap<Integer, ItemStack> left = p.getInventory().addItem(new ItemStack(Material.getMaterial(RedProtect.get().config.configRoot().region_settings.block_id), blocks.size()));
    if (!left.isEmpty()) {
        p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.getMaterial(RedProtect.get().config.configRoot().region_settings.block_id), left.get(0).getAmount() - 1));
    }
    p.updateInventory();
    sign.breakNaturally();
    for (Block rb : blocks) {
        rb.setType(Material.AIR);
    }
}
 
Example 7
Source File: ExplosiveTool.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List<ItemStack> drops) {
    if (b.getType() != Material.AIR && !b.isLiquid() && !MaterialCollections.getAllUnbreakableBlocks().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
        SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK);

        b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
        SlimefunItem sfItem = BlockStorage.check(b);

        if (sfItem != null && !sfItem.useVanillaBlockBreaking()) {
            SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getID());

            if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) {
                drops.add(BlockStorage.retrieve(b));
            }
        }
        else if (b.getType() == Material.PLAYER_HEAD || b.getType().name().endsWith("_SHULKER_BOX")) {
            b.breakNaturally();
        }
        else {
            boolean applyFortune = b.getType().name().endsWith("_ORE") && b.getType() != Material.IRON_ORE && b.getType() != Material.GOLD_ORE;

            for (ItemStack drop : b.getDrops(getItem())) {
                // For some reason this check is necessary with Paper
                if (drop != null && drop.getType() != Material.AIR) {
                    b.getWorld().dropItemNaturally(b.getLocation(), applyFortune ? new CustomItem(drop, fortune) : drop);
                }
            }

            b.setType(Material.AIR);
        }

        damageItem(p, item);
    }
}
 
Example 8
Source File: RaidPortEffect.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
private Town hasValidSign(Location l, RegionType rt, UUID uuid) {
    Player player = Bukkit.getPlayer(uuid);
    Civilian civilian = CivilianManager.getInstance().getCivilian(uuid);
    Block block = l.getBlock().getRelative(BlockFace.UP);
    BlockState state = block.getState();
    if (!(state instanceof Sign)) {
        return null;
    }

    String stringDistance = rt.getEffects().get(KEY);
    int distance = 200;
    if (stringDistance != null) {
        distance = Integer.parseInt(stringDistance);
    }

    Sign sign = (Sign) state;

    Town town;
    try {
        town = TownManager.getInstance().getTown(sign.getLine(0));
    } catch (Exception e) {
        block.breakNaturally();
        player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslation(civilian.getLocale(),
                "raid-target-lost").replace("$1", sign.getLine(0))
                .replace("$2", distance + ""));
        return null;
    }
    if (town == null) {
        for (Town currentTown : TownManager.getInstance().getTowns()) {
            if (currentTown.getName().startsWith(sign.getLine(0))) {
                town = currentTown;
                break;
            }
        }
        if (town == null) {
            block.breakNaturally();
            player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslation(civilian.getLocale(),
                    "raid-target-lost").replace("$1", sign.getLine(0))
                    .replace("$2", distance + ""));
            return null;
        }
    }
    TownType townType = (TownType) ItemManager.getInstance().getItemType(town.getType());

    if (townType.getBuildRadius() + distance < l.distance(town.getLocation())) {
        block.breakNaturally();
        player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslation(civilian.getLocale(),
                "raid-target-lost").replace("$1", town.getName())
                .replace("$2", distance + ""));
        return null;
    }
    return town;
}
 
Example 9
Source File: EncompassRegionBuilder.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
private void drop(Block sign, List<Block> blocks) {
    sign.breakNaturally();
    for (Block rb : blocks) {
        rb.breakNaturally();
    }
}
 
Example 10
Source File: EncompassRegionBuilder.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
private void remove(Block sign, List<Block> blocks) {
    sign.breakNaturally();
    for (Block rb : blocks) {
        rb.setType(Material.AIR);
    }
}
 
Example 11
Source File: GravitationalAxe.java    From NBTEditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onBlockBreak(BlockBreakEvent event, PlayerDetails details) {
	Block root = event.getBlock();
	if (isLog(root.getType())) {
		// Initialize some variables:
		World world = event.getPlayer().getWorld();
		Location location = root.getLocation();
		Random random = new Random();
		Vector vel = event.getPlayer().getLocation().toVector().subtract(location.toVector()).normalize().setY(0).multiply(0.2);
		// Find the blocks
		Set<Block> blocks = getTreeBlocks(root);
		if (blocks.size() > 0) {
			world.playSound(location, Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 0.5f, 1);
		}
		double durability = blocks.size()*0.25;
		// Bring 'em down.
		for (Block block : blocks) {
			Material mat = block.getType();
			if (random.nextFloat() < 0.1f && isLog(mat)) {
				world.playSound(block.getLocation(), Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 0.6f, 1);
				block.breakNaturally();
				durability += 1;
			} else if (random.nextFloat() < 0.4f && isLeaves(mat)) {
				world.playSound(block.getLocation(), Sound.BLOCK_GRASS_BREAK, 0.5f, 1);
				block.breakNaturally();
				durability += 1;
			} else {
				FallingBlock fallingBlock = world.spawnFallingBlock(block.getLocation(), block.getBlockData());
				fallingBlock.setVelocity(vel.multiply(random.nextFloat()*0.2 + 0.9));
				if (isLeaves(mat)) {
					fallingBlock.setDropItem(false);
				}
				block.setType(Material.AIR);
			}
		}
		// Apply durability.
		if (event.getPlayer().getGameMode() != GameMode.CREATIVE) {
			UtilsMc.offsetItemStackDamage(details.getItem(), (int) durability);
		}
	}
}