commands: add offline, login to microsoft account, Closes #83

This commit is contained in:
Bixilon 2023-03-19 21:22:59 +01:00
parent 42200812c3
commit 381a61f932
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 88 additions and 0 deletions

View File

@ -20,5 +20,6 @@ object AccountTargetProperties : TargetProperties<Account>() {
init { init {
register(TypeProperty) register(TypeProperty)
register(StateProperty)
} }
} }

View File

@ -0,0 +1,46 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minosoft.account.selector
import de.bixilon.minosoft.commands.errors.ExpectedArgumentError
import de.bixilon.minosoft.commands.parser.minosoft.enums.EnumParser
import de.bixilon.minosoft.commands.parser.selector.TargetProperty
import de.bixilon.minosoft.commands.parser.selector.TargetPropertyFactory
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.data.accounts.Account
import de.bixilon.minosoft.data.accounts.AccountStates
class StateProperty(
val state: AccountStates,
val negated: Boolean,
) : TargetProperty<Account> {
override fun passes(value: Account): Boolean {
val state = value.state
if (negated) {
return state != this.state
}
return state == this.state
}
companion object : TargetPropertyFactory<Account> {
private val parser = EnumParser(AccountStates)
override val name: String = "state"
override fun read(reader: CommandReader): StateProperty {
val (word, negated) = reader.readNegateable { parser.parse(reader) } ?: throw ExpectedArgumentError(reader)
return StateProperty(word, negated)
}
}
}

View File

@ -13,6 +13,8 @@
package de.bixilon.minosoft.data.accounts package de.bixilon.minosoft.data.accounts
import de.bixilon.kutil.enums.EnumUtil
import de.bixilon.kutil.enums.ValuesEnum
import de.bixilon.minosoft.data.language.translate.Translatable import de.bixilon.minosoft.data.language.translate.Translatable
import de.bixilon.minosoft.data.registries.identified.ResourceLocation import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.util.KUtil.toResourceLocation import de.bixilon.minosoft.util.KUtil.toResourceLocation
@ -55,4 +57,10 @@ enum class AccountStates : Translatable {
; ;
override val translationKey: ResourceLocation = "minosoft:main.account.state.${name.lowercase()}".toResourceLocation() override val translationKey: ResourceLocation = "minosoft:main.account.state.${name.lowercase()}".toResourceLocation()
companion object : ValuesEnum<AccountStates> {
override val VALUES: Array<AccountStates> = values()
override val NAME_MAP: Map<String, AccountStates> = EnumUtil.getEnumValues(VALUES)
}
} }

View File

@ -13,15 +13,20 @@
package de.bixilon.minosoft.terminal.commands package de.bixilon.minosoft.terminal.commands
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.minosoft.commands.nodes.ArgumentNode import de.bixilon.minosoft.commands.nodes.ArgumentNode
import de.bixilon.minosoft.commands.nodes.CommandNode import de.bixilon.minosoft.commands.nodes.CommandNode
import de.bixilon.minosoft.commands.nodes.LiteralNode import de.bixilon.minosoft.commands.nodes.LiteralNode
import de.bixilon.minosoft.commands.parser.brigadier.string.StringParser
import de.bixilon.minosoft.commands.parser.minosoft.account.AccountParser import de.bixilon.minosoft.commands.parser.minosoft.account.AccountParser
import de.bixilon.minosoft.commands.parser.selector.AbstractTarget import de.bixilon.minosoft.commands.parser.selector.AbstractTarget
import de.bixilon.minosoft.commands.stack.CommandStack import de.bixilon.minosoft.commands.stack.CommandStack
import de.bixilon.minosoft.config.profile.profiles.account.AccountProfileManager import de.bixilon.minosoft.config.profile.profiles.account.AccountProfileManager
import de.bixilon.minosoft.data.accounts.Account import de.bixilon.minosoft.data.accounts.Account
import de.bixilon.minosoft.data.accounts.types.offline.OfflineAccount
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.KUtil.table import de.bixilon.minosoft.util.KUtil.table
import de.bixilon.minosoft.util.account.microsoft.MicrosoftOAuthUtils
object AccountManageCommand : Command { object AccountManageCommand : Command {
override var node = LiteralNode("account") override var node = LiteralNode("account")
@ -47,6 +52,34 @@ object AccountManageCommand : Command {
stack.print.print("Selected $account") stack.print.print("Selected $account")
} }
}, },
LiteralNode("add").addChild(
LiteralNode("offline").addChild(ArgumentNode("username", StringParser(), executor = { stack ->
val name = stack.get<String>("username")!!
if (!ProtocolDefinition.MINECRAFT_NAME_VALIDATOR.matcher(name).matches()) throw CommandException("Invalid username: $name")
val profile = AccountProfileManager.selected
if (profile.entries.containsKey(name)) throw CommandException("Account does already exist!")
val account = OfflineAccount(name)
profile.entries[account.id] = account
profile.selected = account
stack.print.print("Successfully created and selected offline account ${account.username}!")
})),
LiteralNode("microsoft", executor = { stack ->
stack.print.print("Obtaining device code...")
DefaultThreadPool += {
MicrosoftOAuthUtils.obtainDeviceCodeAsync({
stack.print.print("§fPlease open §e${it.verificationURI}§f and enter the following code §e${it.userCode}§f and login into your microsoft account.")
}, { it.printStackTrace() }, {
stack.print.print("Logging in into microsoft account...")
val account = MicrosoftOAuthUtils.loginToMicrosoftAccount(it)
val profile = AccountProfileManager.selected
profile.entries[account.id] = account
profile.selected = account
stack.print.print("Successfully logged in and selected microsoft account ${account.username}!")
})
}
}),
),
) )