Skip to content

Commit 1e9a1ae

Browse files
committed
Initial structure files, switch to full neo instance instead of vanilla
1 parent a0d34d0 commit 1e9a1ae

File tree

10 files changed

+248
-10
lines changed

10 files changed

+248
-10
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ bin/
3939
.vscode/
4040

4141
### Mac OS ###
42-
.DS_Store
42+
.DS_Store
43+
runs/
44+
45+
run/
46+
47+
logs/

build.gradle.kts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
plugins {
22
id("java")
3+
id("net.neoforged.gradle.userdev") version "7.0.96"
34
}
45

5-
group = "dev.compactmods"
6-
version = "1.0-SNAPSHOT"
6+
base {
7+
archivesName = "spatial"
8+
group = "dev.compactmods"
9+
version = "0.1.0"
10+
}
11+
12+
java {
13+
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
14+
}
715

816
repositories {
917
mavenCentral()
1018
}
1119

12-
dependencies {
13-
testImplementation(platform("org.junit:junit-bom:5.9.1"))
14-
testImplementation("org.junit.jupiter:junit-jupiter")
20+
runs.create("gameTestServer") {
21+
this.gameTest()
22+
systemProperty("forge.enabledGameTestNamespaces", "spatial")
23+
modSource(sourceSets.main.get())
1524
}
1625

17-
tasks.test {
18-
useJUnitPlatform()
26+
dependencies {
27+
// compileOnly("net.minecraft:neoform_joined:1.20.4-20231207.154220")
28+
29+
implementation(libraries.neoforge)
30+
testImplementation(libraries.neoforge)
1931
}

settings.gradle.kts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
11
rootProject.name = "spatial"
22

3+
dependencyResolutionManagement {
4+
versionCatalogs.create("libraries") {
5+
library("neoforge", "net.neoforged", "neoforge")
6+
.versionRef("neoforge")
7+
8+
version("minecraft", "1.20.4")
9+
version("neoforge", "20.4.189")
10+
}
11+
}
12+
13+
pluginManagement {
14+
repositories {
15+
mavenCentral()
16+
mavenLocal()
17+
gradlePluginPortal()
18+
19+
maven("https://libraries.minecraft.net") {
20+
name = "Minecraft"
21+
}
22+
23+
maven("https://maven.parchmentmc.org") {
24+
name = "ParchmentMC"
25+
content {
26+
includeGroup("org.parchmentmc.data")
27+
}
28+
}
29+
30+
maven("https://maven.neoforged.net/releases") {
31+
name = "NeoForged"
32+
}
33+
}
34+
}
35+
36+
plugins {
37+
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
38+
}
39+
Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1-
package dev.compactmods.spatial.aabb;public class AABBHelper {
2-
}
1+
package dev.compactmods.spatial.aabb;
2+
3+
import net.minecraft.world.phys.AABB;
4+
import net.minecraft.world.phys.Vec3;
5+
6+
public abstract class AABBHelper {
7+
8+
public static Vec3 minCorner(AABB aabb) {
9+
return new Vec3(aabb.minX, aabb.minY, aabb.minZ);
10+
}
11+
12+
public static Vec3 maxCorner(AABB aabb) {
13+
return new Vec3(aabb.maxX, aabb.maxY, aabb.maxZ);
14+
}
15+
16+
/**
17+
* Given an area, normalizes the bounds so that the minimum coordinates
18+
* all equal zero. (i.e. 5,5,5 to 11,11,11 becomes 0,0,0 to 6,6,6)
19+
* @param boundaries Original AABB instance.
20+
* @return A new instance of AABB, normalized to zero coordinates.
21+
*/
22+
public static AABB normalize(AABB boundaries) {
23+
Vec3 offset = minCorner(boundaries).reverse();
24+
return boundaries.move(offset);
25+
}
26+
27+
public static AABB normalizeWithin(AABB source, AABB within) {
28+
Vec3 offset = minCorner(source).subtract(minCorner(within)).reverse();
29+
return source.move(offset);
30+
}
31+
32+
public static AABB alignFloor(AABB source, AABB within) {
33+
double targetY = within.minY;
34+
return alignFloor(source, targetY);
35+
}
36+
37+
public static AABB alignFloor(AABB source, double targetY) {
38+
double offset = source.minY - targetY;
39+
return source.move(0, offset * -1, 0);
40+
}
41+
42+
public static String toString(AABB aabb) {
43+
return "%s,%s,%s".formatted(aabb.getXsize(), aabb.getYsize(), aabb.getZsize());
44+
}
45+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package dev.compactmods.spatial.tests;
2+
3+
import com.google.common.math.DoubleMath;
4+
import dev.compactmods.spatial.aabb.AABBHelper;
5+
import net.minecraft.core.Direction;
6+
import net.minecraft.gametest.framework.GameTest;
7+
import net.minecraft.gametest.framework.GameTestGenerator;
8+
import net.minecraft.gametest.framework.GameTestHelper;
9+
import net.minecraft.gametest.framework.TestFunction;
10+
import net.minecraft.util.RandomSource;
11+
import net.minecraft.world.level.block.Rotation;
12+
import net.minecraft.world.phys.AABB;
13+
import net.minecraft.world.phys.Vec3;
14+
import net.neoforged.neoforge.gametest.GameTestHolder;
15+
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;
16+
17+
import java.util.Collection;
18+
import java.util.stream.Stream;
19+
20+
@GameTestHolder("spatial")
21+
@PrefixGameTestTemplate(false)
22+
public class AABBHelperTests {
23+
private static final String BATCH = "aabb-helper";
24+
25+
@GameTest(template = "empty_1x1", batch = BATCH)
26+
public static void canFloorToY0(final GameTestHelper test) {
27+
// Source minY = 5
28+
AABB before = AABB.ofSize(new Vec3(0, 7.5, 0), 5, 5, 5);
29+
30+
// Align to Y-0
31+
final var after = AABBHelper.alignFloor(before, 0);
32+
33+
test.assertTrue(before.minY == 5, "Before was modified in-place rather than immutably moved.");
34+
test.assertTrue(after.minY == 0, "After y level should be zero. (was: %s)".formatted(after.minY));
35+
test.assertTrue(after.getYsize() == 5, "AABB size was modified; should have remained the same.");
36+
test.succeed();
37+
}
38+
39+
@GameTest(template = "empty_1x1", batch = BATCH)
40+
public static void canFloorToAnotherAABB(final GameTestHelper test) {
41+
// Source minY = 5
42+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
43+
44+
// Target minY = 1 (bounds are Y 1-11)
45+
AABB bounds = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 6), 10, 10, 10);
46+
47+
// Align to Y-0
48+
final var after = AABBHelper.alignFloor(before, bounds);
49+
50+
test.assertTrue(before.minY == 5, "Before was modified in-place rather than immutably moved.");
51+
test.assertTrue(after.minY == 1, "After y level should be 1. (was: %s)".formatted(after.minY));
52+
53+
test.assertTrue(after.getYsize() == 5, "AABB size was modified; should have remained the same.");
54+
test.succeed();
55+
}
56+
57+
@GameTest(template = "empty_1x1", batch = BATCH)
58+
public static void normalizeToZero(final GameTestHelper test) {
59+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
60+
61+
// Align to Y-0
62+
final var after = AABBHelper.normalize(before);
63+
64+
test.assertTrue(before.minY == 5, "Before was modified in-place rather than immutably moved.");
65+
66+
test.assertTrue(after.minX == 0, "After x level was not zero (was: %s)".formatted(after.minX));
67+
test.assertTrue(after.minY == 0, "After y level was not zero (was: %s)".formatted(after.minY));
68+
test.assertTrue(after.minZ == 0, "After z level was not zero (was: %s)".formatted(after.minZ));
69+
70+
test.assertTrue(after.getYsize() == 5, "AABB size was modified; should have remained the same.");
71+
test.succeed();
72+
}
73+
74+
@GameTestGenerator
75+
public static Collection<TestFunction> normalizeBoundaryTests() {
76+
final var random = RandomSource.create();
77+
return Stream.concat(
78+
TestUtils.randomVec3Stream(random).limit(10),
79+
80+
// Ensure at least one negative and one positive bound are part of the test
81+
Stream.of(
82+
Vec3.ZERO.subtract(-3, -2, 5),
83+
Vec3.ZERO.add(2, 5, 1)
84+
)
85+
).map(randomOffset -> new TestFunction(
86+
BATCH,
87+
"normalize_boundaries_%s".formatted(randomOffset.hashCode()),
88+
"spatial:empty_1x1",
89+
Rotation.NONE,
90+
5,
91+
0,
92+
true,
93+
testHelper -> normalizeIntoBoundaries(testHelper, randomOffset)
94+
))
95+
.toList();
96+
}
97+
98+
private static void assertVec3Equals(final GameTestHelper testHelper, Vec3 actual, Vec3 expected) {
99+
if(!DoubleMath.fuzzyEquals(actual.x, expected.x, 0.001))
100+
testHelper.fail("X did not match expected value (was: %s; expected: %s)".formatted(actual.x, expected.x));
101+
102+
if(!DoubleMath.fuzzyEquals(actual.y, expected.y, 0.001))
103+
testHelper.fail("Y did not match expected value (was: %s; expected: %s)".formatted(actual.y, expected.y));
104+
105+
if(!DoubleMath.fuzzyEquals(actual.z, expected.z, 0.001))
106+
testHelper.fail("Z did not match expected value (was: %s; expected: %s)".formatted(actual.z, expected.z));
107+
}
108+
109+
public static void normalizeIntoBoundaries(final GameTestHelper test, Vec3 randomOffset) {
110+
AABB before = AABB.ofSize(Vec3.ZERO.relative(Direction.UP, 7.5), 5, 5, 5);
111+
AABB bounds = AABB.ofSize(randomOffset, 5, 5, 5);
112+
113+
final var after = AABBHelper.normalizeWithin(before, bounds);
114+
115+
test.assertTrue(before.minY == 5, "Before was modified in-place rather than immutably moved.");
116+
117+
assertVec3Equals(test, AABBHelper.minCorner(after), AABBHelper.minCorner(bounds));
118+
119+
test.assertTrue(after.getYsize() == 5, "AABB size was modified; should have remained the same.");
120+
test.succeed();
121+
}
122+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.compactmods.spatial.tests;
2+
3+
import net.minecraft.util.RandomSource;
4+
import net.minecraft.world.phys.Vec3;
5+
6+
import java.util.stream.Stream;
7+
8+
public class TestUtils {
9+
10+
public static Stream<Vec3> randomVec3Stream(RandomSource random) {
11+
return Stream.generate(() -> new Vec3(random.nextDouble(), random.nextDouble(), random.nextDouble()));
12+
}
13+
}
Binary file not shown.
125 Bytes
Binary file not shown.
457 Bytes
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"pack": {
3+
"description": "Spatial test resources",
4+
"pack_format": 18
5+
}
6+
}

0 commit comments

Comments
 (0)