Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: 128931/ClearChat
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.0.3
Choose a base ref
...
head repository: 128931/ClearChat
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 9 commits
  • 7 files changed
  • 1 contributor

Commits on May 19, 2022

  1. [+] Disable the plugin if get command is null

    If the command is null and we can't set the executor, then the plugin isn't precisely usable.
    128931 committed May 19, 2022
    Copy the full SHA
    54f928c View commit details
  2. [+] Readability

    128931 committed May 19, 2022
    Copy the full SHA
    132b396 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    6ce1fda View commit details

Commits on May 20, 2022

  1. Copy the full SHA
    420efc7 View commit details
  2. [-] Forgor 💀

    When ur testing stuff and forgor to remove the code 😿
    128931 committed May 20, 2022
    Copy the full SHA
    97f5e0d View commit details
  3. [-] Omit useless files

    128931 committed May 20, 2022
    Copy the full SHA
    864c59a View commit details
  4. [-] Useless local var

    Imo a constant should only be used if it's used 3 times or more
    128931 committed May 20, 2022
    Copy the full SHA
    dee103e View commit details
  5. [/] Format

    128931 committed May 20, 2022
    Copy the full SHA
    407258a View commit details
  6. Copy the full SHA
    b345613 View commit details
9 changes: 8 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.7.0-Beta"
kotlin("jvm") version "1.7.0-RC"
id("org.sonarqube") version "3.3"
}

@@ -20,6 +20,10 @@ repositories {

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1") {
exclude(module = "kotlin-stdlib-common")
exclude(module = "kotlin-stdlib-jdk8")
}

compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")

@@ -53,6 +57,9 @@ tasks {
"**/*.kotlin_metadata",
"**/*.kotlin_module",
"**/*.kotlin_builtins",
"**/*.pro",
"**/*.version",
"**/*.bin",
"META-INF/maven/**",
"META-INF/versions/**"
)
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package io.github.onetwoeight.clearchat

import io.github.onetwoeight.clearchat.listeners.ChatListener
import io.github.onetwoeight.clearchat.statistics.Metrics
import io.github.onetwoeight.clearchat.utilities.CC
import org.bukkit.plugin.java.JavaPlugin


