Skip to content

Commit 413866f

Browse files
committed
Update to 25w09b
1 parent 4882fb5 commit 413866f

File tree

36 files changed

+619
-283
lines changed

36 files changed

+619
-283
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import net.fabricmc.loom.task.RemapJarTask
44
import java.net.URI
55

66
plugins {
7-
id("fabric-loom") version "1.9-SNAPSHOT"
8-
id("io.github.ladysnake.chenille") version "0.14.0"
7+
id("fabric-loom") version "1.10-SNAPSHOT"
8+
id("io.github.ladysnake.chenille") version "0.15.0"
99
id("org.cadixdev.licenser") version "0.6.1"
1010
}
1111

cardinal-components-base/src/main/java/org/ladysnake/cca/api/v3/component/ComponentContainer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,21 @@ default ComponentKey<?> getKey(Component component) {
121121
@Contract(mutates = "this")
122122
void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
123123

124+
@Contract(mutates = "this")
125+
void fromOrphanTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
126+
124127
/**
125128
* Writes this object's properties to a {@link NbtCompound}.
126129
*
127130
* @param tag a {@code NbtCompound} on which to write this component's serializable data
128131
* @param registryLookup access to dynamic registry data
129132
* @return {@code tag} for easy chaining
130133
*/
131-
@Contract(mutates = "param")
134+
@Contract(mutates = "param1")
132135
NbtCompound toTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
133136

137+
@Nullable NbtCompound toOrphanTag(RegistryWrapper.WrapperLookup registryLookup);
138+
134139
/**
135140
* A factory for {@link ComponentContainer}s.
136141
*

cardinal-components-base/src/main/java/org/ladysnake/cca/internal/base/AbstractComponentContainer.java

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@
2323
package org.ladysnake.cca.internal.base;
2424

2525
import net.minecraft.nbt.NbtCompound;
26-
import net.minecraft.nbt.NbtElement;
2726
import net.minecraft.nbt.NbtList;
2827
import net.minecraft.registry.RegistryWrapper;
2928
import net.minecraft.util.Identifier;
30-
import org.ladysnake.cca.api.v3.component.Component;
31-
import org.ladysnake.cca.api.v3.component.ComponentContainer;
32-
import org.ladysnake.cca.api.v3.component.ComponentKey;
33-
import org.ladysnake.cca.api.v3.component.ComponentRegistry;
34-
import org.ladysnake.cca.api.v3.component.CopyableComponent;
29+
import org.jetbrains.annotations.Nullable;
30+
import org.ladysnake.cca.api.v3.component.*;
3531

3632
import java.util.Iterator;
33+
import java.util.Optional;
3734

3835
/**
3936
* Implementing class for {@link ComponentContainer}.
@@ -74,34 +71,44 @@ public void copyFrom(ComponentContainer other, RegistryWrapper.WrapperLookup reg
7471
*/
7572
@Override
7673
public void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
77-
if(tag.contains(NBT_KEY, NbtElement.LIST_TYPE)) {
78-
NbtList componentList = tag.getList(NBT_KEY, NbtElement.COMPOUND_TYPE);
74+
Optional<NbtList> list = tag.getList(NBT_KEY);
75+
if(list.isPresent()) {
76+
NbtList componentList = list.get();
7977
for (int i = 0; i < componentList.size(); i++) {
80-
NbtCompound nbt = componentList.getCompound(i);
81-
ComponentKey<?> type = ComponentRegistry.get(Identifier.of(nbt.getString("componentId")));
82-
if (type != null) {
83-
Component component = type.getInternal(this);
78+
NbtCompound nbt = componentList.getOrCreateCompound(i);
79+
Optional<ComponentKey<?>> type = nbt.getString("componentId").map(Identifier::tryParse).map(ComponentRegistry::get);
80+
if (type.isPresent()) {
81+
Component component = type.get().getInternal(this);
8482
if (component != null) {
8583
component.readFromNbt(nbt, registryLookup);
8684
}
8785
}
8886
}
89-
} else if (tag.contains(NBT_KEY, NbtElement.COMPOUND_TYPE)) {
90-
NbtCompound componentMap = tag.getCompound(NBT_KEY);
87+
} else {
88+
Optional<NbtCompound> compound = tag.getCompound(NBT_KEY);
9189

92-
for (ComponentKey<?> key : this.keys()) {
93-
String keyId = key.getId().toString();
94-
95-
if (componentMap.contains(keyId, NbtElement.COMPOUND_TYPE)) {
96-
Component component = key.getInternal(this);
97-
assert component != null;
98-
component.readFromNbt(componentMap.getCompound(keyId), registryLookup);
99-
componentMap.remove(keyId);
100-
}
90+
if (compound.isPresent()) {
91+
NbtCompound componentMap = compound.get();
92+
fromOrphanTag(componentMap, registryLookup);
10193
}
94+
}
95+
}
10296

103-
ComponentsInternals.logDeserializationWarnings(componentMap.getKeys());
97+
@Override
98+
public void fromOrphanTag(NbtCompound componentMap, RegistryWrapper.WrapperLookup registryLookup) {
99+
for (ComponentKey<?> key : this.keys()) {
100+
String keyId = key.getId().toString();
101+
102+
Optional<NbtCompound> componentNbt = componentMap.getCompound(keyId);
103+
if (componentNbt.isPresent()) {
104+
Component component = key.getInternal(this);
105+
assert component != null;
106+
component.readFromNbt(componentNbt.get(), registryLookup);
107+
componentMap.remove(keyId);
108+
}
104109
}
110+
111+
ComponentsInternals.logDeserializationWarnings(componentMap.getKeys());
105112
}
106113

107114
/**
@@ -117,25 +124,33 @@ public void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
117124
@Override
118125
public NbtCompound toTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
119126
if(this.hasComponents()) {
120-
NbtCompound componentMap = null;
121-
NbtCompound componentTag = new NbtCompound();
127+
NbtCompound componentMap = toOrphanTag(registryLookup);
128+
if (componentMap != null) {
129+
tag.put(NBT_KEY, componentMap);
130+
}
131+
}
132+
return tag;
133+
}
122134

123-
for (ComponentKey<?> type : this.keys()) {
124-
Component component = type.getFromContainer(this);
125-
component.writeToNbt(componentTag, registryLookup);
135+
@Override
136+
public @Nullable NbtCompound toOrphanTag(RegistryWrapper.WrapperLookup registryLookup) {
137+
NbtCompound componentMap = null;
138+
NbtCompound componentTag = new NbtCompound();
126139

127-
if (!componentTag.isEmpty()) {
128-
if (componentMap == null) {
129-
componentMap = new NbtCompound();
130-
tag.put(NBT_KEY, componentMap);
131-
}
140+
for (ComponentKey<?> type : this.keys()) {
141+
Component component = type.getFromContainer(this);
142+
component.writeToNbt(componentTag, registryLookup);
132143

133-
componentMap.put(type.getId().toString(), componentTag);
134-
componentTag = new NbtCompound(); // recycle tag objects if possible
144+
if (!componentTag.isEmpty()) {
145+
if (componentMap == null) {
146+
componentMap = new NbtCompound();
135147
}
148+
149+
componentMap.put(type.getId().toString(), componentTag);
150+
componentTag = new NbtCompound(); // recycle tag objects if possible
136151
}
137152
}
138-
return tag;
153+
return componentMap;
139154
}
140155

141156
@Override

cardinal-components-base/src/testmod/java/org/ladysnake/cca/internal/base/ComponentRegistryImplTest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,41 @@
2222
*/
2323
package org.ladysnake.cca.internal.base;
2424

25-
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
25+
import net.fabricmc.fabric.api.gametest.v1.GameTest;
2626
import net.minecraft.nbt.NbtCompound;
2727
import net.minecraft.registry.RegistryWrapper;
28-
import net.minecraft.test.GameTest;
28+
import net.minecraft.test.TestContext;
29+
import net.minecraft.text.Text;
2930
import net.minecraft.util.Identifier;
30-
import org.junit.Assert;
3131
import org.ladysnake.cca.api.v3.component.Component;
3232
import org.ladysnake.cca.api.v3.component.ComponentKey;
3333
import org.ladysnake.cca.test.base.CardinalGameTest;
3434

3535
public class ComponentRegistryImplTest implements CardinalGameTest {
3636

3737
@SuppressWarnings({"unchecked", "rawtypes"})
38-
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
39-
public void checksRegisteredClasses() {
38+
@GameTest
39+
public void checksRegisteredClasses(TestContext ctx) {
4040
ComponentRegistryImpl registry = ComponentRegistryImpl.INSTANCE;
41-
Assert.assertThrows("Component class must extend Component", IllegalArgumentException.class, () -> registry.getOrCreate(CcaTesting.TEST_ID_1, (Class) TestNotComponentItf.class));
41+
ctx.assertThrows("Component class must extend Component", IllegalArgumentException.class, () -> registry.getOrCreate(CcaTesting.TEST_ID_1, (Class) TestNotComponentItf.class));
4242
registry.getOrCreate(CcaTesting.TEST_ID_1, TestComponentNotItf.class);
4343
registry.getOrCreate(CcaTesting.TEST_ID_2, TestComponentItf.class);
4444
}
4545

46-
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
47-
public void doesNotDuplicateComponentTypes() {
46+
@GameTest
47+
public void doesNotDuplicateComponentTypes(TestContext ctx) {
4848
ComponentRegistryImpl registry = ComponentRegistryImpl.INSTANCE;
4949
Identifier id = CcaTesting.TEST_ID_1;
5050
ComponentKey<?> type = registry.getOrCreate(id, TestComponentItf.class);
51-
Assert.assertThrows(IllegalStateException.class, () -> registry.getOrCreate(id, TestComponentItf2.class));
52-
Assert.assertThrows(IllegalStateException.class, () -> registry.getOrCreate(id, TestComponentItf3.class));
53-
Assert.assertEquals(type, registry.getOrCreate(id, TestComponentItf.class));
54-
Assert.assertEquals(1, registry.stream().map(ComponentKey::getId).filter(CcaTesting.ALL_TEST_IDS::contains).count());
51+
ctx.assertThrows(IllegalStateException.class, () -> registry.getOrCreate(id, TestComponentItf2.class));
52+
ctx.assertThrows(IllegalStateException.class, () -> registry.getOrCreate(id, TestComponentItf3.class));
53+
ctx.assertEquals(type, registry.getOrCreate(id, TestComponentItf.class), Text.literal("component key"));
54+
ctx.assertEquals(1L, registry.stream().map(ComponentKey::getId).filter(CcaTesting.ALL_TEST_IDS::contains).count(), Text.literal("number of registrations"));
5555
}
5656

5757
@Override
58-
public void tearDown() {
58+
public void tearDown(TestContext ctx) {
59+
ctx.complete();
5960
for (Identifier id : CcaTesting.ALL_TEST_IDS) {
6061
ComponentRegistryImpl.INSTANCE.clear(id);
6162
}

cardinal-components-base/src/testmod/java/org/ladysnake/cca/internal/base/QualifiedComponentFactoryTest.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
*/
2323
package org.ladysnake.cca.internal.base;
2424

25-
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
26-
import net.minecraft.test.GameTest;
25+
import net.fabricmc.fabric.api.gametest.v1.GameTest;
26+
import net.minecraft.test.TestContext;
27+
import net.minecraft.text.Text;
2728
import net.minecraft.util.Identifier;
28-
import org.junit.Assert;
2929
import org.ladysnake.cca.api.v3.component.ComponentKey;
3030
import org.ladysnake.cca.api.v3.component.ComponentRegistry;
3131
import org.ladysnake.cca.internal.base.asm.StaticComponentLoadingException;
@@ -38,14 +38,15 @@
3838

3939
public class QualifiedComponentFactoryTest implements CardinalGameTest {
4040
@Override
41-
public void tearDown() {
41+
public void tearDown(TestContext ctx) {
42+
ctx.complete();
4243
for (Identifier id : CcaTesting.ALL_TEST_IDS) {
4344
ComponentRegistryImpl.INSTANCE.clear(id);
4445
}
4546
}
4647

47-
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
48-
public void sortKeepsOrderByDefault() {
48+
@GameTest
49+
public void sortKeepsOrderByDefault(TestContext ctx) {
4950
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> map = new LinkedHashMap<>();
5051
var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class);
5152
var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class);
@@ -54,58 +55,58 @@ public void sortKeepsOrderByDefault() {
5455
map.put(key2, new QualifiedComponentFactory<>(new Object(), key2.getComponentClass(), Set.of()));
5556
map.put(key3, new QualifiedComponentFactory<>(new Object(), key3.getComponentClass(), Set.of()));
5657
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> sorted = QualifiedComponentFactory.sort(map);
57-
Assert.assertNotSame(map, sorted);
58-
Assert.assertEquals(List.copyOf(map.keySet()), List.copyOf(sorted.keySet()));
58+
ctx.assertTrue("Sorted version should be its own instance", map != sorted);
59+
ctx.assertEquals(List.copyOf(map.keySet()), List.copyOf(sorted.keySet()), Text.literal("Keys should stay the same"));
5960
map = new LinkedHashMap<>();
6061
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of()));
6162
map.put(key3, new QualifiedComponentFactory<>(new Object(), key3.getComponentClass(), Set.of()));
6263
map.put(key2, new QualifiedComponentFactory<>(new Object(), key2.getComponentClass(), Set.of()));
6364
sorted = QualifiedComponentFactory.sort(map);
64-
Assert.assertNotSame(map, sorted);
65-
Assert.assertEquals(List.copyOf(map.keySet()), List.copyOf(sorted.keySet()));
65+
ctx.assertTrue("Sorted version should be its own instance", map != sorted);
66+
ctx.assertEquals(List.copyOf(map.keySet()), List.copyOf(sorted.keySet()), Text.literal("Keys should stay the same"));
6667
}
6768

68-
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
69-
public void sortThrowsOnUnsatisfiedDependency() {
69+
@GameTest
70+
public void sortThrowsOnUnsatisfiedDependency(TestContext ctx) {
7071
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> map = new LinkedHashMap<>();
7172
var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class);
7273
var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class);
7374
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2)));
74-
Assert.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.checkDependenciesSatisfied(map));
75+
ctx.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.checkDependenciesSatisfied(map));
7576
map.put(key2, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of()));
7677
QualifiedComponentFactory.checkDependenciesSatisfied(map);
7778
}
7879

79-
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
80-
public void sortThrowsOnCircularDependency() {
80+
@GameTest
81+
public void sortThrowsOnCircularDependency(TestContext ctx) {
8182
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> map = new LinkedHashMap<>();
8283
var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class);
8384
var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class);
8485
var key3 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class);
8586
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key1)));
86-
Assert.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map));
87+
ctx.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map));
8788
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2)));
8889
map.put(key2, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key1)));
89-
Assert.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map));
90+
ctx.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map));
9091
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2)));
9192
map.put(key2, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key3)));
9293
map.put(key3, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key1)));
93-
Assert.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map));
94+
ctx.assertThrows(StaticComponentLoadingException.class, () -> QualifiedComponentFactory.sort(map));
9495
map.put(key3, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of()));
9596
QualifiedComponentFactory.sort(map);
9697
}
9798

