Search in sources :

Example 1 with IPermissionHandlerFactory

use of net.minecraftforge.server.permission.handler.IPermissionHandlerFactory in project MinecraftForge by MinecraftForge.

the class PermissionAPI method initializePermissionAPI.

/**
 * <p>Helper method for internal use only!</p>
 * <p>Initializes the active permission handler based on the users config.</p>
 */
public static void initializePermissionAPI() {
    Class callerClass = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass();
    if (callerClass != ServerLifecycleHooks.class) {
        LOGGER.warn("{} tried to initialize the PermissionAPI, this call will be ignored.", callerClass.getName());
        return;
    }
    PermissionAPI.activeHandler = null;
    PermissionGatherEvent.Handler handlerEvent = new PermissionGatherEvent.Handler();
    MinecraftForge.EVENT_BUS.post(handlerEvent);
    Map<ResourceLocation, IPermissionHandlerFactory> availableHandlers = handlerEvent.getAvailablePermissionHandlerFactories();
    try {
        ResourceLocation selectedPermissionHandler = new ResourceLocation(ForgeConfig.SERVER.permissionHandler.get());
        if (!availableHandlers.containsKey(selectedPermissionHandler)) {
            LOGGER.error("Unable to find configured permission handler {}, will use {}", selectedPermissionHandler, DefaultPermissionHandler.IDENTIFIER);
            selectedPermissionHandler = DefaultPermissionHandler.IDENTIFIER;
        }
        IPermissionHandlerFactory factory = availableHandlers.get(selectedPermissionHandler);
        PermissionGatherEvent.Nodes nodesEvent = new PermissionGatherEvent.Nodes();
        MinecraftForge.EVENT_BUS.post(nodesEvent);
        PermissionAPI.activeHandler = factory.create(nodesEvent.getNodes());
        if (!selectedPermissionHandler.equals(activeHandler.getIdentifier()))
            LOGGER.warn("Identifier for permission handler {} does not match registered one {}", activeHandler.getIdentifier(), selectedPermissionHandler);
        LOGGER.info("Successfully initialized permission handler {}", PermissionAPI.activeHandler.getIdentifier());
    } catch (ResourceLocationException e) {
        LOGGER.error("Error parsing config value 'permissionHandler'", e);
    }
}
Also used : PermissionGatherEvent(net.minecraftforge.server.permission.events.PermissionGatherEvent) IPermissionHandlerFactory(net.minecraftforge.server.permission.handler.IPermissionHandlerFactory) ResourceLocation(net.minecraft.resources.ResourceLocation) ResourceLocationException(net.minecraft.ResourceLocationException) IPermissionHandler(net.minecraftforge.server.permission.handler.IPermissionHandler) DefaultPermissionHandler(net.minecraftforge.server.permission.handler.DefaultPermissionHandler)

Aggregations

ResourceLocationException (net.minecraft.ResourceLocationException)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 PermissionGatherEvent (net.minecraftforge.server.permission.events.PermissionGatherEvent)1 DefaultPermissionHandler (net.minecraftforge.server.permission.handler.DefaultPermissionHandler)1 IPermissionHandler (net.minecraftforge.server.permission.handler.IPermissionHandler)1 IPermissionHandlerFactory (net.minecraftforge.server.permission.handler.IPermissionHandlerFactory)1