|
| 1 | +/* |
| 2 | + * Copyright 2024 Lambda |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.lambda.command.commands |
| 19 | + |
| 20 | +import com.lambda.Lambda.mc |
| 21 | +import com.lambda.brigadier.CommandResult.Companion.failure |
| 22 | +import com.lambda.brigadier.CommandResult.Companion.success |
| 23 | +import com.lambda.brigadier.argument.literal |
| 24 | +import com.lambda.brigadier.argument.string |
| 25 | +import com.lambda.brigadier.argument.value |
| 26 | +import com.lambda.brigadier.execute |
| 27 | +import com.lambda.brigadier.executeWithResult |
| 28 | +import com.lambda.brigadier.required |
| 29 | +import com.lambda.command.LambdaCommand |
| 30 | +import com.lambda.config.configurations.FriendConfig |
| 31 | +import com.lambda.friend.FriendManager |
| 32 | +import com.lambda.util.Communication.info |
| 33 | +import com.lambda.util.extension.CommandBuilder |
| 34 | +import com.lambda.util.text.ClickEvents |
| 35 | +import com.lambda.util.text.buildText |
| 36 | +import com.lambda.util.text.color |
| 37 | +import com.lambda.util.text.literal |
| 38 | +import com.lambda.util.text.styled |
| 39 | +import java.awt.Color |
| 40 | + |
| 41 | +object FriendCommand : LambdaCommand( |
| 42 | + name = "friend", |
| 43 | + usage = "friend [add/remove] [name]", |
| 44 | + description = "Add or remove a friend" |
| 45 | +) { |
| 46 | + override fun CommandBuilder.create() { |
| 47 | + execute { |
| 48 | + this@FriendCommand.info( |
| 49 | + buildText { |
| 50 | + styled( |
| 51 | + color = Color.CYAN, |
| 52 | + underlined = true, |
| 53 | + clickEvent = ClickEvents.openFile(FriendConfig.primary.absolutePath), |
| 54 | + ) { |
| 55 | + literal("Click to open your friend list") |
| 56 | + } |
| 57 | + } |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + required(literal("add")) { |
| 62 | + required(string("player name")) { player -> |
| 63 | + suggests { _, builder -> |
| 64 | + mc.networkHandler |
| 65 | + ?.playerList |
| 66 | + ?.filter { it.profile != mc.gameProfile } |
| 67 | + ?.map { it.profile.name } |
| 68 | + ?.forEach { builder.suggest(it) } |
| 69 | + |
| 70 | + builder.buildFuture() |
| 71 | + } |
| 72 | + |
| 73 | + executeWithResult { |
| 74 | + val name = player().value() |
| 75 | + if (FriendManager.contains(name)) |
| 76 | + return@executeWithResult failure("This player is already in your friend list") |
| 77 | + |
| 78 | + val id = mc.networkHandler |
| 79 | + ?.playerList |
| 80 | + ?.firstOrNull { |
| 81 | + it.profile.name == name && |
| 82 | + it.profile != mc.gameProfile |
| 83 | + } ?: return@executeWithResult failure("Could not find the player in the server") |
| 84 | + |
| 85 | + FriendManager.add(id.profile) |
| 86 | + |
| 87 | + this@FriendCommand.info(buildText { |
| 88 | + color(Color.GREEN) { |
| 89 | + literal("Added ") |
| 90 | + color(Color.CYAN) { |
| 91 | + literal(name) |
| 92 | + color(Color.WHITE) { literal(" to your friend list") } |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + return@executeWithResult success() |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + required(literal("remove")) { |
| 103 | + required(string("player name")) { player -> |
| 104 | + suggests { _, builder -> |
| 105 | + FriendManager.friends.map { it.name } |
| 106 | + .forEach { builder.suggest(it) } |
| 107 | + |
| 108 | + builder.buildFuture() |
| 109 | + } |
| 110 | + |
| 111 | + executeWithResult { |
| 112 | + val name = player().value() |
| 113 | + val profile = FriendManager.get(name) |
| 114 | + ?: return@executeWithResult failure("This player is not in your friend list") |
| 115 | + |
| 116 | + FriendManager.remove(profile) |
| 117 | + |
| 118 | + this@FriendCommand.info(buildText { |
| 119 | + color(Color.RED) { |
| 120 | + literal("Removed ") |
| 121 | + color(Color.CYAN) { |
| 122 | + literal(profile.name) |
| 123 | + color(Color.WHITE) { literal(" from your friend list") } |
| 124 | + } |
| 125 | + } |
| 126 | + }) |
| 127 | + |
| 128 | + return@executeWithResult success() |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments