Skip to content

Commit

Permalink
improve error message & v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Luluno01 committed May 22, 2020
1 parent a401318 commit 2676b3b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 83 deletions.
69 changes: 39 additions & 30 deletions src/main/kotlin/vip/untitled/bungeesafeguard/commands/Blacklist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.CommandSender
import net.md_5.bungee.api.chat.TextComponent
import vip.untitled.bungeesafeguard.ConfigHolderPlugin
import vip.untitled.bungeesafeguard.helpers.UserNotFoundException
import vip.untitled.bungeesafeguard.helpers.UserUUIDHelper
import java.util.*

Expand All @@ -27,26 +28,30 @@ open class Blacklist(context: ConfigHolderPlugin): ListCommand(context, "blackli
for (usernameOrUUID in args) {
UserUUIDHelper.getUUIDFromString(context, usernameOrUUID) { err, uuid ->
try {
if (err == null) {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized(config) {
if (config.inWhitelist(uuid!!)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already whitelisted, whose priority is lower than blacklist"))
}
if (config.inBlacklist(uuid)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already blacklisted"))
} else {
config.addBlacklistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.AQUA}${usernameOrUUID} added to blacklist"))
synchronized (concurrentTasksHelper) {
concurrentTasksHelper.shouldSaveConfig = true
when (err) {
null -> {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized(config) {
if (config.inWhitelist(uuid!!)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already whitelisted, whose priority is lower than blacklist"))
}
if (config.inBlacklist(uuid)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already blacklisted"))
} else {
config.addBlacklistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.AQUA}${usernameOrUUID} added to blacklist"))
synchronized (concurrentTasksHelper) {
concurrentTasksHelper.shouldSaveConfig = true
}
}
}
}
} else {
sender.sendMessage(TextComponent("${ChatColor.RED} failed to blacklist $usernameOrUUID: $err"))
context.logger.warning("Failed to blacklist $usernameOrUUID:")
err.printStackTrace()
is UserNotFoundException -> sender.sendMessage(TextComponent("${ChatColor.RED}User $usernameOrUUID is not found and therefore cannot be blacklisted"))
else -> {
sender.sendMessage(TextComponent("${ChatColor.RED}Failed to blacklist $usernameOrUUID: $err"))
context.logger.warning("Failed to blacklist $usernameOrUUID:")
err.printStackTrace()
}
}
} finally {
concurrentTasksHelper.notifyCompletion()
Expand All @@ -65,21 +70,25 @@ open class Blacklist(context: ConfigHolderPlugin): ListCommand(context, "blackli
for (usernameOrUUID in args) {
UserUUIDHelper.getUUIDFromString(context, usernameOrUUID) { err, uuid ->
try {
if (err == null) {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized(config) {
if (config.inBlacklist(uuid!!)) {
config.removeBlacklistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} removed from blacklist"))
concurrentTasksHelper.shouldSaveConfig = true
} else {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is not in blacklist"))
when (err) {
null -> {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized (config) {
if (config.inBlacklist(uuid!!)) {
config.removeBlacklistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} removed from blacklist"))
concurrentTasksHelper.shouldSaveConfig = true
} else {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is not in blacklist"))
}
}
}
} else {
sender.sendMessage(TextComponent("${ChatColor.RED} failed to remove $usernameOrUUID from blacklist: $err"))
context.logger.warning("Failed to remove $usernameOrUUID from blacklist:")
err.printStackTrace()
is UserNotFoundException -> sender.sendMessage(TextComponent("${ChatColor.RED}User $usernameOrUUID is not found and therefore cannot be removed from blacklist"))
else -> {
sender.sendMessage(TextComponent("${ChatColor.RED}Failed to remove $usernameOrUUID from blacklist: $err"))
context.logger.warning("Failed to remove $usernameOrUUID from blacklist:")
err.printStackTrace()
}
}
} finally {
concurrentTasksHelper.notifyCompletion()
Expand Down
69 changes: 39 additions & 30 deletions src/main/kotlin/vip/untitled/bungeesafeguard/commands/Whitelist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.CommandSender
import net.md_5.bungee.api.chat.TextComponent
import vip.untitled.bungeesafeguard.ConfigHolderPlugin
import vip.untitled.bungeesafeguard.helpers.UserNotFoundException
import vip.untitled.bungeesafeguard.helpers.UserUUIDHelper
import java.util.*

Expand All @@ -27,26 +28,30 @@ open class Whitelist(context: ConfigHolderPlugin): ListCommand(context, "whiteli
for (usernameOrUUID in args) {
UserUUIDHelper.getUUIDFromString(context, usernameOrUUID) { err, uuid ->
try {
if (err == null) {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized(config) {
if (config.inBlacklist(uuid!!)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already blacklisted, whose priority is higher than whitelist"))
}
if (config.inWhitelist(uuid)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already whitelisted"))
} else {
config.addWhitelistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.GREEN}${usernameOrUUID} added to whitelist"))
synchronized (concurrentTasksHelper) {
concurrentTasksHelper.shouldSaveConfig = true
when (err) {
null -> {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized(config) {
if (config.inBlacklist(uuid!!)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already blacklisted, whose priority is higher than whitelist"))
}
if (config.inWhitelist(uuid)) {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is already whitelisted"))
} else {
config.addWhitelistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.GREEN}${usernameOrUUID} added to whitelist"))
synchronized (concurrentTasksHelper) {
concurrentTasksHelper.shouldSaveConfig = true
}
}
}
}
} else {
sender.sendMessage(TextComponent("${ChatColor.RED} failed to whitelist $usernameOrUUID: $err"))
context.logger.warning("Failed to whitelist $usernameOrUUID:")
err.printStackTrace()
is UserNotFoundException -> sender.sendMessage(TextComponent("${ChatColor.RED}User $usernameOrUUID is not found and therefore cannot be whitelisted"))
else -> {
sender.sendMessage(TextComponent("${ChatColor.RED}Failed to whitelist $usernameOrUUID: $err"))
context.logger.warning("Failed to whitelist $usernameOrUUID:")
err.printStackTrace()
}
}
} finally {
concurrentTasksHelper.notifyCompletion()
Expand All @@ -65,21 +70,25 @@ open class Whitelist(context: ConfigHolderPlugin): ListCommand(context, "whiteli
for (usernameOrUUID in args) {
UserUUIDHelper.getUUIDFromString(context, usernameOrUUID) { err, uuid ->
try {
if (err == null) {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized (config) {
if (config.inWhitelist(uuid!!)) {
config.removeWhitelistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} removed from whitelist"))
concurrentTasksHelper.shouldSaveConfig = true
} else {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is not in whitelist"))
when (err) {
null -> {
assert(uuid != null) { "Both error and UUID are null!" }
synchronized (config) {
if (config.inWhitelist(uuid!!)) {
config.removeWhitelistRecord(uuid)
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} removed from whitelist"))
concurrentTasksHelper.shouldSaveConfig = true
} else {
sender.sendMessage(TextComponent("${ChatColor.YELLOW}${usernameOrUUID} is not in whitelist"))
}
}
}
} else {
sender.sendMessage(TextComponent("${ChatColor.RED} failed to remove $usernameOrUUID from whitelist: $err"))
context.logger.warning("Failed to remove $usernameOrUUID from whitelist:")
err.printStackTrace()
is UserNotFoundException -> sender.sendMessage(TextComponent("${ChatColor.RED}User $usernameOrUUID is not found and therefore cannot be removed from whitelist"))
else -> {
sender.sendMessage(TextComponent("${ChatColor.RED}Failed to remove $usernameOrUUID from whitelist: $err"))
context.logger.warning("Failed to remove $usernameOrUUID from whitelist:")
err.printStackTrace()
}
}
} finally {
concurrentTasksHelper.notifyCompletion()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package vip.untitled.bungeesafeguard.helpers

import java.io.IOException

open class UserNotFoundException : IOException {
constructor()
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,50 @@ import net.md_5.bungee.api.plugin.Plugin
import java.io.IOException
import java.net.URL
import java.util.*
import javax.net.ssl.HttpsURLConnection

object UserUUIDHelper {
fun getUUIDFromUsername(context: Plugin, username: String, callback: (Throwable?, UUID?) -> Unit) {
context.proxy.scheduler.runAsync(context) {
var connection: HttpsURLConnection? = null
try {
val json = TypedJSON.fromString(
URL("https://api.mojang.com/users/profiles/minecraft/${username}")
.openStream()
.bufferedReader()
.use { it.readText() }
)
json.assertObject()
val id = json.getString("id") ?: throw IOException("Invalid response")
val uuid = UUID.fromString(
StringBuilder(id)
.insert(8, '-')
.insert(13, '-')
.insert(18, '-')
.insert(23, '-')
.toString()
)
callback(
null,
uuid
)
connection = URL("https://api.mojang.com/users/profiles/minecraft/${username}").openConnection() as HttpsURLConnection
connection.connect()
val response = connection.inputStream
.bufferedReader()
.use { it.readText() }
when (connection.responseCode) {
200 -> {
val json = TypedJSON.fromString(response)
json.assertObject()
val id = json.getString("id") ?: throw IOException("Invalid response")
val uuid = UUID.fromString(
StringBuilder(id)
.insert(8, '-')
.insert(13, '-')
.insert(18, '-')
.insert(23, '-')
.toString()
)
callback(
null,
uuid
)
}
204 -> {

throw UserNotFoundException("User $username cannot be found from Mojang")
}
else -> throw IOException("Unable to handle response with status code ${connection.responseCode}")
}
} catch (e: Throwable) {
callback(e, null)
} finally {
try {
// InputStream should have been closed
// Will this really close the connection? (I hope it will be reused)
connection?.disconnect()
} catch (err: IOException) {}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#########################################
# BungeeSafeguard Configuration #
# Version: 2.0 #
# Version: 2.1 #
# Author: Untitled #
#########################################

version: "2.0"
version: "2.1"
whitelist-message: :( You are not whitelisted on this server
blacklist-message: :( We can't let you enter this server
enable-whitelist: true
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: BungeeSafeguard
main: vip.untitled.bungeesafeguard.BungeeSafeguard
version: "2.0"
version: "2.1"
author: Untitled

0 comments on commit 2676b3b

Please sign in to comment.