Skip to content

Commit dfe9bc2

Browse files
committed
GameModeCommand.kt improved.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 479b2f6 commit dfe9bc2

1 file changed

Lines changed: 13 additions & 46 deletions

File tree

  • src/main/kotlin/com/mairwunnx/projectessentials/core/impl/vanilla/commands
Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* ! This command implementation by Mojang Game Studios!
2+
* ! This command implementation by Mojang Studios!
33
*
44
* Decompiled with idea source code was converted to kotlin code.
55
* But with additions such as permissions checking and etc.
@@ -9,21 +9,13 @@
99
*/
1010
package com.mairwunnx.projectessentials.core.impl.vanilla.commands
1111

12-
import com.mairwunnx.projectessentials.core.api.v1.SETTING_LOC_ENABLED
1312
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI
1413
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAliases
15-
import com.mairwunnx.projectessentials.core.api.v1.extensions.hoverEventFrom
16-
import com.mairwunnx.projectessentials.core.api.v1.extensions.textComponentFrom
17-
import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission
18-
import com.mairwunnx.projectessentials.core.impl.generalConfiguration
1914
import com.mojang.brigadier.CommandDispatcher
20-
import net.minecraft.command.CommandException
2115
import net.minecraft.command.CommandSource
2216
import net.minecraft.command.Commands
2317
import net.minecraft.command.arguments.EntityArgument
2418
import net.minecraft.command.impl.GameModeCommand
25-
import net.minecraft.entity.player.ServerPlayerEntity
26-
import net.minecraft.util.text.Style
2719
import net.minecraft.world.GameType
2820

2921
internal object GameModeCommand : VanillaCommandBase("gamemode") {
@@ -36,12 +28,14 @@ internal object GameModeCommand : VanillaCommandBase("gamemode") {
3628
GameType.values().forEach { type ->
3729
if (type != GameType.NOT_SET) {
3830
literal.then(
39-
Commands.literal(type.getName()).executes {
40-
checkPermissions(it.source, type, false)
31+
Commands.literal(type.getName()).requires {
32+
isAllowed(it, "gamemode.${type.getName()}", 2)
33+
}.executes {
4134
GameModeCommand.setGameMode(it, setOf(it.source.asPlayer()), type)
4235
}.then(
43-
Commands.argument("target", EntityArgument.players()).executes {
44-
checkPermissions(it.source, type, true)
36+
Commands.argument("target", EntityArgument.players()).requires {
37+
isAllowed(it, "gamemode.${type.getName()}.other", 3)
38+
}.executes {
4539
GameModeCommand.setGameMode(
4640
it, EntityArgument.getPlayers(it, "target"), type
4741
)
@@ -59,43 +53,16 @@ internal object GameModeCommand : VanillaCommandBase("gamemode") {
5953
}
6054

6155
private fun short(short: String, mode: GameType) {
62-
Commands.literal(short).executes {
63-
checkPermissions(it.source, mode, false)
56+
Commands.literal(short).requires {
57+
isAllowed(it, "gamemode.${mode.getName()}", 2)
58+
}.executes {
6459
GameModeCommand.setGameMode(it, setOf(it.source.asPlayer()), mode)
6560
}.then(
66-
Commands.argument("target", EntityArgument.players()).executes {
67-
checkPermissions(it.source, mode, true)
61+
Commands.argument("target", EntityArgument.players()).requires {
62+
isAllowed(it, "gamemode.${mode.getName()}.other", 3)
63+
}.executes {
6864
GameModeCommand.setGameMode(it, EntityArgument.getPlayers(it, "target"), mode)
6965
}
7066
).also { CommandAPI.getDispatcher().register(it) }
7167
}
72-
73-
private fun checkPermissions(source: CommandSource, gameTypeIn: GameType, other: Boolean) {
74-
if (source !is ServerPlayerEntity) return
75-
if (!hasPermission(
76-
source.asPlayer(),
77-
"native.gamemode.${gameTypeIn.getName()}${if (other) ".other" else ""}",
78-
if (other) 3 else 2
79-
)
80-
) {
81-
throw CommandException(
82-
textComponentFrom(
83-
source.asPlayer(),
84-
generalConfiguration.getBool(SETTING_LOC_ENABLED),
85-
"native.command.restricted"
86-
).setStyle(
87-
Style().setHoverEvent(
88-
hoverEventFrom(
89-
source.asPlayer(),
90-
generalConfiguration.getBool(SETTING_LOC_ENABLED),
91-
"native.command.restricted_hover",
92-
"native.gamemode.${gameTypeIn.getName()}${if (other) ".other" else ""}",
93-
if (other) "3" else "2"
94-
)
95-
)
96-
)
97-
)
98-
}
99-
}
10068
}
101-

0 commit comments

Comments
 (0)