mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 19:35:00 -04:00
commands: add offline, login to microsoft account, Closes #83
This commit is contained in:
parent
42200812c3
commit
381a61f932
@ -20,5 +20,6 @@ object AccountTargetProperties : TargetProperties<Account>() {
|
||||
|
||||
init {
|
||||
register(TypeProperty)
|
||||
register(StateProperty)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,8 @@
|
||||
|
||||
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.registries.identified.ResourceLocation
|
||||
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()
|
||||
|
||||
|
||||
companion object : ValuesEnum<AccountStates> {
|
||||
override val VALUES: Array<AccountStates> = values()
|
||||
override val NAME_MAP: Map<String, AccountStates> = EnumUtil.getEnumValues(VALUES)
|
||||
}
|
||||
}
|
||||
|
@ -13,15 +13,20 @@
|
||||
|
||||
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.CommandNode
|
||||
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.selector.AbstractTarget
|
||||
import de.bixilon.minosoft.commands.stack.CommandStack
|
||||
import de.bixilon.minosoft.config.profile.profiles.account.AccountProfileManager
|
||||
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.account.microsoft.MicrosoftOAuthUtils
|
||||
|
||||
object AccountManageCommand : Command {
|
||||
override var node = LiteralNode("account")
|
||||
@ -47,6 +52,34 @@ object AccountManageCommand : Command {
|
||||
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}!")
|
||||
})
|
||||
}
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user