Skip to content

Commit

Permalink
Added AltCommand and AltArgumentType
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Sep 23, 2023
1 parent 4ecc7d5 commit f827a9b
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ loaderVersion=0.14.22
gsonVersion=2.10.1
websocketVersion=1.5.3
reflectionsVersion=0.10.2
brigadierVersion=1.0.18
brigadierVersion=1.1.8
4 changes: 3 additions & 1 deletion progreso-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
val progresoVersion: String by project
val gsonVersion: String by project
val brigadierVersion: String by project

Expand All @@ -6,13 +7,14 @@ plugins {
}

group = "org.progreso"
version = progresoVersion

repositories {
mavenCentral()
maven("https://libraries.minecraft.net")
}

dependencies {
implementation("com.mojang:brigadier:$brigadierVersion")
api("com.mojang:brigadier:$brigadierVersion")
api("com.google.code.gson:gson:$gsonVersion")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ package org.progreso.api.alt
sealed class AltAccount(val username: String) {
val type: String = javaClass.simpleName

override fun toString(): String {
return username
}

class Offline(username: String) : AltAccount(username)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import org.progreso.api.alt.AltAccount
interface AltContainer {
val alts: MutableSet<AltAccount>

fun getAltByName(name: String): AltAccount {
return getAltByNameOrNull(name)!!
}

fun getAltByNameOrNull(name: String): AltAccount? {
return alts.firstOrNull { it.username == name }
}

fun addAlt(alt: AltAccount) {
alts.add(alt)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.progreso.api.command.arguments

import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType
import com.mojang.brigadier.suggestion.Suggestions
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import org.progreso.api.Api
import org.progreso.api.alt.AltAccount
import org.progreso.api.managers.AltManager
import java.util.concurrent.CompletableFuture

class AltArgumentType : ArgumentType<AltAccount> {
companion object {
private val NO_SUCH_ALT = DynamicCommandExceptionType { name: Any ->
Api.TEXT.i18nMessage("argument.alt.error", name)
}

operator fun get(context: CommandContext<*>): AltAccount {
return context.getArgument("alt", AltAccount::class.java)
}
}

override fun parse(reader: StringReader): AltAccount {
val argument = reader.readString()

return AltManager.getAltByNameOrNull(argument)
?: throw NO_SUCH_ALT.create(argument)
}

override fun <S : Any?> listSuggestions(
context: CommandContext<S>,
builder: SuggestionsBuilder
): CompletableFuture<Suggestions> {
return Api.COMMAND.suggestMatching(AltManager.alts.map { it.username }, builder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class FriendArgumentType : ArgumentType<FriendManager.Friend> {
}

override fun parse(reader: StringReader): FriendManager.Friend {
val friendName = reader.readString()
return FriendManager.getFriendByNameOrNull(friendName) ?: throw NO_SUCH_FRIEND.create(friendName)
val argument = reader.readString()

return FriendManager.getFriendByNameOrNull(argument)
?: throw NO_SUCH_FRIEND.create(argument)
}

override fun <S : Any?> listSuggestions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class ModuleArgumentType : ArgumentType<AbstractModule> {
}

override fun parse(reader: StringReader): AbstractModule {
val moduleName = reader.readString()
return ModuleManager.getModuleByNameOrNull(moduleName) ?: throw NO_SUCH_MODULE.create(moduleName)
val argument = reader.readString()

return ModuleManager.getModuleByNameOrNull(argument)
?: throw NO_SUCH_MODULE.create(argument)
}

override fun <S : Any?> listSuggestions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class PluginArgumentType : ArgumentType<AbstractPlugin> {
}

override fun parse(reader: StringReader): AbstractPlugin {
val pluginName = reader.readString()
return PluginManager.getPluginByNameOrNull(pluginName) ?: throw NO_SUCH_PLUGIN.create(pluginName)
val argument = reader.readString()

return PluginManager.getPluginByNameOrNull(argument)
?: throw NO_SUCH_PLUGIN.create(argument)
}

override fun <S : Any?> listSuggestions(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.progreso.client.commands

import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.arguments.StringArgumentType.string
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import org.progreso.api.alt.AltAccount
import org.progreso.api.command.AbstractCommand
import org.progreso.api.command.arguments.AltArgumentType
import org.progreso.api.managers.AltManager

@AbstractCommand.Register("alt")
object AltCommand : AbstractCommand() {
override fun build(builder: LiteralArgumentBuilder<Any>) {
builder.then(
literal("add")
.then(
literal("offline").then(
argument("name", string()).executesSuccess { context ->
val name = StringArgumentType.getString(context, "name")

AltManager.addAlt(AltAccount.Offline(name))
infoLocalized("command.alt.add", name)
}
)
)
)

builder.then(
literal("remove").then(
argument("alt", AltArgumentType()).executesSuccess { context ->
val alt = AltArgumentType[context]

AltManager.removeAlt(alt)
infoLocalized("command.alt.remove", alt.username)
}
)
)

builder.then(literal("list").executesSuccess {
infoLocalized(
AltManager.alts.ifEmpty("command.alt.list", "command.alt.list_empty"),
AltManager.alts.joinToString()
)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FontArgumentType : ArgumentType<String> {
}

override fun parse(reader: StringReader): String {
val argument: String = reader.readString()
val argument = reader.readString()

return ProgresoResourceManager.fonts.firstOrNull { it == argument }
?: throw NO_SUCH_FONT.create(argument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class KeyArgumentType : ArgumentType<Int> {

override fun parse(reader: StringReader): Int {
val argument: String = reader.readString()
if (!KeyboardUtil.keyMap.containsValue(argument)) throw NO_SUCH_KEY.create(argument)
return KeyboardUtil.getKeyCode(argument)

return if (KeyboardUtil.keyMap.containsValue(argument)) KeyboardUtil.getKeyCode(argument)
else throw NO_SUCH_KEY.create(argument)
}

override fun <S> listSuggestions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PlayerArgumentType : ArgumentType<PlayerListEntry> {
}

override fun parse(reader: StringReader): PlayerListEntry {
val argument: String = reader.readString()
val argument = reader.readString()
var playerListEntry: PlayerListEntry? = null

for (player in mc.networkHandler.playerList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"argument.alt.error": "Alt '%s' not found",
"argument.config.error": "Config '%s' not found in %s category",
"argument.font.error": "Font '%s' not found",
"argument.friend.error": "Friend '%s' not found",
Expand All @@ -12,6 +13,11 @@
"command.invalid_syntax": "Invalid syntax: %s",
"command.failed_to_execute": "Failed to execute command",

"command.alt.add": "Added %s alt account",
"command.alt.remove": "Removed %s alt account",
"command.alt.list": "Alts: %s",
"command.alt.list_empty": "Alt list is empty",

"command.bind.key": "%s bound to %s",
"command.bind.reset": "Reset %s bind",
"command.bind.current": "Current %s bind: %s",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"argument.alt.error": "Альт '%s' не найден",
"argument.config.error": "Конфиг '%s' не найден в категории %s",
"argument.font.error": "Шрифт '%s' не найден",
"argument.friend.error": "У вас нету друга с именем %s",
Expand All @@ -12,6 +13,11 @@
"command.invalid_syntax": "Неправильный синтаксис. Сообщение об ошибке: %s",
"command.failed_to_execute": "Ошибка при выполнении команды",

"command.alt.add": "Добавлен альт '%s'",
"command.alt.remove": "Альт '%s' удален",
"command.alt.list": "Альты: %s",
"command.alt.list_empty": "Список альтов пуст",

"command.bind.key": "%s забинжен на клавишу '%s'",
"command.bind.reset": "Клавиша %s сброшена",
"command.bind.current": "Текущий бинд %s: %s",
Expand Down
2 changes: 2 additions & 0 deletions progreso-irc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
val progresoVersion: String by project
val websocketVersion: String by project
val gsonVersion: String by project

Expand All @@ -7,6 +8,7 @@ plugins {
}

group = "org.progreso"
version = progresoVersion

repositories {
mavenCentral()
Expand Down

0 comments on commit f827a9b

Please sign in to comment.