|
| 1 | +package it.renvins.serverpulse.velocity.commands; |
| 2 | + |
| 3 | +import com.mojang.brigadier.Command; |
| 4 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 5 | +import com.mojang.brigadier.context.CommandContext; |
| 6 | +import com.velocitypowered.api.command.BrigadierCommand; |
| 7 | +import com.velocitypowered.api.command.CommandSource; |
| 8 | +import it.renvins.serverpulse.api.ServerPulseProvider; |
| 9 | +import it.renvins.serverpulse.common.utils.ChatUtils; |
| 10 | +import it.renvins.serverpulse.velocity.config.VelocityConfiguration; |
| 11 | +import lombok.RequiredArgsConstructor; |
| 12 | +import net.kyori.adventure.text.Component; |
| 13 | +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; |
| 14 | + |
| 15 | +@RequiredArgsConstructor |
| 16 | +public class ServerPulseCommand { |
| 17 | + |
| 18 | + private final VelocityConfiguration config; |
| 19 | + |
| 20 | + public BrigadierCommand createCommand() { |
| 21 | + // Create the main command |
| 22 | + LiteralArgumentBuilder<CommandSource> command = |
| 23 | + BrigadierCommand.literalArgumentBuilder("serverpulsevelocity") |
| 24 | + .executes(context -> { |
| 25 | + CommandSource source = context.getSource(); |
| 26 | + |
| 27 | + Component message = LegacyComponentSerializer.legacySection().deserialize( |
| 28 | + ChatUtils.format(config.getConfig().getString("messages.usage"))); |
| 29 | + source.sendMessage(message); |
| 30 | + |
| 31 | + return Command.SINGLE_SUCCESS; |
| 32 | + }); |
| 33 | + |
| 34 | + command.then(BrigadierCommand.literalArgumentBuilder("reload") |
| 35 | + .requires(source -> source.hasPermission("serverpulse.reload")) |
| 36 | + .executes(this::executeReload) |
| 37 | + ); |
| 38 | + |
| 39 | + command.then(BrigadierCommand.literalArgumentBuilder("status") |
| 40 | + .requires(source -> source.hasPermission("serverpulse.status")) |
| 41 | + .executes(this::executeStatus) |
| 42 | + ); |
| 43 | + |
| 44 | + return new BrigadierCommand(command.build()); |
| 45 | + } |
| 46 | + |
| 47 | + private int executeReload(CommandContext<CommandSource> context) { |
| 48 | + CommandSource source = context.getSource(); |
| 49 | + |
| 50 | + Component message; |
| 51 | + if (config.load()) { |
| 52 | + message = LegacyComponentSerializer.legacySection().deserialize( |
| 53 | + ChatUtils.format(config.getConfig().getString("messages.reloadConfig"))); |
| 54 | + } else { |
| 55 | + message = LegacyComponentSerializer.legacySection().deserialize( |
| 56 | + ChatUtils.format(config.getConfig().getString("messages.reloadConfigError"))); |
| 57 | + } |
| 58 | + |
| 59 | + source.sendMessage(message); |
| 60 | + return Command.SINGLE_SUCCESS; |
| 61 | + } |
| 62 | + |
| 63 | + private int executeStatus(CommandContext<CommandSource> context) { |
| 64 | + CommandSource source = context.getSource(); |
| 65 | + |
| 66 | + Component message = LegacyComponentSerializer.legacySection().deserialize( |
| 67 | + ChatUtils.format(ServerPulseProvider.get().getDatabaseService().isConnected() ? |
| 68 | + config.getConfig().getString("messages.statusConnected") : |
| 69 | + config.getConfig().getString("messages.statusNotConnected"))); |
| 70 | + |
| 71 | + source.sendMessage(message); |
| 72 | + return Command.SINGLE_SUCCESS; |
| 73 | + } |
| 74 | +} |
0 commit comments