]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/messages/BoundingBoxDeserializer.java
c25a9b95a365abd8117f7ba6174dad38125a397e
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / common / messages / BoundingBoxDeserializer.java
1 package com.irtimaled.bbor.common.messages;
2
3 import com.irtimaled.bbor.common.BoundingBoxType;
4 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
5 import com.irtimaled.bbor.common.models.BoundingBoxCuboid;
6 import com.irtimaled.bbor.common.models.BoundingBoxVillage;
7 import com.irtimaled.bbor.common.models.Coords;
8
9 import java.awt.*;
10 import java.util.HashSet;
11 import java.util.Set;
12
13 class BoundingBoxDeserializer {
14     static AbstractBoundingBox deserialize(PayloadReader reader) {
15         if (!reader.isReadable(2)) return null;
16
17         char type = reader.readChar();
18         switch (type) {
19             case 'V':
20                 return deserializeVillage(reader);
21             case 'S':
22                 return deserializeStructure(reader);
23         }
24         return null;
25     }
26
27     private static AbstractBoundingBox deserializeStructure(PayloadReader reader) {
28         BoundingBoxType type = BoundingBoxType.getByNameHash(reader.readInt());
29         if (type == null) return null;
30         Coords minCoords = reader.readCoords();
31         Coords maxCoords = reader.readCoords();
32         return BoundingBoxCuboid.from(minCoords, maxCoords, type);
33     }
34
35     private static AbstractBoundingBox deserializeVillage(PayloadReader reader) {
36         Coords center = reader.readCoords();
37         int radius = reader.readVarInt();
38         boolean spawnsIronGolems = reader.readBoolean();
39         Color color = new Color(reader.readVarInt());
40         Set<Coords> doors = new HashSet<>();
41         while (reader.isReadable()) {
42             doors.add(reader.readCoords());
43         }
44         return BoundingBoxVillage.from(center, radius, color, spawnsIronGolems, doors);
45     }
46 }