From e8eabbb971b050db900e44b0dc9c05cb56046197 Mon Sep 17 00:00:00 2001 From: Andrew121410 Date: Sun, 17 Nov 2024 03:25:50 -0500 Subject: [PATCH] Add /offlinelocation --- .../world16essentials/World16Essentials.java | 1 + .../commands/OfflineLocationCMD.java | 95 +++++++++++++++++++ src/main/resources/plugin.yml | 5 + 3 files changed, 101 insertions(+) create mode 100644 src/main/java/com/andrew121410/mc/world16essentials/commands/OfflineLocationCMD.java diff --git a/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java b/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java index 14d6717b..fac38b9f 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/World16Essentials.java @@ -198,6 +198,7 @@ private void registerCommands() { new XyzdxdydzCMD(this); new IgnoreAfkCMD(this); new RenderInfoCMD(this); + new OfflineLocationCMD(this); } private void registerListeners() { diff --git a/src/main/java/com/andrew121410/mc/world16essentials/commands/OfflineLocationCMD.java b/src/main/java/com/andrew121410/mc/world16essentials/commands/OfflineLocationCMD.java new file mode 100644 index 00000000..2b5d59ae --- /dev/null +++ b/src/main/java/com/andrew121410/mc/world16essentials/commands/OfflineLocationCMD.java @@ -0,0 +1,95 @@ +package com.andrew121410.mc.world16essentials.commands; + +import com.andrew121410.mc.world16essentials.World16Essentials; +import com.andrew121410.mc.world16essentials.utils.API; +import com.andrew121410.mc.world16utils.chat.Translate; +import com.andrew121410.mc.world16utils.utils.TabUtils; +import net.kyori.adventure.text.event.ClickEvent; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +public class OfflineLocationCMD implements CommandExecutor { + + private final World16Essentials plugin; + private final API api; + + public OfflineLocationCMD(World16Essentials plugin) { + this.plugin = plugin; + this.api = this.plugin.getApi(); + + this.plugin.getCommand("offlinelocation").setExecutor(this); + this.plugin.getCommand("offlinelocation").setTabCompleter((sender, cmd, alias, args) -> { + if (!(sender instanceof Player player)) return null; + if (!player.hasPermission("world16.offlinelocation")) return null; + + if (args.length == 1) { + // Get the array of OfflinePlayers + OfflinePlayer[] playersArray = this.plugin.getServer().getOfflinePlayers(); + + // Filter out broken players and collect names into a list + List offlineNames = Arrays.stream(playersArray) + .filter(Objects::nonNull) // Filter out null OfflinePlayers + .filter(offlinePlayer -> offlinePlayer.getName() != null) // Filter out players with null names + .filter(offlinePlayer -> !offlinePlayer.getName().isEmpty()) // Filter out players with empty names + .filter(offlinePlayer -> !offlinePlayer.getName().equals("null")) // Filter out players with "null" as their name + .map(OfflinePlayer::getName) // Map to player names + .collect(Collectors.toList()); // Collect names into a list + + return TabUtils.getContainsString(args[0], offlineNames); + } + + return null; + }); + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (!sender.hasPermission("world16.offlinelocation")) { + api.sendPermissionErrorMessage(sender); + return true; + } + + if (args.length == 1) { + OfflinePlayer target = this.plugin.getServer().getOfflinePlayer(args[0]); + if (!target.hasPlayedBefore()) { + sender.sendMessage(Translate.miniMessage("Player has never played before!")); + return true; + } + + if (target.isOnline()) { + sender.sendMessage(Translate.miniMessage("That player is online.")); + target = target.getPlayer(); + } + + Location location = target.getLocation(); + if (location == null) { + sender.sendMessage(Translate.miniMessage("Player has never played before!")); + return true; + } + + // Clickable message to teleport to the location. + if (sender instanceof Player player) { + sender.sendMessage(Translate.miniMessage("Player: " + target.getName())); + player.sendMessage(Translate.miniMessage("World: " + location.getWorld().getName())); + player.sendMessage(Translate.miniMessage("Click me to teleport to the location!").clickEvent(ClickEvent.runCommand("/tp " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ()))); + } else { + sender.sendMessage("Player: " + target.getName()); + sender.sendMessage("World: " + location.getWorld().getName()); + sender.sendMessage("There location is " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ()); + } + return true; + } else { + sender.sendMessage(Translate.color("&cUsage: /offlinelocation ")); + } + return true; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 354f61a9..c3a1a379 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -148,6 +148,8 @@ commands: permission: world16.saveinventory keepspawnloaded: permission: world16.keepspawnloaded + offlinelocation: + permission: world16.offlinelocation sign: permission: world16.sign spawnmob: @@ -340,6 +342,9 @@ permissions: world16.keepspawnloaded: description: Allows you to use keepspawnloaded cmd default: op + world16.offlinelocation: + description: Allows you to use offlinelocation cmd + default: op world16.sign: description: Allows you to use sign cmd default: op