adde0109 / Ambassador

This is a Velocity plugin that makes it possible to host a modern forge server behind a Velocity proxy!
GNU Lesser General Public License v2.1
82 stars 16 forks source link

Realized why AuthMeVelocity Doesnt Support Ambassador you don't have API #55

Open john1234brown opened 1 month ago

john1234brown commented 1 month ago

To allow another Velocity plugin to access the MapWithExpiration hashmap in your Ambassador plugin, you can create an API that provides a way for other plugins to interact with it. Here's a basic example of how you can create such an API:

  1. Define an interface that exposes the functionality you want to provide to other plugins. This interface should be placed in a separate package to ensure it's accessible to other plugins.

  2. Implement the interface in your Ambassador class and register an instance of this class as a service that other plugins can access.

Here's how you can do it:

  1. Define the interface in a new package (e.g., org.adde0109.ambassador.api):
package org.adde0109.ambassador.api;

import org.adde0109.ambassador.Ambassador;

import java.util.Map;

public interface AmbassadorAPI {
    Map<String, Ambassador.RegisteredServer> getTemporaryForced();
}
  1. Implement the interface in your Ambassador class:
import org.adde0109.ambassador.api.AmbassadorAPI;

public class Ambassador implements AmbassadorAPI {

    // Existing code...

    @Override
    public Map<String, RegisteredServer> getTemporaryForced() {
        return TEMPORARY_FORCED;
    }

    // Existing code...
}
  1. Register an instance of your Ambassador class as a service in your Ambassador plugin's onProxyInitialization method:
import com.velocitypowered.api.plugin.PluginManager;

public class Ambassador {

    // Existing code...

    @Inject
    public Ambassador(ProxyServer server, PluginManager pluginManager, Logger logger, @DataDirectory Path dataDirectory, Metrics.Factory metricsFactory) {
        this.server = server;
        this.logger = logger;
        this.dataDirectory = dataDirectory;
        this.metricsFactory = metricsFactory;
        Ambassador.instance = this;

        // Register the API
        pluginManager.registerService(AmbassadorAPI.class, this, this.server, PluginContainer.MINECRAFT);
    }

    // Existing code...
}
  1. Another plugin can now retrieve the MapWithExpiration hashmap from your Ambassador plugin like this:
import org.adde0109.ambassador.api.AmbassadorAPI;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.plugin.PluginContainer;

public class YourPlugin {

    private final AmbassadorAPI ambassadorAPI;

    public YourPlugin(ProxyServer server, PluginContainer pluginContainer) {
        this.ambassadorAPI = server.getServices().get(AmbassadorAPI.class).get();
    }

    public void someMethod() {
        // Access the TEMPORARY_FORCED hashmap
        Map<String, RegisteredServer> temporaryForced = ambassadorAPI.getTemporaryForced();
        // Use the hashmap as needed
    }
}

By following these steps, you can create an API for your Ambassador plugin that allows other Velocity plugins to access the MapWithExpiration hashmap.

By allowing access to at least this HashMap will allow the authme plugins to properly bypass authorization for when users are being sent to modded servers as they require the ambassador plugin to disconnect them and reconnect bypassing the authorization hub so doing this will still be secure will ensuring they properly logged in on the lobby to go to the modded server before hand etc..

john1234brown commented 1 week ago

@adde0109 your more then welcome to compile these and include them as your own just give proper references where needed for AuthMeVelocity code usage is all! https://github.com/john1234brown/Ambassador https://github.com/john1234brown/NexAuth-spigot

I've done this with the NextAuth Plugin instead using the same setup as AuthMeVelocity it is worth note trying to do it for AuthMe support for others who would like to use this with authMe rather than my version and setup!

adde0109 commented 1 week ago

Make a PR please