We understand that not everyone wants to add yet another library to their plugin. If you’d prefer to copy & paste the required code, here’s the code we use within our library.

To learn how to use this to perform a license check, see the main page.

The following code is in Kotlin (need the Java version?):

import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.net.HttpURLConnection
import java.net.InetAddress
import java.net.URL
import java.nio.file.Files
import java.nio.file.Pathf
import java.nio.file.Paths
import java.util.logging.Logger

object MCLicense {
    private const val API_URL = "https://api.mclicense.org/validate/"
    private const val TIMEOUT_MS = 10000

    fun validateKey(plugin: JavaPlugin): Boolean {
        val licenseFile = File(plugin.dataFolder, "mclicense.txt")
        if (!licenseFile.exists()) {
            plugin.dataFolder.mkdirs()
            licenseFile.createNewFile()
            Logger.getLogger("MC License").info("License key is empty! Place your key in the 'mclicense.txt' file in the plugin folder and restart the server.")
            return false
        }

        val key = Files.readString(Paths.get(licenseFile.path)).trim()
        if (key.isEmpty()) {
            Logger.getLogger("MC License").info("License key is empty! Place your key in the 'mclicense.txt' file in the plugin folder and restart the server.")
            return false
        }

        return validateKey(key, InetAddress.getLocalHost().hostAddress + ":" + plugin.server.port)
    }

    fun validateKey(filePath: Path, serverIp: String? = null): Boolean {
        if (!Files.exists(filePath)) {
            Files.createDirectories(filePath.parent)
            Files.createFile(filePath)
            Logger.getLogger("MC License").info("License key is empty! Place your key in the following file and restart the server: $filePath")
            return false
        }

        val key = Files.readString(filePath).trim()
        if (key.isEmpty()) {
            Logger.getLogger("MC License").info("License key is empty! Place your key in the following file and restart the server: $filePath")
            return false
        }

        return validateKey(key, serverIp)
    }

    fun validateKey(key: String, serverIp: String? = null): Boolean {
        try {
            val url = URL("${API_URL}$key${serverIp?.let { "?serverIp=$it" } ?: ""}")
            val connection = (url.openConnection() as HttpURLConnection).apply {
                connectTimeout = TIMEOUT_MS
                readTimeout = TIMEOUT_MS
            }

            val stream = if (connection.responseCode >= 400) connection.errorStream else connection.inputStream
            val response = stream?.bufferedReader()?.use { it.readText() } ?: ""

            if (response.isNotEmpty()) {
                val message = response.substringAfter("\"message\":\"").substringBefore("\"}")
                Logger.getLogger("MC License").info("Key validation ${if (connection.responseCode == 200) "succeeded" else "failed"} ($message)")
            }

            return connection.responseCode == 200
        } catch (e: Exception) {
            Logger.getLogger("MC License").info("Internal error validating license key")
            return false
        }
    }
}