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.
99 */
1010package com.mairwunnx.projectessentials.core.impl.vanilla.commands
1111
12- import com.mairwunnx.projectessentials.core.api.v1.SETTING_LOC_ENABLED
1312import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI.getDispatcher
1413import 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
1914import com.mairwunnx.projectessentials.core.impl.nativeMappingsConfiguration
2015import com.mojang.brigadier.CommandDispatcher
2116import com.mojang.brigadier.arguments.IntegerArgumentType
22- import net.minecraft.command.CommandException
2317import net.minecraft.command.CommandSource
2418import net.minecraft.command.Commands
2519import net.minecraft.command.Commands.literal
2620import net.minecraft.command.arguments.TimeArgument
2721import net.minecraft.command.impl.TimeCommand
28- import net.minecraft.entity.player.ServerPlayerEntity
29- import net.minecraft.util.text.Style
3022
3123internal object TimeCommand : VanillaCommandBase(" time" ) {
3224 override fun register (dispatcher : CommandDispatcher <CommandSource >) {
@@ -42,53 +34,65 @@ internal object TimeCommand : VanillaCommandBase("time") {
4234 dispatcher.register(
4335 literal(" time" ).then(
4436 literal(" set" ).then(
45- literal(" day" ).executes { TimeCommand .setTime(it.source, 1000 ) }
37+ literal(" day" ).requires { permission(it, " set" ) }.executes {
38+ TimeCommand .setTime(it.source, 1000 )
39+ }
4640 ).then(
47- literal(" noon" ).executes { TimeCommand .setTime(it.source, 6000 ) }
41+ literal(" noon" ).requires { permission(it, " set" ) }.executes {
42+ TimeCommand .setTime(it.source, 6000 )
43+ }
4844 ).then(
49- literal(" sunset" ).executes { TimeCommand .setTime(it.source, 12000 ) }
45+ literal(" sunset" ).requires { permission(it, " set" ) }.executes {
46+ TimeCommand .setTime(it.source, 12000 )
47+ }
5048 ).then(
51- literal(" night" ).executes { TimeCommand .setTime(it.source, 13000 ) }
49+ literal(" night" ).requires { permission(it, " set" ) }.executes {
50+ TimeCommand .setTime(it.source, 13000 )
51+ }
5252 ).then(
53- literal(" midnight" ).executes { TimeCommand .setTime(it.source, 18000 ) }
53+ literal(" midnight" ).requires { permission(it, " set" ) }.executes {
54+ TimeCommand .setTime(it.source, 18000 )
55+ }
5456 ).then(
55- literal(" sunrise" ).executes { TimeCommand .setTime(it.source, 23000 ) }
57+ literal(" sunrise" ).requires { permission(it, " set" ) }.executes {
58+ TimeCommand .setTime(it.source, 23000 )
59+ }
5660 ).then(
5761 Commands .argument(
5862 " time" , TimeArgument .func_218091_a()
59- ).executes {
60- permission(it.source, " set" )
63+ ).requires { permission(it, " set" ) }.executes {
6164 TimeCommand .setTime(it.source, IntegerArgumentType .getInteger(it, " time" ))
6265 }
6366 )
6467 ).then(
6568 literal(" add" ).then(
6669 Commands .argument(
6770 " time" , TimeArgument .func_218091_a()
68- ).executes {
69- permission(it.source, " add" )
71+ ).requires { permission(it, " add" ) }.executes {
7072 TimeCommand .addTime(it.source, IntegerArgumentType .getInteger(it, " time" ))
7173 }
7274 )
7375 ).then(
7476 literal(" query" ).then(
75- literal(" daytime" ).executes {
76- permission(it.source, " query" )
77+ literal(" daytime" ).requires {
78+ permission(it, " query" )
79+ }.executes {
7780 TimeCommand .sendQueryResults(
78- it.source,
79- TimeCommand .getDayTime(it.source.world)
81+ it.source, TimeCommand .getDayTime(it.source.world)
8082 )
8183 }
8284 ).then(
83- literal(" gametime" ).executes {
84- permission(it.source, " query" )
85+ literal(" gametime" ).requires {
86+ permission(it, " query" )
87+ }.executes {
8588 TimeCommand .sendQueryResults(
8689 it.source, (it.source.world.gameTime % 2147483647L ).toInt()
8790 )
8891 }
8992 ).then(
90- literal(" day" ).executes {
91- permission(it.source, " query" )
93+ literal(" day" ).requires {
94+ permission(it, " query" )
95+ }.executes {
9296 TimeCommand .sendQueryResults(
9397 it.source, (it.source.world.dayTime / 24000L % 2147483647L ).toInt()
9498 )
@@ -98,35 +102,17 @@ internal object TimeCommand : VanillaCommandBase("time") {
98102 )
99103 }
100104
101- private fun permission (source : CommandSource , type : String ) {
102- val node =
103- if (type == " add" || type == " set" ) " native.time.change.$type " else " native.time.query"
104- if (source.entity is ServerPlayerEntity && ! hasPermission(source.asPlayer(), node, 2 )) {
105- throw CommandException (
106- textComponentFrom(
107- source.asPlayer(),
108- generalConfiguration.getBool(SETTING_LOC_ENABLED ),
109- " native.command.restricted"
110- ).setStyle(
111- Style ().setHoverEvent(
112- hoverEventFrom(
113- source.asPlayer(),
114- generalConfiguration.getBool(SETTING_LOC_ENABLED ),
115- " native.command.restricted_hover" ,
116- node, " 2"
117- )
118- )
119- )
120- )
121- }
105+ private fun permission (source : CommandSource , type : String ): Boolean {
106+ val node = if (type == " add" || type == " set" ) {
107+ " time.change.$type "
108+ } else " time.query"
109+ return isAllowed(source, node, 2 )
122110 }
123111
124112 fun short (name : String , time : Int ) {
125113 aliasesOf(name).forEach { command ->
126114 getDispatcher().register(literal(command).requires {
127- if (it.entity is ServerPlayerEntity ) {
128- hasPermission(it.entity as ServerPlayerEntity , " native.time.change.set" , 2 )
129- } else true
115+ permission(it, " set" )
130116 }.executes { TimeCommand .setTime(it.source, time) })
131117 }
132118 }
0 commit comments