Skip to content

Commit

Permalink
Add MultiLobby hook
Browse files Browse the repository at this point in the history
Add new hook manager
  • Loading branch information
Corenb committed Feb 23, 2021
1 parent 3ad219c commit d4f9bb9
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 89 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,12 @@
<version>1.0.5.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cz.gameteam</groupId>
<artifactId>MultiLobby</artifactId>
<version>1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
153 changes: 85 additions & 68 deletions src/main/java/eu/horyzon/premiumconnector/PremiumConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

Expand All @@ -21,31 +19,34 @@
import eu.horyzon.premiumconnector.listeners.MessageChannelListener;
import eu.horyzon.premiumconnector.listeners.PreLoginListener;
import eu.horyzon.premiumconnector.listeners.ServerConnectListener;
import eu.horyzon.premiumconnector.session.PlayerSession;
import eu.horyzon.premiumconnector.manager.RedirectManager;
import eu.horyzon.premiumconnector.redirect.AuthRedirect;
import eu.horyzon.premiumconnector.redirect.MultiLobbyRedirect;
import eu.horyzon.premiumconnector.redirect.ServerRedirect;
import eu.horyzon.premiumconnector.session.PlayerSessionManager;
import eu.horyzon.premiumconnector.sql.DataSource;
import eu.horyzon.premiumconnector.sql.SQLManager;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;

