Skip to content

Commit

Permalink
Merge pull request #134 from SpigotBasics/dev
Browse files Browse the repository at this point in the history
Proper error messages when giving too little input for multi-word-arguments, adds basics-tp module
  • Loading branch information
mfnalex authored Feb 16, 2024
2 parents 633e4de + d6ad00d commit 8e3afcc
Show file tree
Hide file tree
Showing 39 changed files with 618 additions and 249 deletions.
48 changes: 35 additions & 13 deletions common/src/main/kotlin/com/github/spigotbasics/common/Either.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,40 @@ sealed class Either<out L, out R> {
is Left -> fnL(value)
is Right -> fnR(value)
}
}

/**
* Returns the left value or null if this is a [Either.Right].
*
* @return The left value, or null if this is a [Either.Right].
*/
fun <L, R> Either<L, R>.leftOrNull(): L? = (this as? Either.Left<L>)?.value
/**
* Returns the left value or null if this is a [Either.Right].
*
* @return The left value, or null if this is a [Either.Right].
*/
fun leftOrNull(): L? = (this as? Either.Left<L>)?.value

/**
* Returns the right value or null if this is a [Either.Left].
*
* @return The right value, or null if this is a [Either.Left].
*/
fun <L, R> Either<L, R>.rightOrNull(): R? = (this as? Either.Right<R>)?.value
/**
* Returns the right value or null if this is a [Either.Left].
*
* @return The right value, or null if this is a [Either.Left].
*/
fun rightOrNull(): R? = (this as? Either.Right<R>)?.value

