diff --git a/src/main/java/de/pickshadow/plugin/classes/CommandBase.java b/src/main/java/de/pickshadow/plugin/classes/CommandBase.java index b5d5358..4b60387 100644 --- a/src/main/java/de/pickshadow/plugin/classes/CommandBase.java +++ b/src/main/java/de/pickshadow/plugin/classes/CommandBase.java @@ -55,9 +55,15 @@ public abstract class CommandBase implements CommandExecutor { } } - // Command code + // Command code (from Bukkit, handled internally) @Override - public abstract 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) { + getCommand(new Interactable(sender), command, label, args); + return true; + } + + // Command code (abstracted) + public abstract void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args); // Command completion public abstract TabCompleter getCompletion(); diff --git a/src/main/java/de/pickshadow/plugin/classes/Interactable.java b/src/main/java/de/pickshadow/plugin/classes/Interactable.java new file mode 100644 index 0000000..da352a9 --- /dev/null +++ b/src/main/java/de/pickshadow/plugin/classes/Interactable.java @@ -0,0 +1,140 @@ +package de.pickshadow.plugin.classes; + +import de.pickshadow.plugin.base.ObjHolder; +import de.pickshadow.plugin.classes.exceptions.NotAPlayerException; +import de.pickshadow.plugin.utils.Miscellaneous; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.WeatherType; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.permissions.Permission; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Locale; +import java.util.UUID; + +public class Interactable { + // Data + private UUID uuid; + private Player player; + private boolean isConsole; + + // Constructors + public Interactable(@NotNull UUID uuid) { + this.uuid = uuid; + isConsole = false; + } + public Interactable(@NotNull Player player) { + uuid = player.getUniqueId(); + this.player = player; + isConsole = false; + } + public Interactable(@NotNull CommandSender sender) { + if (sender instanceof ConsoleCommandSender) { + isConsole = true; + } else { + player = (Player) sender; + uuid = player.getUniqueId(); + } + } + + // Getters + public boolean isConsole() { + return isConsole; + } + public UUID getUniqueId() throws NotAPlayerException { + if (isConsole) throw new NotAPlayerException(); + return uuid; + } + public boolean hasPermission(Permission permission) { + if (isConsole) return true; + else return player.hasPermission(permission); + } + public boolean hasPermission(String permission) { + if (isConsole) return true; + else return player.hasPermission(permission); + } + public String getClientBrand() { + if (isConsole) return "tty"; + else return player.getClientBrandName(); + } + public Component getDisplayName() { + if (isConsole) return Miscellaneous.format("", Types.FormatType.NONE); + else return player.displayName(); + } + public Locale getLocale() { + if (isConsole) return Locale.US; + else return player.locale(); + } + public String getName() { + if (isConsole) return "CONSOLE"; + else return player.getName(); + } + public int getPing() { + if (isConsole) return 0; + else return player.getPing(); + } + public WeatherType getWeather() throws NotAPlayerException { + if (isConsole) throw new NotAPlayerException(); + else { + if (player.getWorld().isClearWeather()) return WeatherType.CLEAR; + else if (player.getWorld().isThundering()) return WeatherType.DOWNFALL; + else { + ObjHolder.logger.crash(getClass(), "Invalid weather type (not clear or downfall)"); + return null; + } + } + } + public Location getRespawnLocation() { + if (isConsole) return new Location(Bukkit.getWorlds().get(0), 0, -64, 0); + else return player.getRespawnLocation(); + } + public float getFallDistance() { + if (isConsole) return 0f; + else return player.getFallDistance(); + } + public World getWorld() throws NotAPlayerException { + if (isConsole) throw new NotAPlayerException(); + else return player.getWorld(); + } + + // Setters + public void setDisplayName(@Nullable Component name) { + if (!isConsole) player.displayName(name); + } + public void setRespawnLocation(@NotNull Location location) { + setRespawnLocation(location, false); + } + public void setRespawnLocation(@NotNull Location location, boolean force) { + if (!isConsole) player.setRespawnLocation(location, force); + } + public void setFallDistance(float fallDistance) { + if (!isConsole) player.setFallDistance(fallDistance); + } + + // Other methods + public void message(Component message) { + if (isConsole) Bukkit.getConsoleSender().sendMessage(message); + else player.sendMessage(message); + } + public void message(String message) { + message(MiniMessage.miniMessage().deserialize(message)); + } + public void sudo(String chatMessage) { + if (isConsole) Bukkit.dispatchCommand(Bukkit.getConsoleSender(), chatMessage); + else player.chat(chatMessage); + } + public void teleport(Entity entity) { + teleport(entity.getLocation()); + } + public void teleport(Location location) { + if (!isConsole) player.teleport(location); + } +} diff --git a/src/main/java/de/pickshadow/plugin/classes/PlayerData.java b/src/main/java/de/pickshadow/plugin/classes/PlayerData.java index dcfe0c8..37a47ac 100644 --- a/src/main/java/de/pickshadow/plugin/classes/PlayerData.java +++ b/src/main/java/de/pickshadow/plugin/classes/PlayerData.java @@ -19,6 +19,7 @@ package de.pickshadow.plugin.classes; import de.pickshadow.plugin.base.ObjHolder; +import de.pickshadow.plugin.classes.exceptions.NotAPlayerException; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.jetbrains.annotations.ApiStatus; @@ -44,6 +45,9 @@ public class PlayerData { public PlayerData(@NotNull Player player) { playerId = player.getUniqueId(); } + public PlayerData(@NotNull Interactable intact) throws NotAPlayerException { + playerId = intact.getUniqueId(); + } // Used for loading player data off disk @ApiStatus.Internal public PlayerData() {} diff --git a/src/main/java/de/pickshadow/plugin/classes/exceptions/NotAPlayerException.java b/src/main/java/de/pickshadow/plugin/classes/exceptions/NotAPlayerException.java new file mode 100644 index 0000000..dc1ea05 --- /dev/null +++ b/src/main/java/de/pickshadow/plugin/classes/exceptions/NotAPlayerException.java @@ -0,0 +1,7 @@ +package de.pickshadow.plugin.classes.exceptions; + +public class NotAPlayerException extends Exception { + public NotAPlayerException() { + super(); + } +} diff --git a/src/main/java/de/pickshadow/plugin/commands/BroadcastCommand.java b/src/main/java/de/pickshadow/plugin/commands/BroadcastCommand.java index b341a07..d2a4092 100644 --- a/src/main/java/de/pickshadow/plugin/commands/BroadcastCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/BroadcastCommand.java @@ -20,11 +20,11 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.base.ObjHolder; import de.pickshadow.plugin.classes.CommandBase; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.Types; import de.pickshadow.plugin.utils.Miscellaneous; import org.bukkit.Bukkit; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.jetbrains.annotations.NotNull; @@ -34,8 +34,7 @@ public class BroadcastCommand extends CommandBase { commandNames = new String[]{ "broadcast" }; } - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { StringBuilder message = new StringBuilder(); for (String arg : args) { if (message.isEmpty()) { @@ -45,7 +44,6 @@ public class BroadcastCommand extends CommandBase { } } Bukkit.broadcast(Miscellaneous.format(String.valueOf(message), Types.FormatType.NONE, true)); - return true; } public TabCompleter getCompletion() { diff --git a/src/main/java/de/pickshadow/plugin/commands/ClearChatCommand.java b/src/main/java/de/pickshadow/plugin/commands/ClearChatCommand.java index 3189c28..343cdc6 100644 --- a/src/main/java/de/pickshadow/plugin/commands/ClearChatCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/ClearChatCommand.java @@ -1,13 +1,14 @@ package de.pickshadow.plugin.commands; +import de.pickshadow.plugin.base.ObjHolder; import de.pickshadow.plugin.base.Translation; import de.pickshadow.plugin.classes.CommandBase; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.TabCompletionHelper; import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.Bukkit; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -17,10 +18,8 @@ public class ClearChatCommand extends CommandBase { commandNames = new String[]{ "clearchat" }; } - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - for (Player player : Bukkit.getOnlinePlayers()) player.sendMessage(MiniMessage.miniMessage().deserialize("\n".repeat(10000)).append(Miscellaneous.format(Translation.CLEARCHAT.replace("%sender%", sender.getName())))); - return true; + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + for (Player player : Bukkit.getOnlinePlayers()) player.sendMessage(MiniMessage.miniMessage().deserialize("\n".repeat(10000)).append(Miscellaneous.format(Translation.CLEARCHAT.replace("%sender%", intact.getName())))); } @Override diff --git a/src/main/java/de/pickshadow/plugin/commands/DiscordCommand.java b/src/main/java/de/pickshadow/plugin/commands/DiscordCommand.java index 2f752b6..6bf004e 100644 --- a/src/main/java/de/pickshadow/plugin/commands/DiscordCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/DiscordCommand.java @@ -21,11 +21,11 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.classes.CommandBase; import de.pickshadow.plugin.base.Translation; import de.pickshadow.plugin.base.ObjHolder; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.Types; 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.TabCompleter; import org.jetbrains.annotations.NotNull; @@ -37,14 +37,9 @@ public class DiscordCommand extends CommandBase { commandNames = new String[]{ "discord" }; } - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (Objects.equals(ObjHolder.config.inviteLink, "")) { - sender.sendMessage(Miscellaneous.format(Translation.DISCORD_UNAVAILABLE, Types.FormatType.ERROR)); - } else { - sender.sendMessage(Miscellaneous.format(Translation.DISCORD_INVITE.replace("%invite%", ObjHolder.config.inviteLink))); - } - return true; + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (Objects.equals(ObjHolder.config.inviteLink, "")) intact.message(Miscellaneous.format(Translation.DISCORD_UNAVAILABLE, Types.FormatType.ERROR)); + else intact.message(Miscellaneous.format(Translation.DISCORD_INVITE.replace("%invite%", ObjHolder.config.inviteLink))); } public TabCompleter getCompletion() { diff --git a/src/main/java/de/pickshadow/plugin/commands/HomeCommand.java b/src/main/java/de/pickshadow/plugin/commands/HomeCommand.java index d5d61f6..ccd2a31 100644 --- a/src/main/java/de/pickshadow/plugin/commands/HomeCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/HomeCommand.java @@ -20,12 +20,12 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.classes.CommandBase; import de.pickshadow.plugin.base.Translation; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.Types; import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.TabCompletionHelper; import org.bukkit.Location; import org.bukkit.command.*; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; public class HomeCommand extends CommandBase { @@ -33,19 +33,15 @@ public class HomeCommand extends CommandBase { commandNames = new String[]{ "home"}; } - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (Miscellaneous.isCommandSentByConsole(sender, true)) return true; - Player player = (Player) sender; - Location coords = player.getRespawnLocation(); - if (coords == null) { - player.sendMessage(Miscellaneous.format(Translation.HOME_MISSING, Types.FormatType.ERROR)); - } else { - player.sendMessage(Miscellaneous.format(Translation.HOME_TELEPORT)); - player.setFallDistance(0f); - player.teleport(coords); + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (Miscellaneous.isCommandSentByConsole(intact, true)) return; + Location coords = intact.getRespawnLocation(); + if (coords == null) intact.message(Miscellaneous.format(Translation.HOME_MISSING, Types.FormatType.ERROR)); + else { + intact.message(Miscellaneous.format(Translation.HOME_TELEPORT)); + intact.setFallDistance(0f); + intact.teleport(coords); } - return true; } public TabCompleter getCompletion() { diff --git a/src/main/java/de/pickshadow/plugin/commands/MsgCommand.java b/src/main/java/de/pickshadow/plugin/commands/MsgCommand.java index 7bbd120..f3468b2 100644 --- a/src/main/java/de/pickshadow/plugin/commands/MsgCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/MsgCommand.java @@ -21,6 +21,7 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.BuildConfiguration; import de.pickshadow.plugin.base.ObjHolder; import de.pickshadow.plugin.classes.CommandBase; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.PlayerData; import de.pickshadow.plugin.base.Translation; import de.pickshadow.plugin.classes.Types; @@ -28,7 +29,6 @@ import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.TabCompletionHelper; import org.bukkit.Bukkit; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -42,11 +42,11 @@ public class MsgCommand extends CommandBase { } @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (Miscellaneous.isCommandSentByConsole(sender, true)) return true; + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (Miscellaneous.isCommandSentByConsole(intact, true)) return; // Define shared variables - Player player = (Player) sender; + Player player = (Player) intact; Player reciever; StringBuilder message = new StringBuilder(); @@ -56,19 +56,19 @@ public class MsgCommand extends CommandBase { // Check args size if (args.length < 2) { player.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); - return true; + return; } // Get reciever and check if online reciever = Bukkit.getPlayerExact(args[0]); if (reciever == null) { player.sendMessage(Miscellaneous.format(Translation.GLOBAL_PLAYER_NOT_ONLINE.replace("%player%", args[0]))); - return true; + return; } // Check if performing self messaging if (!BuildConfiguration.allowSelfMessaging && player.getUniqueId() == reciever.getUniqueId()) { player.sendMessage(Miscellaneous.format(Translation.MSG_NO_SELF_MESSAGING, Types.FormatType.ERROR)); - return true; + return; } // Update player data @@ -89,21 +89,21 @@ public class MsgCommand extends CommandBase { } // Send message - player.sendMessage(Miscellaneous.format(Translation.MSG_TO.replace("%sender%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); - if (!(!BuildConfiguration.allowSelfMessaging && player.getUniqueId() == reciever.getUniqueId())) reciever.sendMessage(Miscellaneous.format(Translation.MSG_FROM.replace("%sender%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); + player.sendMessage(Miscellaneous.format(Translation.MSG_TO.replace("%intact%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); + if (!(!BuildConfiguration.allowSelfMessaging && player.getUniqueId() == reciever.getUniqueId())) reciever.sendMessage(Miscellaneous.format(Translation.MSG_FROM.replace("%intact%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); break; case "reply": case "r": // Check args size if (args.length < 1) { player.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); - return true; + return; } // Get last messaged player PlayerData playerData = ObjHolder.playerDataLoader.getPlayerData(player.getUniqueId()); if (playerData.lastMessagedPlayer == null) { player.sendMessage(Miscellaneous.format(Translation.MSG_NO_LAST_MESSAGED_PLAYER, Types.FormatType.ERROR)); - return true; + return; } reciever = Bukkit.getPlayer(playerData.lastMessagedPlayer); if (reciever == null) { @@ -116,7 +116,7 @@ public class MsgCommand extends CommandBase { // Check if performing self messaging if (!BuildConfiguration.allowSelfMessaging && player.getUniqueId() == reciever.getUniqueId()) { player.sendMessage(Miscellaneous.format(Translation.MSG_NO_SELF_MESSAGING, Types.FormatType.ERROR)); - return true; + return; } // Build message string from args @@ -129,13 +129,12 @@ public class MsgCommand extends CommandBase { } // Send message - sender.sendMessage(Miscellaneous.format(Translation.MSG_TO.replace("%sender%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); - if (!(!BuildConfiguration.allowSelfMessaging && player.getUniqueId() == reciever.getUniqueId())) reciever.sendMessage(Miscellaneous.format(Translation.MSG_FROM.replace("%sender%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); + intact.message(Miscellaneous.format(Translation.MSG_TO.replace("%intact%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); + if (!(!BuildConfiguration.allowSelfMessaging && player.getUniqueId() == reciever.getUniqueId())) reciever.sendMessage(Miscellaneous.format(Translation.MSG_FROM.replace("%intact%", player.getName()).replace("%reciever%", reciever.getName()).replace("%message%", message), Types.FormatType.NONE, true)); break; default: logger.crash("Invalid command '" + label + "' for " + getClass().getName()); } - return true; } public TabCompleter getCompletion() { diff --git a/src/main/java/de/pickshadow/plugin/commands/PluginCommand.java b/src/main/java/de/pickshadow/plugin/commands/PluginCommand.java index a755c94..029020d 100644 --- a/src/main/java/de/pickshadow/plugin/commands/PluginCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/PluginCommand.java @@ -21,12 +21,12 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.classes.CommandBase; import de.pickshadow.plugin.base.Translation; import de.pickshadow.plugin.base.ObjHolder; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.Types; import de.pickshadow.plugin.classes.Configuration; 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.TabCompleter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -41,71 +41,70 @@ public class PluginCommand extends CommandBase { } @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (!sender.hasPermission("pickshadow.commands.pssp") || args.length < 1) { - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_INFO)); - return true; + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (!intact.hasPermission("pickshadow.commands.pssp") || args.length < 1) { + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_INFO)); + return; } switch (args[0]) { case "reloadconfig": - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_CONFIG)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_CONFIG)); Configuration newConfig = ObjHolder.configLoader.loadConfig(); if (newConfig == null) { - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_CONFIG_FAIL, Types.FormatType.ERROR)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_CONFIG_FAIL, Types.FormatType.ERROR)); } else { - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_CONFIG_SUCCESS)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_CONFIG_SUCCESS)); ObjHolder.config = newConfig; } break; case "reloaddata": - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_DATA)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_DATA)); UUID[] failedReloads = ObjHolder.playerDataLoader.reloadPlayerData(); if (failedReloads.length == 0) { - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_DATA_SUCCESS)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_DATA_SUCCESS)); } else { - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_DATA_FAIL.replace("%uuids%", Arrays.toString(failedReloads)), Types.FormatType.ERROR)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_RELOAD_DATA_FAIL.replace("%uuids%", Arrays.toString(failedReloads)), Types.FormatType.ERROR)); } break; case "gc": Thread gcThread = new Thread(() -> { - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_GARBAGECOLLECT)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_GARBAGECOLLECT)); Miscellaneous.gc(); - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_GARBAGECOLLECT_FINISHED)); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_GARBAGECOLLECT_FINISHED)); }); gcThread.start(); break; case "playerinfo": - if (Miscellaneous.isCommandSentByConsole(sender, true)) break; + if (Miscellaneous.isCommandSentByConsole(intact, true)) break; - Player player = (Player) sender; - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_PLAYERINFO.replace("%playerdata%", ObjHolder.playerDataLoader.getPlayerData(player.getUniqueId()).convertToString()))); + Player player = (Player) intact; + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_PLAYERINFO.replace("%playerdata%", ObjHolder.playerDataLoader.getPlayerData(player.getUniqueId()).convertToString()))); break; case "scheduler": if (args.length < 2) { - sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); - return true; + intact.message(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); + return; } switch (args[1]) { case "minutely": case "minute": - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "minutely"))); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "minutely"))); ObjHolder.scheduler.executeScheduler(Types.SchedulerType.MINUTELY); - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "minutely"))); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "minutely"))); break; case "hourly": case "hour": - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "hourly"))); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "hourly"))); ObjHolder.scheduler.executeScheduler(Types.SchedulerType.HOURLY); - sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "hourly"))); + intact.message(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "hourly"))); break; - default: sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); + default: intact.message(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); } break; - default: sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); + default: intact.message(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); } - return true; } public TabCompleter getCompletion() { diff --git a/src/main/java/de/pickshadow/plugin/commands/SystemInformationCommand.java b/src/main/java/de/pickshadow/plugin/commands/SystemInformationCommand.java index 3294aa9..c87df91 100644 --- a/src/main/java/de/pickshadow/plugin/commands/SystemInformationCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/SystemInformationCommand.java @@ -20,13 +20,13 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.classes.CommandBase; import de.pickshadow.plugin.base.Translation; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.Types; import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.TabCompletionHelper; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.jetbrains.annotations.NotNull; @@ -39,23 +39,23 @@ public class SystemInformationCommand extends CommandBase { } @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { switch (label) { case "sysinfo": case "info": case "stats": if (args.length == 0) { - sender.sendMessage(Objects.requireNonNull(getMessage(0))); + intact.message(Objects.requireNonNull(getMessage(0))); } else { switch (args[0]) { case "memory": - sender.sendMessage(Objects.requireNonNull(getMessage(1))); + intact.message(Objects.requireNonNull(getMessage(1))); break; case "performance": - sender.sendMessage(Objects.requireNonNull(getMessage(2))); + intact.message(Objects.requireNonNull(getMessage(2))); break; default: - sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()))); + intact.message(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()))); break; } } @@ -63,19 +63,18 @@ public class SystemInformationCommand extends CommandBase { case "ram": case "mem": case "memory": - sender.sendMessage(Objects.requireNonNull(getMessage(1))); + intact.message(Objects.requireNonNull(getMessage(1))); break; case "tps": case "mspt": case "perf": - sender.sendMessage(Objects.requireNonNull(getMessage(2))); + intact.message(Objects.requireNonNull(getMessage(2))); break; default: - sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_EXCEPTION, Types.FormatType.ERROR)); + intact.message(Miscellaneous.format(Translation.GLOBAL_EXCEPTION, Types.FormatType.ERROR)); logger.error("Invalid label \"" + label + "\""); break; } - return true; } private Component getMessage(int messageType) { diff --git a/src/main/java/de/pickshadow/plugin/commands/ToggleDownfallCommand.java b/src/main/java/de/pickshadow/plugin/commands/ToggleDownfallCommand.java index 1792582..a206859 100644 --- a/src/main/java/de/pickshadow/plugin/commands/ToggleDownfallCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/ToggleDownfallCommand.java @@ -3,13 +3,12 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.base.ObjHolder; import de.pickshadow.plugin.base.Translation; import de.pickshadow.plugin.classes.CommandBase; +import de.pickshadow.plugin.classes.Interactable; +import de.pickshadow.plugin.classes.exceptions.NotAPlayerException; import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.TabCompletionHelper; -import org.bukkit.World; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.util.Locale; @@ -21,28 +20,27 @@ public class ToggleDownfallCommand extends CommandBase { } @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (Miscellaneous.isCommandSentByConsole(sender, true)) return true; - Player player = (Player) sender; - World world = player.getWorld(); + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (Miscellaneous.isCommandSentByConsole(intact, true)) return; - if (world.isClearWeather()) world.setClearWeatherDuration(new Random().nextInt(6000)); - else if (world.isThundering()) { - world.setThundering(true); - world.setThunderDuration(new Random().nextInt(6000)); - } else { - logger.error("Invalid weather state. isClearWeather() and isThundering() are false."); - player.sendMessage(Miscellaneous.format(Translation.GLOBAL_EXCEPTION)); - return true; + try { + switch (intact.getWeather()) { + case DOWNFALL: + intact.getWorld().setClearWeatherDuration(new Random().nextInt(6000)); + case CLEAR: + intact.getWorld().setThundering(true); + intact.getWorld().setThunderDuration(new Random().nextInt(6000)); + default: logger.crash("Invalid weather type '" + String.valueOf(intact.getWeather()) + "'"); + } + } catch (NotAPlayerException e) { + logger.crash("Tried to operate on console Interactable (THIS SHOULD BE IMPOSSIBLE!)", e.getStackTrace()); } if (ObjHolder.config.useOldMcTranslationForToggleDownfall) { - if (player.locale() == Locale.GERMANY) player.sendMessage("Der Niederschlag wurde umgestellt."); - else player.sendMessage("Toggled downfall."); + if (intact.getLocale() == Locale.GERMANY) intact.message("Der Niederschlag wurde umgestellt."); + else intact.message("Toggled downfall."); } - else player.sendMessage(Miscellaneous.format(Translation.TOGGLEDOWNFALL)); - - return true; + else intact.message(Miscellaneous.format(Translation.TOGGLEDOWNFALL)); } @Override diff --git a/src/main/java/de/pickshadow/plugin/commands/TrollCommand.java b/src/main/java/de/pickshadow/plugin/commands/TrollCommand.java index c31afc0..116d3e1 100644 --- a/src/main/java/de/pickshadow/plugin/commands/TrollCommand.java +++ b/src/main/java/de/pickshadow/plugin/commands/TrollCommand.java @@ -21,6 +21,7 @@ package de.pickshadow.plugin.commands; import de.pickshadow.plugin.base.ObjHolder; import de.pickshadow.plugin.classes.CommandBase; import de.pickshadow.plugin.base.Translation; +import de.pickshadow.plugin.classes.Interactable; import de.pickshadow.plugin.classes.Types; import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.TabCompletionHelper; @@ -44,33 +45,42 @@ public class TrollCommand extends CommandBase { } @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (Miscellaneous.isCommandSentByConsole(sender, true)) return true; - Player player = (Player) sender; + public void getCommand(@NotNull Interactable intact, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + + /* + TODO + ======== + + The 'target' needs to be rewritten to be a Interactable at some point. + Player works, but Interactable would be better. + */ + + if (Miscellaneous.isCommandSentByConsole(intact, true)) return; + if (args.length < 2) { - player.sendMessage(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); - return true; + intact.message(Miscellaneous.format(Translation.GLOBAL_NOT_ENOUGH_ARGUMENTS.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); + return; } Player target = Bukkit.getPlayerExact(args[0]); if (target == null) { - player.sendMessage(Miscellaneous.format(Translation.GLOBAL_PLAYER_NOT_ONLINE.replace("%player%", args[0]), Types.FormatType.ERROR)); - return true; + intact.message(Miscellaneous.format(Translation.GLOBAL_PLAYER_NOT_ONLINE.replace("%player%", args[0]), Types.FormatType.ERROR)); + return; } switch (args[1]) { case "demo": - player.sendMessage(Miscellaneous.format(Translation.TROLL_DEMO.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_DEMO.replace("%target%", args[0]))); target.showDemoScreen(); break; case "credits": - player.sendMessage(Miscellaneous.format(Translation.TROLL_CREDITS.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_CREDITS.replace("%target%", args[0]))); target.showWinScreen(); break; case "guardian": - player.sendMessage(Miscellaneous.format(Translation.TROLL_GUARDIAN.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_GUARDIAN.replace("%target%", args[0]))); target.showElderGuardian(); break; case "hack": - player.sendMessage(Miscellaneous.format(Translation.TROLL_HACK.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_HACK.replace("%target%", args[0]))); String base = ""; MiniMessage mm = MiniMessage.miniMessage(); target.showTitle(Title.title(mm.deserialize(base + "S=)E8tuv9ZM^P)TJZMVP);ZTP)MZIZM989pv(UZ;."), mm.deserialize(""), Title.Times.times(Duration.ofMillis(0L), Duration.ofSeconds(5L), Duration.ofMillis(0L)))); @@ -100,7 +110,7 @@ public class TrollCommand extends CommandBase { }, 20L); break; case "malware": - player.sendMessage(Miscellaneous.format(Translation.TROLL_MALWARE.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_MALWARE.replace("%target%", args[0]))); for (int i = 0; i < 127; i++) { int finalI = i; if (finalI == 100) { @@ -117,26 +127,25 @@ public class TrollCommand extends CommandBase { } break; case "creeper": - player.sendMessage(Miscellaneous.format(Translation.TROLL_CREEPER.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_CREEPER.replace("%target%", args[0]))); target.playSound(Sound.sound(Key.key("entity.creeper.primed"), Sound.Source.MASTER, 1f, 1f)); break; case "heavens": - player.sendMessage(Miscellaneous.format(Translation.TROLL_HEAVENS.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_HEAVENS.replace("%target%", args[0]))); Location locationHeavens = target.getLocation(); locationHeavens.setY(10000); target.teleport(locationHeavens); break; case "crash": - player.sendMessage(Miscellaneous.format(Translation.TROLL_CRASH.replace("%target%", args[0]))); + intact.message(Miscellaneous.format(Translation.TROLL_CRASH.replace("%target%", args[0]))); for (int i = 0; i < 100001; i++) { target.spawnParticle(Particle.EXPLOSION_HUGE, target.getLocation(), 100000); } break; default: - player.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); + intact.message(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); break; } - return true; } public TabCompleter getCompletion() { diff --git a/src/main/java/de/pickshadow/plugin/utils/Miscellaneous.java b/src/main/java/de/pickshadow/plugin/utils/Miscellaneous.java index b98a3f7..ccba52c 100644 --- a/src/main/java/de/pickshadow/plugin/utils/Miscellaneous.java +++ b/src/main/java/de/pickshadow/plugin/utils/Miscellaneous.java @@ -20,12 +20,12 @@ package de.pickshadow.plugin.utils; import de.pickshadow.plugin.base.Translation; import de.pickshadow.plugin.base.ObjHolder; +import de.pickshadow.plugin.classes.Interactable; 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; @@ -73,8 +73,14 @@ public class Miscellaneous { 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 isCommandSentByConsole(new Interactable(sender), issueError); + } + public static boolean isCommandSentByConsole(Interactable intact) { + return isCommandSentByConsole(intact, false); + } + public static boolean isCommandSentByConsole(Interactable intact, boolean issueError) { + if (intact.isConsole()) { + if (issueError) intact.message(Miscellaneous.format(Translation.GLOBAL_NOT_A_PLAYER, Types.FormatType.ERROR)); return true; } return false;