Newer
Older
ChallengesJoinEntities / src / main / java / de / fanta / challengesjoinentities / ChallengesGlobalDataHelper.java
@vollkorntomate vollkorntomate on 16 Jul 2020 1 KB first test implementation
package de.fanta.challengesjoinentities;

import de.fanta.challengesjoinentities.ChallengesGlobalDataHelper.ChallengeMessageType;
import de.iani.cubesideutils.bukkit.plugin.api.GlobalDataHelperBukkit;

import java.io.DataInputStream;
import java.io.IOException;

public class ChallengesGlobalDataHelper extends GlobalDataHelperBukkit<ChallengeMessageType> {

    public static final String CHANNEL = "ChallengesJoinEntities";

    private final ChallengesJoinEntities plugin;

    public ChallengesGlobalDataHelper(ChallengesJoinEntities plugin) {
        super(ChallengeMessageType.class, CHANNEL, plugin);
        this.plugin = plugin;
    }

    @Override
    protected void handleMessage(ChallengeMessageType challengeMessageType, DataInputStream data) throws IOException {
        String serverName = data.readUTF();
        switch (challengeMessageType) {
            case TIMER:
                boolean running = data.readBoolean();
                plugin.setTimerStatus(serverName, running);
                break;

            case SERVER_STATUS:
                boolean online = data.readBoolean();
                plugin.setServerStatus(serverName, online);
                break;

            case PLAYER_COUNT:
                int count = data.readInt();
                plugin.setPlayerCount(serverName, count);
                break;

            default:
                throw new UnsupportedOperationException();
        }
    }

    public void sendTimerUpdate(boolean running) {
        sendData(ChallengeMessageType.TIMER, getThisServerName(), running);
    }

    public void sendServerStatusUpdate(boolean online) {
        sendData(ChallengeMessageType.SERVER_STATUS, getThisServerName(), online);
    }

    public void sendPlayerCountUpdate(int count) {
        sendData(ChallengeMessageType.PLAYER_COUNT, getThisServerName(), count);
    }

    public enum ChallengeMessageType {
        TIMER, SERVER_STATUS, PLAYER_COUNT
    }
}