Skip to content

Commit ea38105

Browse files
committed
Added "/delhome" command and command aliases.
Fixed empty command argument for sethome command.
1 parent c4cbf47 commit ea38105

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialshome/EntryPoint.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mairwunnx.projectessentials.projectessentialshome
22

3+
import com.mairwunnx.projectessentials.projectessentialshome.commands.DelHomeCommand
34
import com.mairwunnx.projectessentials.projectessentialshome.commands.HomeCommand
45
import com.mairwunnx.projectessentials.projectessentialshome.commands.SetHomeCommand
56
import com.mairwunnx.projectessentials.projectessentialshome.storage.StorageBase
@@ -41,6 +42,7 @@ class EntryPoint : EssBase() {
4142
logger.info("Command registering is starting ...")
4243
HomeCommand.register(cmdDispatcher)
4344
SetHomeCommand.register(cmdDispatcher)
45+
DelHomeCommand.register(cmdDispatcher)
4446
}
4547

4648
@Suppress("UNUSED_PARAMETER")
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialshome/commands/SetHomeCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ object SetHomeCommand {
7575
)
7676
)
7777
StorageBase.setData(playerUUID, HomeModel(homeModel))
78-
sendMsg("home", c.source, "home.set.success")
78+
sendMsg("home", c.source, "home.set.success", homeName)
7979
logger.info("New home point for ${player.name.string} installed with data: ")
8080
logger.info(" - name: $homeName")
8181
logger.info(" - world / world id: $clientWorld / $worldId")

src/main/resources/assets/projectessentialshome/lang/en_us.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"project_essentials_home.home.success": "§6Teleported to home §7%s§6.",
44
"project_essentials_home.home.set.restricted": "§cYou §7don't have permission §cto set new home.",
55
"project_essentials_home.home.set.success": "§6New home with name §7\"%s\" §6has been saved.",
6+
"project_essentials_home.home.remove.restricted": "§cYou §7don't have permission §cto set remove home.",
7+
"project_essentials_home.home.remove.success": "§6Home with name §7\"%s\" §6has been removed.",
68
"project_essentials_home.home.not_found": "§cHome with name §7\"%s\" §cnot found."
79
}

src/main/resources/assets/projectessentialshome/lang/ru_ru.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"project_essentials_home.home.success": "§6Вы телепортированы к дому §7%s§6.",
44
"project_essentials_home.home.set.restricted": "§cУ вас §7нет прав §cна установку точки нового дома.",
55
"project_essentials_home.home.set.success": "§6Новая точка дома была установлена с именем §7\"%s\"§6.",
6+
"project_essentials_home.home.remove.restricted": "§cУ вас §7нет прав §cна удаление точки нового дома.",
7+
"project_essentials_home.home.remove.success": "§6Точка дома с именем §7\"%s\"§6 удалена.",
68
"project_essentials_home.home.not_found": "§cТочка дома с именем §7\"%s\" §cне найдена."
79
}

0 commit comments

Comments
 (0)