Skip to content

Commit 1ac22fe

Browse files
committed
Update to 1.21.3, closes #185
1 parent f5443b2 commit 1ac22fe

File tree

24 files changed

+163
-92
lines changed

24 files changed

+163
-92
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.6-SNAPSHOT"
8-
id("io.github.ladysnake.chenille") version "0.12.2"
7+
id("fabric-loom") version "1.8-SNAPSHOT"
8+
id("io.github.ladysnake.chenille") version "0.14.0"
99
id("org.cadixdev.licenser") version "0.6.1"
1010
}
1111

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import io.netty.buffer.Unpooled;
2626
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
27+
import net.fabricmc.loader.api.FabricLoader;
2728
import net.minecraft.network.PacketCallbacks;
2829
import net.minecraft.network.RegistryByteBuf;
2930
import net.minecraft.network.packet.CustomPayload;
@@ -208,7 +209,11 @@ public void syncWith(ServerPlayerEntity player, ComponentProvider provider, Comp
208209
ServerPlayNetworking.getSender(player).sendPacket(payload, PacketCallbacks.always(buf::release));
209210
} else {
210211
if (predicate.isRequiredOnClient()) {
211-
player.networkHandler.disconnect(Text.literal("This server requires Cardinal Components API (unhandled packet: " + payload.getId().id() + ")" + ComponentsInternals.getClientOptionalModAdvice()));
212+
String specificMod = FabricLoader.getInstance().getModContainer(this.id.getNamespace()).map(c -> c.getMetadata().getName() + " and ").orElse("");
213+
player.networkHandler.disconnect(Text.literal(
214+
"This server requires " + specificMod + "Cardinal Components API " +
215+
"(unhandled packet: " + payload.getId().id() + ")" +
216+
ComponentsInternals.getClientOptionalModAdvice()));
212217
}
213218
buf.release();
214219
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public static <T extends ComponentUpdatePayload<?>> void registerComponentSync(C
4242
}
4343
});
4444
} catch (UnknownComponentException e) {
45-
ctx.player().networkHandler.onDisconnected(new DisconnectionInfo(Text.literal(e.getMessage() + "\n(you are probably missing a mod installed on the server)" + ComponentsInternals.getClientOptionalModAdvice())));
45+
ctx.player().networkHandler.onDisconnected(new DisconnectionInfo(Text.literal(
46+
e.getMessage() + "\n(you are probably missing a mod installed on the server)" + ComponentsInternals.getClientOptionalModAdvice())
47+
));
4648
} finally {
4749
payload.buf().release();
4850
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
import com.mojang.datafixers.util.Unit;
2626
import io.netty.buffer.ByteBuf;
2727
import io.netty.buffer.Unpooled;
28-
import net.minecraft.network.PacketByteBuf;
2928
import net.minecraft.network.RegistryByteBuf;
3029
import net.minecraft.network.codec.PacketCodec;
31-
import net.minecraft.util.math.ChunkPos;
3230

3331
public final class MorePacketCodecs {
3432
public static final PacketCodec<ByteBuf, Unit> EMPTY = PacketCodec.unit(Unit.INSTANCE);
@@ -45,15 +43,4 @@ public final class MorePacketCodecs {
4543
return new RegistryByteBuf(copy, buf.getRegistryManager());
4644
}
4745
);
48-
49-
/**
50-
* A codec for a {@link ChunkPos}.
51-
*
52-
* @see PacketByteBuf#readChunkPos()
53-
* @see PacketByteBuf#writeChunkPos(ChunkPos)
54-
*/
55-
public static final PacketCodec<PacketByteBuf, ChunkPos> CHUNKPOS = PacketCodec.ofStatic(
56-
PacketByteBuf::writeChunkPos,
57-
PacketByteBuf::readChunkPos
58-
);
5946
}

cardinal-components-chunk/src/main/java/org/ladysnake/cca/internal/chunk/CardinalComponentsChunk.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
import org.ladysnake.cca.api.v3.component.ComponentKey;
3131
import org.ladysnake.cca.api.v3.component.ComponentProvider;
3232
import org.ladysnake.cca.internal.base.ComponentUpdatePayload;
33-
import org.ladysnake.cca.internal.base.MorePacketCodecs;
3433

