forked from kaboomserver/extras
Rate limit skin command
This commit is contained in:
parent
72bc93b970
commit
44e3d0fbb6
4 changed files with 18 additions and 33 deletions
|
@ -10,6 +10,8 @@ import org.bukkit.entity.Player;
|
||||||
import pw.kaboom.extras.helpers.SkinDownloader;
|
import pw.kaboom.extras.helpers.SkinDownloader;
|
||||||
|
|
||||||
public final class CommandSkin implements CommandExecutor {
|
public final class CommandSkin implements CommandExecutor {
|
||||||
|
public static long millis;
|
||||||
|
|
||||||
@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) {
|
||||||
if (sender instanceof ConsoleCommandSender) {
|
if (sender instanceof ConsoleCommandSender) {
|
||||||
|
@ -17,16 +19,20 @@ public final class CommandSkin implements CommandExecutor {
|
||||||
} else {
|
} else {
|
||||||
final Player player = (Player) sender;
|
final Player player = (Player) sender;
|
||||||
|
|
||||||
|
final long millisDifference = System.currentTimeMillis() - millis;
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||||
} 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 String name = args[0];
|
||||||
final boolean shouldSendMessage = true;
|
final boolean shouldSendMessage = true;
|
||||||
|
|
||||||
SkinDownloader skinDownloader = new SkinDownloader();
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
skinDownloader.applySkin(player, name, shouldSendMessage);
|
skinDownloader.applySkin(player, name, shouldSendMessage);
|
||||||
} else {
|
|
||||||
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
millis = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -7,15 +7,11 @@ import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
|
||||||
|
|
||||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||||
|
|
||||||
import pw.kaboom.extras.Main;
|
|
||||||
|
|
||||||
public final class CommandUsername implements CommandExecutor {
|
public final class CommandUsername implements CommandExecutor {
|
||||||
public static boolean nameInProgress = false;
|
public static long millis;
|
||||||
|
|
||||||
@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) {
|
||||||
|
@ -27,11 +23,15 @@ public final class CommandUsername implements CommandExecutor {
|
||||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
||||||
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
|
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||||
|
|
||||||
|
final long millisDifference = System.currentTimeMillis() - millis;
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||||
} 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");
|
player.sendMessage("Please wait a few seconds before changing your username");
|
||||||
} else if (!name.equals(player.getName())) {
|
} else {
|
||||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||||
if (name.equals(onlinePlayer.getName())) {
|
if (name.equals(onlinePlayer.getName())) {
|
||||||
player.sendMessage("A player with that username is already logged in");
|
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();
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
|
|
||||||
profile.setName(name);
|
profile.setName(name);
|
||||||
|
@ -48,18 +46,9 @@ public final class CommandUsername implements CommandExecutor {
|
||||||
player.setPlayerProfile(profile);
|
player.setPlayerProfile(profile);
|
||||||
player.setOp(true);
|
player.setOp(true);
|
||||||
|
|
||||||
final int tickDelay = 40;
|
millis = System.currentTimeMillis();
|
||||||
|
|
||||||
new BukkitRunnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
nameInProgress = false;
|
|
||||||
}
|
|
||||||
}.runTaskLaterAsynchronously(JavaPlugin.getPlugin(Main.class), tickDelay);
|
|
||||||
|
|
||||||
player.sendMessage("Successfully set your username to \"" + name + "\"");
|
player.sendMessage("Successfully set your username to \"" + name + "\"");
|
||||||
} else {
|
|
||||||
player.sendMessage("You already have the username \"" + name + "\"");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -22,7 +22,6 @@ import com.google.gson.JsonParser;
|
||||||
import pw.kaboom.extras.Main;
|
import pw.kaboom.extras.Main;
|
||||||
|
|
||||||
public final class SkinDownloader {
|
public final class SkinDownloader {
|
||||||
public static HashSet<UUID> skinInProgress = new HashSet<UUID>();
|
|
||||||
public static HashMap<UUID, PlayerProfile> skinProfiles = new HashMap<UUID, PlayerProfile>();
|
public static HashMap<UUID, PlayerProfile> skinProfiles = new HashMap<UUID, PlayerProfile>();
|
||||||
|
|
||||||
private HttpsURLConnection skinConnection;
|
private HttpsURLConnection skinConnection;
|
||||||
|
@ -35,8 +34,6 @@ public final class SkinDownloader {
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
skinInProgress.add(player.getUniqueId());
|
|
||||||
|
|
||||||
final PlayerProfile profile = player.getPlayerProfile();
|
final PlayerProfile profile = player.getPlayerProfile();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -57,7 +54,6 @@ public final class SkinDownloader {
|
||||||
player.sendMessage("A player with that username doesn't exist");
|
player.sendMessage("A player with that username doesn't exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
skinInProgress.remove(player.getUniqueId());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,8 +64,6 @@ public final class SkinDownloader {
|
||||||
player.setPlayerProfile(profile);
|
player.setPlayerProfile(profile);
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
skinInProgress.remove(player.getUniqueId());
|
|
||||||
}
|
}
|
||||||
}.runTask(JavaPlugin.getPlugin(Main.class));
|
}.runTask(JavaPlugin.getPlugin(Main.class));
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,8 +45,6 @@ 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 {
|
||||||
|
@ -123,8 +121,6 @@ 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");
|
||||||
|
@ -178,7 +174,7 @@ public final class PlayerConnection implements Listener {
|
||||||
void onPlayerQuit(PlayerQuitEvent event) {
|
void onPlayerQuit(PlayerQuitEvent event) {
|
||||||
PlayerCommand.commandMillisList.remove(event.getPlayer().getUniqueId());
|
PlayerCommand.commandMillisList.remove(event.getPlayer().getUniqueId());
|
||||||
PlayerInteract.interactMillisList.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();
|
/*final World world = event.getPlayer().getWorld();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue