Skip to content
Aeldit edited this page Aug 2, 2023 · 35 revisions

CyanLib Wiki

This wiki is a work in progress

This library has 2 purposes :

  • Provide a simple config system (only accessible via commands on server and via commands or screen on single player worlds) (this part is needed for the library to work)
  • Provide a way to use custom translations for the users of your mods (this part is not needed for the library to work)

Now lets see how you can use this library in your mod

I). Instances

ModNameConfig.class and getDefaultTranslations() will be defined later in this tutorial

In my mods, I created a class Utils to store all the things needed to use this library, but you can call yours anything.

Before we do anything, we must create a constant that will store the modid, which is used many times :

public static final String MODNAME_MODID = "modname";

Then, we need to create instances of the library's different classes (in the Utils class)

These 3 instantiations are required for the library to work

// This instance stores, reads and writes the config
public static CyanLibOptionsStorage MODNAME_OPTIONS_STORAGE = new CyanLibOptionsStorage(MODNAME_MODID, ModNameConfig.class);

// Main instance of the library, used for various things
public static CyanLib MODNAME_LIB_UTILS = new CyanLib(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, MODNAME_LANGUAGE_UTILS);

// This instance allows you to register the config commands, to edit the config (we will see later how to use it)
public static CyanLibConfigCommands MODNAME_CONFIG_COMMANDS = new CyanLibConfigCommands(MODNAME_MODID, MODNAME_LIB_UTILS);

Additionally, you can use the CyanLibLanguageUtils class to use custom translations

public static CyanLibLanguageUtils MODNAME_LANGUAGE_UTILS = new CyanLibLanguageUtils(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, getDefaultTranslations());

// If you use the CyanLibLanguageUtils class, you need to add 1 parameter to the CyanLib instantiation
public static CyanLib MODNAME_LIB_UTILS = new CyanLib(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, MODNAME_LANGUAGE_UTILS);

A). Config system

To create your options, you need to create a class called ModNameConfig, in which will define your options and their default value (as well as the rule that applies to each option, but this isn't necessary for every option)

public static final CyanLibOptionsStorage.BooleanOption ALLOW_BED = YOURMODNAME_OPTIONS_STORAGE.new BooleanOption("allowBed", true);

public static final CyanLibOptionsStorage.IntegerOption MIN_OP_LVL_KGI = YOURMODNAME_OPTIONS_STORAGE.new IntegerOption("minOpLvlKgi", 4, RULES.OP_LEVELS);

public static final CyanLibOptionsStorage.BooleanOption USE_CUSTOM_TRANSLATIONS = YOURMODNAME_OPTIONS_STORAGE.new BooleanOption("useCustomTranslations", false, RULES.LOAD_CUSTOM_TRANSLATIONS);

For details on the rules, see RULES.java

B). Config Commands

Utils.java

// This instance allow the creation of commands to edit your config. To use it see the code bellow
public static CyanLibConfigCommands MODNAME_CONFIG_COMMANDS = new CyanLibConfigCommands(MODNAME_MODID, MODNAME_LIB_UTILS);

To use this, you need to write the line bellow in your onInitializeClient() function

CommandRegistrationCallback.EVENT.register((dispatcher, dedicated, environment) -> CYAN_CONFIG_COMMANDS.register(dispatcher));

C). Language system

This system allows you to choose between the translations stored in the assets of your mod (which requires the user to have the mod or a resource pack with the translations when your mod is installed on a server), and custom translations that every user of your mod (the host of the server for example) can write themselves.

// This Map needs to be defined before any instantiation of this mod's classes
private static final HashMap<String, String> YOURMODNAME_DEFAULT_TRANSLATIONS = new HashMap<>();

// If you want to use the config system with the language part, you can instantiate the library's main class like so :
public static CyanLib MODNAME_LIB_UTILS = new CyanLib(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, MODNAME_LANGUAGE_UTILS);

public static CyanLibLanguageUtils MODNAME_LANGUAGE_UTILS = new CyanLibLanguageUtils(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, getDefaultTranslations());

public static @NotNull Map<String, String> getDefaultTranslations()
{
    if (MODNAME_DEFAULT_TRANSLATIONS .isEmpty())
    {
        MODNAME_DEFAULT_TRANSLATIONS.put("error.optionNotFound", "§cThis option does not exist");
        MODNAME_DEFAULT_TRANSLATIONS.put("error.wrongType", "§cThis option can only be set to the %s §ctype");

        MODNAME_DEFAULT_TRANSLATIONS.put("currentValue", "§7Current value : %s");
        MODNAME_DEFAULT_TRANSLATIONS.put("setValue", "§7Set value to : %s  %s  %s  %s  %s");
        MODNAME_DEFAULT_TRANSLATIONS .put("translationsReloaded", "§3Custom translations have been reloaded");

        MODNAME_DEFAULT_TRANSLATIONS.put("set.useCustomTranslations", "§3Toggled custom translations %s");
        MODNAME_DEFAULT_TRANSLATIONS.put("set.msgToActionBar", "§3Toggled messages to action bar %s");
        MODNAME_DEFAULT_TRANSLATIONS.put("set.minOpLevelExeEditConfig", "§3The minimum OP level to edit the config is now %s");

        MODNAME_DEFAULT_TRANSLATIONS.put("dashSeparation", "§6------------------------------------");
        MODNAME_DEFAULT_TRANSLATIONS.put("headerDescCmd", "§6CyanLib - DESCRIPTION (commands)\n");
        MODNAME_DEFAULT_TRANSLATIONS.put("headerDescOptions", "§6CyanLib - DESCRIPTION (options) :\n");

        MODNAME_DEFAULT_TRANSLATIONS.put("desc.useCustomTranslations", "§3The §duseCustomTranslations §3option defines whether the custom translation will be used or not");
        MODNAME_DEFAULT_TRANSLATIONS.put("desc.msgToActionBar", "§3The §dmsgToActionBar §3option defines whether the messages will be sent to the action bar or not");
        MODNAME_DEFAULT_TRANSLATIONS.put("desc.minOpLevelExeEditConfig", "§3The §dminOpLevelExeEditConfig §3option defines the OP level required to edit the config");

        MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.header", "§6CyanLib - OPTIONS\n");
        MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.useCustomTranslations", "§6- §3Use custom translations : %s");
        MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.msgToActionBar", "§6- §3Messages to action bar : %s");
        MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.minOpLevelExeEditConfig", "§6- §3Minimum OP level required to edit the config : %s");
    }
    return MODNAME_DEFAULT_TRANSLATIONS ;
}

