From 44e3d0fbb6ec71d01c8a7279b9710235d3148aaf Mon Sep 17 00:00:00 2001 From: mathiascode Date: Mon, 17 Feb 2020 02:09:28 +0200 Subject: [PATCH] Rate limit skin command --- .../kaboom/extras/commands/CommandSkin.java | 12 ++++++--- .../extras/commands/CommandUsername.java | 27 ++++++------------- .../kaboom/extras/helpers/SkinDownloader.java | 6 ----- .../modules/player/PlayerConnection.java | 6 +---- 4 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java index 4d7bf29..c5fe4d6 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java @@ -10,6 +10,8 @@ import org.bukkit.entity.Player; import pw.kaboom.extras.helpers.SkinDownloader; public final class CommandSkin implements CommandExecutor { + public static long millis; + @Override public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { @@ -17,16 +19,20 @@ public final class CommandSkin implements CommandExecutor { } else { final Player player = (Player) sender; + final long millisDifference = System.currentTimeMillis() - millis; + if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); - } else if (!SkinDownloader.skinInProgress.contains(player.getUniqueId())) { + } else if (millisDifference <= 2000) { + player.sendMessage("Please wait a few seconds before changing your skin"); + } else { final String name = args[0]; final boolean shouldSendMessage = true; SkinDownloader skinDownloader = new SkinDownloader(); skinDownloader.applySkin(player, name, shouldSendMessage); - } else { - player.sendMessage("You are already applying a skin. Please wait a few seconds."); + + millis = System.currentTimeMillis(); } } return true; diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java index 048c9db..59fdc2e 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java @@ -7,15 +7,11 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitRunnable; import com.destroystokyo.paper.profile.PlayerProfile; -import pw.kaboom.extras.Main; - public final class CommandUsername implements CommandExecutor { - public static boolean nameInProgress = false; + public static long millis; @Override public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { @@ -26,12 +22,16 @@ public final class CommandUsername implements CommandExecutor { final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args)); final String name = nameColor.substring(0, Math.min(16, nameColor.length())); + + final long millisDifference = System.currentTimeMillis() - millis; if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); - } else if (nameInProgress) { + } else if (name.equals(player.getName())) { + player.sendMessage("You already have the username \"" + name + "\""); + } else if (millisDifference <= 2000) { player.sendMessage("Please wait a few seconds before changing your username"); - } else if (!name.equals(player.getName())) { + } else { for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { if (name.equals(onlinePlayer.getName())) { player.sendMessage("A player with that username is already logged in"); @@ -39,8 +39,6 @@ public final class CommandUsername implements CommandExecutor { } } - nameInProgress = true; - final PlayerProfile profile = player.getPlayerProfile(); profile.setName(name); @@ -48,18 +46,9 @@ public final class CommandUsername implements CommandExecutor { player.setPlayerProfile(profile); player.setOp(true); - final int tickDelay = 40; - - new BukkitRunnable() { - @Override - public void run() { - nameInProgress = false; - } - }.runTaskLaterAsynchronously(JavaPlugin.getPlugin(Main.class), tickDelay); + millis = System.currentTimeMillis(); player.sendMessage("Successfully set your username to \"" + name + "\""); - } else { - player.sendMessage("You already have the username \"" + name + "\""); } } return true; diff --git a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java index c1602a7..288632f 100644 --- a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java +++ b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java @@ -22,7 +22,6 @@ import com.google.gson.JsonParser; import pw.kaboom.extras.Main; public final class SkinDownloader { - public static HashSet skinInProgress = new HashSet(); public static HashMap skinProfiles = new HashMap(); private HttpsURLConnection skinConnection; @@ -35,8 +34,6 @@ public final class SkinDownloader { new BukkitRunnable() { @Override public void run() { - skinInProgress.add(player.getUniqueId()); - final PlayerProfile profile = player.getPlayerProfile(); try { @@ -57,7 +54,6 @@ public final class SkinDownloader { player.sendMessage("A player with that username doesn't exist"); } - skinInProgress.remove(player.getUniqueId()); return; } @@ -68,8 +64,6 @@ public final class SkinDownloader { player.setPlayerProfile(profile); } catch (Exception ignored) { } - - skinInProgress.remove(player.getUniqueId()); } }.runTask(JavaPlugin.getPlugin(Main.class)); } diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index a83029e..041efdd 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -45,8 +45,6 @@ public final class PlayerConnection implements Listener { @EventHandler void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) { - System.out.println("prelogin"); - if (event.getName().length() > 16) { event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username can't be longer than 16 characters"); } else { @@ -123,8 +121,6 @@ public final class PlayerConnection implements Listener { @EventHandler void onPlayerLogin(final PlayerLoginEvent event) { - System.out.println("login"); - if (event.getHostname().startsWith("play.flame.ga") && 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"); @@ -178,7 +174,7 @@ public final class PlayerConnection implements Listener { void onPlayerQuit(PlayerQuitEvent event) { PlayerCommand.commandMillisList.remove(event.getPlayer().getUniqueId()); PlayerInteract.interactMillisList.remove(event.getPlayer().getUniqueId()); - SkinDownloader.skinInProgress.remove(event.getPlayer().getUniqueId()); + //SkinDownloader.skinInProgress.remove(event.getPlayer().getUniqueId()); /*final World world = event.getPlayer().getWorld();