All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.chestmc.common.extensions.Files.kt Maven / Gradle / Ivy

package net.chestmc.common.extensions

import org.bukkit.plugin.Plugin
import java.io.File

/**
 * Creates this file if not exists.
 */
fun File.create(): File = transform {
  if (!exists()) {
    if (parentFile != null)
      parentFile.mkdirs()
    createNewFile()
  }
}

/**
 * Creates a folder from this file if not exists.
 */
fun File.createFolder(): File = transform {
  if (!exists())
    mkdirs()
}

/**
 * Gets a file from the specified path. This not creates the file.
 */
fun file(path: String) = File(path)

/**
 * Gets a file from the specified path. This not creates the file.
 */
fun file(parent: String, name: String) = File(parent, name)

/**
 * Gets a file from the specified path. This not creates the file.
 */
fun file(parent: File, name: String) = File(parent, name)

/**
 * Gets a file from the specified path and creates them.
 */
fun newFile(path: String) = File(path).create()

/**
 * Gets a file from the specified path and creates them.
 */
fun newFile(parent: String, name: String) = File(parent, name).create()

/**
 * Gets a file from the specified path and creates them.
 */
fun newFile(parent: File, name: String) = File(parent, name).create()

/**
 * Gets a folder from the specified path and creates them.
 */
fun newFolder(path: String) = File(path).createFolder()

/**
 * Gets a folder from the specified path and creates them.
 */
fun newFolder(parent: String, name: String) = File(parent, name).createFolder()

/**
 * Gets a folder from the specified path and creates them.
 */
fun newFolder(parent: File, name: String) = File(parent, name).createFolder()

/**
 * Gets a file from the data folder of this plugin.
 * This not creates the file.
 */
fun Plugin.file(name: String) = File(dataFolder, name)

/**
 * Gets a file from the data folder of this plugin and creates them.
 */
fun Plugin.newFile(name: String) = File(dataFolder, name).create()

/**
 * Gets a folder from the data folder of this plugin and creates them.
 */
fun Plugin.newFolder(name: String) = File(dataFolder, name).createFolder()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy