aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java
blob: 669a9786969ea4d911261c05ecd07afc94deb0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2021 - 2023 Elytrium
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.elytrium.limboauth.command;

import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.elytrium.java.commons.mc.serialization.Serializer;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

public class LimboAuthCommand implements SimpleCommand {

  private static final List<Component> HELP_MESSAGE = List.of(
      Component.text("This server is using LimboAuth and LimboAPI.", NamedTextColor.YELLOW),
      Component.text("(C) 2021 - 2023 Elytrium", NamedTextColor.YELLOW),
      Component.text("https://elytrium.net/github/", NamedTextColor.GREEN),
      Component.empty()
  );
  private static final Map<String, Component> SUBCOMMANDS = Map.of(
      /*
      "serverstats", Component.textOfChildren(
          Component.text("  /limboauth serverstats", NamedTextColor.GREEN),
          Component.text(" - ", NamedTextColor.DARK_GRAY),
          Component.text("Query for server stats.", NamedTextColor.YELLOW)
      ),
      "playerstats", Component.textOfChildren(
          Component.text("  /limboauth playerstats <player>", NamedTextColor.GREEN),
          Component.text(" - ", NamedTextColor.DARK_GRAY),
          Component.text("Query for stats of specified player.", NamedTextColor.YELLOW)
      ),
      */
      "reload", Component.textOfChildren(
          Component.text("  /limboauth reload", NamedTextColor.GREEN),
          Component.text(" - ", NamedTextColor.DARK_GRAY),
          Component.text("Reload config.", NamedTextColor.YELLOW)
      )
  );
  private static final Component AVAILABLE_SUBCOMMANDS_MESSAGE = Component.text("Available subcommands:", NamedTextColor.WHITE);
  private static final Component NO_AVAILABLE_SUBCOMMANDS_MESSAGE = Component.text("There is no available subcommands for you.", NamedTextColor.WHITE);

  private final LimboAuth plugin;

  public LimboAuthCommand(LimboAuth plugin) {
    this.plugin = plugin;
  }

  @Override
  public List<String> suggest(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();

    if (args.length == 0) {
      return SUBCOMMANDS.keySet().stream()
          .filter(cmd -> source.hasPermission("limboauth.admin." + cmd))
          .collect(Collectors.toList());
    } else if (args.length == 1) {
      String argument = args[0];
      return SUBCOMMANDS.keySet().stream()
          .filter(cmd -> source.hasPermission("limboauth.admin." + cmd))
          .filter(str -> str.regionMatches(true, 0, argument, 0, argument.length()))
          .collect(Collectors.toList());
      /*
    } else if (args[0].equalsIgnoreCase("playerstats") && source.hasPermission("limboauth.admin.playerstats")) {
      return SuggestUtils.suggestPlayers(this.plugin.getServer(), args, 2);
      */
    } else {
      return ImmutableList.of();
    }
  }

  @Override
  public void execute(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();

    int argsAmount = args.length;
    if (argsAmount > 0) {
      String command = args[0];
      Serializer serializer = LimboAuth.getSerializer();
      if (argsAmount == 1) {
        if (command.equalsIgnoreCase("reload") && source.hasPermission("limboauth.admin.reload")) {
          try {
            this.plugin.reload();
            source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD));
          } catch (Exception e) {
            e.printStackTrace();
            source.sendMessage(serializer.deserialize(Settings.IMP.MAIN.STRINGS.RELOAD_FAILED));
          }

          return;
        }
        /*
        else if (command.equalsIgnoreCase("serverstats") && source.hasPermission("limboauth.admin.serverstats")) {
          return;
        } else if (command.equalsIgnoreCase("playerstats") && source.hasPermission("limboauth.admin.playerstats")) {
          source.sendMessage(Component.text("Please specify a player."));
          return;
        }
        */
      }
      /*
      else if (argsAmount == 2) {
        if (command.equalsIgnoreCase("playerstats") && source.hasPermission("limboauth.admin.playerstats")) {
          RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.plugin.getPlayerDao(), args[1]);
          if (player == null) {
            source.sendMessage(Component.text("Игрок даезент екзистс."));
          } else {
            source.sendMessage(Component.text("Стата геймера под ником {player}:"));
            source.sendMessage(Component.empty());
            source.sendMessage(Component.text("Ласт айпи: " + player.getIP()));
            source.sendMessage(Component.text("2fa: " + (player.getTotpToken().isEmpty() ? "Нет" : "Есть")));
          }

          return;
        }
      }
      */
    }

    this.showHelp(source);
  }

  private void showHelp(CommandSource source) {
    for (Component component : HELP_MESSAGE) {
      source.sendMessage(component);
    }
    List<Map.Entry<String, Component>> availableSubcommands = SUBCOMMANDS.entrySet().stream()
        .filter(command -> source.hasPermission("limboauth.admin." + command.getKey()))
        .collect(Collectors.toList());
    if (availableSubcommands.size() > 0) {
      source.sendMessage(AVAILABLE_SUBCOMMANDS_MESSAGE);
      availableSubcommands.forEach(command -> source.sendMessage(command.getValue()));
    } else {
      source.sendMessage(NO_AVAILABLE_SUBCOMMANDS_MESSAGE);
    }
  }
}