Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New redesigned command system #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ Test server: [``ely.su``](https://hotmc.ru/minecraft-server-203216)

Intel Core i9-9700K, DDR4 (a server that is not running any programs):

| Plugin | Pings per second count |
| - | - |
| FastMOTD | 1 700 000 - 2 000 000 pings per second |
| Without MOTD plugins | 900 000 - 1 100 000 pings per second |
| MiniMOTD | 480 000 - 580 000 pings per second |
| Plugin | Pings per second count |
|----------------------|----------------------------------------|
| FastMOTD | 1 700 000 - 2 000 000 pings per second |
| Without MOTD plugins | 900 000 - 1 100 000 pings per second |
| MiniMOTD | 480 000 - 580 000 pings per second |

Intel Xeon E3-1270, DDR3 (a PC with several applications running):
| Plugin | Pings per second count |
| - | - |
| FastMOTD | 840 000 - 1 000 000 pings per second |
| Without MOTD plugins | 330 000 - 430 000 pings per second |
| MiniMOTD | 150 000 - 200 000 pings per second |

| Plugin | Pings per second count |
|----------------------|--------------------------------------|
| FastMOTD | 840 000 - 1 000 000 pings per second |
| Without MOTD plugins | 330 000 - 430 000 pings per second |
| MiniMOTD | 150 000 - 200 000 pings per second |

## Commands and permissions

### Admin

- ***fastmotd.info* | /fastmotd info** - The command to get general information about the current state of the plugin
- ***fastmotd.reload* | /fastmotd reload** - Reload plugin command
- ***fastmotd.maintenance* | /maintenance** - Maintenance mode setting command
46 changes: 36 additions & 10 deletions src/main/java/net/elytrium/fastmotd/FastMOTD.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import com.google.inject.Inject;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.ServerPing;
import com.velocitypowered.api.scheduler.ScheduledTask;
Expand Down Expand Up @@ -59,8 +61,8 @@
import java.util.stream.IntStream;
import net.elytrium.commons.utils.reflection.ReflectionException;
import net.elytrium.commons.utils.updates.UpdatesChecker;
import net.elytrium.fastmotd.command.FastmotdCommand;
import net.elytrium.fastmotd.command.MaintenanceCommand;
import net.elytrium.fastmotd.command.ReloadCommand;
import net.elytrium.fastmotd.injection.ServerChannelInitializerHook;
import net.elytrium.fastmotd.listener.CompatPingListener;
import net.elytrium.fastmotd.listener.DisconnectOnZeroPlayersListener;
Expand Down Expand Up @@ -99,6 +101,9 @@ public class FastMOTD {
private final Map<String, MOTDGenerator> domainMaintenanceMOTD = new HashMap<>();
private PreparedPacketFactory preparedPacketFactory;
private ScheduledTask updater;
private CommandMeta fastmotdCommandMeta;
private CommandMeta maintenanceCommandMeta;
private Component kickReasonComponent;
private PreparedPacket kickReason;
private Set<InetAddress> kickWhitelist;

Expand Down Expand Up @@ -137,6 +142,10 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
this.preparedPacketFactory =
new PreparedPacketFactory(PreparedPacket::new, StateRegistry.LOGIN, false, 1, 1, false, true);

final CommandManager commandManager = this.server.getCommandManager();
this.fastmotdCommandMeta = commandManager.metaBuilder("fastmotd").plugin(this).build();
this.maintenanceCommandMeta = commandManager.metaBuilder("maintenance").plugin(this).build();

this.reload();
}

