]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/messages/BoundingBoxDeserializer.java
Stop using BlockPos
[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.*;
5 import net.minecraft.network.PacketBuffer;
6
7 import java.awt.*;
8 import java.util.HashSet;
9 import java.util.Set;
10
11 class BoundingBoxDeserializer {
12     static BoundingBox deserialize(PacketBuffer buf) {
13         if (!buf.isReadable(2)) return null;
14
15         char type = buf.readChar();
16         switch (type) {
17             case 'V':
18                 return deserializeVillage(buf);
19             case 'S':
20                 return deserializeStructure(buf);
21             case 'M':
22                 return deserializeMobSpawner(buf);
23         }
24         return null;
25     }
26
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);
33     }
34
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);
43             doors.add(door);
44         }
45         return BoundingBoxVillage.from(center, radius, color, spawnsIronGolems, doors);
46     }
47
48     private static BoundingBox deserializeMobSpawner(PacketBuffer buf) {
49         Coords center = deserializeCoords(buf);
50         return BoundingBoxMobSpawner.from(center);
51     }
52
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);
58     }
59 }