Craftopia

Craftopia

Command Handler
Psychloor 4 Jul, 2023 @ 6:33am
Quickstart Guide
Start off by going to your plugin class and adding the dependencyattribute
// will only load plugin if commandhandler is installed [BepInDependency("eradev.craftopia.CommandHandler", BepInDependency.DependencyFlags.HardDependency)] // Alternatively to only add commandhandler code if it's installed [BepInDependency("eradev.craftopia.CommandHandler", BepInDependency.DependencyFlags.SoftDependency)]

import the reference to CommandHandler.dll
found at ´workshop\content\1307550\2996848886\plugins´

in your code where you got the command(s) begin creating ConfigEntryies for them
private ConfigEntry<string> myCustomCommandEntry;

then create a method/function to register your configentries
internal void RegisterConfig(ConfigFile configFile) { myCustomCommandEntry= configFile.Bind("Commands", "My Command", "mycommand", "[Args (name)] Description of what your command does"); }
[Args] is used in the description if there's any arguments/parameters to your command.

register your commandnames in plugin awake/start method.
private void Start() { Commands.RegisterConfig(base.Config); }

now it's time to make a method/function to register and handle your command(s)
internal void RegisterCommands() { CommandHandler.RegisterCommandsEvt += (_, _) => { // Can add several commands here CommandHandler.TryAddCommand("my mod name", ref myCustomCommandEntry); }; CommandHandler.HandleCommandEvt += (_, command) => { if (command.Name.Equals(myCustomCommandEntry.Value)) { // do things in here and return if you're handling several commands in this event return; } } }

now back to the Start method. add this after register config
Commands.RegisterCommands();

alternatively if you're using soft-dependency
var commandHandlerInstalled = BepInEx.Bootstrap.Chainloader.PluginInfos.Any(p => p.Value.Metadata.GUID.Equals("eradev.craftopia.CommandHandler", StringComparison.OrdinalIgnoreCase)); if (commandHandlerInstalled) { Commands.RegisterCommands(); }

hope this helps and good luck
Last edited by Psychloor; 7 Jul, 2023 @ 7:52am