Added support for setups with multiple servers with Authme installed

This commit is contained in:
4drian3d 2022-03-30 18:12:03 -05:00
parent 6f40766d75
commit bd66525125
5 changed files with 56 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -25,6 +26,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
public class AuthMeVelocityPlugin { public class AuthMeVelocityPlugin {
public static final ChannelIdentifier AUTHMEVELOCITY_CHANNEL = MinecraftChannelIdentifier.create("authmevelocity", "main");
private final ProxyServer proxy; private final ProxyServer proxy;
private final Logger logger; private final Logger logger;
private final Path pluginDirectory; private final Path pluginDirectory;
@ -48,7 +50,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(AUTHMEVELOCITY_CHANNEL);
proxy.getEventManager().register(this, new ProxyListener(config, api, logger, proxy)); 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));

View File

@ -3,6 +3,7 @@ package com.glyart.authmevelocity.proxy.listener;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
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.event.PreSendOnLoginEvent; import com.glyart.authmevelocity.proxy.event.PreSendOnLoginEvent;
@ -41,7 +42,7 @@ public class PluginMessageListener {
@Subscribe @Subscribe
public void onPluginMessage(final PluginMessageEvent event, Continuation continuation) { public void onPluginMessage(final PluginMessageEvent event, Continuation continuation) {
if (!(event.getSource() instanceof ServerConnection) || !event.getIdentifier().getId().equals("authmevelocity:main")){ if (!(event.getSource() instanceof ServerConnection) || !event.getIdentifier().equals(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL)){
continuation.resume(); continuation.resume();
return; return;
} }
@ -49,7 +50,7 @@ public class PluginMessageListener {
event.setResult(PluginMessageEvent.ForwardResult.handled()); event.setResult(PluginMessageEvent.ForwardResult.handled());
ByteArrayDataInput input = event.dataAsDataStream(); final ByteArrayDataInput input = event.dataAsDataStream();
final String sChannel = input.readUTF(); final String sChannel = input.readUTF();
final String playername = input.readUTF(); final String playername = input.readUTF();
final @Nullable Player loggedPlayer = proxy.getPlayer(playername).orElse(null); final @Nullable Player loggedPlayer = proxy.getPlayer(playername).orElse(null);

View File

@ -2,10 +2,13 @@ package com.glyart.authmevelocity.proxy.listener;
import java.util.Optional; 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.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
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.PostOrder;
@ -14,6 +17,7 @@ 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.PlayerChooseInitialServerEvent;
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
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;
@ -78,19 +82,28 @@ public final class ProxyListener {
@Subscribe @Subscribe
public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) { public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) {
if (api.isLogged(event.getPlayer())){ if (!event.getResult().isAllowed() && api.isLogged(event.getPlayer())){
continuation.resume(); continuation.resume();
return; return;
} }
if(!event.getResult().getServer().map(api::isAuthServer).orElse(false)){
event.getResult().getServer().ifPresent(server -> { continuation.resume();
if(!api.isAuthServer(server)){ return;
event.setResult(ServerPreConnectEvent.ServerResult.denied()); }
} event.setResult(ServerPreConnectEvent.ServerResult.denied());
});
continuation.resume(); continuation.resume();
} }
@Subscribe
public void onServerPostConnect(ServerPostConnectEvent event) {
Player player = event.getPlayer();
if(api.isInAuthServer(player)){
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("LOGIN");
player.sendPluginMessage(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL, buf.toByteArray());
}
}
@Subscribe(order = PostOrder.FIRST) @Subscribe(order = PostOrder.FIRST)
public EventTask onTabComplete(TabCompleteEvent event){ public EventTask onTabComplete(TabCompleteEvent event){
return EventTask.async(() -> { return EventTask.async(() -> {
@ -106,8 +119,7 @@ public final class ProxyListener {
continuation.resume(); continuation.resume();
return; return;
} }
Optional<RegisteredServer> optionalSV = event.getInitialServer(); if(event.getInitialServer().map(api::isAuthServer).orElse(false)){
if(optionalSV.isPresent() && api.isAuthServer(optionalSV.get())){
continuation.resume(); continuation.resume();
return; return;
} }

View File

@ -1,6 +1,7 @@
package com.glyart.authmevelocity.spigot; package com.glyart.authmevelocity.spigot;
import com.glyart.authmevelocity.spigot.listeners.AuthMeListener; import com.glyart.authmevelocity.spigot.listeners.AuthMeListener;
import com.glyart.authmevelocity.spigot.listeners.MessageListener;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
@ -14,6 +15,7 @@ public class AuthMeVelocityPlugin extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
this.getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL); this.getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL);
this.getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, new MessageListener());
this.getServer().getPluginManager().registerEvents(new AuthMeListener(this), this); this.getServer().getPluginManager().registerEvents(new AuthMeListener(this), this);
if(this.getServer().getPluginManager().isPluginEnabled("MiniPlaceholders")){ if(this.getServer().getPluginManager().isPluginEnabled("MiniPlaceholders")){

View File

@ -0,0 +1,27 @@
package com.glyart.authmevelocity.spigot.listeners;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull;
import fr.xephi.authme.api.v3.AuthMeApi;
public class MessageListener implements PluginMessageListener {
@Override
public void onPluginMessageReceived(@NotNull String identifier, @NotNull Player player, @NotNull byte[] bytes) {
if(identifier.equals("authmevelocity")){
ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
String subchannel = input.readUTF();
if("main".equals(subchannel)){
String msg = input.readUTF();
if("LOGIN".equals(msg)){
AuthMeApi.getInstance().forceLogin(player);
}
}
}
}
}