]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/models/BoundingBoxVillage.java
Move Mob Spawner processing to client side
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / common / models / BoundingBoxVillage.java
1 package com.irtimaled.bbor.common.models;
2
3 import com.irtimaled.bbor.common.BoundingBoxType;
4 import com.irtimaled.bbor.common.TypeHelper;
5 import com.irtimaled.bbor.common.VillageColorCache;
6 import com.irtimaled.bbor.common.VillageHelper;
7
8 import java.awt.*;
9 import java.util.Set;
10
11 public class BoundingBoxVillage extends BoundingBoxSphere {
12     private final boolean spawnsIronGolems;
13     private final Color color;
14     private final Set<Coords> doors;
15     private final int villageHash;
16
17     private BoundingBoxVillage(Coords center, Integer radius, Color color, boolean spawnsIronGolems, Set<Coords> doors) {
18         super(center, radius, BoundingBoxType.VillageSpheres);
19         this.color = color;
20         this.spawnsIronGolems = spawnsIronGolems;
21         this.doors = doors;
22         this.villageHash = VillageHelper.computeHash(center, radius, spawnsIronGolems, doors);
23         calculateCenterOffsets(doors);
24     }
25
26     public static BoundingBoxVillage from(Coords center, Integer radius, Color color, boolean spawnsIronGolems, Set<Coords> doors) {
27         return new BoundingBoxVillage(center, radius, color, spawnsIronGolems, doors);
28     }
29
30     public static BoundingBoxVillage from(Coords center, Integer radius, int villageId, int population, Set<Coords> doors) {
31         boolean spawnsIronGolems = VillageHelper.shouldSpawnIronGolems(population, doors.size());
32         Color color = VillageColorCache.getColor(villageId);
33         return new BoundingBoxVillage(center, radius, color, spawnsIronGolems, doors);
34     }
35
36     private void calculateCenterOffsets(Set<Coords> doors) {
37         boolean processedFirstDoor = false;
38         int minX = 0;
39         int maxX = 0;
40         int minZ = 0;
41         int maxZ = 0;
42         for (Coords door : doors) {
43             if (!processedFirstDoor ||
44                     (minX > door.getX()))
45                 minX = door.getX();
46             if (!processedFirstDoor ||
47                     maxX < door.getX())
48                 maxX = door.getX();
49             if (!processedFirstDoor ||
50                     minZ > door.getZ())
51                 minZ = door.getZ();
52             if (!processedFirstDoor ||
53                     maxZ < door.getZ())
54                 maxZ = door.getZ();
55
56             processedFirstDoor = true;
57         }
58         setCenterOffsets(Math.abs(maxX - minX) % 2 == 0 ? 0.5 : (minX < 0 ? 0 : 1), 0.0d, Math.abs(maxZ - minZ) % 2 == 0 ? 0.5 : (minZ < 0 ? 0 : 1));
59     }
60
61     public Color getColor() {
62         return color;
63     }
64
65     @Override
66     public int hashCode() {
67         return TypeHelper.combineHashCodes(super.hashCode(), villageHash);
68     }
69
70     public boolean getSpawnsIronGolems() {
71         return spawnsIronGolems;
72     }
73
74     public Set<Coords> getDoors() {
75         return doors;
76     }
77
78     public int getVillageHash() {
79         return villageHash;
80     }
81 }