Add "Ensure first server is an Auth Server" config
- Add isAuthServer(String) to API - Add support for HEX colors in messages
This commit is contained in:
parent
97882d1e31
commit
38c11882f6
@ -49,7 +49,7 @@ public class AuthMeVelocityPlugin {
|
|||||||
AuthMeConfig config = Objects.requireNonNull(new AuthMeConfig(toml), "configuration cannot be null");
|
AuthMeConfig config = Objects.requireNonNull(new AuthMeConfig(toml), "configuration cannot be null");
|
||||||
this.api = new AuthmeVelocityAPI(this, config);
|
this.api = new AuthmeVelocityAPI(this, config);
|
||||||
proxy.getChannelRegistrar().register(MinecraftChannelIdentifier.create("authmevelocity", "main"));
|
proxy.getChannelRegistrar().register(MinecraftChannelIdentifier.create("authmevelocity", "main"));
|
||||||
proxy.getEventManager().register(this, new ProxyListener(config, api));
|
proxy.getEventManager().register(this, new ProxyListener(config, api, logger, proxy));
|
||||||
proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api));
|
proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api));
|
||||||
|
|
||||||
if(proxy.getPluginManager().isLoaded("fastlogin")){
|
if(proxy.getPluginManager().isLoaded("fastlogin")){
|
||||||
|
@ -86,4 +86,13 @@ public final class AuthmeVelocityAPI {
|
|||||||
public boolean isAuthServer(@NotNull ServerConnection connection){
|
public boolean isAuthServer(@NotNull ServerConnection connection){
|
||||||
return config.getAuthServers().contains(connection.getServerInfo().getName());
|
return config.getAuthServers().contains(connection.getServerInfo().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a string is an name of an auth server
|
||||||
|
* @param server the server name
|
||||||
|
* @return if the server is an auth serverr
|
||||||
|
*/
|
||||||
|
public boolean isAuthServer(@NotNull String server){
|
||||||
|
return config.getAuthServers().contains(server);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,13 @@ public final class AuthMeConfig {
|
|||||||
private final List<String> authServers;
|
private final List<String> authServers;
|
||||||
private final ServerOnLogin serverOnLogin;
|
private final ServerOnLogin serverOnLogin;
|
||||||
private final Commands commands;
|
private final Commands commands;
|
||||||
|
private final EnsureAuthServer ensure;
|
||||||
|
|
||||||
public AuthMeConfig(Toml toml){
|
public AuthMeConfig(Toml toml){
|
||||||
this.authServers = toml.getList("authServers");
|
this.authServers = toml.getList("authServers");
|
||||||
this.serverOnLogin = toml.getTable("SendOnLogin").to(ServerOnLogin.class);
|
this.serverOnLogin = toml.getTable("SendOnLogin").to(ServerOnLogin.class);
|
||||||
this.commands = toml.getTable("Commands").to(Commands.class);
|
this.commands = toml.getTable("Commands").to(Commands.class);
|
||||||
|
this.ensure = toml.getTable("EnsureAuthServer").to(EnsureAuthServer.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ServerOnLogin {
|
public static class ServerOnLogin {
|
||||||
@ -42,6 +44,20 @@ public final class AuthMeConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class EnsureAuthServer {
|
||||||
|
private boolean ensureFirstServerIsAuthServer;
|
||||||
|
private String disconnectMessage;
|
||||||
|
|
||||||
|
public boolean ensureAuthServer(){
|
||||||
|
return this.ensureFirstServerIsAuthServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDisconnectMessage(){
|
||||||
|
return this.disconnectMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Commands getCommandsConfig(){
|
public Commands getCommandsConfig(){
|
||||||
return this.commands;
|
return this.commands;
|
||||||
}
|
}
|
||||||
@ -50,6 +66,10 @@ public final class AuthMeConfig {
|
|||||||
return this.serverOnLogin;
|
return this.serverOnLogin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EnsureAuthServer getEnsureOptions(){
|
||||||
|
return this.ensure;
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> getAuthServers(){
|
public List<String> getAuthServers(){
|
||||||
return this.authServers;
|
return this.authServers;
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,11 @@ import com.velocitypowered.api.proxy.Player;
|
|||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
|
|
||||||
public final class ConfigUtils {
|
public final class ConfigUtils {
|
||||||
|
public static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder().character('&').hexColors().build();
|
||||||
public static void sendBlockedMessage(Player player, AuthMeConfig config){
|
public static void sendBlockedMessage(Player player, AuthMeConfig config){
|
||||||
String blockedMessage = config.getCommandsConfig().getBlockedMessage();
|
String blockedMessage = config.getCommandsConfig().getBlockedMessage();
|
||||||
if(!blockedMessage.isBlank()){
|
if(!blockedMessage.isBlank()){
|
||||||
player.sendMessage(
|
player.sendMessage(SERIALIZER.deserialize(blockedMessage));
|
||||||
LegacyComponentSerializer.legacyAmpersand().deserialize(
|
|
||||||
blockedMessage));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private ConfigUtils(){}
|
private ConfigUtils(){}
|
||||||
|
@ -1,28 +1,42 @@
|
|||||||
package com.glyart.authmevelocity.proxy.listener;
|
package com.glyart.authmevelocity.proxy.listener;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin;
|
||||||
import com.glyart.authmevelocity.proxy.AuthmeVelocityAPI;
|
import com.glyart.authmevelocity.proxy.AuthmeVelocityAPI;
|
||||||
import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
|
import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
|
||||||
import com.glyart.authmevelocity.proxy.config.ConfigUtils;
|
import com.glyart.authmevelocity.proxy.config.ConfigUtils;
|
||||||
import com.glyart.authmevelocity.proxy.utils.AuthmeUtils;
|
import com.glyart.authmevelocity.proxy.utils.AuthmeUtils;
|
||||||
import com.velocitypowered.api.event.Continuation;
|
import com.velocitypowered.api.event.Continuation;
|
||||||
import com.velocitypowered.api.event.EventTask;
|
import com.velocitypowered.api.event.EventTask;
|
||||||
|
import com.velocitypowered.api.event.PostOrder;
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
import com.velocitypowered.api.event.Subscribe;
|
||||||
import com.velocitypowered.api.event.command.CommandExecuteEvent;
|
import com.velocitypowered.api.event.command.CommandExecuteEvent;
|
||||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||||
|
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent;
|
||||||
import com.velocitypowered.api.event.player.ServerPreConnectEvent;
|
import com.velocitypowered.api.event.player.ServerPreConnectEvent;
|
||||||
import com.velocitypowered.api.event.player.TabCompleteEvent;
|
import com.velocitypowered.api.event.player.TabCompleteEvent;
|
||||||
import com.velocitypowered.api.proxy.Player;
|
import com.velocitypowered.api.proxy.Player;
|
||||||
|
import com.velocitypowered.api.proxy.ProxyServer;
|
||||||
|
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public final class ProxyListener {
|
public final class ProxyListener {
|
||||||
private final AuthMeConfig config;
|
private final AuthMeConfig config;
|
||||||
private final AuthmeVelocityAPI api;
|
private final AuthmeVelocityAPI api;
|
||||||
|
private final ProxyServer proxy;
|
||||||
|
private final Logger logger;
|
||||||
|
|
||||||
public ProxyListener(@NotNull AuthMeConfig config, AuthmeVelocityAPI api) {
|
public ProxyListener(@NotNull AuthMeConfig config, AuthmeVelocityAPI api, Logger logger, ProxyServer proxy) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.api = api;
|
this.api = api;
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.logger = logger;
|
||||||
|
this.proxy = proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
@ -30,7 +44,7 @@ public final class ProxyListener {
|
|||||||
api.removePlayer(event.getPlayer());
|
api.removePlayer(event.getPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe(order = PostOrder.FIRST)
|
||||||
public void onCommandExecute(final CommandExecuteEvent event, Continuation continuation) {
|
public void onCommandExecute(final CommandExecuteEvent event, Continuation continuation) {
|
||||||
if (!(event.getCommandSource() instanceof Player)){
|
if (!(event.getCommandSource() instanceof Player)){
|
||||||
continuation.resume();
|
continuation.resume();
|
||||||
@ -57,7 +71,7 @@ public final class ProxyListener {
|
|||||||
continuation.resume();
|
continuation.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe(order = PostOrder.FIRST)
|
||||||
public void onPlayerChat(final PlayerChatEvent event) {
|
public void onPlayerChat(final PlayerChatEvent event) {
|
||||||
if (!api.isLogged(event.getPlayer())) {
|
if (!api.isLogged(event.getPlayer())) {
|
||||||
event.setResult(PlayerChatEvent.ChatResult.denied());
|
event.setResult(PlayerChatEvent.ChatResult.denied());
|
||||||
@ -79,11 +93,42 @@ public final class ProxyListener {
|
|||||||
continuation.resume();
|
continuation.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe(order = PostOrder.FIRST)
|
||||||
public EventTask onTabComplete(TabCompleteEvent event){
|
public EventTask onTabComplete(TabCompleteEvent event){
|
||||||
if (!api.isLogged(event.getPlayer())){
|
if (!api.isLogged(event.getPlayer())){
|
||||||
return EventTask.async(() -> event.getSuggestions().clear());
|
return EventTask.async(() -> event.getSuggestions().clear());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe(order = PostOrder.LATE)
|
||||||
|
public void onInitialServer(PlayerChooseInitialServerEvent event, Continuation continuation){
|
||||||
|
if(!config.getEnsureOptions().ensureAuthServer()){
|
||||||
|
continuation.resume();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Optional<RegisteredServer> optionalSV = event.getInitialServer();
|
||||||
|
if(optionalSV.isPresent() && api.isAuthServer(optionalSV.get())){
|
||||||
|
continuation.resume();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
RegisteredServer server = getAvailableServer();
|
||||||
|
if(server == null) {
|
||||||
|
continuation.resume();
|
||||||
|
logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername());
|
||||||
|
event.getPlayer().disconnect(ConfigUtils.SERIALIZER.deserialize(config.getEnsureOptions().getDisconnectMessage()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.setInitialServer(server);
|
||||||
|
continuation.resume();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable RegisteredServer getAvailableServer(){
|
||||||
|
for(String sv : config.getAuthServers()){
|
||||||
|
Optional<RegisteredServer> opt = proxy.getServer(sv);
|
||||||
|
if(opt.isPresent()) return opt.get();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,4 +19,11 @@ authServers = ["auth1", "auth2"]
|
|||||||
|
|
||||||
# Sets the message to send in case a non-logged-in player executes an unauthorized command
|
# Sets the message to send in case a non-logged-in player executes an unauthorized command
|
||||||
# To deactivate the message, leave it empty
|
# To deactivate the message, leave it empty
|
||||||
blockedCommandMessage = "&4You cannot execute commands if you are not logged in yet"
|
blockedCommandMessage = "&4You cannot execute commands if you are not logged in yet"
|
||||||
|
|
||||||
|
[EnsureAuthServer]
|
||||||
|
# Ensure that the first server to which players connect is an auth server
|
||||||
|
ensureFirstServerIsAuthServer = false
|
||||||
|
|
||||||
|
# Message to be sent to the player in case no auth server is available
|
||||||
|
disconnectMessage = "&4You could not connect to a login server, please try again later"
|
Loading…
x
Reference in New Issue
Block a user