@@ -13,7 +14,15 @@ class ClearChatPlugin : JavaPlugin() {

override fun onEnable() {
saveDefaultConfig()
getCommand("cc")?.setExecutor(ChatListener(this))
getCommand("cc")?.setExecutor(ChatListener(this)) ?: run {
server.consoleSender.sendMessage(
CC.translate(
"[${description.name}] &cExpression 'getCommand(\"cc\")' must not be null&r"
)
)
isEnabled = false
return
}
Metrics(this, 14_968)
logger.info("${description.name} v${description.version} Enabled")
}
Original file line number Diff line number Diff line change
@@ -18,19 +18,17 @@ class ChatListener(private val plugin: ClearChatPlugin) : CommandExecutor {

override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<String>): Boolean {
val prefix = "Prefix"
val send = "%sender%"
val global = "cc.global"
val player = "cc.player"
if (sender.hasPermission(global) && args.isEmpty()) {
if (args.isEmpty()) {
for (i in 0..999) {
Bukkit.broadcastMessage(Random.nextSpace(16))
}
Bukkit.broadcastMessage(
CC.translate(
plugin.config.getString(prefix) + plugin.config.getString("Global")?.replace(send, sender.name)
plugin.config.getString(prefix) + plugin.config.getString("Global")
?.replace("%sender%", sender.name)
)
)
} else if (sender.hasPermission(player) && args.size == 1) {
} else if (args.size == 1) {
for (i in 0..999) {
if (Bukkit.getPlayer(args[0])?.isOnline == true) {
Bukkit.getPlayer(args[0])?.sendMessage(Random.nextSpace(16))
@@ -42,7 +40,8 @@ class ChatListener(private val plugin: ClearChatPlugin) : CommandExecutor {
if (Bukkit.getPlayer(args[0])?.isOnline == true) {
Bukkit.getPlayer(args[0])?.sendMessage(
CC.translate(
plugin.config.getString(prefix) + plugin.config.getString("Player")?.replace(send, sender.name)
plugin.config.getString(prefix) + plugin.config.getString("Player")
?.replace("%sender%", sender.name)
)
)
sender.sendMessage(
@@ -52,13 +51,7 @@ class ChatListener(private val plugin: ClearChatPlugin) : CommandExecutor {
)
)
}
} else if (!sender.hasPermission(global) && args.isEmpty() || !sender.hasPermission(player) && args.isNotEmpty()) {
sender.sendMessage(
CC.translate(
plugin.config.getString("NoPermission")?.replace(send, sender.name).toString()
)
)
} else if (sender.hasPermission(player) && args.size > 1) {
} else if (args.size > 1) {
sender.sendMessage("${ChatColor.RED}Please refrain from using 2 or more args${ChatColor.RESET}")
}
return true
Original file line number Diff line number Diff line change
@@ -2,18 +2,18 @@ package io.github.onetwoeight.clearchat.statistics

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import java.io.*
import java.lang.reflect.InvocationTargetException
import java.net.URL
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.function.BiConsumer
import java.util.function.Consumer
import java.util.function.Supplier
import java.util.logging.Level
import java.util.zip.GZIPOutputStream
import javax.net.ssl.HttpsURLConnection
@@ -72,16 +72,12 @@ class Metrics(
val logSentData = config.getBoolean("logSentData", false)
val logResponseStatusText = config.getBoolean("logResponseStatusText", false)
MetricsBase(
"bukkit",
plugin,
serverUUID.toString(),
serviceId,
enabled,
appendPlatformDataConsumer = ::appendPlatformData,
appendServiceDataConsumer = ::appendServiceData,
submitTaskConsumer = { it.let { Bukkit.getScheduler().runTaskAsynchronously(plugin, it) } },
checkServiceEnabledSupplier = { plugin.isEnabled },
errorLogger = { message, error -> plugin.logger.log(Level.WARNING, message, error) },
infoLogger = plugin.logger::info,
::appendPlatformData,
::appendServiceData,
logErrors,
logSentData,
logResponseStatusText
@@ -121,16 +117,12 @@ class Metrics(
}

private class MetricsBase(
private val platform: String,
private val plugin: JavaPlugin,
private val serverUuid: String,
private val serviceId: Int,
private val enabled: Boolean,
enabled: Boolean,
private val appendPlatformDataConsumer: Consumer<JsonObject>,
private val appendServiceDataConsumer: Consumer<JsonObject>,
private val submitTaskConsumer: Consumer<Runnable>,
private val checkServiceEnabledSupplier: Supplier<Boolean>,
private val errorLogger: BiConsumer<String, Throwable>,
private val infoLogger: Consumer<String>,
private val logErrors: Boolean,
private val logSentData: Boolean,
private val logResponseStatusText: Boolean
@@ -146,17 +138,6 @@ class Metrics(
}

private fun startSubmitting() {
val submitTask = Runnable {
if (!enabled || java.lang.Boolean.FALSE == checkServiceEnabledSupplier.get()) {
// Submitting data or service is disabled
scheduler.shutdown()
return@Runnable
}
submitTaskConsumer.accept(Runnable {
submitData()
})
}

/*
Instead of using random values that constantly bombard my console with 429 (too many requests) errors,
we simply set it to send the request every 30 minutes to resolve the problem. Furthermore,
@@ -168,9 +149,13 @@ class Metrics(
bStats staff if you are concerned with the fact that I transmit a request every 30 minutes, please let me know,
and I will raise it to 35 minutes since that was the most amount of minutes your rng could make/reach.
*/
scheduler.scheduleAtFixedRate(
submitTask, 1_800_000L, 1_800_000L, TimeUnit.MILLISECONDS
)
CoroutineScope(Dispatchers.Default).launch {
// If someone knows a cleaner way to do this please lmk
while (true) {
delay(1_800_000L)
submitData()
}
}
}

private fun submitData() {
@@ -183,24 +168,24 @@ class Metrics(
baseJsonBuilder.add("service", serviceJsonBuilder)
baseJsonBuilder.addProperty("serverUUID", serverUuid)
baseJsonBuilder.addProperty("metricsVersion", "3.0.0")
scheduler.execute {
CoroutineScope(Dispatchers.Default).launch {
try {
// Send the data
sendData(baseJsonBuilder)
} catch (e: IOException) {
// Something went wrong! :(
if (logErrors) {
errorLogger.accept("Could not submit bStats metrics data", e)
plugin.logger.log(Level.WARNING, "Could not submit bStats metrics data", e)
}
}
}
}

private fun sendData(data: JsonObject) {
if (logSentData) {
infoLogger.accept("Sent bStats metrics data: $data")
plugin.logger.info("Sent bStats metrics data: $data")
}
val url = "https://bStats.org/api/v2/data/$platform"
val url = "https://bStats.org/api/v2/data/bukkit"
val connection = URL(url).openConnection() as HttpsURLConnection
// Compress the data to save bandwidth
val compressedData = compress("$data")
@@ -224,29 +209,22 @@ class Metrics(
}
}
if (logResponseStatusText) {
infoLogger.accept("Sent data to bStats and received response: $builder")
plugin.logger.info("Sent data to bStats and received response: $builder")
}
}

companion object {
private val scheduler =
Executors.newScheduledThreadPool(1) {
Thread(it, "bStats-Metrics")
}

/**
* Zips the given string.
*
* @param str The string to gzip.
* @return The gzipped string.
*/
private fun compress(str: String): ByteArray {
val outputStream = ByteArrayOutputStream()
GZIPOutputStream(outputStream).use {
it.write(str.toByteArray(Charsets.UTF_8))
}
return outputStream.toByteArray()
/**
* Zips the given string.
*
* @param str The string to gzip.
* @return The gzipped string.
*/
private fun compress(str: String): ByteArray {
val outputStream = ByteArrayOutputStream()
GZIPOutputStream(outputStream).use {
it.write(str.toByteArray(Charsets.UTF_8))
}
return outputStream.toByteArray()
}
}
}
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package io.github.onetwoeight.clearchat.utilities

import kotlin.random.Random


/**
* @author onetwoeight
* @since 5/14/2022
3 changes: 0 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -15,6 +15,3 @@ Player: '&cYour chat was cleared by %sender%&r'

# After successfully clearing someone's chat, this message will be delivered to the %sender%.
Success: "&cYou successfully cleared %person%'s chat&r"

# The message that will be sent if the person who performed the command does not have the necessary permissions.
NoPermission: '&cYou do not have permission to do that&r'
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -11,4 +11,6 @@ commands:
cc:
aliases: clearchat
description: This command is used to clear a single player's or everyone's chat.
permission: cc.use
permission-message: "§cYou do not have permission to do that§r"
usage: /<command> [player]