98-
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
99-
public void sortRespectsDependencyOrdering() {
99+
@GameTest
100+
public void sortRespectsDependencyOrdering(TestContext ctx) {
100101
Map<ComponentKey<?>, QualifiedComponentFactory<Object>> map = new LinkedHashMap<>();
101102
var key1 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_1, ComponentRegistryImplTest.TestComponentNotItf.class);
102103
var key2 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_2, ComponentRegistryImplTest.TestComponentNotItf.class);
103104
var key3 = ComponentRegistry.getOrCreate(CcaTesting.TEST_ID_3, ComponentRegistryImplTest.TestComponentNotItf.class);
104105
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2)));
105106
map.put(key2, new QualifiedComponentFactory<>(new Object(), key2.getComponentClass(), Set.of()));
106-
Assert.assertEquals(List.of(key2, key1), List.copyOf(QualifiedComponentFactory.sort(map).keySet()));
107+
ctx.assertEquals(List.of(key2, key1), List.copyOf(QualifiedComponentFactory.sort(map).keySet()), Text.literal("Sorted map should have correct key order"));
107108
map.put(key1, new QualifiedComponentFactory<>(new Object(), key1.getComponentClass(), Set.of(key2, key3)));
108109
map.put(key3, new QualifiedComponentFactory<>(new Object(), key3.getComponentClass(), Set.of(key2)));
109-
Assert.assertEquals(List.of(key2, key3, key1), List.copyOf(QualifiedComponentFactory.sort(map).keySet()));
110+
ctx.assertEquals(List.of(key2, key3, key1), List.copyOf(QualifiedComponentFactory.sort(map).keySet()), Text.literal("Sorted map should have correct key order"));
110111
}
111112
}

