Skip to content

Commit e58ee25

Browse files
committed
Update to 25w20a
1 parent 3af6af1 commit e58ee25

File tree

32 files changed

+252
-189
lines changed

32 files changed

+252
-189
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
package org.ladysnake.cca.api.v3.component;
2424

2525
import net.minecraft.nbt.NbtCompound;
26-
import net.minecraft.registry.RegistryWrapper;
26+
import net.minecraft.storage.ReadView;
27+
import net.minecraft.storage.WriteView;
2728
import org.jetbrains.annotations.Contract;
2829

2930
/**
@@ -36,21 +37,19 @@ public interface Component {
3637
/**
3738
* Reads this component's properties from a {@link NbtCompound}.
3839
*
39-
* @param tag a {@code NbtCompound} on which this component's serializable data has been written
40-
* @param registryLookup access to dynamic registry data
40+
* @param readView a {@code NbtCompound} on which this component's serializable data has been written
4141
* @implNote implementations should not assert that the data written on the tag corresponds to any
4242
* specific scheme, as saved data is susceptible to external tempering, and may come from an earlier
4343
* version.
4444
*/
4545
@Contract(mutates = "this")
46-
void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
46+
void readData(ReadView readView);
4747

4848
/**
4949
* Writes this component's properties to a {@link NbtCompound}.
5050
*
51-
* @param tag a {@code NbtCompound} on which to write this component's serializable data
52-
* @param registryLookup access to dynamic registry data
51+
* @param writeView a {@code NbtCompound} on which to write this component's serializable data
5352
*/
5453
@Contract(mutates = "param1")
55-
void writeToNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
54+
void writeData(WriteView writeView);
5655
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import net.fabricmc.api.EnvType;
2626
import net.minecraft.nbt.NbtCompound;
2727
import net.minecraft.registry.RegistryWrapper;
28+
import net.minecraft.storage.ReadView;
29+
import net.minecraft.storage.WriteView;
2830
import org.jetbrains.annotations.ApiStatus;
2931
import org.jetbrains.annotations.Contract;
3032
import org.jetbrains.annotations.Nullable;
@@ -111,28 +113,28 @@ default ComponentKey<?> getKey(Component component) {
111113
/**
112114
* Reads this object's properties from a {@link NbtCompound}.
113115
*
114-
* @param tag a {@code NbtCompound} on which this object's serializable data has been written
115-
* @param registryLookup access to dynamic registry data
116+
* @param readView a {@code NbtCompound} on which this object's serializable data has been written
116117
* @implNote implementations must not assert that the data written on the tag corresponds to any
117118
* specific scheme, as saved data is susceptible to external tempering, and may come from an earlier
118119
* version. They should also store values into {@code tag} using only unique namespaced keys, as other
119120
* information may be stored in said tag.
120121
*/
121122
@Contract(mutates = "this")
122-
void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
123+
void readData(ReadView readView);
123124

124125
@Contract(mutates = "this")
125-
void fromOrphanTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
126+
void readOrphanData(ReadView readView);
126127

127128
/**
128129
* Writes this object's properties to a {@link NbtCompound}.
129130
*
130-
* @param tag a {@code NbtCompound} on which to write this component's serializable data
131-
* @param registryLookup access to dynamic registry data
132-
* @return {@code tag} for easy chaining
131+
* @param writeView a {@code NbtCompound} on which to write this component's serializable data
133132
*/
134133
@Contract(mutates = "param1")
135-
NbtCompound toTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
134+
void writeData(WriteView writeView);
135+
136+
@Contract(mutates = "param1")
137+
void writeOrphanData(WriteView writeView);
136138

137139
@Nullable NbtCompound toOrphanTag(RegistryWrapper.WrapperLookup registryLookup);
138140

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void syncWith(ServerPlayerEntity player, ComponentProvider provider) {
200200
@ApiStatus.Experimental
201201
public void syncWith(ServerPlayerEntity player, ComponentProvider provider, ComponentPacketWriter writer, PlayerSyncPredicate predicate) {
202202
if (predicate.shouldSyncWith(player)) {
203-
RegistryByteBuf buf = new RegistryByteBuf(Unpooled.buffer(), player.getServerWorld().getRegistryManager());
203+
RegistryByteBuf buf = new RegistryByteBuf(Unpooled.buffer(), player.getWorld().getRegistryManager());
204204
writer.writeSyncPacket(buf, player);
205205
CustomPayload payload = provider.toComponentPacket(this, predicate.isRequiredOnClient(), buf);
206206

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public interface CopyableComponent<C extends Component> extends Component {
3535
/**
3636
* Copies the data from {@code other} into {@code this}.
3737
*
38-
* @implSpec The default implementation {@linkplain Component#writeToNbt(NbtCompound, RegistryWrapper.WrapperLookup) serializes}
39-
* the component data to a {@link NbtCompound} and calls {@link Component#readFromNbt(NbtCompound, RegistryWrapper.WrapperLookup)}.
38+
* @implSpec The default implementation {@linkplain Component#writeData(net.minecraft.storage.WriteView) serializes}
39+
* the component data to a {@link NbtCompound} and calls {@link Component#readData(net.minecraft.storage.ReadView)}.
4040
* @implNote The default implementation should generally be overridden.
4141
* The serialization done by the default implementation assumes NBT consistency
4242
* between implementations, and is generally slower than a direct copy.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222
*/
2323
package org.ladysnake.cca.api.v3.component;
2424

25-
import net.minecraft.nbt.NbtCompound;
26-
import net.minecraft.registry.RegistryWrapper;
25+
import net.minecraft.storage.ReadView;
26+
import net.minecraft.storage.WriteView;
2727
import org.jetbrains.annotations.Nullable;
2828

2929
/**
3030
* Utility interface for components that do not hold any data
3131
*/
3232
public interface TransientComponent extends Component {
3333
@Override
34-
default void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
34+
default void readData(ReadView readView) {
3535
// Nothing to read
3636
}
3737

3838
@Override
39-
default void writeToNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
39+
default void writeData(WriteView writeView) {
4040
// Nothing to write
4141
}
4242

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
import net.minecraft.entity.Entity;
2727
import net.minecraft.nbt.NbtCompound;
2828
import net.minecraft.network.RegistryByteBuf;
29-
import net.minecraft.registry.RegistryWrapper.WrapperLookup;
3029
import net.minecraft.server.network.ServerPlayerEntity;
30+
import net.minecraft.storage.NbtReadView;
31+
import net.minecraft.storage.NbtWriteView;
32+
import net.minecraft.util.ErrorReporter;
3133
import net.minecraft.world.World;
3234
import org.jetbrains.annotations.Contract;
3335
import org.ladysnake.cca.api.v3.component.Component;
3436
import org.ladysnake.cca.api.v3.component.ComponentKey;
3537
import org.ladysnake.cca.api.v3.util.CheckEnvironment;
38+
import org.ladysnake.cca.internal.base.ComponentsInternals;
3639

3740
/**
3841
* A {@link Component} implementing this interface will have its data automatically
@@ -68,7 +71,7 @@ default boolean shouldSyncWith(ServerPlayerEntity player) {
6871
* @param buf the buffer to write the data to
6972
* @param recipient the player to which the packet will be sent
7073
* @implSpec The default implementation writes the whole NBT representation
71-
* of this component to the buffer using {@link Component#writeToNbt(NbtCompound, WrapperLookup)}.
74+
* of this component to the buffer using {@link Component#writeData(net.minecraft.storage.WriteView)}.
7275
* @implNote The default implementation should generally be overridden.
7376
* The serialization done by the default implementation sends possibly hidden
7477
* information to clients, uses a wasteful data format, and does not support
@@ -81,16 +84,18 @@ default boolean shouldSyncWith(ServerPlayerEntity player) {
8184
@Contract(mutates = "param1")
8285
@Override
8386
default void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient) {
84-
NbtCompound tag = new NbtCompound();
85-
this.writeToNbt(tag, buf.getRegistryManager());
86-
buf.writeNbt(tag);
87+
try (var errorReporter = new ErrorReporter.Logging(ComponentsInternals.LOGGER)) {
88+
NbtWriteView writeView = NbtWriteView.create(errorReporter, buf.getRegistryManager());
89+
this.writeData(writeView);
90+
buf.writeNbt(writeView.getNbt());
91+
}
8792
}
8893

8994
/**
9095
* Reads this component's data from {@code buf}.
9196
*
9297
* @implSpec The default implementation converts the buffer's content
93-
* to a {@link NbtCompound} and calls {@link Component#readFromNbt(NbtCompound, WrapperLookup)}.
98+
* to a {@link NbtCompound} and calls {@link Component#readData(net.minecraft.storage.ReadView)}.
9499
* @implNote any implementing class overriding {@link #writeSyncPacket(RegistryByteBuf, ServerPlayerEntity)}
95100
* such that it uses a different data format must override this method.
96101
* @see #writeSyncPacket(RegistryByteBuf, ServerPlayerEntity)
@@ -99,7 +104,9 @@ default void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient)
99104
default void applySyncPacket(RegistryByteBuf buf) {
100105
NbtCompound tag = buf.readNbt();
101106
if (tag != null) {
102-
this.readFromNbt(tag, buf.getRegistryManager());
107+
try (var errorReporter = new ErrorReporter.Logging(ComponentsInternals.LOGGER)) {
108+
this.readData(NbtReadView.create(errorReporter, buf.getRegistryManager(), tag));
109+
}
103110
}
104111
}
105112
}

cardinal-components-base/src/main/java/org/ladysnake/cca/api/v3/util/NbtSerializable.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
*/
2323
package org.ladysnake.cca.api.v3.util;
2424

25-
import com.mojang.serialization.Dynamic;
2625
import net.minecraft.nbt.NbtCompound;
27-
import net.minecraft.nbt.NbtOps;
28-
import net.minecraft.registry.RegistryWrapper;
26+
import net.minecraft.storage.ReadView;
27+
import net.minecraft.storage.WriteView;
2928
import org.jetbrains.annotations.ApiStatus;
3029
import org.jetbrains.annotations.Contract;
3130

@@ -34,33 +33,20 @@ public interface NbtSerializable {
3433
/**
3534
* Reads this object's properties from a {@link NbtCompound}.
3635
*
37-
* @param tag a {@code NbtCompound} on which this object's serializable data has been written
38-
* @param registryLookup access to dynamic registry data
36+
* @param readView a {@code NbtCompound} on which this object's serializable data has been written
3937
* @implNote implementations must not assert that the data written on the tag corresponds to any
4038
* specific scheme, as saved data is susceptible to external tempering, and may come from an earlier
4139
* version. They should also store values into {@code tag} using only unique namespaced keys, as other
4240
* information may be stored in said tag.
4341
*/
4442
@Contract(mutates = "this")
45-
void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
43+
void readData(ReadView readView);
4644

4745
/**
4846
* Writes this object's properties to a {@link NbtCompound}.
4947
*
50-
* @param tag a {@code NbtCompound} on which to write this component's serializable data
51-
* @param registryLookup access to dynamic registry data
52-
* @return {@code tag} for easy chaining
48+
* @param writeView a {@code NbtCompound} on which to write this component's serializable data
5349
*/
5450
@Contract(mutates = "param")
55-
NbtCompound toTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup);
56-
57-
@Contract(mutates = "this")
58-
default void fromDynamic(Dynamic<?> dynamic, RegistryWrapper.WrapperLookup registryLookup) {
59-
this.fromTag((NbtCompound) dynamic.convert(NbtOps.INSTANCE).getValue(), registryLookup);
60-
}
61-
62-
@Contract(pure = true)
63-
default <T> Dynamic<T> toDynamic(Dynamic<T> dynamic, RegistryWrapper.WrapperLookup wrapperLookup) {
64-
return dynamic.convert(NbtOps.INSTANCE).map(tag -> this.toTag((NbtCompound)tag, wrapperLookup)).convert(dynamic.getOps());
65-
}
51+
void writeData(WriteView writeView);
6652
}

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

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

2525
import net.minecraft.nbt.NbtCompound;
26-
import net.minecraft.nbt.NbtList;
2726
import net.minecraft.registry.RegistryWrapper;
28-
import net.minecraft.util.Identifier;
27+
import net.minecraft.storage.NbtReadView;
28+
import net.minecraft.storage.NbtWriteView;
29+
import net.minecraft.storage.ReadView;
30+
import net.minecraft.storage.WriteView;
31+
import net.minecraft.util.ErrorReporter;
2932
import org.jetbrains.annotations.Nullable;
3033
import org.ladysnake.cca.api.v3.component.*;
34+
import org.ladysnake.cca.mixin.base.NbtReadViewAccessor;
3135

3236
import java.util.Iterator;
33-
import java.util.Optional;
3437

3538
/**
3639
* Implementing class for {@link ComponentContainer}.
@@ -51,9 +54,11 @@ public void copyFrom(ComponentContainer other, RegistryWrapper.WrapperLookup reg
5154
@SuppressWarnings("unchecked") CopyableComponent<Component> copyable = (CopyableComponent<Component>) ours;
5255
copyable.copyFrom(theirs, registryLookup);
5356
} else {
54-
NbtCompound tag = new NbtCompound();
55-
theirs.writeToNbt(tag, registryLookup);
56-
ours.readFromNbt(tag, registryLookup);
57+
try (var errorReporter = new ErrorReporter.Logging(ComponentsInternals.LOGGER)) {
58+
NbtWriteView writeView = NbtWriteView.create(errorReporter, registryLookup);
59+
theirs.writeData(writeView);
60+
ours.readData(NbtReadView.create(errorReporter, registryLookup, writeView.getNbt()));
61+
}
5762
}
5863
}
5964
}
@@ -70,45 +75,28 @@ public void copyFrom(ComponentContainer other, RegistryWrapper.WrapperLookup reg
7075
* type, the component tag is skipped.
7176
*/
7277
@Override
73-
public void fromTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
74-
Optional<NbtList> list = tag.getList(NBT_KEY);
75-
if(list.isPresent()) {
76-
NbtList componentList = list.get();
77-
for (int i = 0; i < componentList.size(); i++) {
78-
NbtCompound nbt = componentList.getCompoundOrEmpty(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);
82-
if (component != null) {
83-
component.readFromNbt(nbt, registryLookup);
84-
}
85-
}
86-
}
87-
} else {
88-
Optional<NbtCompound> compound = tag.getCompound(NBT_KEY);
89-
90-
if (compound.isPresent()) {
91-
NbtCompound componentMap = compound.get();
92-
fromOrphanTag(componentMap, registryLookup);
93-
}
94-
}
78+
public void readData(ReadView readView) {
79+
readOrphanData(readView.getReadView(NBT_KEY));
9580
}
9681

9782
@Override
98-
public void fromOrphanTag(NbtCompound componentMap, RegistryWrapper.WrapperLookup registryLookup) {
83+
public void readOrphanData(ReadView componentMap) {
84+
NbtCompound underlyingNbt = componentMap instanceof NbtReadViewAccessor nbtReadView ? nbtReadView.getNbt() : null;
9985
for (ComponentKey<?> key : this.keys()) {
10086
String keyId = key.getId().toString();
10187

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);
88+
ReadView componentData = componentMap.getReadView(keyId);
89+
Component component = key.getInternal(this);
90+
assert component != null;
91+
component.readData(componentData);
92+
if (underlyingNbt != null) {
93+
underlyingNbt.remove(keyId);
10894
}
10995
}
11096

111-
ComponentsInternals.logDeserializationWarnings(componentMap.getKeys());
97+
if (underlyingNbt != null) {
98+
ComponentsInternals.logDeserializationWarnings(underlyingNbt.getKeys());
99+
}
112100
}
113101

114102
/**
@@ -117,40 +105,34 @@ public void fromOrphanTag(NbtCompound componentMap, RegistryWrapper.WrapperLooku
117105
* @implSpec This implementation first checks if the container is empty; if so it
118106
* returns immediately. Then, it iterates over this container's mappings, and creates
119107
* a compound tag for each component. The tag is then passed to the component's
120-
* {@link Component#writeToNbt(NbtCompound, RegistryWrapper.WrapperLookup)} method. Every such serialized component is appended
108+
* {@link Component#writeData(WriteView)} method. Every such serialized component is appended
121109
* to a {@code NbtCompound}, using the component type's identifier as the key.
122110
* The serialized map is finally appended to the passed in tag using the "cardinal_components" key.
123111
*/
124112
@Override
125-
public NbtCompound toTag(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
113+
public void writeData(WriteView writeView) {
126114
if(this.hasComponents()) {
127-
NbtCompound componentMap = toOrphanTag(registryLookup);
128-
if (componentMap != null) {
129-
tag.put(NBT_KEY, componentMap);
130-
}
115+
writeOrphanData(writeView.get(NBT_KEY));
131116
}
132-
return tag;
133117
}
134118

135119
@Override
136-
public @Nullable NbtCompound toOrphanTag(RegistryWrapper.WrapperLookup registryLookup) {
137-
NbtCompound componentMap = null;
138-
NbtCompound componentTag = new NbtCompound();
139-
120+
public void writeOrphanData(WriteView writeView) {
140121
for (ComponentKey<?> type : this.keys()) {
141122
Component component = type.getFromContainer(this);
142-
component.writeToNbt(componentTag, registryLookup);
143-
144-
if (!componentTag.isEmpty()) {
145-
if (componentMap == null) {
146-
componentMap = new NbtCompound();
147-
}
148-
149-
componentMap.put(type.getId().toString(), componentTag);
150-
componentTag = new NbtCompound(); // recycle tag objects if possible
123+
if (!(component instanceof TransientComponent)) {
124+
component.writeData(writeView.get(type.getId().toString()));
151125
}
152126
}
153-
return componentMap;
127+
}
128+
129+
@Override
130+
public @Nullable NbtCompound toOrphanTag(RegistryWrapper.WrapperLookup registryLookup) {
131+
try (var errorReporter = new ErrorReporter.Logging(ComponentsInternals.LOGGER)) {
132+
NbtWriteView writeView = NbtWriteView.create(errorReporter, registryLookup);
133+
writeOrphanData(writeView);
134+
return writeView.isEmpty() ? null : writeView.getNbt();
135+
}
154136
}
155137

156138
@Override

0 commit comments

Comments
 (0)