Skip to content

Commit

Permalink
Merge pull request #42 from 4drian3d/feat/command
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d authored Jul 27, 2022
2 parents 01b41e4 + 609a02e commit 30023ec
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.glyart.authmevelocity</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>2.1.1</version>
<version>2.2.0</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
2 changes: 1 addition & 1 deletion proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.glyart.authmevelocity</groupId>
<version>2.1.1</version>
<version>2.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package com.glyart.authmevelocity.proxy;

import com.glyart.authmevelocity.proxy.commands.AuthmeCommand;
import com.glyart.authmevelocity.proxy.config.AuthMeConfig;
import com.glyart.authmevelocity.proxy.listener.FastLoginListener;
import com.glyart.authmevelocity.proxy.listener.PluginMessageListener;
import com.glyart.authmevelocity.proxy.listener.ProxyListener;
import com.google.inject.Inject;
import com.moandjiezana.toml.Toml;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;

import net.kyori.adventure.text.minimessage.MiniMessage;

import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
Expand All @@ -32,7 +38,9 @@ public class AuthMeVelocityPlugin {
private final Logger logger;
private final Path pluginDirectory;
private AuthmeVelocityAPI api;
private AuthMeConfig config;

private final List<Object> listeners = new ArrayList<>(3);
protected final Set<UUID> loggedPlayers = Collections.synchronizedSet(new HashSet<>());

@Inject
Expand All @@ -44,30 +52,20 @@ public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Pat

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
Toml toml = this.loadConfig(pluginDirectory);
if (toml == null) {
if (!this.reload()) {
logger.warn("Failed to load config.toml. Shutting down.");
return;
}
AuthMeConfig config = new AuthMeConfig(toml);
this.api = new AuthmeVelocityAPI(this, config);
proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL);
proxy.getEventManager().register(this, new ProxyListener(config, api, logger, proxy));
proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api));

if (proxy.getPluginManager().isLoaded("fastlogin")) {
proxy.getEventManager().register(this, new FastLoginListener(proxy, api));
}
proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL);

if (proxy.getPluginManager().isLoaded("miniplaceholders")) {
AuthmePlaceholders.getExpansion(this).register();
}

logger.info("-- AuthMeVelocity enabled --");
logger.info("AuthServers: {}", config.getAuthServers());
if (config.getToServerOptions().sendToServer()) {
logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers());
}
AuthmeCommand.register(this, proxy.getCommandManager());

this.sendInfoMessage();
}

protected ProxyServer getProxy(){
Expand All @@ -93,9 +91,46 @@ private Toml loadConfig(Path path){

return new Toml().read(Files.newInputStream(configPath));
} catch (IOException ex) {
logger.info("An error ocurred on configuration initialization", ex);
logger.error("An error ocurred on configuration initialization", ex);
return null;
} catch(IllegalStateException ex) {
logger.error("Invalid configuration provided", ex);
return null;
}

}

public boolean reload() {
Toml toml = this.loadConfig(pluginDirectory);
if (toml == null) {
return false;
}

this.config = new AuthMeConfig(toml);
this.api = new AuthmeVelocityAPI(this, config);

listeners.forEach(listener -> proxy.getEventManager().unregisterListener(this, listener));
listeners.clear();

listeners.add(new ProxyListener(config, api, logger, proxy));
listeners.add(new PluginMessageListener(proxy, logger, config, api));

if (proxy.getPluginManager().isLoaded("fastlogin")) {
listeners.add(new FastLoginListener(proxy, api));
}

listeners.forEach(listener -> proxy.getEventManager().register(this, listener));
return true;
}

public void sendInfoMessage() {
CommandSource source = proxy.getConsoleCommandSource();
source.sendMessage(MiniMessage.miniMessage().deserialize(
" <gray>--- <aqua>AuthMeVelocity</aqua> ---"));
source.sendMessage(MiniMessage.miniMessage().deserialize(
"<gray>AuthServers: <green>" + config.getAuthServers()));
if (config.getToServerOptions().sendToServer()) {
source.sendMessage(MiniMessage.miniMessage().deserialize(
"<gray>LobbyServers: <green>" + config.getToServerOptions().getTeleportServers()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.glyart.authmevelocity.proxy.commands;

import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;

import net.kyori.adventure.text.minimessage.MiniMessage;

public class AuthmeCommand {
private AuthmeCommand() {}

public static void register(AuthMeVelocityPlugin plugin, CommandManager manager) {
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder.<CommandSource>literal("authmevelocity")
.requires(src -> src.hasPermission("authmevelocity.commands"))
.then(LiteralArgumentBuilder.<CommandSource>literal("reload")
.executes(cmd -> {
CommandSource source = cmd.getSource();
if (plugin.reload()) {
plugin.sendInfoMessage();
source.sendMessage(MiniMessage.miniMessage().deserialize(
"<aqua>AuthmeVelocity <green>has been successfully reloaded"));
} else {
source.sendMessage(MiniMessage.miniMessage().deserialize(
"<dark_red>There was an error while reloading the configuration. <red>Check the server console"));
}
return Command.SINGLE_SUCCESS;
})
).build();

BrigadierCommand brigadier = new BrigadierCommand(command);
CommandMeta meta = manager.metaBuilder(brigadier)
.plugin(plugin)
.aliases("vauthme", "authmev")
.build();

manager.register(meta, brigadier);

}
}
2 changes: 1 addition & 1 deletion spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.glyart.authmevelocity</groupId>
<version>2.1.1</version>
<version>2.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down

0 comments on commit 30023ec

Please sign in to comment.