Skip to content

Commit

Permalink
Add /offlinelocation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew121410 committed Nov 17, 2024
1 parent 06c5690 commit e8eabbb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private void registerCommands() {
new XyzdxdydzCMD(this);
new IgnoreAfkCMD(this);
new RenderInfoCMD(this);
new OfflineLocationCMD(this);
}

private void registerListeners() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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("<red>Player has never played before!"));
return true;
}

if (target.isOnline()) {
sender.sendMessage(Translate.miniMessage("<green>That player is online."));
target = target.getPlayer();
}

Location location = target.getLocation();
if (location == null) {
sender.sendMessage(Translate.miniMessage("<red>Player has never played before!"));
return true;
}

// Clickable message to teleport to the location.
if (sender instanceof Player player) {
sender.sendMessage(Translate.miniMessage("<yellow>Player: " + target.getName()));
player.sendMessage(Translate.miniMessage("<yellow>World: " + location.getWorld().getName()));
player.sendMessage(Translate.miniMessage("<yellow><u>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 <player>"));
}
return true;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ commands:
permission: world16.saveinventory
keepspawnloaded:
permission: world16.keepspawnloaded
offlinelocation:
permission: world16.offlinelocation
sign:
permission: world16.sign
spawnmob:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e8eabbb

Please sign in to comment.