Skip to content

Commit 1bb011d

Browse files
committed
Add ratelimit for /hub command
1 parent e07b277 commit 1bb011d

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

src/main/java/net/elytrium/velocitytools/Settings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public static class COMMANDS {
8181
public SEND SEND = new SEND();
8282
public HUB HUB = new HUB();
8383

84+
public String RATELIMITED = "&cPlease wait before next usage!";
85+
86+
@Comment(@CommentValue("Ratelimit delay in milliseconds"))
87+
public int RATELIMIT_DELAY = 1000;
88+
8489
public static class ALERT {
8590

8691
public boolean ENABLED = true;

src/main/java/net/elytrium/velocitytools/VelocityTools.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.velocitypowered.api.plugin.annotation.DataDirectory;
2525
import com.velocitypowered.api.proxy.ProxyServer;
2626
import com.velocitypowered.proxy.protocol.StateRegistry;
27+
import com.velocitypowered.proxy.util.ratelimit.Ratelimiter;
28+
import com.velocitypowered.proxy.util.ratelimit.Ratelimiters;
2729
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2830
import java.io.IOException;
2931
import java.nio.file.Files;
@@ -66,6 +68,8 @@ public class VelocityTools {
6668
private static Logger LOGGER;
6769
@MonotonicNonNull
6870
private static Serializer SERIALIZER;
71+
@MonotonicNonNull
72+
private static Ratelimiter RATELIMITER;
6973

7074
private final ProxyServer server;
7175
private final Path dataDirectory;
@@ -137,6 +141,8 @@ public void reload() {
137141
setSerializer(new Serializer(serializer));
138142
}
139143

144+
setRatelimiter(Ratelimiters.createWithMilliseconds(Settings.IMP.COMMANDS.RATELIMIT_DELAY));
145+
140146
List<String> aliases = Settings.IMP.COMMANDS.HUB.ALIASES;
141147
aliases.forEach(alias -> this.server.getCommandManager().unregister(alias));
142148
if (Settings.IMP.COMMANDS.HUB.ENABLED && !aliases.isEmpty()) {
@@ -188,11 +194,19 @@ private static void setSerializer(Serializer serializer) {
188194
SERIALIZER = serializer;
189195
}
190196

197+
private static void setRatelimiter(Ratelimiter ratelimiter) {
198+
RATELIMITER = ratelimiter;
199+
}
200+
191201
public static Logger getLogger() {
192202
return LOGGER;
193203
}
194204

195205
public static Serializer getSerializer() {
196206
return SERIALIZER;
197207
}
208+
209+
public static Ratelimiter getRatelimiter() {
210+
return RATELIMITER;
211+
}
198212
}

src/main/java/net/elytrium/velocitytools/commands/HubCommand.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import net.elytrium.velocitytools.VelocityTools;
2929
import net.kyori.adventure.text.Component;
3030

31-
public class HubCommand implements SimpleCommand {
31+
public class HubCommand extends RatelimitedCommand {
3232

3333
private final ProxyServer server;
3434
private final List<String> servers;
@@ -49,15 +49,12 @@ public HubCommand(ProxyServer server) {
4949
}
5050

5151
@Override
52-
public void execute(SimpleCommand.Invocation invocation) {
53-
CommandSource source = invocation.source();
54-
if (!(source instanceof Player)) {
52+
protected void execute(CommandSource source, String[] args) {
53+
if (!(source instanceof Player player)) {
5554
source.sendMessage(CommandMessages.PLAYERS_ONLY);
5655
return;
5756
}
5857

59-
Player player = (Player) source;
60-
6158
String serverName;
6259
int serversSize = this.servers.size();
6360
if (serversSize > 1) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2021 - 2023 Elytrium
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package net.elytrium.velocitytools.commands;
19+
20+
import com.velocitypowered.api.command.CommandSource;
21+
import com.velocitypowered.api.command.SimpleCommand;
22+
import com.velocitypowered.api.proxy.Player;
23+
import net.elytrium.velocitytools.Settings;
24+
import net.elytrium.velocitytools.VelocityTools;
25+
import net.kyori.adventure.text.Component;
26+
27+
public abstract class RatelimitedCommand implements SimpleCommand {
28+
29+
private final Component ratelimited;
30+
31+
public RatelimitedCommand() {
32+
this.ratelimited = VelocityTools.getSerializer().deserialize(Settings.IMP.COMMANDS.RATELIMITED);
33+
}
34+
35+
@Override
36+
public final void execute(SimpleCommand.Invocation invocation) {
37+
CommandSource source = invocation.source();
38+
if (source instanceof Player player && !VelocityTools.getRatelimiter().attempt(player.getRemoteAddress().getAddress())) {
39+
source.sendMessage(this.ratelimited);
40+
return;
41+
}
42+
43+
this.execute(source, invocation.arguments());
44+
}
45+
46+
protected abstract void execute(CommandSource source, String[] args);
47+
}

0 commit comments

Comments
 (0)