All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.network.packet.client.play.ClientUpdateSignPacket Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.network.packet.client.play;

import net.minestom.server.coordinate.Point;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.packet.client.ClientPacket;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import static net.minestom.server.network.NetworkBuffer.*;

public record ClientUpdateSignPacket(
        @NotNull Point blockPosition,
        boolean isFrontText,
        @NotNull List lines
) implements ClientPacket {
    public ClientUpdateSignPacket {
        lines = List.copyOf(lines);
        if (lines.size() != 4) {
            throw new IllegalArgumentException("Signs must have 4 lines!");
        }
        for (String line : lines) {
            if (line.length() > 384) {
                throw new IllegalArgumentException("Signs must have a maximum of 384 characters per line!");
            }
        }
    }

    public static final NetworkBuffer.Type SERIALIZER = new NetworkBuffer.Type<>() {
        @Override
        public void write(@NotNull NetworkBuffer buffer, @NotNull ClientUpdateSignPacket value) {
            buffer.write(BLOCK_POSITION, value.blockPosition);
            buffer.write(BOOLEAN, value.isFrontText);
            buffer.write(STRING, value.lines.get(0));
            buffer.write(STRING, value.lines.get(1));
            buffer.write(STRING, value.lines.get(2));
            buffer.write(STRING, value.lines.get(3));
        }

        @Override
        public @NotNull ClientUpdateSignPacket read(@NotNull NetworkBuffer buffer) {
            return new ClientUpdateSignPacket(buffer.read(BLOCK_POSITION), buffer.read(BOOLEAN), readLines(buffer));
        }
    };

    private static List readLines(@NotNull NetworkBuffer reader) {
        return List.of(reader.read(STRING), reader.read(STRING),
                reader.read(STRING), reader.read(STRING));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy