|
| 1 | +package com.mairwunnx.projectessentials.projectessentialshome.commands |
| 2 | + |
| 3 | +import com.mairwunnx.projectessentials.projectessentialshome.models.HomeModel |
| 4 | +import com.mairwunnx.projectessentials.projectessentialshome.storage.StorageBase |
| 5 | +import com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases |
| 6 | +import com.mairwunnx.projectessentialscore.extensions.isPlayerSender |
| 7 | +import com.mairwunnx.projectessentialscore.extensions.sendMsg |
| 8 | +import com.mairwunnx.projectessentialscore.helpers.ONLY_PLAYER_CAN |
| 9 | +import com.mairwunnx.projectessentialscore.helpers.PERMISSION_LEVEL |
| 10 | +import com.mairwunnx.projectessentialspermissions.permissions.PermissionsAPI |
| 11 | +import com.mojang.brigadier.CommandDispatcher |
| 12 | +import com.mojang.brigadier.arguments.StringArgumentType |
| 13 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal |
| 14 | +import com.mojang.brigadier.context.CommandContext |
| 15 | +import net.minecraft.command.CommandSource |
| 16 | +import net.minecraft.command.Commands |
| 17 | +import org.apache.logging.log4j.LogManager |
| 18 | + |
| 19 | +@Suppress("DuplicatedCode") |
| 20 | +object DelHomeCommand { |
| 21 | + private val aliases = arrayOf("delhome", "edelhome", "removehome", "remhome") |
| 22 | + private val logger = LogManager.getLogger() |
| 23 | + |
| 24 | + fun register(dispatcher: CommandDispatcher<CommandSource>) { |
| 25 | + logger.info(" - register \"/delhome\" command ...") |
| 26 | + aliases.forEach { command -> |
| 27 | + dispatcher.register( |
| 28 | + literal<CommandSource>(command).executes { |
| 29 | + return@executes execute(it) |
| 30 | + }.then( |
| 31 | + Commands.argument( |
| 32 | + "home name", StringArgumentType.string() |
| 33 | + ).executes { |
| 34 | + return@executes execute(it) |
| 35 | + } |
| 36 | + ) |
| 37 | + ) |
| 38 | + } |
| 39 | + applyCommandAliases() |
| 40 | + } |
| 41 | + |
| 42 | + private fun applyCommandAliases() { |
| 43 | + try { |
| 44 | + Class.forName( |
| 45 | + "com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases" |
| 46 | + ) |
| 47 | + CommandsAliases.aliases["delhome"] = aliases.toMutableList() |
| 48 | + logger.info(" - applying aliases: $aliases") |
| 49 | + } catch (_: ClassNotFoundException) { |
| 50 | + // ignored |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private fun execute(c: CommandContext<CommandSource>): Int { |
| 55 | + if (c.isPlayerSender()) { |
| 56 | + val player = c.source.asPlayer() |
| 57 | + if (PermissionsAPI.hasPermission(player.name.string, "ess.home.remove")) { |
| 58 | + val playerUUID = player.uniqueID.toString() |
| 59 | + val homeName: String = try { |
| 60 | + StringArgumentType.getString(c, "home name") |
| 61 | + } catch (_: IllegalArgumentException) { |
| 62 | + "home" |
| 63 | + } |
| 64 | + val homeModel = StorageBase.getData(playerUUID).homes |
| 65 | + StorageBase.getData(playerUUID).homes.forEach { |
| 66 | + if (it.home == homeName) { |
| 67 | + homeModel.remove(it) |
| 68 | + StorageBase.setData(playerUUID, HomeModel(homeModel)) |
| 69 | + sendMsg("home", c.source, "home.remove.success", homeName) |
| 70 | + logger.info("Executed command \"/delhome\" from ${player.name.string}") |
| 71 | + return 0 |
| 72 | + } |
| 73 | + } |
| 74 | + sendMsg("home", c.source, "home.not_found", homeName) |
| 75 | + } else { |
| 76 | + sendMsg("home", c.source, "home.remove.restricted") |
| 77 | + logger.info( |
| 78 | + PERMISSION_LEVEL |
| 79 | + .replace("%0", player.name.string) |
| 80 | + .replace("%1", "delhome") |
| 81 | + ) |
| 82 | + } |
| 83 | + } else { |
| 84 | + logger.info(ONLY_PLAYER_CAN.replace("%0", "delhome")) |
| 85 | + } |
| 86 | + return 0 |
| 87 | + } |
| 88 | +} |
0 commit comments