companion object {
/**
* Constructs an Either object from two nullable values. If both are null, or both are non-null, an exception is thrown.
*
* @param L? The nullable left value.
* @param R? The nullable right value.
* @return Either object encapsulating the non-null value.
* @throws IllegalArgumentException if both L and R are null or non-null.
*/
fun <L, R> of(
l: L?,
r: R?,
): Either<L, R> {
return when {
l != null && r != null -> throw IllegalArgumentException("Both L and R cannot be non-null")
l != null -> Left(l)
r != null -> Right(r)
else -> throw IllegalArgumentException("Both L and R cannot be null")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class BasicsCommandManager(private val serverCommandMap: SimpleCommandMap) {
(simpleCommandMap_knownCommands?.get(serverCommandMap) as? MutableMap<String, Command>)?.let { knownCommands ->
knownCommands.entries.removeIf { it.value == command }
}
} catch (e: Exception) {
} catch (_: Exception) {
}
command.disableExecutor()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,109 +27,104 @@ class BasicsCommand internal constructor(
coreConfig: CoreConfig,
val coreMessages: CoreMessages,
val messageFactory: MessageFactory,
) :
Command(info.name) {
private val logger = BasicsLoggerFactory.getCoreLogger(BasicsCommand::class)
) : Command(info.name) {
private val logger = BasicsLoggerFactory.getCoreLogger(BasicsCommand::class)

init {
val permString = info.permission.name
permission = if (coreConfig.hideCommandsWhenNoPermission) permString else null
description = info.description ?: ""
usage = info.usage
permissionMessage = info.permissionMessage.tagParsed("permission", permString).toLegacyString()
init {
val permString = info.permission.name
permission = if (coreConfig.hideCommandsWhenNoPermission) permString else null
description = info.description ?: ""
usage = info.usage
permissionMessage = info.permissionMessage.tagParsed("permission", permString).toLegacyString()
}

override fun execute(
sender: CommandSender,
commandLabel: String,
origArgs: Array<out String>,
): Boolean {
if (!sender.hasPermission(info.permission)) {
coreMessages.noPermission
.apply { if (sender is Player) concerns(sender) }
.tagParsed("permission", info.permission.name)
.sendToSender(sender)
return true
}

override fun execute(
sender: CommandSender,
commandLabel: String,
origArgs: Array<out String>,
): Boolean {
if (!sender.hasPermission(info.permission)) {
coreMessages.noPermission
.apply { if (sender is Player) concerns(sender) }
.tagParsed("permission", info.permission.name)
.sendToSender(sender)
return true
}
val args = origArgs.toMutableList()

val args = origArgs.toMutableList()
val context =
RawCommandContext(
sender = sender,
command = this,
label = commandLabel,
args = args,
location = if (sender is Entity) sender.location else null,
)

val context =
RawCommandContext(
sender = sender,
command = this,
label = commandLabel,
args = args,
location = if (sender is Entity) sender.location else null,
)
if (executor == null) {
coreMessages.commandModuleDisabled.sendToSender(sender)
return true
}

if (executor == null) {
coreMessages.commandModuleDisabled.sendToSender(sender)
return true
}
val returned: CommandResult?
try {
returned = executor!!.execute(context)

val returned: CommandResult?
try {
returned = executor!!.execute(context)

try {
returned?.process(context)
} catch (e: Exception) {
logger.log(Level.SEVERE, "Error processing returned command result for ${info.name}", e)
}

return true
} catch (e: BasicsCommandException) {
try {
e.commandResult.process(context)
} catch (e: Exception) {
logger.log(Level.SEVERE, "Error processing thrown command result for ${info.name}", e)
}
return true
} catch (e: UnsupportedServerSoftwareException) {
// Also want to catch NoSuchMethod and NoSuchField exception here
coreMessages.unsupportedServerSoftware(e.feature).sendToSender(sender)
return true
} catch (e: Throwable) {
coreMessages.errorExecutingCommand(sender, e).sendToSender(sender)
logger.log(Level.SEVERE, "Error executing command ${info.name}", e)
return true
returned?.process(context)
} catch (e: Exception) {
logger.log(Level.SEVERE, "Error processing returned command result for ${info.name}", e)
}
} catch (e: BasicsCommandException) {
try {
e.commandResult.process(context)
} catch (e: Exception) {
logger.log(Level.SEVERE, "Error processing thrown command result for ${info.name}", e)
}
} catch (e: UnsupportedServerSoftwareException) {
// Also want to catch NoSuchMethod and NoSuchField exception here
coreMessages.unsupportedServerSoftware(e.feature).sendToSender(sender)
} catch (e: Throwable) {
coreMessages.errorExecutingCommand(sender, e).sendToSender(sender)
logger.log(Level.SEVERE, "Error executing command ${info.name}", e)
}
return true
}

internal fun replaceCommand(command: BasicsCommand) {
this.executor = command.executor
this.info = command.info
}
internal fun replaceCommand(command: BasicsCommand) {
this.executor = command.executor
this.info = command.info
}

override fun tabComplete(
sender: CommandSender,
alias: String,
args: Array<out String>,
location: Location?,
): List<String> {
if (!sender.hasPermission(info.permission)) return mutableListOf()
override fun tabComplete(
sender: CommandSender,
alias: String,
args: Array<out String>,
location: Location?,
): List<String> {
if (!sender.hasPermission(info.permission)) return mutableListOf()

val context =
RawCommandContext(
sender = sender,
command = this,
label = alias,
args = args.toMutableList(),
location = location,
)
if (tabCompleter == null) return mutableListOf()
val result = tabCompleter!!.tabComplete(context)
return result ?: super.tabComplete(sender, alias, args, location)
}
val context =
RawCommandContext(
sender = sender,
command = this,
label = alias,
args = args.toMutableList(),
location = location,
)
if (tabCompleter == null) return mutableListOf()
val result = tabCompleter!!.tabComplete(context)
return result ?: super.tabComplete(sender, alias, args, location)
}

/**
* Disable this command's executor by setting it to null. Disabled commands will always return true and
* print a message to the sender.
*
*/
fun disableExecutor() {
executor = null
tabCompleter = null
}
/**
* Disable this command's executor by setting it to null. Disabled commands will always return true and
* print a message to the sender.
*
*/
fun disableExecutor() {
executor = null
tabCompleter = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class ParsedCommandBuilderFactory(
name = name,
permission = permission,
)
builder.block() // Apply the DSL configurations
return builder // return .build() ?
// Apply the DSL configurations
// return .build() ?
return builder.apply(block)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class RawCommandBuilder(
}

fun register(): BasicsCommand {
val command = build()
commandManager.registerCommand(command)
return command
return build().also(commandManager::registerCommand)
}

private fun build(): BasicsCommand {
Expand Down
Loading

0 comments on commit 8e3afcc

Please sign in to comment.