|
| 1 | +package com.mairwunnx.projectessentials.chat.commands |
| 2 | + |
| 3 | +import com.mairwunnx.projectessentials.chat.EntryPoint |
| 4 | +import com.mairwunnx.projectessentials.chat.api.MuteAPI |
| 5 | +import com.mairwunnx.projectessentials.chat.models.ChatModelUtils |
| 6 | +import com.mairwunnx.projectessentials.core.extensions.isPlayerSender |
| 7 | +import com.mairwunnx.projectessentials.core.extensions.playerName |
| 8 | +import com.mairwunnx.projectessentials.core.extensions.sendMsg |
| 9 | +import com.mojang.brigadier.CommandDispatcher |
| 10 | +import com.mojang.brigadier.arguments.StringArgumentType |
| 11 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal |
| 12 | +import com.mojang.brigadier.context.CommandContext |
| 13 | +import net.minecraft.command.CommandSource |
| 14 | +import net.minecraft.command.Commands |
| 15 | +import net.minecraft.command.arguments.EntityArgument |
| 16 | +import net.minecraft.entity.player.ServerPlayerEntity |
| 17 | +import net.minecraft.util.text.TranslationTextComponent |
| 18 | +import org.apache.logging.log4j.LogManager |
| 19 | + |
| 20 | +object UnmuteCommand { |
| 21 | + private val logger = LogManager.getLogger() |
| 22 | + |
| 23 | + fun register(dispatcher: CommandDispatcher<CommandSource>) { |
| 24 | + logger.info("Register \"/unmute\" command") |
| 25 | + |
| 26 | + dispatcher.register( |
| 27 | + literal<CommandSource>("unmute") |
| 28 | + .then( |
| 29 | + Commands.argument( |
| 30 | + "player", EntityArgument.player() |
| 31 | + ).executes { |
| 32 | + return@executes execute(it) |
| 33 | + } |
| 34 | + ).then( |
| 35 | + Commands.argument( |
| 36 | + "player name", StringArgumentType.string() |
| 37 | + ).executes { |
| 38 | + return@executes execute(it) |
| 39 | + } |
| 40 | + ) |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + private fun execute( |
| 45 | + context: CommandContext<CommandSource> |
| 46 | + ): Int { |
| 47 | + if (context.isPlayerSender() && !EntryPoint.hasPermission( |
| 48 | + context.source.asPlayer(), |
| 49 | + "ess.chat.unmute", |
| 50 | + 3 |
| 51 | + ) |
| 52 | + ) { |
| 53 | + sendMsg("chat", context.source, "chat.unmute_restricted") |
| 54 | + return 0 |
| 55 | + } |
| 56 | + |
| 57 | + val player = getPlayer(context) |
| 58 | + val playerName = getPlayerName(context) |
| 59 | + |
| 60 | + if (player != null) { |
| 61 | + if (MuteAPI.unmutePlayer(player.name.string)) { |
| 62 | + if (ChatModelUtils.chatModel.mute.notifyAllAboutUnmute) { |
| 63 | + if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) { |
| 64 | + context.source.server.playerList.sendMessage( |
| 65 | + TranslationTextComponent( |
| 66 | + "project_essentials_chat.notify_unmuted", |
| 67 | + player.name.string, |
| 68 | + context.playerName() |
| 69 | + ) |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + sendMsg( |
| 75 | + "chat", |
| 76 | + player.commandSource, |
| 77 | + "chat.youre_unmuted", |
| 78 | + context.playerName() |
| 79 | + ) |
| 80 | + sendMsg( |
| 81 | + "chat", |
| 82 | + context.source, |
| 83 | + "chat.unmute_success", |
| 84 | + player.name.string |
| 85 | + ) |
| 86 | + } else { |
| 87 | + sendMsg( |
| 88 | + "chat", |
| 89 | + context.source, |
| 90 | + "chat.unmute_failed", |
| 91 | + player.name.string |
| 92 | + ) |
| 93 | + } |
| 94 | + } else if (playerName != null) { |
| 95 | + if (MuteAPI.unmutePlayer(playerName)) { |
| 96 | + if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) { |
| 97 | + context.source.server.playerList.sendMessage( |
| 98 | + TranslationTextComponent( |
| 99 | + "project_essentials_chat.notify_unmuted", |
| 100 | + playerName, |
| 101 | + context.playerName() |
| 102 | + ) |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + sendMsg( |
| 107 | + "chat", |
| 108 | + context.source, |
| 109 | + "chat.unmute_success", |
| 110 | + playerName |
| 111 | + ) |
| 112 | + } else { |
| 113 | + sendMsg( |
| 114 | + "chat", |
| 115 | + context.source, |
| 116 | + "chat.unmute_failed", |
| 117 | + playerName |
| 118 | + ) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return 0 |
| 123 | + } |
| 124 | + |
| 125 | + private fun getPlayer(context: CommandContext<CommandSource>): ServerPlayerEntity? = try { |
| 126 | + EntityArgument.getPlayer(context, "player") |
| 127 | + } catch (ex: IllegalArgumentException) { |
| 128 | + null |
| 129 | + } |
| 130 | + |
| 131 | + private fun getPlayerName(context: CommandContext<CommandSource>): String? = try { |
| 132 | + StringArgumentType.getString(context, "player name") |
| 133 | + } catch (ex: IllegalArgumentException) { |
| 134 | + null |
| 135 | + } |
| 136 | +} |
0 commit comments