Skip to content

Commit

Permalink
⚒ | Added autodetect command
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Jul 27, 2021
1 parent 81db250 commit 9bfd2d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import me.lorenzo0111.multilang.commands.executor.SubcommandExecutor;
import me.lorenzo0111.multilang.commands.subcommands.admin.AdminHelpCommand;
import me.lorenzo0111.multilang.commands.subcommands.admin.DebugCommand;
import me.lorenzo0111.multilang.commands.subcommands.admin.DetectCommand;
import me.lorenzo0111.multilang.commands.subcommands.admin.ReloadCommand;
import me.lorenzo0111.pluginslib.command.Customization;
import me.lorenzo0111.pluginslib.command.ICommand;
Expand All @@ -51,6 +52,7 @@ public AdminLangCommand(MultiLangPlugin plugin, String command, @Nullable Custom
super(plugin, command, customization);

this.addSubcommand(new ReloadCommand(this));
this.addSubcommand(new DetectCommand(this));
this.addSubcommand(new DebugCommand(this));
try {
Field subcommands = this.getClass().getSuperclass().getDeclaredField("subcommands");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class EditCommand extends SubCommand {

Expand Down Expand Up @@ -82,7 +83,7 @@ public void handleSubcommand(CommandSender commandSender, String[] args) {
setLang(player,lang,this);
}

public static void setLang(LocalizedPlayer player, String localeName, SubCommand subcommand) {
public static void setLang(@NotNull LocalizedPlayer player, String localeName, SubCommand subcommand) {
if (player.getLocale().getName().equalsIgnoreCase(localeName)) {
player.getPlayer().sendMessage(subcommand.format(MessagesManager.get("already")));
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package me.lorenzo0111.multilang.commands.subcommands.admin;

import me.lorenzo0111.multilang.MultiLangPlugin;
import me.lorenzo0111.multilang.api.objects.LocalizedPlayer;
import me.lorenzo0111.multilang.commands.SubCommand;
import me.lorenzo0111.multilang.commands.subcommands.EditCommand;
import me.lorenzo0111.multilang.utils.Reflection;
import me.lorenzo0111.pluginslib.command.ICommand;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class DetectCommand extends SubCommand {

public DetectCommand(ICommand<MultiLangPlugin> command) {
super(command);
}

@Override
public String getName() {
return "detect";
}

@Override
public String getDescription() {
return "Force language auto-detection for a user";
}

@Override
public void handleSubcommand(CommandSender sender, String[] args) {
if (args.length != 2) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getPlugin().getConfig("prefix") + "&cTry using /alang detect (Player)"));
return;
}

Player target = Bukkit.getPlayer(args[1]);
if (target == null || !target.isOnline()) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getPlugin().getConfig("prefix") + "&cThis user is not online"));
return;
}

LocalizedPlayer player = LocalizedPlayer.from(target);
EditCommand.setLang(player, Reflection.getLocale(target), this);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getPlugin().getConfig("prefix") + "&7Locale autodetected to &9" + player.getLocale().getName()));
}
}

0 comments on commit 9bfd2d8

Please sign in to comment.