Expand Down Expand Up @@ -174,13 +183,11 @@ public void reload() {
this.maintenanceMOTDGenerators.clear();
this.domainMaintenanceMOTD.clear();

CommandManager commandManager = this.server.getCommandManager();
commandManager.unregister("fastmotdreload");
commandManager.unregister("maintenance");

commandManager.register("fastmotdreload", new ReloadCommand(this));
commandManager.register("maintenance",
new MaintenanceCommand(this, serializer.deserialize(Settings.IMP.MAINTENANCE.COMMAND.USAGE)));
final CommandManager commandManager = this.server.getCommandManager();
commandManager.unregister(this.fastmotdCommandMeta);
commandManager.unregister(this.maintenanceCommandMeta);
commandManager.register(this.fastmotdCommandMeta, FastmotdCommand.createBrigadierCommand(this, serializer));
commandManager.register(this.maintenanceCommandMeta, MaintenanceCommand.createBrigadierCommand(this, serializer));

EventManager eventManager = this.server.getEventManager();
eventManager.unregisterListeners(this);
Expand All @@ -198,10 +205,10 @@ public void reload() {
this.kickReason.release();
}

Component kickReasonComponent = serializer.deserialize(Settings.IMP.MAINTENANCE.KICK_MESSAGE.replace("{NL}", "\n"));
this.kickReasonComponent = serializer.deserialize(Settings.IMP.MAINTENANCE.KICK_MESSAGE.replace("{NL}", "\n"));
this.kickReason = this.preparedPacketFactory
.createPreparedPacket(ProtocolVersion.MINIMUM_VERSION, ProtocolVersion.MAXIMUM_VERSION)
.prepare(version -> DisconnectPacket.create(kickReasonComponent, version, StateRegistry.LOGIN))
.prepare(version -> DisconnectPacket.create(this.kickReasonComponent, version, StateRegistry.LOGIN))
.build();

this.kickWhitelist = Settings.IMP.MAINTENANCE.KICK_WHITELIST.stream().map((String host) -> {
Expand All @@ -212,6 +219,10 @@ public void reload() {
}
}).collect(Collectors.toSet());

if (Settings.IMP.MAINTENANCE.MAINTENANCE_ENABLED) {
this.kickNotWhitelisted();
}

this.generateMOTDGenerators(serializer, Settings.IMP.MAIN.VERSION_NAME, Settings.IMP.MAIN.DESCRIPTIONS,
Settings.IMP.MAIN.FAVICONS, Settings.IMP.MAIN.INFORMATION, this.motdGenerators, this.protocolPointers,
Settings.IMP.MAIN.VERSIONS.DESCRIPTIONS, Settings.IMP.MAIN.VERSIONS.FAVICONS, Settings.IMP.MAIN.VERSIONS.INFORMATION,
Expand Down Expand Up @@ -377,10 +388,25 @@ public void inject(MinecraftConnection connection, ChannelPipeline pipeline) {
this.preparedPacketFactory.inject(false, connection, pipeline);
}

public void kickNotWhitelisted() {
if (!Settings.IMP.MAINTENANCE.SHOULD_KICK_ONLINE) {
return;
}
for (Player player : this.server.getAllPlayers()) {
if (!this.kickWhitelist.contains(player.getRemoteAddress().getAddress())) {
player.disconnect(this.kickReasonComponent);
}
}
}

public boolean checkKickWhitelist(InetAddress inetAddress) {
return this.kickWhitelist.contains(inetAddress);
}

public Set<InetAddress> getKickWhitelist() {
return this.kickWhitelist;
}

public VelocityServer getServer() {
return this.server;
}
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/net/elytrium/fastmotd/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.elytrium.serializer.annotations.Comment;
import net.elytrium.serializer.annotations.CommentValue;
import net.elytrium.serializer.annotations.Final;
import net.elytrium.serializer.annotations.RegisterPlaceholders;
import net.elytrium.serializer.language.object.YamlSerializable;

public class Settings extends YamlSerializable {
Expand Down Expand Up @@ -92,6 +93,16 @@ public static class VERSIONS {
}

public Map<String, DOMAIN_MOTD_NODE> DOMAINS = Map.of("example.com:25565", new DOMAIN_MOTD_NODE());

public MESSAGES MESSAGES = new MESSAGES();

public static class MESSAGES {
public List<String> USAGE = List.of("<gold><bold>FastMOTD command usage:</bold></gold>", " <gold>/fastmotd info</gold>", " <gold>/fastmotd reload</gold>");
public List<String> INFO = List.of("<gold><bold>FastMOTD Info:</bold></gold>", " Maintenance status: {MAINTENANCE_ENABLED}");
public String YES = "<green>✓</green>";
public String NO = "<red>✕</red>";
public String RELOAD = "FastMOTD <gold>>></gold> Reloaded successfully!";
}
}

public MAINTENANCE MAINTENANCE = new MAINTENANCE();
Expand All @@ -100,6 +111,8 @@ public static class MAINTENANCE {
public boolean MAINTENANCE_ENABLED = false;
public boolean SHOW_VERSION = true;
public boolean SHOULD_KICK_ON_JOIN = true;
@Comment(@CommentValue("This parameter determines whether to remove non-whitelisted players from the game when maintenance mode is activated."))
public boolean SHOULD_KICK_ONLINE = false;
public List<String> KICK_WHITELIST = List.of("127.0.0.1");
public String KICK_MESSAGE = "<red>Try to join the server later</red>";
public String VERSION_NAME = "MAINTENANCE MODE ENABLED!!";
Expand Down Expand Up @@ -128,10 +141,20 @@ public static class VERSIONS {

public Map<String, DOMAIN_MOTD_NODE> DOMAINS = Map.of("example.com:25565", new DOMAIN_MOTD_NODE());

public COMMAND COMMAND = new COMMAND();

public static class COMMAND {
public String USAGE = "FastMOTD <gold>>></gold> Usage: <gold>/maintenance <off|on|toggle></gold>";
public MESSAGES MESSAGES = new MESSAGES();

public static class MESSAGES {
public List<String> USAGE = List.of("<gold><bold>FastMOTD maintenance command usage:</bold></gold>", " <gold>/maintenance <off | on | toggle></gold> - change maintenance mode", " <gold>/maintenance list [-p]</gold> - get all ip addresses in whitelist ([-p] - get all online players in whitelist)", " <gold>/maintenance <add | remove> <player | ip></gold> - add/remove ip (or current player ip) from whitelist");
public String OFF = "FastMOTD <gold>>></gold> You have <red>disabled</red> the maintenance mode!";
public String ON = "FastMOTD <gold>>></gold> You have <green>enabled</green> maintenance mode!";
@RegisterPlaceholders("{KICK_WHITELIST}")
public String LIST = "FastMOTD <gold>>></gold> Whitelist: <gold>{KICK_WHITELIST}</gold>";
public String LIST_PLAYER_FORMAT = "<gold>{PLAYER}</gold> <grey>({IP})</grey>";
public String SUCCESSFULLY_ADDED = "FastMOTD <gold>>></gold> You have successfully added the IP address to the whitelist!";
public String SUCCESSFULLY_REMOVED = "FastMOTD <gold>>></gold> You have successfully removed the IP address to the whitelist!";
public String INVALID_INPUT = "FastMOTD <gold>>></gold> This is not an online player or IP address.";
public String ALREADY_IN = "FastMOTD <gold>>></gold> This IP address has already been added!";
public String NOT_IN_WHITELIST = "FastMOTD <gold>>></gold> This IP address has not yet been added!";
}
}

Expand Down
73 changes: 73 additions & 0 deletions src/main/java/net/elytrium/fastmotd/command/FastmotdCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2022 - 2023 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.fastmotd.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource;
import net.elytrium.fastmotd.FastMOTD;
import net.elytrium.fastmotd.Settings;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ComponentSerializer;

public class FastmotdCommand {
/**
* This method creates a {@link com.velocitypowered.api.command.BrigadierCommand} for the fastmotd command.
*
* @param plugin The FastMOTD plugin instance.
* @param serializer The component serializer.
* @return The maintenance command.
*/
public static BrigadierCommand createBrigadierCommand(final FastMOTD plugin, final ComponentSerializer<Component, Component, String> serializer) {
final Component usageComponent = serializer.deserialize(String.join("\n", Settings.IMP.MAIN.MESSAGES.USAGE));
final Component reloadComponent = serializer.deserialize(Settings.IMP.MAIN.MESSAGES.RELOAD);
final String infoString = String.join("\n", Settings.IMP.MAIN.MESSAGES.INFO);

LiteralCommandNode<CommandSource> fastmotdNode = BrigadierCommand.literalArgumentBuilder("fastmotd")
.requires(source -> source.hasPermission("fastmotd.reload") || source.hasPermission("fastmotd.info"))
.executes(context -> {
context.getSource().sendMessage(usageComponent);
return Command.SINGLE_SUCCESS;
})
// info subcommand
.then(BrigadierCommand.literalArgumentBuilder("info")
.requires(source -> source.hasPermission("fastmotd.info"))
.executes(context -> {
context.getSource().sendMessage(serializer.deserialize(infoString
.replace(
"{MAINTENANCE_ENABLED}",
Settings.IMP.MAINTENANCE.MAINTENANCE_ENABLED ? Settings.IMP.MAIN.MESSAGES.YES : Settings.IMP.MAIN.MESSAGES.NO
)
));
return Command.SINGLE_SUCCESS;
})
)
// reload subcommand
.then(BrigadierCommand.literalArgumentBuilder("reload")
.requires(source -> source.hasPermission("fastmotd.reload"))
.executes(context -> {
plugin.reload();
context.getSource().sendMessage(reloadComponent);
return Command.SINGLE_SUCCESS;
})
)
.build();
return new BrigadierCommand(fastmotdNode);
}
}
Loading
Loading