Server support

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 );
    }
}