Rate limit username command

This commit is contained in:
mathiascode 2020-02-17 01:23:13 +02:00
parent 4169747681
commit 72bc93b970
2 changed files with 23 additions and 29 deletions

View file

@ -1,8 +1,5 @@
package pw.kaboom.extras.commands; package pw.kaboom.extras.commands;
import java.util.HashSet;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -14,14 +11,11 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import com.destroystokyo.paper.profile.PlayerProfile; import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import com.google.common.base.Charsets;
import pw.kaboom.extras.Main; import pw.kaboom.extras.Main;
import pw.kaboom.extras.helpers.SkinDownloader;
public final class CommandUsername implements CommandExecutor { public final class CommandUsername implements CommandExecutor {
public static HashSet<String> busyNames = new HashSet<String>(); public static boolean nameInProgress = false;
@Override @Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
@ -35,42 +29,37 @@ public final class CommandUsername implements CommandExecutor {
if (args.length == 0) { if (args.length == 0) {
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>"); player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
} else if (!busyNames.contains(name)) { } else if (nameInProgress) {
busyNames.add(name); player.sendMessage("Please wait a few seconds before changing your username");
} else if (!name.equals(player.getName())) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (name.equals(onlinePlayer.getName())) {
player.sendMessage("A player with that username is already logged in");
return true;
}
}
nameInProgress = true;
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
final PlayerProfile profile = player.getPlayerProfile(); final PlayerProfile profile = player.getPlayerProfile();
profile.setId(offlineUUID);
profile.setName(name); profile.setName(name);
player.setPlayerProfile(profile); player.setPlayerProfile(profile);
player.setOp(true); player.setOp(true);
busyNames.remove(name);
final int tickDelay = 40;
/*new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)); nameInProgress = false;
final PlayerProfile profile = player.getPlayerProfile();
//profile.clearProperties();
profile.setId(offlineUUID);
profile.setName(name);
new BukkitRunnable() {
@Override
public void run() {
player.setPlayerProfile(profile);
player.setOp(true);
busyNames.remove(name);
}
}.runTask(JavaPlugin.getPlugin(Main.class));
} }
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));*/ }.runTaskLaterAsynchronously(JavaPlugin.getPlugin(Main.class), tickDelay);
player.sendMessage("Successfully set your username to \"" + name + "\""); player.sendMessage("Successfully set your username to \"" + name + "\"");
} else { } else {
player.sendMessage("A player with that username is already logged in"); player.sendMessage("You already have the username \"" + name + "\"");
} }
} }
return true; return true;

View file

@ -1,5 +1,6 @@
package pw.kaboom.extras.modules.player; package pw.kaboom.extras.modules.player;
import com.destroystokyo.paper.event.player.PlayerHandshakeEvent;
import com.destroystokyo.paper.event.profile.PreLookupProfileEvent; import com.destroystokyo.paper.event.profile.PreLookupProfileEvent;
import com.destroystokyo.paper.profile.PlayerProfile; import com.destroystokyo.paper.profile.PlayerProfile;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
@ -44,6 +45,8 @@ public final class PlayerConnection implements Listener {
@EventHandler @EventHandler
void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) { void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) {
System.out.println("prelogin");
if (event.getName().length() > 16) { if (event.getName().length() > 16) {
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username can't be longer than 16 characters"); event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username can't be longer than 16 characters");
} else { } else {
@ -120,6 +123,8 @@ public final class PlayerConnection implements Listener {
@EventHandler @EventHandler
void onPlayerLogin(final PlayerLoginEvent event) { void onPlayerLogin(final PlayerLoginEvent event) {
System.out.println("login");
if (event.getHostname().startsWith("play.flame.ga") if (event.getHostname().startsWith("play.flame.ga")
&& event.getHostname().endsWith(":25565")) { && event.getHostname().endsWith(":25565")) {
event.disallow(Result.KICK_OTHER, "You connected to the server using an outdated server address/IP.\nPlease use the following address/IP:\n\nkaboom.pw"); event.disallow(Result.KICK_OTHER, "You connected to the server using an outdated server address/IP.\nPlease use the following address/IP:\n\nkaboom.pw");