Skip to content

Commit b6eeeb8

Browse files
committed
Fix error when adding BO2s to a BO3 structure
Fixes #419. The dangerous method CustomObjectCoordinate.getStructuredObject() has been removed.
1 parent 4665e3a commit b6eeeb8

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

common/src/main/java/com/khorn/terraincontrol/customobjects/CustomObjectCoordinate.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.Random;
99

1010
/**
11-
* Holds a custom object along with the absolute spawn coordinates.
11+
* Represents an object along with its location in the world.
1212
*/
1313
public class CustomObjectCoordinate
1414
{
@@ -53,24 +53,12 @@ public CustomObject getObject()
5353
return object;
5454
}
5555

56-
/**
57-
* Returns the object of this coordinate, casted to a
58-
* StructuredCustomObject. Will throw a ClassCastExcpetion
59-
* if the object isn't a StructuredCustomObject
60-
*
61-
* @return The casted object.
62-
*/
63-
public StructuredCustomObject getStructuredObject()
64-
{
65-
return (StructuredCustomObject) object;
66-
}
67-
6856
public Rotation getRotation()
6957
{
7058
return rotation;
7159
}
7260

73-
public boolean spawnWithChecks(LocalWorld world, StructurePartSpawnHeight height, Random random)
61+
boolean spawnWithChecks(LocalWorld world, StructurePartSpawnHeight height, Random random)
7462
{
7563
int y = height.getCorrectY(world, x, this.y, z);
7664
if (!object.canSpawnAt(world, rotation, x, y, z))
@@ -125,7 +113,7 @@ public int hashCode()
125113
* Gets the chunk that should populate for this object.
126114
* @return The chunk.
127115
*/
128-
public ChunkCoordinate getPopulatingChunk()
116+
ChunkCoordinate getPopulatingChunk()
129117
{
130118
// In the past we simply returned the chunk populating for the origin
131119
// of the object. However, the origin is not guaranteed to be at the

common/src/main/java/com/khorn/terraincontrol/customobjects/CustomObjectStructure.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import com.khorn.terraincontrol.LocalWorld;
44
import com.khorn.terraincontrol.util.ChunkCoordinate;
5+
import com.khorn.terraincontrol.util.Rotation;
56
import com.khorn.terraincontrol.util.helpers.RandomHelper;
67

78
import java.util.*;
89

910
/**
10-
* This class creates the branch structure based on one parent object, and spawns all
11-
* objects that should spawn in a chunk.
12-
*
13-
* Although it shouldn't be too slow to recalculate, a structure cache should be kept.
11+
* Represents a collection of all {@link CustomObject}s in a structure. It is
12+
* calculated by finding the branches of one object, then finding the branches
13+
* of those branches, etc., until
14+
* {@link StructuredCustomObject#getMaxBranchDepth()} is reached.
1415
*
1516
*/
1617
public class CustomObjectStructure
@@ -19,20 +20,21 @@ public class CustomObjectStructure
1920
protected LocalWorld world;
2021
protected CustomObjectCoordinate start;
2122
protected StructurePartSpawnHeight height;
22-
protected Map<ChunkCoordinate, Set<CustomObjectCoordinate>> objectsToSpawn;
23-
protected int maxBranchDepth;
23+
private Map<ChunkCoordinate, Set<CustomObjectCoordinate>> objectsToSpawn;
24+
private int maxBranchDepth;
2425

25-
public CustomObjectStructure(LocalWorld world, CustomObjectCoordinate start)
26+
CustomObjectStructure(LocalWorld world, CustomObjectCoordinate start)
2627
{
2728
if (!(start.getObject() instanceof StructuredCustomObject))
2829
{
2930
throw new IllegalArgumentException("Start object has to be a structure!");
3031
}
32+
StructuredCustomObject object = (StructuredCustomObject) start.getObject();
3133

3234
this.world = world;
3335
this.start = start;
34-
this.height = start.getStructuredObject().getStructurePartSpawnHeight();
35-
this.maxBranchDepth = start.getStructuredObject().getMaxBranchDepth();
36+
this.height = object.getStructurePartSpawnHeight();
37+
this.maxBranchDepth = object.getMaxBranchDepth();
3638
random = RandomHelper.getRandomForCoords(start.getX(), start.getY(), start.getZ(), world.getSeed());
3739

3840
// Calculate all branches and add them to a list
@@ -43,7 +45,7 @@ public CustomObjectStructure(LocalWorld world, CustomObjectCoordinate start)
4345

4446
private void addBranches(CustomObjectCoordinate coordObject, int depth)
4547
{
46-
for (Branch branch : coordObject.getStructuredObject().getBranches(coordObject.getRotation()))
48+
for (Branch branch : getBranches(coordObject.getObject(), coordObject.getRotation()))
4749
{
4850
CustomObjectCoordinate childCoordObject = branch.toCustomObjectCoordinate(world, random, coordObject.getX(),
4951
coordObject.getY(), coordObject.getZ());
@@ -65,12 +67,21 @@ private void addBranches(CustomObjectCoordinate coordObject, int depth)
6567
}
6668
}
6769

70+
private Branch[] getBranches(CustomObject customObject, Rotation rotation)
71+
{
72+
if (customObject instanceof StructuredCustomObject)
73+
{
74+
return ((StructuredCustomObject) customObject).getBranches(rotation);
75+
}
76+
return new Branch[0];
77+
}
78+
6879
/**
6980
* Adds the object to the spawn list of each chunk that the object
7081
* touches.
7182
* @param coordObject The object.
7283
*/
73-
void addToSpawnList(CustomObjectCoordinate coordObject)
84+
private void addToSpawnList(CustomObjectCoordinate coordObject)
7485
{
7586
ChunkCoordinate chunkCoordinate = coordObject.getPopulatingChunk();
7687

@@ -83,6 +94,10 @@ void addToSpawnList(CustomObjectCoordinate coordObject)
8394
objectsInChunk.add(coordObject);
8495
}
8596

97+
/**
98+
* Spawns all the objects that should be spawned in that chunk.
99+
* @param chunkCoordinate The chunk to spawn in.
100+
*/
86101
public void spawnForChunk(ChunkCoordinate chunkCoordinate)
87102
{
88103
Set<CustomObjectCoordinate> objectsInChunk = objectsToSpawn.get(chunkCoordinate);

0 commit comments

Comments
 (0)