Skip to content

Commit

Permalink
add console command support
Browse files Browse the repository at this point in the history
  • Loading branch information
TWME-TW committed May 4, 2024
1 parent 0a18d45 commit 865013f
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.twme</groupId>
<artifactId>DebugStickPro</artifactId>
<version>0.4.4</version>
<version>0.4.5</version>
<packaging>jar</packaging>

<name>DebugStickPro</name>
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/dev/twme/debugstickpro/commands/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import dev.twme.debugstickpro.commands.subcommands.HelpCommand;
import dev.twme.debugstickpro.commands.subcommands.ModeCommand;
import dev.twme.debugstickpro.commands.subcommands.ReloadCommand;
import dev.twme.debugstickpro.commands.subcommands.console.ConsoleGiveCommand;
import dev.twme.debugstickpro.commands.subcommands.console.ConsoleHelpCommand;
import dev.twme.debugstickpro.commands.subcommands.console.ConsoleReloadCommand;
import dev.twme.debugstickpro.localization.I18n;
import dev.twme.debugstickpro.localization.Lang;
import net.kyori.adventure.text.Component;
Expand All @@ -19,10 +22,12 @@ public class MainCommand implements CommandExecutor {
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {

if (!(commandSender instanceof Player)) {
MiniMessage mm = MiniMessage.miniMessage();
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.YouAreNotPlayer));
commandSender.sendMessage(parsed);
return true;
return switch (args[0].toLowerCase()) {
case "help" -> ConsoleHelpCommand.onHelpCommand(commandSender, args);
case "give" -> ConsoleGiveCommand.onGiveCommand(commandSender, args);
case "reload" -> ConsoleReloadCommand.onReloadCommand(commandSender, args);
default -> false;
};
}

Player player = (Player) commandSender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
public class MainCommandTabComplete implements TabCompleter {
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
if (!(commandSender instanceof Player)) {
return null;
}

ArrayList<String> list = new ArrayList<>();
Player player = (Player) commandSender;
if (args.length == 1) {
Expand All @@ -28,8 +26,11 @@ public class MainCommandTabComplete implements TabCompleter {
if (player.hasPermission("debugstickpro.give")) {
list.add("give");
}
if (player.hasPermission("debugstickpro.mode")) {
list.add("mode");

if (commandSender instanceof Player) {
if (player.hasPermission("debugstickpro.mode")) {
list.add("mode");
}
}
return list;
}
Expand All @@ -38,9 +39,8 @@ public class MainCommandTabComplete implements TabCompleter {

if ("give".equalsIgnoreCase(args[0])) {
return null;
}
} else if (args[0].equalsIgnoreCase("mode")) {

if (args[0].equalsIgnoreCase("mode")) {
if (player.hasPermission("debugstickpro.mode")) {
list.add("classic");
}
Expand All @@ -51,6 +51,8 @@ public class MainCommandTabComplete implements TabCompleter {
list.add("freeze");
}
return list;
} else if (!(commandSender instanceof Player)) {
return null;
}
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static boolean onGiveCommand(Player player, String[] args) {

// Give another player a debug stick
onlinePlayer.getInventory().addItem(DebugStickItem.getDebugStickItem());
Component parsed = mm.deserialize(I18n.string(playerUUID, Lang.CommandsMessages.Give.Success).replace("%player%", player.getName()));
Component parsed = mm.deserialize(I18n.string(playerUUID, Lang.CommandsMessages.Give.Success).replace("%player%", onlinePlayer.getName()));
player.sendMessage(parsed);

if (DebugStickItem.checkPlayer(onlinePlayer)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dev.twme.debugstickpro.commands.subcommands.console;

import dev.twme.debugstickpro.localization.I18n;
import dev.twme.debugstickpro.localization.Lang;
import dev.twme.debugstickpro.playerdata.PlayerDataManager;
import dev.twme.debugstickpro.utils.DebugStickItem;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ConsoleGiveCommand {
public static boolean onGiveCommand(CommandSender sender, String[] args) {
MiniMessage mm = MiniMessage.miniMessage();

if (!sender.hasPermission("debugstickpro.give")) {
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.NoPermission));
sender.sendMessage(parsed);
return true;
}

// Directly give the player a debug stick
if (args.length == 1) {
if (!(sender instanceof Player)) {
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.YouAreNotPlayer));
sender.sendMessage(parsed);
return true;
}
return true;
}

if (args.length != 2) {
return false;
}

Player onlinePlayer = Bukkit.getPlayerExact(args[1]);

// Check if player not exist
if (onlinePlayer == null) {
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.Give.NoPlayer));
sender.sendMessage(parsed);
return true;
}

// Give another player a debug stick
onlinePlayer.getInventory().addItem(DebugStickItem.getDebugStickItem());
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.Give.Success).replace("%player%", onlinePlayer.getName()));
sender.sendMessage(parsed);

if (DebugStickItem.checkPlayer(onlinePlayer)) {
PlayerDataManager.addPlayerToDisplayList(onlinePlayer.getUniqueId());
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.twme.debugstickpro.commands.subcommands.console;

import dev.twme.debugstickpro.localization.I18n;
import dev.twme.debugstickpro.localization.Lang;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;

public class ConsoleHelpCommand {
public static boolean onHelpCommand(CommandSender sender, String[] args) {
MiniMessage mm = MiniMessage.miniMessage();

if (args.length > 1) {
return false;
}
if (!sender.hasPermission("debugstickpro.help")) {
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.NoPermission));
sender.sendMessage(parsed);
return true;
}
for (String message : I18n.list(Lang.CommandsMessages.Help.HelpMessage)) {
sender.sendMessage(mm.deserialize(message));
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.twme.debugstickpro.commands.subcommands.console;

import dev.twme.debugstickpro.DebugStickPro;
import dev.twme.debugstickpro.localization.I18n;
import dev.twme.debugstickpro.localization.Lang;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;

public class ConsoleReloadCommand {
public static boolean onReloadCommand(CommandSender sender, String[] args) {
MiniMessage mm = MiniMessage.miniMessage();

if (args.length > 1) {
return false;
}
if (!sender.hasPermission("debugstickpro.reload")) {
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.NoPermission));
sender.sendMessage(parsed);
return true;
}
DebugStickPro.getInstance().onReload();
Component parsed = mm.deserialize(I18n.string(Lang.CommandsMessages.Reload.Success));
sender.sendMessage(parsed);
return true;
}
}
7 changes: 7 additions & 0 deletions src/main/java/dev/twme/debugstickpro/localization/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ public static List<String> list(UUID playerUUID, String key) {

return lang.getList(key);
}

public static List<String> list(String key) {

LangFileReader lang = LangFileManager.getLang(ConfigFile.Language.DefaultLanguage);

return lang.getList(key);
}
}

0 comments on commit 865013f

Please sign in to comment.