Skip to content

Commit

Permalink
Multi-module project, initial velocity implementation [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
sgdc3 committed Aug 19, 2023
1 parent 24a8924 commit f832dfb
Show file tree
Hide file tree
Showing 42 changed files with 1,510 additions and 728 deletions.
74 changes: 74 additions & 0 deletions bungee/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>fr.xephi</groupId>
<artifactId>authmeproxy-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
</parent>

<artifactId>authmeproxy-bungee</artifactId>
<name>AuthMeProxy-Bungee</name>

<build>
<finalName>${project.name}-${project.version}-noshade</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<!-- Bungeecord repository -->
<repository>
<id>oss-repo</id>
<url>https://oss.sonatype.org/content/groups/public</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>fr.xephi</groupId>
<artifactId>authmeproxy-common</artifactId>
<version>2.3.0-SNAPSHOT</version>
</dependency>

<!-- Bungeecord API -->
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<type>javadoc</type>
<scope>provided</scope>
</dependency>

<!-- Metrics -->
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bungeecord</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.xephi.authmeproxy.bungee;

import fr.xephi.authmeproxy.common.AbstractAuthMeLogger;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;

public class AuthMeBungeeLogger extends AbstractAuthMeLogger {

public java.util.logging.Logger logger;

public AuthMeBungeeLogger(java.util.logging.Logger logger) {
this.logger = logger;
}

@Override
public void send(String message) {
ProxyServer.getInstance().getConsole().sendMessage(TextComponent.fromLegacyText(message));
}

@Override
public void info(String... messages) {
this.logger.info(String.join(" ", messages));
}

@Override
public void warn(String... messages) {
this.logger.warning(String.join(" ", messages));
}

@Override
public void error(String... messages) {
this.logger.severe(String.join(" ", messages));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fr.xephi.authmeproxy.bungee;

import fr.xephi.authmeproxy.bungee.listeners.MessageListener;
import fr.xephi.authmeproxy.bungee.listeners.PlayerListener;
import fr.xephi.authmeproxy.bungee.platform.BungeePlatform;
import fr.xephi.authmeproxy.bungee.platform.BungeePlayer;
import fr.xephi.authmeproxy.common.AbstractAuthMeProxy;
import fr.xephi.authmeproxy.common.commands.AbstractCommand;
import fr.xephi.authmeproxy.common.commands.ReloadCommand;
import fr.xephi.authmeproxy.common.platform.AbstractPlayer;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;
import org.bstats.bungeecord.Metrics;

public class AuthMeProxyBungee extends Plugin {

private final AuthMeProxyBungeeImpl implementation;

public AuthMeProxyBungee() {
implementation = new AuthMeProxyBungeeImpl(this);
}

// Dirty hack as we can't extend multiple classes
public static class AuthMeProxyBungeeImpl extends AbstractAuthMeProxy<BungeePlatform> {

private final AuthMeProxyBungee plugin;

public AuthMeProxyBungeeImpl(AuthMeProxyBungee plugin) {
super(
new BungeePlatform(),
new AuthMeBungeeLogger(plugin.getLogger()),
plugin.getDataFolder()
);
this.plugin = plugin;
}

@Override
protected void registerCommands() {
plugin.registerCommand(injector.getSingleton(ReloadCommand.class));
}

@Override
protected void registerListeners() {
ProxyServer.getInstance().getPluginManager().registerListener(plugin, injector.getSingleton(MessageListener.class));
ProxyServer.getInstance().getPluginManager().registerListener(plugin, injector.getSingleton(PlayerListener.class));
}

@Override
protected void registerMetrics() {
// Send metrics data
new Metrics(plugin, 1880);
}
}

@Override
public void onEnable() {
implementation.initialize();
}

private void registerCommand(AbstractCommand command) {
ProxyServer.getInstance().getPluginManager().registerCommand(this, new Command(
command.getCommand(),
command.getPermission()
) {
@Override
public void execute(CommandSender sender, String[] args) {
ProxiedPlayer player = null;
if (sender instanceof ProxiedPlayer) {
player = (ProxiedPlayer) sender;
}
AbstractPlayer abstractPlayer = player == null ? null : BungeePlayer.fromPlayer(player);
command.execute(abstractPlayer, args);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.xephi.authmeproxy.bungee.listeners;

import fr.xephi.authmeproxy.common.services.MessageHandleService;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

import javax.inject.Inject;

public class MessageListener implements Listener {

@Inject
private MessageHandleService messageHandleService;

@EventHandler
public void onPluginMessage(final PluginMessageEvent event) {
if (event.isCancelled()) {
return;
}

boolean fromPlayer = !(event.getSender() instanceof Server);
messageHandleService.handleMessage(fromPlayer, event.getTag(), event.getData());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package fr.xephi.authmeproxy.bungee.listeners;

import fr.xephi.authmeproxy.bungee.platform.BungeePlayer;
import fr.xephi.authmeproxy.common.services.AuthStateService;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.*;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;

import javax.inject.Inject;

public class PlayerListener implements Listener {

@Inject
private AuthStateService authStateService;

@EventHandler
public void onPlayerJoin(final PostLoginEvent event) {
// Register player in our list
authStateService.trackUser(event.getPlayer().getName());
}

@EventHandler
public void onPlayerDisconnect(final PlayerDisconnectEvent event) {
// Remove player from out list
authStateService.untrackUser(event.getPlayer().getName());
}

@EventHandler(priority = EventPriority.LOWEST)
public void onCommand(final ChatEvent event) {
if (event.isCancelled()) {
return;
}
if (!event.isCommand() && event.isProxyCommand()) {
return;
}
if (!(event.getSender() instanceof ProxiedPlayer)) {
return;
}
ProxiedPlayer player = (ProxiedPlayer) event.getSender();
String command = event.getMessage().split(" ")[0].toLowerCase();
if (!authStateService.shouldAllowCommand(BungeePlayer.fromPlayer(player), command)) {
event.setCancelled(true);
}
}

// Priority is set to lowest to keep compatibility with some chat plugins
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChat(final ChatEvent event) {
if (event.isCancelled()) {
return;
}
if (event.isCommand() || event.isProxyCommand()) {
return;
}
if (!(event.getSender() instanceof ProxiedPlayer)) {
return;
}
ProxiedPlayer player = (ProxiedPlayer) event.getSender();

if (!authStateService.shouldAllowChat(BungeePlayer.fromPlayer(player))) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerConnectedToServer(final ServerConnectedEvent event) {
ProxiedPlayer player = event.getPlayer();
Server server = event.getServer();
authStateService.handleServerConnected(BungeePlayer.fromPlayer(player), server.getInfo().getName());
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerConnectingToServer(final ServerConnectEvent event) {
if (event.isCancelled()) {
return;
}

ProxiedPlayer player = event.getPlayer();
String cancelReason = authStateService.shouldAllowServerSwitch(
BungeePlayer.fromPlayer(player),
event.getTarget().getName()
).orElse(null);
if (cancelReason == null) {
return;
}
event.setCancelled(true);
BaseComponent[] parsedCancelReason = TextComponent.fromLegacyText(cancelReason);
if (event.getPlayer().getServer() == null || !event.getPlayer().getServer().isConnected()) {
player.disconnect(parsedCancelReason);
} else {
player.sendMessage(parsedCancelReason);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.xephi.authmeproxy.bungee.platform;

import fr.xephi.authmeproxy.common.platform.AbstractPlatform;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;

import java.util.Collection;
import java.util.stream.Collectors;

public class BungeePlatform extends AbstractPlatform<BungeePlayer> {

public BungeePlatform() {
}

@Override
public Collection<BungeePlayer> getAllPlayers() {
return ProxyServer.getInstance().getPlayers().stream()
.map(BungeePlayer::fromPlayer)
.collect(Collectors.toUnmodifiableList());
}

@Override
public boolean connect(BungeePlayer player, String serverName) {
ServerInfo serverInfo = ProxyServer.getInstance().getServerInfo(serverName);
if (serverInfo == null) {
return false;
}
player.getHandle().connect(serverInfo);
return true;
}
}
Loading

0 comments on commit f832dfb

Please sign in to comment.