|
1 | 1 | package com.solegendary.reignofnether.resources;
|
2 | 2 |
|
| 3 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 4 | +import com.mojang.brigadier.context.CommandContext; |
| 5 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
3 | 6 | import com.solegendary.reignofnether.ReignOfNether;
|
| 7 | +import com.solegendary.reignofnether.alliance.AlliancesServerEvents; |
4 | 8 | import com.solegendary.reignofnether.building.*;
|
5 | 9 | import com.solegendary.reignofnether.player.PlayerServerEvents;
|
6 | 10 | import com.solegendary.reignofnether.registrars.BlockRegistrar;
|
|
11 | 15 | import com.solegendary.reignofnether.unit.UnitServerEvents;
|
12 | 16 | import com.solegendary.reignofnether.unit.interfaces.Unit;
|
13 | 17 | import net.minecraft.client.resources.language.I18n;
|
| 18 | +import net.minecraft.commands.CommandSourceStack; |
| 19 | +import net.minecraft.commands.Commands; |
| 20 | +import net.minecraft.commands.arguments.EntityArgument; |
14 | 21 | import net.minecraft.core.BlockPos;
|
| 22 | +import net.minecraft.network.chat.Component; |
15 | 23 | import net.minecraft.server.level.ServerLevel;
|
16 | 24 | import net.minecraft.world.entity.LivingEntity;
|
| 25 | +import net.minecraft.world.entity.player.Player; |
17 | 26 | import net.minecraft.world.level.GameRules;
|
18 | 27 | import net.minecraft.world.level.Level;
|
19 | 28 | import net.minecraft.world.level.block.*;
|
20 | 29 | import net.minecraft.world.level.block.state.BlockState;
|
21 | 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | +import net.minecraftforge.event.RegisterCommandsEvent; |
22 | 32 | import net.minecraftforge.event.TickEvent;
|
23 | 33 | import net.minecraftforge.event.entity.player.PlayerEvent;
|
24 | 34 | import net.minecraftforge.event.level.BlockEvent;
|
@@ -282,29 +292,80 @@ public static void fellAdjacentLogs(BlockPos bp, ArrayList<BlockPos> bpsExcluded
|
282 | 292 | }
|
283 | 293 | }
|
284 | 294 |
|
285 |
| - public static void trySendingResources(String playerGiving, String playerReceiving, ResourceName resourceName, int amount) { |
286 |
| - if (canAfford(playerGiving, resourceName, amount)) { |
287 |
| - addSubtractResources(new Resources(playerGiving, |
288 |
| - resourceName == ResourceName.FOOD ? amount : 0, |
289 |
| - resourceName == ResourceName.WOOD ? amount : 0, |
290 |
| - resourceName == ResourceName.ORE ? amount : 0 |
| 295 | + @SubscribeEvent |
| 296 | + public static void onRegisterCommand(RegisterCommandsEvent evt) { |
| 297 | + |
| 298 | + evt.getDispatcher().register(Commands.literal("send-food") |
| 299 | + .then(Commands.argument("player", EntityArgument.player()) |
| 300 | + .then(Commands.argument("amount", IntegerArgumentType.integer(0, Integer.MAX_VALUE)) |
| 301 | + .executes((command) -> trySendingResources(command, ResourceName.FOOD))))); |
| 302 | + |
| 303 | + evt.getDispatcher().register(Commands.literal("send-wood") |
| 304 | + .then(Commands.argument("player", EntityArgument.player()) |
| 305 | + .then(Commands.argument("amount", IntegerArgumentType.integer(0, Integer.MAX_VALUE)) |
| 306 | + .executes((command) -> trySendingResources(command, ResourceName.WOOD))))); |
| 307 | + |
| 308 | + evt.getDispatcher().register(Commands.literal("send-ore") |
| 309 | + .then(Commands.argument("player", EntityArgument.player()) |
| 310 | + .then(Commands.argument("amount", IntegerArgumentType.integer(0, Integer.MAX_VALUE)) |
| 311 | + .executes((command) -> trySendingResources(command, ResourceName.ORE))))); |
| 312 | + } |
| 313 | + |
| 314 | + public static int trySendingResources(CommandContext<CommandSourceStack> context, ResourceName resourceName) throws CommandSyntaxException { |
| 315 | + Player sendingPlayer = context.getSource().getPlayer(); |
| 316 | + Player receivingPlayer = EntityArgument.getPlayer(context, "player"); |
| 317 | + int amount = IntegerArgumentType.getInteger(context, "amount"); |
| 318 | + if (sendingPlayer == null) |
| 319 | + return 0; |
| 320 | + String sendingPlayerName = sendingPlayer.getName().getString(); |
| 321 | + String receivingPlayerName = receivingPlayer.getName().getString(); |
| 322 | + |
| 323 | + Resources res = null; |
| 324 | + for (Resources resources : resourcesList) |
| 325 | + if (resources.ownerName.equals(sendingPlayer.getName().getString())) |
| 326 | + res = resources; |
| 327 | + if (res == null) |
| 328 | + return 0; |
| 329 | + |
| 330 | + if (sendingPlayerName.equals(receivingPlayerName)) { |
| 331 | + PlayerServerEvents.sendMessageToPlayer(sendingPlayerName, "server.resources.reignofnether.sending_to_self"); |
| 332 | + return 0; |
| 333 | + } else if (!AlliancesServerEvents.isAllied(sendingPlayerName, receivingPlayerName)) { |
| 334 | + PlayerServerEvents.sendMessageToPlayer(sendingPlayerName, "server.resources.reignofnether.not_allies"); |
| 335 | + return 0; |
| 336 | + } else if (!canAfford(sendingPlayerName, resourceName, amount)) { |
| 337 | + ResourcesClientboundPacket.warnInsufficientResources(sendingPlayerName, |
| 338 | + resourceName == ResourceName.FOOD, |
| 339 | + resourceName == ResourceName.WOOD, |
| 340 | + resourceName == ResourceName.ORE |
| 341 | + ); |
| 342 | + return 0; |
| 343 | + } else { |
| 344 | + addSubtractResources(new Resources(sendingPlayerName, |
| 345 | + resourceName == ResourceName.FOOD ? -amount : 0, |
| 346 | + resourceName == ResourceName.WOOD ? -amount : 0, |
| 347 | + resourceName == ResourceName.ORE ? -amount : 0 |
| 348 | + )); |
| 349 | + addSubtractResources(new Resources(receivingPlayerName, |
| 350 | + resourceName == ResourceName.FOOD ? -amount : 0, |
| 351 | + resourceName == ResourceName.WOOD ? -amount : 0, |
| 352 | + resourceName == ResourceName.ORE ? -amount : 0 |
291 | 353 | ));
|
292 |
| - String resString = null; |
293 | 354 | switch (resourceName) {
|
294 |
| - case FOOD -> resString = I18n.get("resources.reignofnether.food"); |
295 |
| - case WOOD -> resString = I18n.get("resources.reignofnether.wood"); |
296 |
| - case ORE -> resString = I18n.get("resources.reignofnether.ore"); |
297 |
| - } |
298 |
| - if (resString != null) { |
299 |
| - PlayerServerEvents.sendMessageToPlayer(playerGiving, I18n.get("server.resources.reignofnether.sent_resources", amount, resString, playerReceiving)); |
300 |
| - PlayerServerEvents.sendMessageToPlayer(playerReceiving, I18n.get("server.resources.reignofnether.received_resources", amount, resString, playerGiving)); |
| 355 | + case FOOD -> { |
| 356 | + PlayerServerEvents.sendMessageToPlayer(sendingPlayerName, "server.resources.reignofnether.sent_food", false, amount, receivingPlayerName); |
| 357 | + PlayerServerEvents.sendMessageToPlayer(receivingPlayerName, "server.resources.reignofnether.received_food", false, amount, sendingPlayerName); |
| 358 | + } |
| 359 | + case WOOD -> { |
| 360 | + PlayerServerEvents.sendMessageToPlayer(sendingPlayerName, "server.resources.reignofnether.sent_wood", false, amount, receivingPlayerName); |
| 361 | + PlayerServerEvents.sendMessageToPlayer(receivingPlayerName, "server.resources.reignofnether.received_wood", false, amount, sendingPlayerName); |
| 362 | + } |
| 363 | + case ORE -> { |
| 364 | + PlayerServerEvents.sendMessageToPlayer(sendingPlayerName, "server.resources.reignofnether.sent_ore", false, amount, receivingPlayerName); |
| 365 | + PlayerServerEvents.sendMessageToPlayer(receivingPlayerName, "server.resources.reignofnether.received_ore", false, amount, sendingPlayerName); |
| 366 | + } |
301 | 367 | }
|
302 |
| - } else { |
303 |
| - ResourcesClientboundPacket.warnInsufficientResources(playerGiving, |
304 |
| - resourceName == ResourceName.FOOD, |
305 |
| - resourceName == ResourceName.WOOD, |
306 |
| - resourceName == ResourceName.ORE |
307 |
| - ); |
| 368 | + return 1; |
308 | 369 | }
|
309 | 370 | }
|
310 | 371 |
|
|
0 commit comments