Skip to content

Server support

Warning

This page is for the LabyMod 3 Addon API, which is no longer maintained nor supported. Visit https://wiki.labymod.net/pages/addon/ for the LabyMod 4 Addon API Wiki.

Our server system

With our server system you're able to create own server support in LabyMod. We provide a ServerSupportModule you can use to display information.

Creating your own server class

You can create your server class by just using net.labymod.servermanager.Server as the superclass.

Registering your server class

You can register your server class simply by using the method LabyModAddon#getApi()#registerServerSupport( LabyModAddon, Server ).

Adding lines to the ServerSupportModule

Just add the method Server#addModuleLines( List lines ) to your class and add the desired lines to the list.

Plugin message communication

You can handle incoming plugin messages from the server by using the abstract method Server#handlePluginMessage( String channelName, PacketBuffer packetBuffer ). Sending a plugin message to the server can simply be done by calling the method Server#sendPluginMessage( String channelName, Consumer<PacketBuffer> bufferCallback ).

Example server class

package net.labymod.addons.test;

import com.google.gson.JsonObject;
import net.labymod.api.events.TabListEvent;
import net.labymod.ingamegui.moduletypes.ColoredTextModule;
import net.labymod.servermanager.ChatDisplayAction;
import net.labymod.servermanager.Server;
import net.labymod.settings.elements.BooleanElement;
import net.labymod.settings.elements.ControlElement;
import net.labymod.settings.elements.SettingsElement;
import net.labymod.utils.Material;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.network.PacketBuffer;

import java.util.Collections;
import java.util.List;

/**
 * Test server class
 */
public class TestServer extends Server {

    private int elo = -1;
    private int wins = -1;

    private boolean displayElo;
    private boolean displayWins;

    public TestServer() {
        super( "testing_server" /* unique server id */, "test.net", "test.de" /* <- Server addresses */ );
    }

    /**
     * Called when the client joins the server
     *
     * @param serverData the server-data
     */
    @Override
    public void onJoin( ServerData serverData ) {

    }

    /**
     * This method will be called when the client receives a chat message from this server
     *
     * @param clean     message without color codes
     * @param formatted message with color codes
     * @return how the chat should handle this message - should the message show up on the other chat or should it be hidden?
     */
    @Override
    public ChatDisplayAction handleChatMessage( String clean, String formatted ) throws Exception {
        if ( clean.startsWith( "Your elo: " ) ) {
            try {
                elo = Integer.parseInt( clean.replace( "Your elo: ", "" ) );
            } catch ( NumberFormatException ex ) {
                elo = -1;
            }
        }

        return ChatDisplayAction.NORMAL;
    }

    /**
     * This method will be called when the client receives a plugin message from this server
     *
     * @param channelName  plugin message channel name
     * @param packetBuffer the message's content
     * @throws Exception
     */
    @Override
    public void handlePluginMessage( String channelName, PacketBuffer packetBuffer ) throws Exception {
        if ( channelName.equals( "TEST_wins" ) ) {
            wins = packetBuffer.readInt();
        }
    }

    /**
     * Called when the tablist header or footer gets updated on this server
     *
     * @param tabInfoType     header or footer
     * @param formattedText   message with color codes
     * @param unformattedText clean message without color codes
     * @throws Exception
     */
    @Override
    public void handleTabInfoMessage( TabListEvent.Type tabInfoType, String formattedText, String unformattedText ) throws Exception {

    }

    /**
     * Called when the server's config gets initialized
     *
     * @param config the config's JsonObject
     */
    @Override
    protected void initConfig( JsonObject config ) {
        super.initConfig( config );

        this.displayElo = config.get( "displayElo" ).getAsBoolean();
        this.displayWins = config.get( "displayWins" ).getAsBoolean();
    }

    /**
     * Called after the server has been added - those settings will be added to the addon's settings
     *
     * @param subSettings the addon's sub settings
     */
    @Override
    public void fillSubSettings( List<SettingsElement> subSettings ) {
        subSettings.add( new BooleanElement( "Display Elo", this, new ControlElement.IconData( Material.DIAMOND_SWORD ), "displayElo" ) );
        subSettings.add( new BooleanElement( "Display Wins", this, new ControlElement.IconData( Material.DIAMOND_SWORD ), "displayWins" ) );
    }

    /**
     * This method can be used to add lines to the ServerSupport module
     *
     * @param lines the list you can add your custom lines to
     */
    @Override
    public void addModuleLines( List<DisplayLine> lines ) {
        if ( displayElo )
            lines.add( new DisplayLine( "Elo", Collections.singletonList( ColoredTextModule.Text.getText( String.valueOf( elo ) ) ) ) );

        if ( displayWins )
            lines.add( new DisplayLine( "Wins", Collections.singletonList( ColoredTextModule.Text.getText( String.valueOf( wins ) ) ) ) );

        super.addModuleLines( lines );
    }
}