1 package com.irtimaled.bbor.common.messages;
3 import com.irtimaled.bbor.common.BoundingBoxType;
4 import com.irtimaled.bbor.common.models.*;
5 import net.minecraft.network.PacketBuffer;
8 import java.util.HashSet;
11 class BoundingBoxDeserializer {
12 static BoundingBox deserialize(PacketBuffer buf) {
13 if (!buf.isReadable(2)) return null;
15 char type = buf.readChar();
18 return deserializeVillage(buf);
20 return deserializeStructure(buf);
22 return deserializeMobSpawner(buf);
27 private static BoundingBox deserializeStructure(PacketBuffer buf) {
28 BoundingBoxType type = BoundingBoxType.getByNameHash(buf.readInt());
29 if (type == null) return null;
30 Coords minCoords = deserializeCoords(buf);
31 Coords maxCoords = deserializeCoords(buf);
32 return BoundingBoxStructure.from(minCoords, maxCoords, type);
35 private static BoundingBox deserializeVillage(PacketBuffer buf) {
36 Coords center = deserializeCoords(buf);
37 int radius = buf.readVarInt();
38 boolean spawnsIronGolems = buf.readBoolean();
39 Color color = new Color(buf.readVarInt());
40 Set<Coords> doors = new HashSet<>();
41 while (buf.isReadable()) {
42 Coords door = deserializeCoords(buf);
45 return BoundingBoxVillage.from(center, radius, color, spawnsIronGolems, doors);
48 private static BoundingBox deserializeMobSpawner(PacketBuffer buf) {
49 Coords center = deserializeCoords(buf);
50 return BoundingBoxMobSpawner.from(center);
53 private static Coords deserializeCoords(PacketBuffer buf) {
54 int x = buf.readVarInt();
55 int y = buf.readVarInt();
56 int z = buf.readVarInt();
57 return new Coords(x, y, z);