cardinal-components-base/src/testmod/java/org/ladysnake/cca/test/base/BaseVita.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setVitality(int value) {
5050

5151
@Override
5252
public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
53-
this.vitality = tag.getInt("vitality");
53+
this.vitality = tag.getInt("vitality", 0);
5454
}
5555

5656
@Override

cardinal-components-base/src/testmod/java/org/ladysnake/cca/test/base/CardinalGameTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,26 @@
2222
*/
2323
package org.ladysnake.cca.test.base;
2424

25-
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
26-
import net.minecraft.test.GameTestException;
2725
import net.minecraft.test.TestContext;
26+
import net.minecraft.text.Text;
2827
import org.apache.logging.log4j.LogManager;
2928
import org.apache.logging.log4j.Logger;
3029

3130
import java.lang.reflect.InvocationTargetException;
3231
import java.lang.reflect.Method;
3332

34-
public interface CardinalGameTest extends FabricGameTest {
33+
public interface CardinalGameTest {
3534
Logger LOGGER = LogManager.getLogger();
3635

37-
@Override
3836
default void invokeTestMethod(TestContext context, Method method) {
3937
try {
4038
this.setUp();
41-
if (method.getParameterCount() > 1) {
42-
method.invoke(this, context);
43-
} else {
39+
if (method.getParameterCount() == 0) {
4440
method.invoke(this);
4541
context.complete();
42+
} else {
43+
method.invoke(this, context);
4644
}
47-
this.tearDown();
4845
} catch (Throwable e) {
4946
String message;
5047
Throwable cause;
@@ -56,15 +53,17 @@ default void invokeTestMethod(TestContext context, Method method) {
5653
cause = e;
5754
}
5855
LOGGER.error("Failed test", cause);
59-
throw new GameTestException(message);
56+
throw context.createError(Text.literal(message));
57+
} finally {
58+
this.tearDown(context);
6059
}
6160
}
6261

6362
default void setUp() {
6463

6564
}
6665

67-
default void tearDown() {
66+
default void tearDown(TestContext ctx) {
6867

6968
}
7069
}

0 commit comments

Comments
 (0)