package net.glowstone.net.message.play.game;

import com.flowpowered.networking.Message;
import lombok.Data;
import net.glowstone.entity.meta.profile.PlayerProfile;
import net.glowstone.util.TextMessage;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Data
public final class UserListItemMessage implements Message {

    private final Action action;
    private final List<Entry> entries;

    public UserListItemMessage(Action action, List<Entry> entries) {
        this.action = action;
        this.entries = entries;

        for (Entry entry : entries) {
            if (entry.action != action) {
                throw new IllegalArgumentException("Entries must be " + action + ", not " + entry.action);
            }
        }
    }

    public UserListItemMessage(Action action, Entry entry) {
        this(action, Arrays.asList(entry));
    }

    // add

    public static Entry add(PlayerProfile profile) {
        return add(profile, 0, 0, null);
    }

    public static Entry add(PlayerProfile profile, int gameMode, int ping, TextMessage displayName) {
        return new Entry(Action.ADD_PLAYER, profile.getUniqueId(), profile, gameMode, ping, displayName);
    }

    public static UserListItemMessage addOne(PlayerProfile profile) {
        return new UserListItemMessage(Action.ADD_PLAYER, add(profile));
    }

    // gamemode

    public static Entry gameMode(UUID uuid, int gameMode) {
        return new Entry(Action.UPDATE_GAMEMODE, uuid, null, gameMode, 0, null);
    }

    public static UserListItemMessage gameModeOne(UUID uuid, int gameMode) {
        return new UserListItemMessage(Action.UPDATE_GAMEMODE, gameMode(uuid, gameMode));
    }

    // latency

    public static Entry latency(UUID uuid, int ping) {
        return new Entry(Action.UPDATE_LATENCY, uuid, null, 0, ping, null);
    }

    public static UserListItemMessage latencyOne(UUID uuid, int ping) {
        return new UserListItemMessage(Action.UPDATE_LATENCY, latency(uuid, ping));
    }

    // display name

    public static Entry displayName(UUID uuid, TextMessage displayName) {
        return new Entry(Action.UPDATE_DISPLAY_NAME, uuid, null, 0, 0, displayName);
    }

    public static UserListItemMessage displayNameOne(UUID uuid, TextMessage displayName) {
        return new UserListItemMessage(Action.UPDATE_DISPLAY_NAME, displayName(uuid, displayName));
    }

    // remove

    public static Entry remove(UUID uuid) {
        return new Entry(Action.REMOVE_PLAYER, uuid, null, 0, 0, null);
    }

    public static UserListItemMessage removeOne(UUID uuid) {
        return new UserListItemMessage(Action.REMOVE_PLAYER, remove(uuid));
    }

    // inner classes

    public enum Action {
        ADD_PLAYER,
        UPDATE_GAMEMODE,
        UPDATE_LATENCY,
        UPDATE_DISPLAY_NAME,
        REMOVE_PLAYER
    }

    @Data
    public static final class Entry {
        public final UUID uuid;
        public final PlayerProfile profile;
        public final int gameMode;
        public final int ping;
        public final TextMessage displayName;
        private final Action action;
    }
}

@@ -1112,35 +1112,16 @@
 w Entry(
-Action.ADD_PLAYER, 
 profile.
@@ -1163,32 +1163,51 @@
 ing, displayName
+, Action.ADD_PLAYER
 );%0A    %7D%0A%0A    pu
@@ -1456,32 +1456,8 @@
 try(
-Action.UPDATE_GAMEMODE, 
 uuid
@@ -1473,32 +1473,56 @@
 ameMode, 0, null
+, Action.UPDATE_GAMEMODE
 );%0A    %7D%0A%0A    pu
@@ -1785,39 +1785,16 @@
 w Entry(
-Action.UPDATE_LATENCY, 
 uuid, nu
@@ -1806,24 +1806,47 @@
 , ping, null
+, Action.UPDATE_LATENCY
 );%0A    %7D%0A%0A  
@@ -2130,36 +2130,8 @@
 try(
-Action.UPDATE_DISPLAY_NAME, 
 uuid
@@ -2155,16 +2155,44 @@
 playName
+, Action.UPDATE_DISPLAY_NAME
 );%0A    %7D
@@ -2475,38 +2475,16 @@
 w Entry(
-Action.REMOVE_PLAYER, 
 uuid, nu
@@ -2489,32 +2489,54 @@
 null, 0, 0, null
+, Action.REMOVE_PLAYER
 );%0A    %7D%0A%0A    pu

Fix constructor calls in UserListItemMessage