D). Example File

You should now have a file like this

package your.package;

import fr.aeldit.cyanlib.lib.CyanLib;
import fr.aeldit.cyanlib.lib.CyanLibLanguageUtils;
import fr.aeldit.cyanlib.lib.commands.CyanLibConfigCommands;
import fr.aeldit.cyanlib.lib.config.CyanLibOptionsStorage;

import java.util.HashMap;
import java.util.Map;

public class Utils
{
    public static final String MODNAME_MODID = "modid";

    private static final HashMap<String, String> MODNAME_DEFAULT_TRANSLATIONS = new HashMap<>();

    public static CyanLibOptionsStorage MODNAME_OPTIONS_STORAGE = new CyanLibOptionsStorage(MODNAME_MODID, YourModConfig.class);
    public static CyanLibLanguageUtils MODNAME_LANGUAGE_UTILS = new CyanLibLanguageUtils(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, getDefaultTranslations());
    public static CyanLib MODNAME_LIB_UTILS = new CyanLib(MODNAME_MODID, MODNAME_OPTIONS_STORAGE, MODNAME_LANGUAGE_UTILS);
    public static CyanLibConfigCommands MODNAME_CONFIG_COMMANDS = new CyanLibConfigCommands(MODNAME_MODID, MODNAME_LIB_UTILS);

    public static Map<String, String> getDefaultTranslations()
    {
        if (MODNAME_DEFAULT_TRANSLATIONS.isEmpty())
        {
            MODNAME_DEFAULT_TRANSLATIONS.put("dashSeparation", "§6------------------------------------");
            MODNAME_DEFAULT_TRANSLATIONS.put("listLocations", "§6Cyan - LOCATIONS :\n");
            MODNAME_DEFAULT_TRANSLATIONS.put("desc.surface", "§3The §d/surface §3command teleports you to the highest block located at your position");

            MODNAME_DEFAULT_TRANSLATIONS.put("desc.allowBed", "§3The §eallowBed §3option toggles the use of the §d/bed §3command");
            MODNAME_DEFAULT_TRANSLATIONS.put("desc.allowKgi", "§3The §eallowKgi §3option toggles the use of the §d/kgi §3command");
            MODNAME_DEFAULT_TRANSLATIONS.put("desc.allowSurface", "§3The §eallowSurface §3option toggles the use of the §d/surface §3command");
            MODNAME_DEFAULT_TRANSLATIONS.put("desc.allowLocations", "§3The §eallowLocations §3option toggles the use of the §dlocation §3commands");
            MODNAME_DEFAULT_TRANSLATIONS.put("desc.allowBackTp", "§3The §eallowBackTp §3option toggles the use of the §d/back §3command");

            MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.header", "§6Cyan - OPTIONS :\n");
            MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.allowBed", "§6- §d/bed §3: %s");
            MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.allowKgi", "§6- §d/kgi §3: %s");
            MODNAME_DEFAULT_TRANSLATIONS.put("getCfg.allowSurface", "§6- §d/surface §3: %s");

            MODNAME_DEFAULT_TRANSLATIONS.put("set.allowBed", "§3Toggled §d/bed §3command %s");
            MODNAME_DEFAULT_TRANSLATIONS.put("set.allowKgi", "§3Toggled §d/kgi §3command %s");
            MODNAME_DEFAULT_TRANSLATIONS.put("set.allowSurface", "§3Toggled §d/surface §3command %s");
            MODNAME_DEFAULT_TRANSLATIONS.put("set.allowLocations", "§3Toggled §dlocation §3commands %s");

            MODNAME_DEFAULT_TRANSLATIONS.put("error.notOp", "§cYou don't have the required permission to do that");
            MODNAME_DEFAULT_TRANSLATIONS.put("error.bedDisabled", "§cThe /bed command is disabled");

            MODNAME_DEFAULT_TRANSLATIONS.put("bed", "§3You have been teleported to your bed");
            MODNAME_DEFAULT_TRANSLATIONS.put("respawnAnchor", "§3You have been teleported to your respawn anchor");
        }
        return CYAN_DEFAULT_TRANSLATIONS;
    }
}