Add isCommandSentByConsole method

This commit is contained in:
JeremyStar™ 2024-04-30 22:46:21 +02:00
parent 027b86d5f2
commit 78925ccb9a
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 18 additions and 18 deletions

View file

@ -35,10 +35,7 @@ public class HomeCommand extends CommandBase {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
return true;
}
if (Miscellaneous.isCommandSentByConsole(sender, true)) return true;
Player player = (Player) sender;
Location coords = player.getRespawnLocation();
if (coords == null) {

View file

@ -29,7 +29,6 @@ import de.pickshadow.plugin.utils.TabCompletionHelper;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -44,10 +43,7 @@ public class MsgCommand extends CommandBase {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
return true;
}
if (Miscellaneous.isCommandSentByConsole(sender, true)) return true;
// Define shared variables
Player player = (Player) sender;

View file

@ -27,7 +27,6 @@ import de.pickshadow.plugin.utils.Miscellaneous;
import de.pickshadow.plugin.utils.TabCompletionHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -77,10 +76,7 @@ public class PluginCommand extends CommandBase {
gcThread.start();
break;
case "playerinfo":
if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
break;
}
if (Miscellaneous.isCommandSentByConsole(sender, true)) break;
Player player = (Player) sender;
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_PLAYERINFO.replace("%playerdata%", ObjHolder.playerDataLoader.getPlayerData(player.getUniqueId()).convertToString())));

View file

@ -45,10 +45,7 @@ public class TrollCommand extends CommandBase {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof ConsoleCommandSender) {
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
return true;
}
if (Miscellaneous.isCommandSentByConsole(sender, true)) return true;
Player player = (Player) sender;
if (args.length < 2) {
player.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR));

View file

@ -24,6 +24,8 @@ import de.pickshadow.plugin.classes.Types;
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.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;
import java.lang.ref.WeakReference;
@ -66,6 +68,18 @@ public class Miscellaneous {
ObjHolder.logger.warn(Miscellaneous.class, "Garbage collector finished");
}
// Check if command was sent by console
public static boolean isCommandSentByConsole(CommandSender sender) {
return isCommandSentByConsole(sender, false);
}
public static boolean isCommandSentByConsole(CommandSender sender, boolean issueError) {
if (sender instanceof ConsoleCommandSender) {
if (issueError) sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
return true;
}
return false;
}
// Data type conversion
/// Bytes <-> Mebibytes
public static double byteToMib(int bytes, boolean flatten) {