Skip to content

Commit

Permalink
Added matcher to match commands if we couldn't find any
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Jul 30, 2022
1 parent 2e28cc2 commit 4017d71
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.despical</groupId>
<artifactId>king-of-the-ladder</artifactId>
<version>2.3.1</version>
<version>2.3.2</version>
<name>King of the Ladder</name>

<properties>
Expand Down Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>com.github.Despical</groupId>
<artifactId>CommandFramework</artifactId>
<version>1.1.0</version>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>me.clip</groupId>
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/me/despical/kotl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ public void onEnable() {

private boolean validateIfPluginShouldStart() {
if (!VersionResolver.isCurrentBetween(VersionResolver.ServerVersion.v1_8_R1, VersionResolver.ServerVersion.v1_19_R1)) {
LogUtils.sendConsoleMessage("&cYour server version is not supported by King of the Ladder!");
LogUtils.sendConsoleMessage("&cSadly, we must shut off. Maybe you consider changing your server version?");
LogUtils.sendConsoleMessage("[KOTL] &cYour server version is not supported by King of the Ladder!");
LogUtils.sendConsoleMessage("[KOTL] &cSadly, we must shut off. Maybe you consider changing your server version?");
return false;
}

if (JavaVersion.getCurrentVersion().isAt(JavaVersion.JAVA_8)) {
LogUtils.sendConsoleMessage("&cThis plugin won't support Java 8 in future updates.");
LogUtils.sendConsoleMessage("&cSo, maybe consider to update your version, right?");
LogUtils.sendConsoleMessage("[KOTL] &cThis plugin won't support Java 8 in future updates.");
LogUtils.sendConsoleMessage("[KOTL] &cSo, maybe consider to update your version, right?");
}

try {
Class.forName("org.spigotmc.SpigotConfig");
} catch (Exception e) {
LogUtils.sendConsoleMessage("&cYour server software is not supported by King of the Ladder!");
LogUtils.sendConsoleMessage("&cWe support only Spigot and Spigot forks only! Shutting off...");
LogUtils.sendConsoleMessage("[KOTL] &cYour server software is not supported by King of the Ladder!");
LogUtils.sendConsoleMessage("[KOTL] &cWe support only Spigot and Spigot forks only! Shutting off...");
return false;
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/me/despical/kotl/command/AdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ public void reloadCommand(CommandArguments arguments) {

@Command(
name = "kotl.help",
permission = "kotl.admin.help",
usage = "/kotl help",
desc = "Sends all of the command and their usages"
permission = "kotl.admin.help"
)
public void helpCommand(CommandArguments arguments) {
arguments.sendMessage("");
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/me/despical/kotl/command/PlayerCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

import me.despical.commandframework.Command;
import me.despical.commandframework.CommandArguments;
import me.despical.commons.string.StringMatcher;
import me.despical.kotl.ConfigPreferences;
import me.despical.kotl.Main;
import me.despical.kotl.api.StatsStorage;
import me.despical.kotl.handler.ChatManager;
import me.despical.kotl.user.User;
import me.despical.kotl.user.data.MysqlManager;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* @author Despical
Expand All @@ -34,6 +36,17 @@ public PlayerCommands(Main plugin) {
this.plugin = plugin;
this.chatManager = plugin.getChatManager();
this.plugin.getCommandFramework().registerCommands(this);
this.plugin.getCommandFramework().setAnyMatch(arguments -> {
if (arguments.isArgumentsEmpty()) return;

String label = arguments.getLabel(), arg = arguments.getArgument(0);

List<StringMatcher.Match> matches = StringMatcher.match(arg, plugin.getCommandFramework().getCommands().stream().map(cmd -> cmd.name().replace(label + ".", "")).collect(Collectors.toList()));

if (!matches.isEmpty()) {
arguments.sendMessage(chatManager.prefixedMessage("commands.did_you_mean").replace("%command%", label + " " + matches.get(0).getMatch()));
}
});
}

@Command(
Expand All @@ -45,8 +58,8 @@ public void mainCommand(CommandArguments arguments) {

if (arguments.hasPermission("kotl.admin")) {
arguments.sendMessage(chatManager.coloredRawMessage("&3Commands: &b/" + arguments.getLabel() + " help"));
arguments.sendMessage(plugin.getChatManager().coloredRawMessage("&3If you liked this version then consider buying the premium one with better performance and additional features."));
arguments.sendMessage(plugin.getChatManager().coloredRawMessage("&3>> &bhttps://www.spigotmc.org/resources/king-of-the-ladder-premium-1-8-1-19.102644/"));
arguments.sendMessage(chatManager.coloredRawMessage("&3If you liked this version then consider buying the premium one with better performance and additional features."));
arguments.sendMessage(chatManager.coloredRawMessage("&3>> &bhttps://www.spigotmc.org/resources/king-of-the-ladder-premium-1-8-1-19.102644/"));
}
}
}
Expand All @@ -56,7 +69,7 @@ public void mainCommand(CommandArguments arguments) {
senderType = Command.SenderType.PLAYER
)
public void statsCommand(CommandArguments arguments) {
Player sender = arguments.getSender(), player = !arguments.isArgumentsEmpty() ? Bukkit.getPlayer(arguments.getArgument(0)) : sender;
Player sender = arguments.getSender(), player = !arguments.isArgumentsEmpty() ? plugin.getServer().getPlayer(arguments.getArgument(0)) : sender;

if (player == null) {
arguments.sendMessage(chatManager.prefixedMessage("commands.player_not_found"));
Expand All @@ -78,8 +91,7 @@ public void statsCommand(CommandArguments arguments) {
}

@Command(
name = "kotl.top",
senderType = Command.SenderType.PLAYER
name = "kotl.top"
)
public void leaderboardCommand(CommandArguments arguments) {
if (arguments.isArgumentsEmpty()) {
Expand All @@ -95,7 +107,7 @@ public void leaderboardCommand(CommandArguments arguments) {
}

private void printLeaderboard(CommandSender sender, StatsStorage.StatisticType statisticType) {
sender.sendMessage(plugin.getChatManager().message("commands.statistics.header"));
sender.sendMessage(chatManager.message("commands.statistics.header"));

final Map<UUID, Integer> stats = StatsStorage.getStats(statisticType);
final String statistic = StringUtils.capitalize(statisticType.name().toLowerCase(java.util.Locale.ENGLISH).replace("_", " "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ private void sendProTip(Player player) {
case 8:
tip = "Check out our other plugins: https://spigotmc.org/resources/authors/despical.615094/";
break;
case 9:
tip = "You liked KOTL? Check out my other plugin that you can like it too: https://spigotmc.org/resources/whack-me-1-9-1-19.103482/";
break;
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# +---------------------------------------------------------+ #
# | King of the Ladder by Despical (Version 2.3.1) | #
# | King of the Ladder by Despical (Version 2.3.2) | #
# | | #
# | https://www.patreon.com/despical | #
# | Support us with becoming Patron and use Patron benefits | #
Expand Down

0 comments on commit 4017d71

Please sign in to comment.