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 @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof ConsoleCommandSender) { if (Miscellaneous.isCommandSentByConsole(sender, true)) return true;
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
return true;
}
Player player = (Player) sender; Player player = (Player) sender;
Location coords = player.getRespawnLocation(); Location coords = player.getRespawnLocation();
if (coords == null) { if (coords == null) {

View file

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

View file

@ -27,7 +27,6 @@ import de.pickshadow.plugin.utils.Miscellaneous;
import de.pickshadow.plugin.utils.TabCompletionHelper; import de.pickshadow.plugin.utils.TabCompletionHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -77,10 +76,7 @@ public class PluginCommand extends CommandBase {
gcThread.start(); gcThread.start();
break; break;
case "playerinfo": case "playerinfo":
if (sender instanceof ConsoleCommandSender) { if (Miscellaneous.isCommandSentByConsole(sender, true)) break;
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
break;
}
Player player = (Player) sender; Player player = (Player) sender;
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_PLAYERINFO.replace("%playerdata%", ObjHolder.playerDataLoader.getPlayerData(player.getUniqueId()).convertToString()))); 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 @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof ConsoleCommandSender) { if (Miscellaneous.isCommandSentByConsole(sender, true)) return true;
sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR));
return true;
}
Player player = (Player) sender; Player player = (Player) sender;
if (args.length < 2) { if (args.length < 2) {
player.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); 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.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -66,6 +68,18 @@ public class Miscellaneous {
ObjHolder.logger.warn(Miscellaneous.class, "Garbage collector finished"); 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 // Data type conversion
/// Bytes <-> Mebibytes /// Bytes <-> Mebibytes
public static double byteToMib(int bytes, boolean flatten) { public static double byteToMib(int bytes, boolean flatten) {