Skip to content

Language API

SirBlobman edited this page May 22, 2023 · 6 revisions

Information

BlueSlimeCore uses LanguageManager objects to assist you with player messages, action bars, titles, and scoreboards. If the server is running 1.12.2 or higher, BlueSlimeCore supports languages set by the player.

MiniMessage

BlueSlimeCore uses the Adventure MiniMessage format. Legacy Bukkit color codes are not supported in language files. Adventure MiniMessage is an amazing formatting tool that lets you use hover messages, gradients, RGB, and much more. Please check out their formatting guide:
Adventure MiniMessage Format

LanguageManager Instance

Option 01) Pre-Built Plugin

You can make your plugin's main class extend com.github.sirblobman.api.plugin.ConfigurablePlugin instead of JavaPlugin to use the built-in method: getLanguageManager()

Option 02) Custom Instance

You can create a custom instance in your own plugin class if you don't want to use the ConfigurablePlugin as a parent.

import org.bukkit.plugin.java.JavaPlugin;
import com.github.sirblobman.api.language.LanguageManager;

public final class YourPlugin extends JavaPlugin {
    private final ConfigurationManager configurationManager;
    private final LanguageManager languageManager;
    
    public YourPlugin() {
        this.configurationManager = new ConfigurationManager(this);
        this.languageManager = new LanguageManager(this.configurationManager);
    }
    
    public ConfigurationManager getConfigurationManager() {
        return this.configurationManager;
    }
    
    public LanguageManager getLanguageManager() {
        return this.languageManager;
    }
}

This code is always required:

@Override
public void onLoad() {
    // Other Code here...
    LanguageManager languageManager = getLanguageManager();
    languageManager.saveDefaultLanguages();
}

@Override
public void onEnable() {
    // Other Code here...

    LanguageManager languageManager = getLanguageManager();
    languageManager.reloadLanguages();
    languageManager.onPluginEnable();
}

Using LanguageManager

Configuration File

Example File

debug-mode: false
enforce-default-locale: false
default-locale: "en_us"
console-locale: "en_us"
use-placeholder-api: false

debug-mode: When this option is set to true, message parsing debug will be sent to the server console. This debug can be considered spam and may cause the console to lag behind. Only enable this if you are trying to fix an issue.

enforce-default-locale: This option can be set to true if your server only supports one language or if you don't want players to change the language. This option also makes the console-locale option useless.

default-locale: This option allows you to modify the default language. The default language is used when a language can't be detected for a player, or when the 'enforce-default-locale' option is set to 'true'

console-locale: This allows you to change the language used for messages sent to the server console.

use-placeholder-api: See PlaceholderAPI Support

Example Locale File: en_us.lang.yml

# Simple Message
message-one: "<green>Hello!</green>"

# Variable Message
message-one-variable: "<green>Hello, {user}!</green>"

# Modifiable Message
message-two:
  type: ACTION_BAR # Type can be CHAT or ACTION_BAR
  content: "<green>Testing 123!</green>"

# Example Sound
sound-one:
  sound: "minecraft:music_disc.13" # Sounds from resource packs can also be played
  category: "master"
  volume: 1.0
  pitch: 1.0

# Example Title
# The 'title' and 'subtitle' are normal messages.
# 'fade-in', 'stay', and 'fade-out' are integers for the amount of ticks.
title-one:
  title: "<green>Hello!</green>"
  subtitle: "<gray>This is a subtitle.</gray>"
  fade-in: 10
  stay: 70
  fade-out: 20

PlaceholderAPI Support

PlaceholderAPI replacements can be enabled in the language.yml file on the server. When the use-placeholder-api option is set to true, the plugin will request replacements from PlaceholderAPI

Sending Messages

You can send messages to any CommandSender object on the server. This includes the server console, players, and entities. Non-player command senders will use the default-locale setting. The server console will use the console-locale.

public void sendExamples(CommandSender sender) {
    LanguageManager languageManager = getLanguageManager();
    
    // Send chat message
    languageManager.sendMessage(sender, "message-one");

    // Send chat message with a variable.
    // Code variables don't require PlaceholderAPI support.
    Replacer replacer = new StringReplacer("{user}", sender.getName());
    languageManager.sendMessage(sender, "message-one-variable", replacer);
    
    // Send action bar message (only visible by players)
    languageManager.sendActionBar(sender, "message-one");
    
    // Send action bar or chat (depends on configuration)
    languageManager.sendModifiableMessage(sender, "message-two")

    // Send a sound (only hearable by players)
    languageManager.sendSound(sender, "sound-one")
    
    // Send a title (only visible by players)
    languageManager.sendTitle(sender, "title-one")
}

Extra Information

You can find out more information about the language API in BlueSlimeCore by reviewing the source files