API & Neuron

How to use the API that Synapse & Neuron Provides

Synapse is separated in multiple Services and each one is responsible for another Part of the Synapse API

The Player Service manage all Players, the Permission Service all Permissions and so on. This means you need to get them to get access to the API

How can I get a Service?

Every Service and almost all Handlers, even the ones from Plugins, will be created and binded by Neuron to the Kernel.

What does this Mean?

It means that Neuron stores all these objects like services and they can be accessed through Neuron.

To make this easier, can you use this static method:

Synapse3.SynapseModule.Synapse.Get<T>()

This means if you want to get for Example the PlayerService can you just use Synapse.Get<PlayerService>() to get it. This will as stated before work with almost all objects created by Neuron.

Another Example would be your own Plugin Class that you can always get with

Synapse.Get<ExamplePlugin>();

Another Advantage of this is that we can properly resolve dependencies of a constructor.

using Neuron.Core.Events;
using Neuron.Core.Meta;
using Synapse3.SynapseModule.Player;

namespace DocsExample;

//You don't need to understand these two lines the constructor below is the important part
[Automatic]
public class EventHandlers : Listener
{
    private readonly PlayerService _playerService;
    
    //This constructor is automatically called by Synapse
    public EventHandlers(PlayerService playerService)
    {
        _playerService = playerService;
    }
}

Plugin and Modules class however needs to use the Inject Annotation since it is always the first object that is created and can't be resolved when it uses something that is created later

using Neuron.Core.Plugins;
using Ninject;
using Synapse3.SynapseModule.Player;

namespace DocsExample;

[Plugin(
    Author = "Dimenzio",
    Name = "ExamplePlugin",
    Description = "An Example Plugin for the Docs",
    Version = "1.0.0"
)]
public class ExamplePlugin : Plugin
{
    //The Inject Attribute will causes Neuron to automatically sets the PlayerService
    //This is a injected field like we mentioned on the previous page
    [Inject]
    public PlayerService PlayerService { get; set; }
    
    public override void Load()
    {
        Logger.Info("My Plugin Loaded");
    }

    public override void Enable()
    {
        Logger.Info("My Plugin was Enabled");
    }
}

Plugin/Module classes can only use Inject.

They can't get any Services from the constructor

Most Services/Handlers can't use inject.

They have to get other Services through the constructor

Automatic

When working with Synapse 3 you will often see the Automatic Attribute

This attribute has no function itself, however it marks a Type as this should be Automatically be handled by a Neuron Module.

Depending on what Type it is on, will the module do something different but most often it just means: create this object after the Services are all ready and register it inside the Module API

Examples of use for the Automatic Attribute

//In this case Automatic means create this Type and add it 
//to the Command Handler
[Automatic]
[SynapseCommand(
    CommandName = "Command1",
    //...
    )]
public class Command1 : SynapseCommand
{
    public override void Execute(SynapseContext context, ref CommandResult result)
    {
        ///...
    }
}

//Here does it mean create this EventHandler and Hook all Event
[Automatic]
public class EventHandlers : Listener
{
    private readonly PlayerService _playerService;

    public EventHandlers(PlayerService playerService)
    {
        _playerService = playerService;
    }

    [EventHandler]
    public void RoundStart(RoundStartEvent ev)
    {
        ///...
    }
}

Dependencies

When working with Synapse3 you will often work with Automatically created objects.

These can use every service however they will not be resolved between each other which means if you create for example 2 Commands Automatically and try to get one command in the constructor of the other this can throw an Error (if you're lucky the other Commands already exist and everything works fine but don't depend on this). If you really need to access the other Command store him inside your PluginClass and access it through the PluginClass later

This can throw an Error

using Neuron.Core.Meta;
using Neuron.Modules.Commands;
using Synapse3.SynapseModule.Command;

namespace DocsExample;

[Automatic]
[SynapseCommand(
    CommandName = "Command1"
    )]
public class Command1 : SynapseCommand
{
    public float Value { get; set; } = 5f;
    
    public override void Execute(SynapseContext context, ref CommandResult result)
    {
        ///...
    }
}

[Automatic]
[SynapseCommand(
    CommandName = "Command2"
)]
public class Command2 : SynapseCommand
{
    private readonly Command1 _otherCommand;

    //Don't access another Automatic created object in the constructor like here
    public Command2(Command1 otherCommand)
    {
        _otherCommand = otherCommand;
    }
    
    public override void Execute(SynapseContext context, ref CommandResult result)
    {
        ///...
        _otherCommand.Value = 10;
    }
}

All Synapse Services

  • SynapseCommandService

  • SynapseConfigService

  • DummyService

  • ItemService

  • ElevatorService

  • RoomService

  • SchematicService

  • CustomAttributeService

  • Scp914Service

  • CassieService

  • DecontaminationService

  • HeavyZoneService

  • IntercomService

  • MapService

  • NukeService

  • RoundService

  • PermissionService

  • RemoteAdminCategoryService

  • PlayerService

  • RoleService

  • TeamService

  • ServerService

Last updated