tweaks724/src/main/java/eu/m724/tweaks/auth/AuthCommands.java
Minecon724 29df176ff7
Some checks failed
/ deploy (push) Has been cancelled
Auth
2024-12-06 17:13:33 +01:00

93 lines
3.6 KiB
Java

/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.auth;
import eu.m724.tweaks.TweaksConfig;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Bukkit;
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 org.jetbrains.annotations.NotNull;
import java.io.FileNotFoundException;
import java.util.UUID;
public class AuthCommands implements CommandExecutor {
private final AuthStorage authStorage;
AuthCommands(AuthStorage authStorage) {
this.authStorage = authStorage;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length == 0) {
sender.sendMessage("<key> | new | delete");
return true;
}
String action = args[0];
if (action.equals("new")) {
String key = authStorage.generateKey();
authStorage.create(key);
String hostname = key + "." + TweaksConfig.getConfig().authDomain();
if (sender instanceof Player) {
BaseComponent component = new ComponentBuilder("Generated a new key. Click to copy.")
.underlined(true)
.color(ChatColor.GRAY)
.event(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, hostname))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to copy")))
.build();
sender.spigot().sendMessage(component);
} else {
sender.sendMessage("Generated a new key: " + hostname);
}
} else if (action.equals("delete")) {
if (args.length == 1) {
sender.sendMessage("You need to pass the key you want to delete as an argument");
return true;
}
UUID user = authStorage.delete(args[1].split("\\.")[0]);
if (user != null) {
if (user.getLeastSignificantBits() == 0 && user.getMostSignificantBits() == 0) {
sender.sendMessage("Key deleted. It wasn't assigned to any player.");
} else {
sender.sendMessage("Key deleted. It was assigned to " + Bukkit.getOfflinePlayer(user).getName());
}
} else {
sender.sendMessage("There's no such key.");
}
} else {
try {
UUID user = authStorage.getUserOfKey(action.split("\\.")[0]);
if (user == null) {
sender.sendMessage("Key has no owner, meaning anybody who uses it gets to keep it");
} else {
OfflinePlayer player = Bukkit.getOfflinePlayer(user);
sender.sendMessage("Key is assigned to %s %s".formatted(player.getName(), user));
}
} catch (FileNotFoundException | AuthStorage.InvalidKeyException e) {
sender.sendMessage("No such key. Enter a valid key. Or you meant 'new' or 'delete'?");
}
}
return true;
}
}