/
MapDataCodec.java
55 lines (47 loc) · 1.84 KB
/
MapDataCodec.java
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
package net.glowstone.net.codec.play.game;
import com.flowpowered.network.Codec;
import com.flowpowered.network.util.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.DecoderException;
import net.glowstone.net.GlowBufUtils;
import net.glowstone.net.message.play.game.MapDataMessage;
import net.glowstone.net.message.play.game.MapDataMessage.Icon;
import net.glowstone.net.message.play.game.MapDataMessage.Section;
import java.io.IOException;
import java.util.List;
public final class MapDataCodec implements Codec<MapDataMessage> {
@Override
public MapDataMessage decode(ByteBuf buf) throws IOException {
throw new DecoderException("Cannot decode MapDataMessage");
}
@Override
public ByteBuf encode(ByteBuf buf, MapDataMessage message) throws IOException {
List<Icon> icons = message.getIcons();
ByteBufUtils.writeVarInt(buf, message.getId());
buf.writeByte(message.getScale());
ByteBufUtils.writeVarInt(buf, icons.size());
for (Icon icon : icons) {
buf.writeByte(icon.facing << 4 | icon.type);
buf.writeByte(icon.x);
buf.writeByte(icon.z);
buf.writeByte(icon.direction);
boolean hasDisplayName = icon.displayName != null;
buf.writeBoolean(hasDisplayName);
if (hasDisplayName) {
GlowBufUtils.writeChat(buf, icon.displayName);
}
}
Section section = message.getSection();
if (section == null) {
buf.writeByte(0);
} else {
buf.writeByte(section.width);
buf.writeByte(section.height);
buf.writeByte(section.x);
buf.writeByte(section.z);
ByteBufUtils.writeVarInt(buf, section.data.length);
buf.writeBytes(section.data);
}
return buf;
}
}