2019-12-17 12:37:59 +00:00
|
|
|
package pw.kaboom.extras.commands;
|
2019-07-30 14:41:54 +00:00
|
|
|
|
2022-05-21 19:28:42 +00:00
|
|
|
import com.destroystokyo.paper.profile.PlayerProfile;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
2019-12-22 19:19:01 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2019-07-30 14:41:54 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2019-09-22 02:37:43 +00:00
|
|
|
import org.bukkit.command.ConsoleCommandSender;
|
2019-07-30 14:41:54 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2020-02-16 03:53:06 +00:00
|
|
|
|
2022-05-21 19:28:42 +00:00
|
|
|
import javax.annotation.Nonnull;
|
2019-07-30 14:41:54 +00:00
|
|
|
|
2019-12-21 13:39:31 +00:00
|
|
|
public final class CommandUsername implements CommandExecutor {
|
2022-05-20 02:35:48 +00:00
|
|
|
private long millis;
|
|
|
|
|
|
|
|
@Override
|
2022-05-21 19:28:42 +00:00
|
|
|
public boolean onCommand(final @Nonnull CommandSender sender,
|
|
|
|
final @Nonnull Command command,
|
|
|
|
final @Nonnull String label,
|
2022-05-20 03:07:38 +00:00
|
|
|
final String[] args) {
|
2022-05-20 02:35:48 +00:00
|
|
|
if (sender instanceof ConsoleCommandSender) {
|
2022-05-21 19:28:42 +00:00
|
|
|
sender.sendMessage(Component
|
|
|
|
.text("Command has to be run by a player"));
|
2022-05-20 02:35:48 +00:00
|
|
|
} else {
|
|
|
|
final Player player = (Player) sender;
|
|
|
|
|
2022-05-20 03:07:38 +00:00
|
|
|
final String nameColor = ChatColor.translateAlternateColorCodes(
|
2022-05-21 19:28:42 +00:00
|
|
|
'&', String.join(" ", args));
|
2022-05-20 02:35:48 +00:00
|
|
|
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
|
|
|
|
|
|
|
|
final long millisDifference = System.currentTimeMillis() - millis;
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
2022-05-21 19:28:42 +00:00
|
|
|
player.sendMessage(Component
|
|
|
|
.text("Usage: /" + label + " <username>",
|
|
|
|
NamedTextColor.RED));
|
2022-05-20 02:35:48 +00:00
|
|
|
} else if (name.equals(player.getName())) {
|
2022-05-21 19:28:42 +00:00
|
|
|
player.sendMessage(Component
|
|
|
|
.text("You already have the username \"" + name + "\""));
|
2022-05-20 02:35:48 +00:00
|
|
|
} else if (millisDifference <= 2000) {
|
2022-05-21 19:28:42 +00:00
|
|
|
player.sendMessage(Component
|
|
|
|
.text("Please wait a few seconds before changing your username."));
|
2022-05-20 02:35:48 +00:00
|
|
|
} else {
|
2022-05-21 19:28:42 +00:00
|
|
|
if (Bukkit.getPlayer(name) != null) {
|
|
|
|
player.sendMessage(Component
|
|
|
|
.text("A player with that username is already logged in."));
|
2022-05-20 02:35:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final PlayerProfile profile = player.getPlayerProfile();
|
|
|
|
|
2022-05-21 19:28:42 +00:00
|
|
|
//FIXME Marked for removal
|
2022-05-20 02:35:48 +00:00
|
|
|
profile.setName(name);
|
|
|
|
|
|
|
|
player.setPlayerProfile(profile);
|
|
|
|
|
|
|
|
millis = System.currentTimeMillis();
|
|
|
|
|
2022-05-21 19:28:42 +00:00
|
|
|
player.sendMessage(Component
|
|
|
|
.text("Successfully set your username to \"" + name + "\""));
|
2022-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-30 14:41:54 +00:00
|
|
|
}
|