Skip to content

Commit 04c175d

Browse files
committed
add core modules
1 parent 7b825ef commit 04c175d

File tree

38 files changed

+1108
-81
lines changed

38 files changed

+1108
-81
lines changed

core/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.github.projectunified</groupId>
8+
<artifactId>block-util</artifactId>
9+
<version>5.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>block-util-core</artifactId>
13+
<name>BlockUtil Core</name>
14+
</project>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package io.github.projectunified.blockutil.core.box;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* A custom box to bound blocks
7+
*/
8+
public class BlockBox {
9+
/**
10+
* The minimum x coordinate
11+
*/
12+
public final int minX;
13+
/**
14+
* The minimum y coordinate
15+
*/
16+
public final int minY;
17+
/**
18+
* The minimum z coordinate
19+
*/
20+
public final int minZ;
21+
/**
22+
* The maximum x coordinate
23+
*/
24+
public final int maxX;
25+
/**
26+
* The maximum y coordinate
27+
*/
28+
public final int maxY;
29+
/**
30+
* The maximum z coordinate
31+
*/
32+
public final int maxZ;
33+
34+
/**
35+
* Create a new block box
36+
*
37+
* @param x1 the first x
38+
* @param y1 the first y
39+
* @param z1 the first z
40+
* @param x2 the second x
41+
* @param y2 the second y
42+
* @param z2 the second z
43+
*/
44+
public BlockBox(int x1, int y1, int z1, int x2, int y2, int z2) {
45+
minX = Math.min(x1, x2);
46+
minY = Math.min(y1, y2);
47+
minZ = Math.min(z1, z2);
48+
maxX = Math.max(x1, x2);
49+
maxY = Math.max(y1, y2);
50+
maxZ = Math.max(z1, z2);
51+
}
52+
53+
/**
54+
* Create a new block box
55+
*
56+
* @param pos1 the first position
57+
* @param pos2 the second position
58+
*/
59+
public BlockBox(Position pos1, Position pos2) {
60+
this((int) pos1.x, (int) pos1.y, (int) pos1.z, (int) pos2.x, (int) pos2.y, (int) pos2.z);
61+
}
62+
63+
/**
64+
* Create a new block box that increases the maximum position by the given offset
65+
*
66+
* @param x the x-axis offset
67+
* @param y the y-axis offset
68+
* @param z the z-axis offset
69+
* @return the new box
70+
*/
71+
public BlockBox expandMax(int x, int y, int z) {
72+
return new BlockBox(min(), max().move(x, y, z));
73+
}
74+
75+
/**
76+
* Create a new block box that decreases the minimum position by the given offset
77+
*
78+
* @param x the x-axis offset
79+
* @param y the y-axis offset
80+
* @param z the z-axis offset
81+
* @return the new box
82+
*/
83+
public BlockBox expandMin(int x, int y, int z) {
84+
return new BlockBox(min().move(-x, -y, -z), max());
85+
}
86+
87+
/**
88+
* Create a new block box that expands the minimum and maximum position by the given offset
89+
*
90+
* @param x the x-axis offset
91+
* @param y the y-axis offset
92+
* @param z the z-axis offset
93+
* @return the new box
94+
*/
95+
public BlockBox expand(int x, int y, int z) {
96+
return expandMin(x, y, z).expandMax(x, y, z);
97+
}
98+
99+
/**
100+
* Create a new block box that increases the maximum position by one.
101+
* Use this to include the block at the maximum position as a part of the box.
102+
*
103+
* @return the new box
104+
*/
105+
public BlockBox maxInclusive() {
106+
return expandMax(1, 1, 1);
107+
}
108+
109+
/**
110+
* Check if the location is in the box
111+
*
112+
* @param x the x
113+
* @param y the y
114+
* @param z the z
115+
* @return true if it is in the box
116+
*/
117+
public boolean contains(double x, double y, double z) {
118+
return x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ;
119+
}
120+
121+
/**
122+
* Check if the location is in the box
123+
*
124+
* @param pos the position
125+
* @return true if it is in the box
126+
*/
127+
public boolean contains(Position pos) {
128+
return contains(pos.x, pos.y, pos.z);
129+
}
130+
131+
/**
132+
* Get the center of the box
133+
*
134+
* @return the center
135+
*/
136+
public Position center() {
137+
return new Position((minX + maxX) / 2.0, (minY + maxY) / 2.0, (minZ + maxZ) / 2.0);
138+
}
139+
140+
/**
141+
* Get the minimum position
142+
*
143+
* @return the minimum position
144+
*/
145+
public Position min() {
146+
return new Position(minX, minY, minZ);
147+
}
148+
149+
/**
150+
* Get the maximum position
151+
*
152+
* @return the maximum position
153+
*/
154+
public Position max() {
155+
return new Position(maxX, maxY, maxZ);
156+
}
157+
158+
/**
159+
* Get the X-size of the box
160+
*
161+
* @return the size
162+
*/
163+
public double sizeX() {
164+
return maxX - minX;
165+
}
166+
167+
/**
168+
* Get the Y-size of the box
169+
*
170+
* @return the size
171+
*/
172+
public double sizeY() {
173+
return maxY - minY;
174+
}
175+
176+
/**
177+
* Get the Z-size of the box
178+
*
179+
* @return the size
180+
*/
181+
public double sizeZ() {
182+
return maxZ - minZ;
183+
}
184+
185+
@Override
186+
public boolean equals(Object o) {
187+
if (this == o) return true;
188+
if (o == null || getClass() != o.getClass()) return false;
189+
BlockBox blockBox = (BlockBox) o;
190+
return minX == blockBox.minX && minY == blockBox.minY && minZ == blockBox.minZ && maxX == blockBox.maxX && maxY == blockBox.maxY && maxZ == blockBox.maxZ;
191+
}
192+
193+
@Override
194+
public int hashCode() {
195+
return Objects.hash(minX, minY, minZ, maxX, maxY, maxZ);
196+
}
197+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.projectunified.blockutil.core.box;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* The immutable 3D position / location / vector
7+
*/
8+
public class Position {
9+
/**
10+
* The x-axis
11+
*/
12+
public final double x;
13+
/**
14+
* The y-axis
15+
*/
16+
public final double y;
17+
/**
18+
* The z-axis
19+
*/
20+
public final double z;
21+
22+
/**
23+
* Create a new {@link Position}
24+
*
25+
* @param x the x-axis
26+
* @param y the y-axis
27+
* @param z the z-axis
28+
*/
29+
public Position(double x, double y, double z) {
30+
this.x = x;
31+
this.y = y;
32+
this.z = z;
33+
}
34+
35+
/**
36+
* Create a new {@link Position} that offset from this position
37+
*
38+
* @param x the x-axis offset
39+
* @param y the y-axis offset
40+
* @param z the z-axis offset
41+
* @return the new position
42+
*/
43+
public Position move(double x, double y, double z) {
44+
return new Position(this.x + x, this.y + y, this.z + z);
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (o == null || getClass() != o.getClass()) return false;
51+
Position position = (Position) o;
52+
return Double.compare(position.x, x) == 0 && Double.compare(position.y, y) == 0 && Double.compare(position.z, z) == 0;
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(x, y, z);
58+
}
59+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.projectunified.blockutil.core.iterator;
2+
3+
import io.github.projectunified.blockutil.core.box.BlockBox;
4+
5+
/**
6+
* The abstract {@link PositionIterator} for {@link BlockBox}
7+
*/
8+
public abstract class AbstractPositionIterator implements PositionIterator {
9+
/**
10+
* The box
11+
*/
12+
public final BlockBox box;
13+
14+
/**
15+
* Create a new {@link AbstractPositionIterator}
16+
*
17+
* @param box the box
18+
*/
19+
protected AbstractPositionIterator(BlockBox box) {
20+
this.box = box;
21+
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.github.projectunified.blockutil.core.iterator;
2+
3+
import io.github.projectunified.blockutil.core.box.BlockBox;
4+
import io.github.projectunified.blockutil.core.box.Position;
5+
6+
import java.util.NoSuchElementException;
7+
import java.util.concurrent.atomic.AtomicReference;
8+
9+
/**
10+
* The base {@link PositionIterator} for {@link BlockBox}
11+
*/
12+
public abstract class BasePositionIterator extends AbstractPositionIterator {
13+
private final AtomicReference<Position> current;
14+
15+
/**
16+
* Create a new {@link BasePositionIterator}
17+
*
18+
* @param box the box
19+
*/
20+
protected BasePositionIterator(BlockBox box) {
21+
super(box);
22+
this.current = new AtomicReference<>();
23+
}
24+
25+
/**
26+
* Get the current {@link Position}
27+
*
28+
* @return the current position
29+
*/
30+
public Position getCurrent() {
31+
return this.current.get();
32+
}
33+
34+
/**
35+
* Get the initial {@link Position}
36+
*
37+
* @return the initial position
38+
*/
39+
public abstract Position initial();
40+
41+
/**
42+
* Get the next {@link Position}
43+
*
44+
* @param current the current position
45+
* @return the next position
46+
* @throws NoSuchElementException if there is no next position
47+
*/
48+
public abstract Position getContinue(Position current) throws NoSuchElementException;
49+
50+
/**
51+
* Check if there is a next {@link Position}
52+
*
53+
* @param current the current position
54+
* @return true if there is a next position
55+
*/
56+
public abstract boolean hasContinue(Position current);
57+
58+
@Override
59+
public void reset() {
60+
this.current.set(null);
61+
}
62+
63+
@Override
64+
public boolean hasNext() {
65+
Position position = this.current.get();
66+
return position == null || hasContinue(position);
67+
}
68+
69+
@Override
70+
public Position next() {
71+
Position position = getCurrent();
72+
if (position == null) {
73+
position = initial();
74+
} else {
75+
position = getContinue(position);
76+
}
77+
this.current.set(position);
78+
return position;
79+
}
80+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.projectunified.blockutil.core.iterator;
2+
3+
import io.github.projectunified.blockutil.core.box.Position;
4+
5+
import java.util.Iterator;
6+
7+
/**
8+
* The {@link Iterator} for {@link Position}
9+
*/
10+
public interface PositionIterator extends Iterator<Position> {
11+
/**
12+
* Reset the iterator
13+
*/
14+
void reset();
15+
}

0 commit comments

Comments
 (0)