3534
public final class CardinalComponentsChunk {
3635
public static final CustomPayload.Id<ComponentUpdatePayload<ChunkPos>> PACKET_ID = ComponentUpdatePayload.id("chunk_sync");
3736

3837
public static void init() {
3938
if (FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1")) {
40-
ComponentUpdatePayload.register(PACKET_ID, MorePacketCodecs.CHUNKPOS);
39+
ComponentUpdatePayload.register(PACKET_ID, ChunkPos.PACKET_CODEC);
4140
ChunkSyncCallback.EVENT.register((player, tracked) -> {
4241
for (ComponentKey<?> key : tracked.asComponentProvider().getComponentContainer().keys()) {
4342
key.syncWith(player, (ComponentProvider) tracked);
Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,68 @@
2424

2525
import net.minecraft.nbt.NbtCompound;
2626
import net.minecraft.nbt.NbtElement;
27+
import net.minecraft.registry.DynamicRegistryManager;
2728
import net.minecraft.server.world.ServerWorld;
2829
import net.minecraft.util.math.ChunkPos;
29-
import net.minecraft.world.ChunkSerializer;
30+
import net.minecraft.world.HeightLimitView;
3031
import net.minecraft.world.chunk.Chunk;
3132
import net.minecraft.world.chunk.ProtoChunk;
33+
import net.minecraft.world.chunk.SerializedChunk;
3234
import net.minecraft.world.chunk.WrapperProtoChunk;
3335
import net.minecraft.world.poi.PointOfInterestStorage;
3436
import net.minecraft.world.storage.StorageKey;
37+
import org.jetbrains.annotations.Nullable;
3538
import org.ladysnake.cca.internal.base.AbstractComponentContainer;
3639
import org.spongepowered.asm.mixin.Mixin;
40+
import org.spongepowered.asm.mixin.Unique;
3741
import org.spongepowered.asm.mixin.injection.At;
3842
import org.spongepowered.asm.mixin.injection.Inject;
3943
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
4044

41-
@Mixin(ChunkSerializer.class)
42-
public abstract class MixinChunkSerializer {
43-
@Inject(method = "deserialize", at = @At("RETURN"))
44-
private static void deserialize(ServerWorld world, PointOfInterestStorage poiStorage, StorageKey key, ChunkPos chunkPos, NbtCompound tag, CallbackInfoReturnable<ProtoChunk> cir) {
45+
@Mixin(SerializedChunk.class)
46+
public abstract class MixinSerializedChunk {
47+
@Unique
48+
private @Nullable NbtCompound cca$serializedComponents;
49+
50+
@Inject(method = "fromNbt", at = @At("RETURN"))
51+
private static void fromNbt(HeightLimitView world, DynamicRegistryManager registryManager, NbtCompound nbt, CallbackInfoReturnable<SerializedChunk> cir) {
52+
MixinSerializedChunk ret = (MixinSerializedChunk) (Object) cir.getReturnValue();
53+
if (ret != null) {
54+
ret.cca$serializedComponents = new NbtCompound();
55+
ret.cca$serializedComponents.put(AbstractComponentContainer.NBT_KEY, nbt.get(AbstractComponentContainer.NBT_KEY));
56+
}
57+
}
58+
59+
@Inject(method = "convert", at = @At("RETURN"))
60+
private void convert(ServerWorld world, PointOfInterestStorage poiStorage, StorageKey key, ChunkPos expectedPos, CallbackInfoReturnable<ProtoChunk> cir) {
61+
NbtCompound tag = cca$serializedComponents;
62+
if (tag == null) return;
4563
ProtoChunk ret = cir.getReturnValue();
4664
Chunk chunk = ret instanceof WrapperProtoChunk ? ((WrapperProtoChunk) ret).getWrappedChunk() : ret;
4765
chunk.asComponentProvider().getComponentContainer().fromTag(tag, world.getRegistryManager());
4866
// If components have been removed, we need to make the chunk save again
4967
if (tag.contains(AbstractComponentContainer.NBT_KEY, NbtElement.COMPOUND_TYPE)) {
5068
int remainingComponentCount = tag.getCompound(AbstractComponentContainer.NBT_KEY).getSize();
51-
chunk.setNeedsSaving(remainingComponentCount > 0);
69+
if (remainingComponentCount > 0) {
70+
chunk.markNeedsSaving();
71+
}
72+
}
73+
}
74+
75+
@Inject(method = "fromChunk", at = @At("RETURN"))
76+
private static void fromChunk(ServerWorld world, Chunk chunk, CallbackInfoReturnable<SerializedChunk> cir) {
77+
MixinSerializedChunk ret = (MixinSerializedChunk) (Object) cir.getReturnValue();
78+
if (ret != null) {
79+
ret.cca$serializedComponents = new NbtCompound();
80+
chunk.asComponentProvider().getComponentContainer().toTag(ret.cca$serializedComponents, world.getRegistryManager());
5281
}
5382
}
5483

5584
@Inject(method = "serialize", at = @At("RETURN"))
56-
private static void serialize(ServerWorld world, Chunk chunk, CallbackInfoReturnable<NbtCompound> cir) {
57-
NbtCompound ret = cir.getReturnValue();
58-
chunk.asComponentProvider().getComponentContainer().toTag(ret, world.getRegistryManager());
85+
private void serialize(CallbackInfoReturnable<NbtCompound> cir) {
86+
if (cca$serializedComponents != null) {
87+
NbtCompound ret = cir.getReturnValue();
88+
ret.put(AbstractComponentContainer.NBT_KEY, cca$serializedComponents.get(AbstractComponentContainer.NBT_KEY));
89+
}
5990
}
6091
}

cardinal-components-chunk/src/main/resources/mixins.cardinal_components_chunk.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"common.MixinChunk",
88
"common.MixinChunkDataSender",
99
"common.MixinChunkHolder",
10-
"common.MixinChunkSerializer",
1110
"common.MixinEmptyChunk",
11+
"common.MixinSerializedChunk",
1212
"common.MixinServerWorld",
1313
"common.MixinWorldChunk",
1414
"common.MixinWrapperProtoChunk"

cardinal-components-chunk/src/testmod/java/org/ladysnake/cca/test/chunk/CcaChunkTestSuite.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import net.minecraft.test.TestContext;
2929
import net.minecraft.util.math.BlockPos;
3030
import net.minecraft.util.math.ChunkPos;
31-
import net.minecraft.world.ChunkSerializer;
3231
import net.minecraft.world.chunk.Chunk;
32+
import net.minecraft.world.chunk.SerializedChunk;
3333
import net.minecraft.world.chunk.WorldChunk;
3434
import net.minecraft.world.storage.StorageKey;
3535
import org.ladysnake.cca.test.base.LoadAwareTestComponent;
@@ -43,8 +43,9 @@ public void chunksSerialize(TestContext ctx) {
4343
ChunkPos pos = new ChunkPos(ctx.getAbsolutePos(new BlockPos(1, 0, 1)));
4444
Chunk c = new WorldChunk(ctx.getWorld(), pos);
4545
c.getComponent(Vita.KEY).setVitality(42);
46-
NbtCompound nbt = ChunkSerializer.serialize(ctx.getWorld(), c);
47-
Chunk c1 = ChunkSerializer.deserialize(ctx.getWorld(), ctx.getWorld().getPointOfInterestStorage(), new StorageKey("", ctx.getWorld().getRegistryKey(), ""), pos, nbt);
46+
NbtCompound nbt = SerializedChunk.fromChunk(ctx.getWorld(), c).serialize();
47+
Chunk c1 = SerializedChunk.fromNbt(ctx.getWorld(), ctx.getWorld().getRegistryManager(), nbt)
48+
.convert(ctx.getWorld(), ctx.getWorld().getPointOfInterestStorage(), new StorageKey("", ctx.getWorld().getRegistryKey(), ""), pos);
4849
GameTestUtil.assertTrue("Chunk component data should survive deserialization", c1.getComponent(Vita.KEY).getVitality() == 42);
4950
ctx.complete();
5051
}

cardinal-components-chunk/src/testmod/java/org/ladysnake/cca/test/chunk/ChunkVita.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public ChunkVita(Chunk owner) {
3737
public void setVitality(int value) {
3838
super.setVitality(value);
3939
this.owner.syncComponent(KEY);
40-
this.owner.setNeedsSaving(true);
40+
this.owner.markNeedsSaving();
4141
}
4242
}

cardinal-components-entity/src/main/java/org/ladysnake/cca/internal/entity/CardinalComponentsEntity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import net.fabricmc.loader.api.FabricLoader;
3232
import net.minecraft.entity.Entity;
3333
import net.minecraft.entity.LivingEntity;
34+
import net.minecraft.entity.conversion.EntityConversionContext;
35+
import net.minecraft.entity.conversion.EntityConversionType;
3436
import net.minecraft.network.codec.PacketCodecs;
3537
import net.minecraft.network.packet.CustomPayload;
3638
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
@@ -106,18 +108,18 @@ public static void init() {
106108
StaticEntityComponentPlugin.INSTANCE.ensureInitialized();
107109
}
108110

109-
private static void copyData(LivingEntity original, LivingEntity clone, boolean keepInventory) {
111+
private static void copyData(LivingEntity original, LivingEntity clone, EntityConversionContext context) {
110112
Set<ComponentKey<?>> keys = ((ComponentProvider) original).getComponentContainer().keys();
111113

112114
for (ComponentKey<?> key : keys) {
113115
if (key.isProvidedBy(clone)) {
114-
copyData(original, clone, original.getRegistryManager(), false, keepInventory, true, key);
116+
copyData(original, clone, original.getRegistryManager(), false, context.keepEquipment(), context.type() == EntityConversionType.SINGLE, key);
115117
}
116118
}
117119
}
118120

119121
private static void copyData(ServerPlayerEntity original, ServerPlayerEntity clone, boolean lossless) {
120-
boolean keepInventory = original.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY) || clone.isSpectator();
122+
boolean keepInventory = original.getServerWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY) || clone.isSpectator();
121123
Set<ComponentKey<?>> keys = ((ComponentProvider) original).getComponentContainer().keys();
122124

123125
for (ComponentKey<?> key : keys) {

0 commit comments

Comments
 (0)