mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-21 12:42:51 +00:00
feat: add Java collection occlusion query
This commit is contained in:
1 parent
6744f5b04c
commit
d70f69e79f
11 files changed
+317
-25
No files matched your search
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -587,6 +588,24 @@ class ForgeEnvironmentSmokeTest {
|
||||
assertFalse(tile.occlusionTest(tile.partList(), new PartialPart(voxel(0))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaOcclusionQueryDispatchesThroughTheGeneratedPartialTrait() {
|
||||
PartialPart existing = new PartialPart(voxel(0));
|
||||
PartialPart overlapping = new PartialPart(voxel(0));
|
||||
PartialPart disjoint = new PartialPart(voxel(1));
|
||||
TileMultipart tile = MultipartHelper.createTileFromParts(Collections.singletonList(existing));
|
||||
|
||||
assertTrue(
|
||||
new TileMultipart().testOcclusion(Collections.singletonList(existing), overlapping),
|
||||
"Pair tests alone allow these parts; the generated aggregate check must reject them");
|
||||
assertFalse(tile.testOcclusion(Collections.singletonList(existing), overlapping));
|
||||
assertTrue(tile.testOcclusion(Collections.singletonList(existing), disjoint));
|
||||
assertEquals(Collections.singletonList(existing), tile.jPartList());
|
||||
assertSame(tile, existing.tile());
|
||||
assertNull(overlapping.tile());
|
||||
assertNull(disjoint.tile());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedServerPassThroughDelegatesCopiesAndAllowsOnlyOneImplementor() throws Exception {
|
||||
PassThroughPart part = new PassThroughPart();
|
||||
|
||||
@@ -415,7 +415,40 @@ public class TileMultipart extends TileEntity implements IChunkLoadTile {
|
||||
return occlusionTest(others, npart);
|
||||
}
|
||||
|
||||
/** Returns true if parts do not occlude npart. */
|
||||
/**
|
||||
* Tests a candidate against a shallow snapshot of the supplied parts through the legacy
|
||||
* {@link #occlusionTest(Seq, TMultiPart)} hook, including generated partial-occlusion overrides. Preserves
|
||||
* collection iteration order, duplicates and part identities; changes to the caller's collection during callbacks
|
||||
* do not change this query's sequence.
|
||||
*
|
||||
* <p>
|
||||
* The default hook calls each existing part's occlusion test first, then the candidate's reverse test, stopping
|
||||
* immediately on rejection or an exception. It includes detached parts and does not exclude the candidate itself.
|
||||
* Part objects remain shared, so callback changes to their state are visible. Existing overrides may add checks.
|
||||
*
|
||||
* <p>
|
||||
* This tests the supplied geometry, not full placement validity: it does not select tile capabilities, check
|
||||
* placement permissions or slot occupancy, bind parts or change stored parts. Use the appropriate composite tile
|
||||
* for the candidate's capabilities; use {@link #canAddPart(TMultiPart)} /
|
||||
* {@link #canReplacePart(TMultiPart, TMultiPart)} for normal tile checks. Those methods still dispatch directly
|
||||
* through the legacy hook.
|
||||
*
|
||||
* @param parts parts to copy before dispatch; null fails before calling the legacy hook
|
||||
* @param candidate part to test; no eager null check, so the default hook accepts null against an empty collection
|
||||
* @return true when all applicable occlusion checks allow the candidate
|
||||
*/
|
||||
public boolean testOcclusion(Collection<? extends TMultiPart> parts, TMultiPart candidate) {
|
||||
return occlusionTest(toSeq(new ArrayList<>(parts)), candidate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if parts do not occlude npart.
|
||||
*
|
||||
* @deprecated Call {@link #testOcclusion(Collection, TMultiPart)} from Java. Retained as the virtual hook for
|
||||
* existing subclasses, generated traits and tile placement/replacement checks; its sequence behavior is
|
||||
* unchanged.
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean occlusionTest(Seq<TMultiPart> parts, TMultiPart npart) {
|
||||
for (TMultiPart part : JavaConversions.seqAsJavaList(parts)) {
|
||||
if (!part.occlusionTest(npart) || !npart.occlusionTest(part)) {
|
||||
|
||||
@@ -9,10 +9,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
import codechicken.multipart.examples.OcclusionExample;
|
||||
import scala.collection.JavaConversions;
|
||||
import scala.collection.Seq;
|
||||
|
||||
@@ -85,6 +90,105 @@ class TileMultipartOcclusionTest {
|
||||
assertEquals(Arrays.asList(outgoing, retained), tile.jPartList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaQuerySnapshotsTheSuppliedCollectionAndRetainsDuplicatesAndUnboundParts() {
|
||||
TileMultipart tile = new TileMultipart();
|
||||
List<String> calls = new ArrayList<>();
|
||||
Part stored = new Part("stored", calls);
|
||||
stored.accepts = false;
|
||||
tile.setPartList(Arrays.asList(stored));
|
||||
Part first = new Part("first", calls);
|
||||
Part second = new Part("second", calls);
|
||||
Part candidate = new Part("candidate", calls);
|
||||
List<Part> input = new ArrayList<>(Arrays.asList(first, second, first));
|
||||
first.onTest = input::clear;
|
||||
|
||||
assertTrue(tile.testOcclusion(input, candidate));
|
||||
assertEquals(
|
||||
Arrays.asList(
|
||||
"first:candidate",
|
||||
"candidate:first",
|
||||
"second:candidate",
|
||||
"candidate:second",
|
||||
"first:candidate",
|
||||
"candidate:first"),
|
||||
calls);
|
||||
assertTrue(input.isEmpty());
|
||||
assertEquals(
|
||||
Arrays.asList(stored),
|
||||
tile.jPartList(),
|
||||
"The query uses the supplied collection, not tile storage");
|
||||
assertNull(first.tile());
|
||||
assertNull(candidate.tile());
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaQueryPreservesTheLegacyOverrideAndDoesNotReplaceTheInternalHook() {
|
||||
List<String> calls = new ArrayList<>();
|
||||
List<Seq<TMultiPart>> inputs = new ArrayList<>();
|
||||
TileMultipart tile = new TileMultipart() {
|
||||
|
||||
@Override
|
||||
public boolean occlusionTest(Seq<TMultiPart> parts, TMultiPart candidate) {
|
||||
calls.add("legacy");
|
||||
inputs.add(parts);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testOcclusion(Collection<? extends TMultiPart> parts, TMultiPart candidate) {
|
||||
calls.add("java");
|
||||
return super.testOcclusion(parts, candidate);
|
||||
}
|
||||
};
|
||||
Part first = new Part("first", calls);
|
||||
Part second = new Part("second", calls);
|
||||
Part candidate = new Part("candidate", calls);
|
||||
assertFalse(tile.testOcclusion(new LinkedHashSet<>(Arrays.asList(first, second)), candidate));
|
||||
tile.setPartList(Arrays.asList(first, second));
|
||||
assertFalse(tile.canReplacePart(first, candidate));
|
||||
assertEquals(Arrays.asList("java", "legacy", "legacy"), calls);
|
||||
assertEquals(Arrays.asList(seq(first, second), seq(second)), inputs);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaQueryKeepsShortCircuitingAndCallbackFailureBoundaries() {
|
||||
TileMultipart tile = new TileMultipart();
|
||||
List<String> calls = new ArrayList<>();
|
||||
Part first = new Part("first", calls);
|
||||
Part candidate = new Part("candidate", calls);
|
||||
assertTrue(tile.testOcclusion(Collections.emptyList(), null));
|
||||
assertThrows(NullPointerException.class, () -> tile.testOcclusion(null, candidate));
|
||||
first.accepts = false;
|
||||
assertFalse(tile.testOcclusion(Arrays.asList(first, null), null));
|
||||
assertEquals(Arrays.asList("first:null"), calls);
|
||||
first.accepts = true;
|
||||
candidate.accepts = false;
|
||||
calls.clear();
|
||||
assertFalse(tile.testOcclusion(Arrays.asList(first, null), candidate));
|
||||
assertEquals(Arrays.asList("first:candidate", "candidate:first"), calls);
|
||||
calls.clear();
|
||||
IllegalStateException failure = new IllegalStateException("candidate callback");
|
||||
candidate.failure = failure;
|
||||
assertSame(
|
||||
failure,
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> tile.testOcclusion(Arrays.asList(first, null), candidate)));
|
||||
assertEquals(Arrays.asList("first:candidate", "candidate:first"), calls);
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaShapeExampleDistinguishesTouchingFromOverlappingBounds() {
|
||||
TileMultipart tile = new TileMultipart();
|
||||
NormallyOccludedPart existing = new NormallyOccludedPart(new Cuboid6(0, 0, 0, 0.5, 1, 1));
|
||||
List<NormallyOccludedPart> input = Arrays.asList(existing);
|
||||
assertTrue(OcclusionExample.fitsShape(tile, input, new Cuboid6(0.5, 0, 0, 1, 1, 1)));
|
||||
assertFalse(OcclusionExample.fitsShape(tile, input, new Cuboid6(0.25, 0, 0, 1, 1, 1)));
|
||||
assertTrue(tile.jPartList().isEmpty());
|
||||
assertNull(existing.tile());
|
||||
}
|
||||
|
||||
private static Seq<TMultiPart> seq(TMultiPart... parts) {
|
||||
return JavaConversions.asScalaBuffer(Arrays.asList(parts)).toList();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package codechicken.multipart.examples;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import codechicken.lib.vec.Cuboid6;
|
||||
import codechicken.multipart.NormallyOccludedPart;
|
||||
import codechicken.multipart.TMultiPart;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
|
||||
/** Compiling example for docs/api/OCCLUSION.md; this checks geometry, not full placement validity. */
|
||||
public final class OcclusionExample {
|
||||
|
||||
private OcclusionExample() {}
|
||||
|
||||
public static boolean fitsShape(TileMultipart tile, Collection<? extends TMultiPart> parts, Cuboid6 bounds) {
|
||||
return tile.testOcclusion(parts, new NormallyOccludedPart(bounds));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user