|
1 | 1 | /** |
2 | | - * This command implementation by Mojang. |
3 | | - * And decompiled with idea source code was converted |
4 | | - * to kotlin code. |
5 | | - * Also added some logic, for example checking on |
6 | | - * permissions, and for some commands shorten aliases. |
| 2 | + * ! This command implementation by Mojang Studios! |
| 3 | + * |
| 4 | + * Decompiled with idea source code was converted to kotlin code. |
| 5 | + * But with additions such as permissions checking and etc. |
7 | 6 | * |
8 | 7 | * 1. This can be bad code. |
9 | 8 | * 2. This file can be not formatter pretty. |
10 | 9 | */ |
11 | | - |
12 | 10 | package com.mairwunnx.projectessentials.core.impl.vanilla.commands |
13 | 11 |
|
14 | | - |
15 | | -import com.mairwunnx.projectessentials.core.api.v1.SETTING_LOC_ENABLED |
16 | | -import com.mairwunnx.projectessentials.core.api.v1.SETTING_WEATHER_COMMAND_DEFAULT_DURATION |
17 | 12 | import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI |
18 | 13 | import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAliases |
19 | | -import com.mairwunnx.projectessentials.core.api.v1.extensions.hoverEventFrom |
20 | | -import com.mairwunnx.projectessentials.core.api.v1.extensions.textComponentFrom |
21 | | -import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission |
| 14 | +import com.mairwunnx.projectessentials.core.impl.nativeMappingsConfiguration |
22 | 15 | import com.mojang.brigadier.CommandDispatcher |
23 | | -import com.mojang.brigadier.arguments.IntegerArgumentType |
24 | | -import com.mojang.brigadier.exceptions.CommandSyntaxException |
25 | | -import net.minecraft.command.CommandException |
| 16 | +import com.mojang.brigadier.arguments.IntegerArgumentType.getInteger |
| 17 | +import com.mojang.brigadier.arguments.IntegerArgumentType.integer |
26 | 18 | import net.minecraft.command.CommandSource |
27 | 19 | import net.minecraft.command.Commands |
28 | | -import net.minecraft.util.text.Style |
29 | | -import net.minecraft.util.text.TranslationTextComponent |
30 | | - |
31 | | -/** |
32 | | - * Weather type uses for [WeatherType] enum elements. |
33 | | - * @since 2.0.0-SNAPSHOT.1. |
34 | | - */ |
35 | | -interface IWeatherType { |
36 | | - /** |
37 | | - * Weather type name for permissions checking. |
38 | | - */ |
39 | | - val type: String |
40 | | -} |
41 | | - |
42 | | -/** |
43 | | - * Weather type enum class, contains all weather types for |
44 | | - * other essentials modules. |
45 | | - * @since 2.0.0-SNAPSHOT.1. |
46 | | - */ |
47 | | -enum class WeatherType : IWeatherType { |
48 | | - /** |
49 | | - * Sunny in-game weather. |
50 | | - */ |
51 | | - Sunny { |
52 | | - override val type = "clear" |
53 | | - }, |
54 | | - |
55 | | - /** |
56 | | - * Rainy in-game weather. |
57 | | - */ |
58 | | - Rainy { |
59 | | - override val type = "rain" |
60 | | - }, |
61 | | - |
62 | | - /** |
63 | | - * Thunder in-game weather. |
64 | | - */ |
65 | | - Thunder { |
66 | | - override val type = "thunder" |
67 | | - } |
68 | | -} |
69 | | - |
70 | | -internal object WeatherCommand : VanillaCommandBase() { |
71 | | - private var sunAliases = configuration.take().aliases.sun + "sun" |
72 | | - private var rainAliases = configuration.take().aliases.rain + "rain" |
73 | | - private var thunderAliases = configuration.take().aliases.thunder + "thunder" |
| 20 | +import net.minecraft.command.impl.WeatherCommand |
74 | 21 |
|
| 22 | +internal object WeatherCommand : VanillaCommandBase("weather") { |
75 | 23 | private val durationArgument = Commands.argument( |
76 | | - "duration", IntegerArgumentType.integer(0, 1000000) |
| 24 | + "duration", integer(0, 1000000) |
77 | 25 | ) |
78 | 26 |
|
79 | | - private fun tryAssignAliases() { |
80 | | - CommandAliases.aliases["sun"] = (sunAliases + "weather").toMutableList() |
81 | | - CommandAliases.aliases["rain"] = (rainAliases + "weather").toMutableList() |
82 | | - CommandAliases.aliases["thunder"] = (thunderAliases + "weather").toMutableList() |
83 | | - CommandAliases.aliases["weather"] = |
84 | | - (sunAliases + rainAliases + thunderAliases).toMutableList() |
85 | | - } |
86 | | - |
87 | | - private fun registerSun(dispatcher: CommandDispatcher<CommandSource>) { |
88 | | - sunAliases.forEach { |
89 | | - dispatcher.register( |
90 | | - Commands.literal(it).executes { p_198861_0_ -> |
91 | | - setClear( |
92 | | - p_198861_0_.source, |
93 | | - generalConfiguration.getIntOrDefault( |
94 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
95 | | - ) |
96 | | - ) |
97 | | - }.then( |
98 | | - durationArgument.executes { p_198864_0_ -> |
99 | | - setClear( |
100 | | - p_198864_0_.source, |
101 | | - IntegerArgumentType.getInteger(p_198864_0_, "duration") * 20 |
102 | | - ) |
103 | | - } |
104 | | - ) |
105 | | - ) |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - private fun registerRain(dispatcher: CommandDispatcher<CommandSource>) { |
110 | | - rainAliases.forEach { |
111 | | - dispatcher.register( |
112 | | - Commands.literal(it).executes { p_198860_0_ -> |
113 | | - setRain( |
114 | | - p_198860_0_.source, |
115 | | - generalConfiguration.getIntOrDefault( |
116 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
117 | | - ) |
118 | | - ) |
119 | | - }.then( |
120 | | - durationArgument.executes { p_198866_0_ -> |
121 | | - setRain( |
122 | | - p_198866_0_.source, |
123 | | - IntegerArgumentType.getInteger(p_198866_0_, "duration") * 20 |
124 | | - ) |
125 | | - } |
126 | | - ) |
127 | | - ) |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - private fun registerThunder(dispatcher: CommandDispatcher<CommandSource>) { |
132 | | - thunderAliases.forEach { |
133 | | - dispatcher.register( |
134 | | - Commands.literal(it).executes { p_198859_0_ -> |
135 | | - setThunder( |
136 | | - p_198859_0_.source, |
137 | | - generalConfiguration.getIntOrDefault( |
138 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
139 | | - ) |
140 | | - ) |
141 | | - }.then( |
142 | | - durationArgument.executes { p_198867_0_ -> |
143 | | - setThunder( |
144 | | - p_198867_0_.source, |
145 | | - IntegerArgumentType.getInteger(p_198867_0_, "duration") * 20 |
146 | | - ) |
147 | | - } |
148 | | - ) |
149 | | - ) |
150 | | - } |
151 | | - } |
152 | | - |
153 | | - fun register(dispatcher: CommandDispatcher<CommandSource>) { |
154 | | - registerSun(dispatcher) |
155 | | - registerRain(dispatcher) |
156 | | - registerThunder(dispatcher) |
157 | | - tryAssignAliases() |
158 | | - CommandAPI.removeCommand("weather") |
159 | | - |
| 27 | + override fun register(dispatcher: CommandDispatcher<CommandSource>) { |
| 28 | + super.register(dispatcher) |
| 29 | + short("sun").also { short("rain") }.also { short("thunder") }.also { aliases() } |
160 | 30 | dispatcher.register( |
161 | | - Commands.literal("weather").then( |
162 | | - Commands.literal("clear").executes { p_198861_0_ -> |
163 | | - setClear( |
164 | | - p_198861_0_.source, |
165 | | - generalConfiguration.getIntOrDefault( |
166 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
167 | | - ) |
168 | | - ) |
| 31 | + Commands.literal(name).then( |
| 32 | + Commands.literal("clear").requires { |
| 33 | + isAllowed(it, "weather.sun", 2) |
| 34 | + }.executes { |
| 35 | + WeatherCommand.setClear(it.source, 6000) |
169 | 36 | }.then( |
170 | | - durationArgument.executes { p_198864_0_ -> |
171 | | - setClear( |
172 | | - p_198864_0_.source, |
173 | | - IntegerArgumentType.getInteger(p_198864_0_, "duration") * 20 |
174 | | - ) |
| 37 | + durationArgument.executes { |
| 38 | + WeatherCommand.setClear(it.source, getInteger(it, "duration") * 20) |
175 | 39 | } |
176 | 40 | ) |
177 | 41 | ).then( |
178 | | - Commands.literal("rain").executes { p_198860_0_ -> |
179 | | - setRain( |
180 | | - p_198860_0_.source, |
181 | | - generalConfiguration.getIntOrDefault( |
182 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
183 | | - ) |
184 | | - ) |
| 42 | + Commands.literal("rain").requires { |
| 43 | + isAllowed(it, "weather.rain", 2) |
| 44 | + }.executes { |
| 45 | + WeatherCommand.setRain(it.source, 6000) |
185 | 46 | }.then( |
186 | | - durationArgument.executes { p_198866_0_ -> |
187 | | - setRain( |
188 | | - p_198866_0_.source, |
189 | | - IntegerArgumentType.getInteger(p_198866_0_, "duration") * 20 |
190 | | - ) |
| 47 | + durationArgument.executes { |
| 48 | + WeatherCommand.setRain(it.source, getInteger(it, "duration") * 20) |
191 | 49 | } |
192 | 50 | ) |
193 | 51 | ).then( |
194 | | - Commands.literal("thunder").executes { p_198859_0_ -> |
195 | | - setThunder( |
196 | | - p_198859_0_.source, |
197 | | - generalConfiguration.getIntOrDefault( |
198 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
199 | | - ) |
200 | | - ) |
| 52 | + Commands.literal("thunder").requires { |
| 53 | + isAllowed(it, "weather.thunder", 2) |
| 54 | + }.executes { |
| 55 | + WeatherCommand.setThunder(it.source, 6000) |
201 | 56 | }.then( |
202 | | - durationArgument.executes { p_198867_0_ -> |
203 | | - setThunder( |
204 | | - p_198867_0_.source, |
205 | | - IntegerArgumentType.getInteger(p_198867_0_, "duration") * 20 |
206 | | - ) |
| 57 | + durationArgument.executes { |
| 58 | + WeatherCommand.setThunder(it.source, getInteger(it, "duration") * 20) |
207 | 59 | } |
208 | 60 | ) |
209 | 61 | ) |
210 | 62 | ) |
211 | 63 | } |
212 | 64 |
|
213 | | - private fun checkPermissions(source: CommandSource, weatherType: WeatherType, timed: Boolean) { |
214 | | - try { |
215 | | - if ( |
216 | | - !hasPermission( |
217 | | - source.asPlayer(), |
218 | | - if (timed) "native.weather.${weatherType.type}.timed" else "native.weather.${weatherType.type}", |
219 | | - if (timed) 3 else 2 |
220 | | - ) |
221 | | - ) { |
222 | | - throw CommandException( |
223 | | - textComponentFrom( |
224 | | - source.asPlayer(), |
225 | | - generalConfiguration.getBool(SETTING_LOC_ENABLED), |
226 | | - "native.command.restricted" |
227 | | - ).setStyle( |
228 | | - Style().setHoverEvent( |
229 | | - hoverEventFrom( |
230 | | - source.asPlayer(), |
231 | | - generalConfiguration.getBool(SETTING_LOC_ENABLED), |
232 | | - "native.command.restricted_hover", |
233 | | - if (timed) "native.weather.${weatherType.type}.timed" else "native.weather.${weatherType.type}", |
234 | | - if (timed) "3" else "2" |
235 | | - ) |
236 | | - ) |
237 | | - ) |
238 | | - ) |
239 | | - } |
240 | | - } catch (e: CommandSyntaxException) { |
241 | | - // ignored, because command executed by server. |
| 65 | + private fun short(name: String) { |
| 66 | + aliasesOf(name).forEach { command -> |
| 67 | + CommandAPI.getDispatcher().register(Commands.literal(command).requires { |
| 68 | + isAllowed(it, "weather.$name", 2) |
| 69 | + }.then( |
| 70 | + durationArgument.executes { |
| 71 | + val duration = getInteger(it, "duration") * 20 |
| 72 | + val source = it.source |
| 73 | + if (name == "sun") WeatherCommand.setClear(source, duration) |
| 74 | + if (name == "rain") WeatherCommand.setRain(source, duration) |
| 75 | + if (name == "thunder") WeatherCommand.setThunder(source, duration) |
| 76 | + else return@executes -1 |
| 77 | + } |
| 78 | + ).executes { |
| 79 | + val source = it.source |
| 80 | + if (name == "sun") WeatherCommand.setClear(source, 6000) |
| 81 | + if (name == "rain") WeatherCommand.setRain(source, 6000) |
| 82 | + if (name == "thunder") WeatherCommand.setThunder(source, 6000) |
| 83 | + else return@executes -1 |
| 84 | + }) |
242 | 85 | } |
243 | 86 | } |
244 | 87 |
|
245 | | - private fun setClear(source: CommandSource, time: Int): Int { |
246 | | - checkPermissions( |
247 | | - source, |
248 | | - WeatherType.Sunny, |
249 | | - time != generalConfiguration.getIntOrDefault( |
250 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
251 | | - ) |
252 | | - ) |
253 | | - source.world.worldInfo.clearWeatherTime = time |
254 | | - source.world.worldInfo.rainTime = 0 |
255 | | - source.world.worldInfo.thunderTime = 0 |
256 | | - source.world.worldInfo.isRaining = false |
257 | | - source.world.worldInfo.isThundering = false |
258 | | - source.sendFeedback( |
259 | | - TranslationTextComponent( |
260 | | - "commands.weather.set.clear" |
261 | | - ), true |
262 | | - ) |
263 | | - return time |
| 88 | + private fun aliases() { |
| 89 | + CommandAliases.aliases["sun"] = (aliasesOf("sun") + "weather").toMutableList() |
| 90 | + CommandAliases.aliases["thunder"] = (aliasesOf("thunder") + "weather").toMutableList() |
| 91 | + CommandAliases.aliases["rain"] = (aliasesOf("rain") + "weather").toMutableList() |
264 | 92 | } |
265 | 93 |
|
266 | | - private fun setRain(source: CommandSource, time: Int): Int { |
267 | | - checkPermissions( |
268 | | - source, |
269 | | - WeatherType.Rainy, |
270 | | - time != generalConfiguration.getIntOrDefault( |
271 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
272 | | - ) |
273 | | - ) |
274 | | - source.world.worldInfo.clearWeatherTime = 0 |
275 | | - source.world.worldInfo.rainTime = time |
276 | | - source.world.worldInfo.thunderTime = time |
277 | | - source.world.worldInfo.isRaining = true |
278 | | - source.world.worldInfo.isThundering = false |
279 | | - source.sendFeedback( |
280 | | - TranslationTextComponent( |
281 | | - "commands.weather.set.rain" |
282 | | - ), true |
283 | | - ) |
284 | | - return time |
285 | | - } |
286 | | - |
287 | | - private fun setThunder(source: CommandSource, time: Int): Int { |
288 | | - checkPermissions( |
289 | | - source, |
290 | | - WeatherType.Thunder, |
291 | | - time != generalConfiguration.getIntOrDefault( |
292 | | - SETTING_WEATHER_COMMAND_DEFAULT_DURATION, 6000 |
293 | | - ) |
294 | | - ) |
295 | | - source.world.worldInfo.clearWeatherTime = 0 |
296 | | - source.world.worldInfo.rainTime = time |
297 | | - source.world.worldInfo.thunderTime = time |
298 | | - source.world.worldInfo.isRaining = true |
299 | | - source.world.worldInfo.isThundering = true |
300 | | - source.sendFeedback( |
301 | | - TranslationTextComponent( |
302 | | - "commands.weather.set.thunder" |
303 | | - ), true |
304 | | - ) |
305 | | - return time |
306 | | - } |
| 94 | + private fun aliasesOf(origin: String) = nativeMappingsConfiguration.aliases[origin]?.let { |
| 95 | + return@let it.split(',') + origin |
| 96 | + } ?: let { return@let listOf(origin) } |
307 | 97 | } |
0 commit comments