Skip to content

Commit 64f281e

Browse files
author
rx97
committed
rx: bugs squashed
1 parent e109306 commit 64f281e

8 files changed

Lines changed: 100 additions & 39 deletions

File tree

src/main/kotlin/net/rx/modules/GitHandler.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import net.rx.modules.commands.*
44
import net.rx.modules.config.ConfigManager
55
import java.io.IOException
66
import java.util.concurrent.TimeUnit
7+
import kotlin.io.path.exists
78

89
object GitHandler {
910
var executing: Boolean = false
@@ -20,15 +21,33 @@ object GitHandler {
2021
}
2122

2223
fun runGit(path: String, args: String, source: Source) {
23-
source.sendFeedback(
24-
gray("executing: git $args"), true)
25-
2624
val pathToGitConfig = ConfigManager.dirPath
2725
.resolve("gitconfig")
2826
.resolve("${source.player.uuidAsString}")
29-
.toAbsolutePath()
3027

31-
runGit("git -c include.path=${pathToGitConfig.toString()} -C \"$path\" $args", source)
28+
29+
if (ConfigManager.config.forceGitConfig && !pathToGitConfig.toFile().exists()) {
30+
/* TODO: Make this cleaner */
31+
source.sendFeedback(
32+
red("Error. You do not have a gitconfig setup"), false)
33+
34+
source.sendFeedback(
35+
gray("To create a gitconfig, you can use the /gitconfig command. Examples:"), false)
36+
37+
source.sendFeedback(
38+
gray(" /gitconfig user.name ${source.player.entityName}"), false)
39+
40+
source.sendFeedback(
41+
gray(" /gitconfig user.email ${source.player.entityName}@email.com"), false)
42+
43+
source.sendFeedback(
44+
gray(" /gitconfig remote.origin.url ${source.player.entityName}:<API TOKEN>@<GIT-REPO>"), false)
45+
} else {
46+
source.sendFeedback(
47+
gray("executing: git $args"), true)
48+
49+
runGit("git -c include.path=${pathToGitConfig.toAbsolutePath().toString()} -C \"$path\" $args", source)
50+
}
3251
}
3352

3453
fun runGit(cmd: String, source: Source) {

src/main/kotlin/net/rx/modules/commands/GitAdminCommand.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package net.rx.modules.commands
33
import com.github.p03w.aegis.AegisCommandBuilder
44
import com.mojang.brigadier.arguments.StringArgumentType
55
import net.minecraft.command.argument.EntityArgumentType
6+
import net.minecraft.server.PlayerManager
67
import net.minecraft.server.network.ServerPlayerEntity
78
import net.rx.modules.config.ConfigManager
89

@@ -35,6 +36,10 @@ object GitAdminCommand : Command() {
3536
}
3637
}
3738

39+
literal("list") {
40+
executes(::listOperators)
41+
}
42+
3843
}
3944
}.build()
4045
)
@@ -48,27 +53,39 @@ object GitAdminCommand : Command() {
4853
}
4954

5055
private fun addOperator(context: Context, player: ServerPlayerEntity): Int {
51-
val out = ConfigManager.removeOperator(player.entityName, player.uuid.toString())
56+
val out = ConfigManager.addOperator(player.entityName, player.uuid.toString())
5257

53-
if (out == 1)
58+
if (out == 1) {
5459
context.source.sendFeedback(
55-
gray("Successfully added ${player.entityName} as an operator"), true)
60+
gray("Successfully added ${player.entityName} as an operator"), true
61+
)
62+
context.source.player.server.playerManager.sendCommandTree(context.source.player)
63+
}
5664
else
5765
context.source.sendFeedback(
5866
red("Could not added, ${player.entityName} is already an operator"), true)
5967
return 1
6068
}
6169

6270
private fun removeOperator(context: Context, player: ServerPlayerEntity): Int {
63-
val out = ConfigManager.addOperator(player.entityName, player.uuid.toString())
71+
val out = ConfigManager.removeOperator(player.entityName, player.uuid.toString())
6472

65-
if (out == 1)
73+
if (out == 1) {
6674
context.source.sendFeedback(
67-
gray("Successfully removed ${player.entityName} as an operator"), true)
75+
gray("Successfully removed ${player.entityName} as an operator"), true
76+
)
77+
context.source.player.server.playerManager.sendCommandTree(context.source.player)
78+
}
6879
else
6980
context.source.sendFeedback(
7081
red("Could not remove, ${player.entityName} is not an operator"), true)
7182

7283
return out
7384
}
85+
86+
private fun listOperators(context: Context): Int {
87+
context.source.sendFeedback(
88+
gray("Operators: ${ConfigManager.getOperators().joinToString(",")}"), false)
89+
return 1
90+
}
7491
}