public class PremiumConnector extends Plugin {
private static PremiumConnector instance;

private Configuration config;
private MojangResolver resolver;
private DataSource source;
private SQLManager SQLManager;
private ServerInfo crackedServer;
private boolean secondAttempt,
blockServerSwitch;
private int timeCommand;

private Set<String> geyserProxies = new HashSet<>(),
secondAttempts = new HashSet<>();
private Map<String, ServerInfo> pendingRedirections = new HashMap<>();
private Map<String, PlayerSession> playerSession = new HashMap<>();

private RedirectManager redirectManager;
private PlayerSessionManager playerSessionManager;

@Override
public void onEnable() {
Expand All @@ -55,47 +56,30 @@ public void onEnable() {
getDataFolder().mkdir();

try {
Configuration config = loadConfiguration(getDataFolder(), "config.yml");

getLogger().setLevel(Level.parse(config.getString("debug", "INFO")));
getLogger().info("Debug level set to " + getLogger().getLevel());

if ( (crackedServer = getProxy().getServerInfo(config.getString("authServer"))) == null) {
getLogger().warning("Please provide a correct cracked server name in the configuration file.");
return;
}

secondAttempt = config.getBoolean("secondAttempt", true);
blockServerSwitch = config.getBoolean("blockServerSwitch", true);
timeCommand = config.getInt("timeToConfirm", 30);
geyserProxies.addAll(config.getStringList("geyserProxy"));

// Setup Database
Configuration configBackend = config.getSection("backend");
try {
source = new DataSource(this, configBackend.getString("driver"), configBackend.getString("host"), configBackend.getInt("port", 3306), configBackend.getString("user"), configBackend.getString("password"), configBackend.getString("database"), configBackend.getString("table"), configBackend.getBoolean("useSSL", true));
} catch (RuntimeException | SQLException exception) {
if (configBackend.getString("driver").contains("sqlite"))
getLogger().warning("You select SQLite as backend but Bungeecord don't support it per default.\r\nIf you want to use SQLite, you need to install the driver yourself\r\nEasy way : https://www.spigotmc.org/resources/sqlite-for-bungeecord.57191/\r\nHard way : https://gist.github.com/games647/d2a57abf90f707c0bd1107e432c580f3");
setupConfiguration();
setupDatabase();
setupMessages();
setupCommands();

// Initialize AuthRedirect
AuthRedirect authRedirect;
MultiLobbyRedirect multiLobby = new MultiLobbyRedirect(this);
if (multiLobby.isEnabled())
authRedirect = multiLobby;
else {
ServerInfo crackedServer = getProxy().getServerInfo(config.getString("authServer"));
if (crackedServer != null)
authRedirect = new ServerRedirect(crackedServer);
else
getLogger().warning("Please configure your database informations.");

exception.printStackTrace();
return;
throw new Exception("Please provide a correct cracked server name in the configuration file.");
}

// Initialize SQLManager
SQLManager = new SQLManager(this, source);
redirectManager = new RedirectManager(this, authRedirect);
playerSessionManager = new PlayerSessionManager();

// Initialize MojangResolver
resolver = new MojangResolver();

Message.setup(loadConfiguration(getDataFolder(), "locales/message_" + config.getString("locale", "en") + ".yml"));

// INITIATE COMMANDS
for (CommandType command : CommandType.values())
getProxy().getPluginManager().registerCommand(this, new CommandBase(this, command));

getProxy().getPluginManager().registerListener(this, new ServerConnectListener(this));
getProxy().getPluginManager().registerListener(this, new PreLoginListener(this));
if (!config.getBoolean("fixUUID", true))
Expand All @@ -111,7 +95,53 @@ public void onEnable() {
} catch (IOException exception) {
exception.printStackTrace();
getLogger().warning("Error on loading configuration file...");
} catch (Exception exception) {
exception.printStackTrace();
getLogger().warning("Error on loading plugin...");
}
}

private void setupConfiguration() throws IOException {
config = loadConfiguration(getDataFolder(), "config.yml");

getLogger().setLevel(Level.parse(config.getString("debug", "INFO")));
getLogger().info("Debug level set to " + getLogger().getLevel());

secondAttempt = config.getBoolean("secondAttempt", true);
blockServerSwitch = config.getBoolean("blockServerSwitch", true);
geyserProxies.addAll(config.getStringList("geyserProxy"));
}

private void setupDatabase() throws Exception {
Configuration backendSection = config.getSection("backend");
try {
source = new DataSource(this, backendSection.getString("driver"), backendSection.getString("host"), backendSection.getInt("port", 3306), backendSection.getString("user"), backendSection.getString("password"), backendSection.getString("database"), backendSection.getString("table"), backendSection.getBoolean("useSSL", true));
} catch (RuntimeException | SQLException exception) {
if (backendSection.getString("driver").contains("sqlite"))
getLogger().warning("You select SQLite as backend but Bungeecord don't support it per default.\r\nIf you want to use SQLite, you need to install the driver yourself\r\nEasy way : https://www.spigotmc.org/resources/sqlite-for-bungeecord.57191/\r\nHard way : https://gist.github.com/games647/d2a57abf90f707c0bd1107e432c580f3");
else
getLogger().warning("Please configure your database informations.");

throw exception;
}

SQLManager = new SQLManager(this, source);
}

private void setupMessages() throws IOException {
Configuration messageConfiguration;
try {
messageConfiguration = loadConfiguration(getDataFolder(), "locales/message_" + config.getString("locale", "en") + ".yml");
} catch (NullPointerException exception) {
messageConfiguration = loadConfiguration(getDataFolder(), "locales/message_en.yml");
}

Message.setup(messageConfiguration);
}

private void setupCommands() {
for (CommandType command : CommandType.values())
getProxy().getPluginManager().registerCommand(this, new CommandBase(this, command));
}

private Configuration loadConfiguration(File directory, String fileName) throws IOException {
Expand All @@ -122,10 +152,6 @@ private Configuration loadConfiguration(File directory, String fileName) throws

try (InputStream in = getResourceAsStream(fileName)) {
Files.copy(in, file.toPath());
} catch (NullPointerException exception) {
return loadConfiguration(directory, "message_en.yml");
} catch (IOException exception) {
exception.printStackTrace();
}
}

Expand All @@ -136,6 +162,10 @@ public static PremiumConnector getInstance() {
return instance;
}

public Configuration getConfig() {
return config;
}

public DataSource getDataSource() {
return source;
}
Expand All @@ -144,12 +174,16 @@ public SQLManager getSQLManager() {
return SQLManager;
}

public MojangResolver getResolver() {
return resolver;
public RedirectManager getRedirectManager() {
return redirectManager;
}

public ServerInfo getCrackedServer() {
return crackedServer;
public PlayerSessionManager getPlayerSessionManager() {
return playerSessionManager;
}

public MojangResolver getResolver() {
return resolver;
}

public boolean isFloodgate() {
Expand All @@ -164,27 +198,10 @@ public boolean isBlockServerSwitch() {
return blockServerSwitch;
}

public int getTimeCommand() {
return timeCommand;
}

public Set<String> getSecondAttempts() {
return secondAttempts;
}

public Map<String, ServerInfo> getRedirectionRequests() {
return pendingRedirections;
}

public Map<String, PlayerSession> getPlayerSession() {
return playerSession;
}

public void redirect(String name) {
if (pendingRedirections.containsKey(name) && pendingRedirections.get(name) != null)
((UserConnection) getProxy().getPlayer(name)).connect(pendingRedirections.remove(name), null, true, ServerConnectEvent.Reason.PLUGIN);
}

public boolean isFromGeyserProxy(String address) {
return geyserProxies.stream().anyMatch((proxy) -> address.matches(proxy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
public class CommandBase extends Command {
private final PremiumConnector plugin;
private final CommandType command;
private final int cooldown;
private final Map<UUID, Long> confirm = new HashMap<>();

public CommandBase(PremiumConnector plugin, CommandType command) {
super(command.getCommandName(), command.getPermission(), command.getAliases());
this.plugin = plugin;
this.command = command;
cooldown = plugin.getConfig().getInt("timeToConfirm", 30);
}

@Override
Expand Down Expand Up @@ -70,6 +72,6 @@ protected void update(CommandSender sender, String playerName, CommandType comma
}

private boolean canConfirm(Long time) {
return time == null ? false : System.currentTimeMillis() - time < plugin.getTimeCommand() * 1000;
return time == null ? false : System.currentTimeMillis() - time < cooldown * 1000;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eu.horyzon.premiumconnector.exceptions;

import eu.horyzon.premiumconnector.hooks.Hook;

public class InitHookException extends Exception {
private final Hook hook;

public InitHookException(Hook hook, String message) {
super(message);
this.hook = hook;
}

public Hook getHook() {
return hook;
}
}
38 changes: 38 additions & 0 deletions src/main/java/eu/horyzon/premiumconnector/hooks/Hook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eu.horyzon.premiumconnector.hooks;

import eu.horyzon.premiumconnector.PremiumConnector;

public abstract class Hook {
protected final PremiumConnector plugin;
protected final HookType type;
protected boolean enabled = false;

protected Hook(HookType type, PremiumConnector plugin) {
this.type = type;
this.plugin = plugin;

if (! (enabled = ! (plugin.getProxy().getPluginManager().getPlugin(getName()) == null)))
return;

init();

if (isEnabled())
plugin.getLogger().info("Hook with " + getName() + " correctly loaded !");
else
plugin.getLogger().warning("Hook with " + getName() + " detected but not properly loaded !");
}

public String getName() {
return type.getPluginName();
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

protected void init() {}
}
15 changes: 15 additions & 0 deletions src/main/java/eu/horyzon/premiumconnector/hooks/HookType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package eu.horyzon.premiumconnector.hooks;

public enum HookType {
MULTI_LOBBY("MultiLobby");

private final String pluginName;

HookType(String hookName) {
this.pluginName = hookName;
}

public String getPluginName() {
return pluginName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,20 @@ public LockLoginListener(PremiumConnector plugin) {

@EventHandler
public void onPlayerRegister(PlayerRegisterEvent event) {
String name = event.getPlayer().getName();
plugin.redirect(name.toLowerCase());
plugin.getLogger().fine("Plugin receive register event from LockLogin for player " + name + ".");
ProxiedPlayer player = event.getPlayer();
plugin.getRedirectManager().redirect(player);
plugin.getLogger().fine("Plugin receive register event from LockLogin for player " + player.getName() + ".");
}

@EventHandler
public void onPlayerVerify(PlayerAuthEvent event) {
String name = event.getPlayer().getName();
switch (event.getAuthResult()) {
case SUCCESS:
case SUCCESS_TEMP:
plugin.redirect(name.toLowerCase());
plugin.getLogger().fine("Plugin receive login event from LockLogin for player " + name + ".");
ProxiedPlayer player = event.getPlayer();

plugin.getRedirectManager().redirect(player);
plugin.getLogger().fine("Plugin receive login event from LockLogin for player " + player.getName() + ".");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void onPluginMessage(final PluginMessageEvent event) {
switch (type) {
case "login":
String name = dataIn.readUTF();
plugin.redirect(name);
plugin.getRedirectManager().redirect(name);

plugin.getLogger().fine("Plugin receive login message from AuthMe for player " + name + ".");
}
}
Expand Down
Loading

0 comments on commit d4f9bb9

Please sign in to comment.