src/main/kotlin/net/rx/modules/commands/GitCommand.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object GitCommand : Command() {
1818
override fun register(dispatcher: Dispatcher) {
1919
dispatcher.register(
2020
AegisCommandBuilder("git") {
21-
requires { it.hasPermissionLevel(4) }
21+
requires { ConfigManager.isOperator(it.player.uuidAsString) }
2222

2323
executes { invalidCommand(it, "Invalid invocation. Try /git status") }
2424

@@ -65,5 +65,4 @@ object GitCommand : Command() {
6565
return builder.buildFuture()
6666
}
6767
}
68-
6968
}

src/main/kotlin/net/rx/modules/commands/GitConfigCommand.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@ package net.rx.modules.commands
22

33
import com.github.p03w.aegis.AegisCommandBuilder
44
import com.mojang.brigadier.arguments.StringArgumentType
5+
import com.mojang.brigadier.suggestion.SuggestionProvider
6+
import com.mojang.brigadier.suggestion.Suggestions
7+
import com.mojang.brigadier.suggestion.SuggestionsBuilder
58
import kotlinx.coroutines.Dispatchers
69
import kotlinx.coroutines.GlobalScope
710
import kotlinx.coroutines.launch
811
import net.rx.modules.GitHandler
912
import net.rx.modules.config.ConfigManager
13+
import java.util.concurrent.CompletableFuture
1014

1115
object GitConfigCommand : Command() {
1216
override fun register(dispatcher: Dispatcher) {
1317
dispatcher.register(
1418
AegisCommandBuilder("gitconfig") {
15-
requires { it.hasPermissionLevel(4) }
19+
requires { ConfigManager.isOperator(it.player.uuidAsString) }
1620

17-
literal("edit") {
18-
greedyString("args") {
19-
executes {
20-
editGitConfig(it, StringArgumentType.getString(it, "args"))
21-
}
21+
greedyString("args") {
22+
executes {
23+
editGitConfig(it, StringArgumentType.getString(it, "args"))
2224
}
2325
}
26+
27+
suggests(GitConfigSuggestionProvider::getSuggestions)
2428
}.build()
2529
)
2630
}
@@ -39,4 +43,19 @@ object GitConfigCommand : Command() {
3943

4044
return 1
4145
}
46+
47+
internal object GitConfigSuggestionProvider : SuggestionProvider<Source> {
48+
override fun getSuggestions(
49+
context: Context, builder: SuggestionsBuilder
50+
): CompletableFuture<Suggestions> {
51+
// Suggestions for common git sub-commands to run
52+
listOf(
53+
"user.name <name>",
54+
"user.email <name>@email.com",
55+
"remote.origin.url <username>:<token>@<git-repo>",
56+
).map { builder.suggest(it) }
57+
58+
return builder.buildFuture()
59+
}
60+
}
4261
}

src/main/kotlin/net/rx/modules/commands/Permissions.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/kotlin/net/rx/modules/commands/Utils.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package net.rx.modules.commands
22

33
import com.mojang.brigadier.CommandDispatcher
44
import com.mojang.brigadier.context.CommandContext
5+
import com.mojang.brigadier.suggestion.SuggestionProvider
6+
import com.mojang.brigadier.suggestion.Suggestions
7+
import com.mojang.brigadier.suggestion.SuggestionsBuilder
58
import net.minecraft.server.command.ServerCommandSource
69
import net.minecraft.text.LiteralText
710
import net.minecraft.text.Style
811
import net.minecraft.text.Text
912
import net.minecraft.util.Formatting
1013
import net.rx.modules.config.ConfigManager
14+
import java.util.concurrent.CompletableFuture
1115

1216
/*
1317
Handy aliases to use through out command code
@@ -30,6 +34,15 @@ fun infoMessage(context: Context, msg: LiteralText): Int {
3034
return 0
3135
}
3236

37+
//fun suggestFactory(suggestions: List<String>): SuggestionProvider<Source> {
38+
// return SuggestionProvider<Source> { context, builder ->
39+
// // Suggestions for common git sub-commands to run
40+
// suggestions.map { builder.suggest(it) }
41+
//
42+
// builder.buildFuture()
43+
// }
44+
//}
45+
3346

3447
fun red(string: String): Text = LiteralText(string).setStyle(Style.EMPTY.withColor(Formatting.RED))
3548
fun green(string: String): Text = LiteralText(string).setStyle(Style.EMPTY.withColor(Formatting.GREEN))

src/main/kotlin/net/rx/modules/config/Config.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ data class Config(
1515
var gitPath: String = "",
1616

1717
// operators to handle, UUID: name
18-
var operators: MutableList<Operator> = mutableListOf()
18+
var operators: MutableList<Operator> = mutableListOf(),
19+
20+
// force personal git configs
21+
var forceGitConfig: Boolean = false
1922

2023
)
2124

src/main/kotlin/net/rx/modules/config/ConfigManager.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object ConfigManager {
2828

2929
private lateinit var configPath: Path
3030

31-
private lateinit var config: Config
31+
lateinit var config: Config
3232

3333
private lateinit var server: MinecraftServer
3434

@@ -132,13 +132,13 @@ object ConfigManager {
132132
return config.operators.map{ it.name }.toSet()
133133
}
134134

135-
/**
136-
* Gets Operator which matches UUID, otherwise null
137-
*
138-
* @return Operator(uuid) of that uuid
139-
*/
140-
fun getOperator(uuid: String): Operator? {
141-
return config.operators.filter{ it.uuid.equals(uuid) }?.get(0)
135+
fun isOperator(uuid: String): Boolean {
136+
for (op in config.operators) {
137+
if (op.uuid == uuid) {
138+
return true
139+
}
140+
}
141+
return false
142142
}
143143

144144
/**
@@ -151,7 +151,7 @@ object ConfigManager {
151151
}
152152

153153
fun addOperator(name: String, uuid: String): Int {
154-
if (getOperators().contains(uuid)) {
154+
if (isOperator(uuid)) {
155155
return 0
156156
}
157157

@@ -161,7 +161,7 @@ object ConfigManager {
161161
}
162162

163163
fun removeOperator(name: String, uuid: String): Int {
164-
if (!getOperators().contains(uuid)) {
164+
if (!isOperator(uuid)) {
165165
return 0
166166
}
167167

0 commit comments

Comments
 (0)