Skip to content

Commit

Permalink
Update to 1.21.3, closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Oct 25, 2024
1 parent f5443b2 commit 1ac22fe
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 92 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import net.fabricmc.loom.task.RemapJarTask
import java.net.URI

plugins {
id("fabric-loom") version "1.6-SNAPSHOT"
id("io.github.ladysnake.chenille") version "0.12.2"
id("fabric-loom") version "1.8-SNAPSHOT"
id("io.github.ladysnake.chenille") version "0.14.0"
id("org.cadixdev.licenser") version "0.6.1"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.packet.CustomPayload;
Expand Down Expand Up @@ -208,7 +209,11 @@ public void syncWith(ServerPlayerEntity player, ComponentProvider provider, Comp
ServerPlayNetworking.getSender(player).sendPacket(payload, PacketCallbacks.always(buf::release));
} else {
if (predicate.isRequiredOnClient()) {
player.networkHandler.disconnect(Text.literal("This server requires Cardinal Components API (unhandled packet: " + payload.getId().id() + ")" + ComponentsInternals.getClientOptionalModAdvice()));
String specificMod = FabricLoader.getInstance().getModContainer(this.id.getNamespace()).map(c -> c.getMetadata().getName() + " and ").orElse("");
player.networkHandler.disconnect(Text.literal(
"This server requires " + specificMod + "Cardinal Components API " +
"(unhandled packet: " + payload.getId().id() + ")" +
ComponentsInternals.getClientOptionalModAdvice()));
}
buf.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public static <T extends ComponentUpdatePayload<?>> void registerComponentSync(C
}
});
} catch (UnknownComponentException e) {
ctx.player().networkHandler.onDisconnected(new DisconnectionInfo(Text.literal(e.getMessage() + "\n(you are probably missing a mod installed on the server)" + ComponentsInternals.getClientOptionalModAdvice())));
ctx.player().networkHandler.onDisconnected(new DisconnectionInfo(Text.literal(
e.getMessage() + "\n(you are probably missing a mod installed on the server)" + ComponentsInternals.getClientOptionalModAdvice())
));
} finally {
payload.buf().release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
import com.mojang.datafixers.util.Unit;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.util.math.ChunkPos;

public final class MorePacketCodecs {
public static final PacketCodec<ByteBuf, Unit> EMPTY = PacketCodec.unit(Unit.INSTANCE);
Expand All @@ -45,15 +43,4 @@ public final class MorePacketCodecs {
return new RegistryByteBuf(copy, buf.getRegistryManager());
}
);

/**
* A codec for a {@link ChunkPos}.
*
* @see PacketByteBuf#readChunkPos()
* @see PacketByteBuf#writeChunkPos(ChunkPos)
*/
public static final PacketCodec<PacketByteBuf, ChunkPos> CHUNKPOS = PacketCodec.ofStatic(
PacketByteBuf::writeChunkPos,
PacketByteBuf::readChunkPos
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
import org.ladysnake.cca.api.v3.component.ComponentKey;
import org.ladysnake.cca.api.v3.component.ComponentProvider;
import org.ladysnake.cca.internal.base.ComponentUpdatePayload;
import org.ladysnake.cca.internal.base.MorePacketCodecs;

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

public static void init() {
if (FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1")) {
ComponentUpdatePayload.register(PACKET_ID, MorePacketCodecs.CHUNKPOS);
ComponentUpdatePayload.register(PACKET_ID, ChunkPos.PACKET_CODEC);
ChunkSyncCallback.EVENT.register((player, tracked) -> {
for (ComponentKey<?> key : tracked.asComponentProvider().getComponentContainer().keys()) {
key.syncWith(player, (ComponentProvider) tracked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,68 @@

import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.ChunkSerializer;
import net.minecraft.world.HeightLimitView;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ProtoChunk;
import net.minecraft.world.chunk.SerializedChunk;
import net.minecraft.world.chunk.WrapperProtoChunk;
import net.minecraft.world.poi.PointOfInterestStorage;
import net.minecraft.world.storage.StorageKey;
import org.jetbrains.annotations.Nullable;
import org.ladysnake.cca.internal.base.AbstractComponentContainer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ChunkSerializer.class)
public abstract class MixinChunkSerializer {
@Inject(method = "deserialize", at = @At("RETURN"))
private static void deserialize(ServerWorld world, PointOfInterestStorage poiStorage, StorageKey key, ChunkPos chunkPos, NbtCompound tag, CallbackInfoReturnable<ProtoChunk> cir) {
@Mixin(SerializedChunk.class)
public abstract class MixinSerializedChunk {
@Unique
private @Nullable NbtCompound cca$serializedComponents;

@Inject(method = "fromNbt", at = @At("RETURN"))
private static void fromNbt(HeightLimitView world, DynamicRegistryManager registryManager, NbtCompound nbt, CallbackInfoReturnable<SerializedChunk> cir) {
MixinSerializedChunk ret = (MixinSerializedChunk) (Object) cir.getReturnValue();
if (ret != null) {
ret.cca$serializedComponents = new NbtCompound();
ret.cca$serializedComponents.put(AbstractComponentContainer.NBT_KEY, nbt.get(AbstractComponentContainer.NBT_KEY));
}
}

@Inject(method = "convert", at = @At("RETURN"))
private void convert(ServerWorld world, PointOfInterestStorage poiStorage, StorageKey key, ChunkPos expectedPos, CallbackInfoReturnable<ProtoChunk> cir) {
NbtCompound tag = cca$serializedComponents;
if (tag == null) return;
ProtoChunk ret = cir.getReturnValue();
Chunk chunk = ret instanceof WrapperProtoChunk ? ((WrapperProtoChunk) ret).getWrappedChunk() : ret;
chunk.asComponentProvider().getComponentContainer().fromTag(tag, world.getRegistryManager());
// If components have been removed, we need to make the chunk save again
if (tag.contains(AbstractComponentContainer.NBT_KEY, NbtElement.COMPOUND_TYPE)) {
int remainingComponentCount = tag.getCompound(AbstractComponentContainer.NBT_KEY).getSize();
chunk.setNeedsSaving(remainingComponentCount > 0);
if (remainingComponentCount > 0) {
chunk.markNeedsSaving();
}
}
}

@Inject(method = "fromChunk", at = @At("RETURN"))
private static void fromChunk(ServerWorld world, Chunk chunk, CallbackInfoReturnable<SerializedChunk> cir) {
MixinSerializedChunk ret = (MixinSerializedChunk) (Object) cir.getReturnValue();
if (ret != null) {
ret.cca$serializedComponents = new NbtCompound();
chunk.asComponentProvider().getComponentContainer().toTag(ret.cca$serializedComponents, world.getRegistryManager());
}
}

@Inject(method = "serialize", at = @At("RETURN"))
private static void serialize(ServerWorld world, Chunk chunk, CallbackInfoReturnable<NbtCompound> cir) {
NbtCompound ret = cir.getReturnValue();
chunk.asComponentProvider().getComponentContainer().toTag(ret, world.getRegistryManager());
private void serialize(CallbackInfoReturnable<NbtCompound> cir) {
if (cca$serializedComponents != null) {
NbtCompound ret = cir.getReturnValue();
ret.put(AbstractComponentContainer.NBT_KEY, cca$serializedComponents.get(AbstractComponentContainer.NBT_KEY));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"common.MixinChunk",
"common.MixinChunkDataSender",
"common.MixinChunkHolder",
"common.MixinChunkSerializer",
"common.MixinEmptyChunk",
"common.MixinSerializedChunk",
"common.MixinServerWorld",
"common.MixinWorldChunk",
"common.MixinWrapperProtoChunk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import net.minecraft.test.TestContext;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.ChunkSerializer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.SerializedChunk;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.storage.StorageKey;
import org.ladysnake.cca.test.base.LoadAwareTestComponent;
Expand All @@ -43,8 +43,9 @@ public void chunksSerialize(TestContext ctx) {
ChunkPos pos = new ChunkPos(ctx.getAbsolutePos(new BlockPos(1, 0, 1)));
Chunk c = new WorldChunk(ctx.getWorld(), pos);
c.getComponent(Vita.KEY).setVitality(42);
NbtCompound nbt = ChunkSerializer.serialize(ctx.getWorld(), c);
Chunk c1 = ChunkSerializer.deserialize(ctx.getWorld(), ctx.getWorld().getPointOfInterestStorage(), new StorageKey("", ctx.getWorld().getRegistryKey(), ""), pos, nbt);
NbtCompound nbt = SerializedChunk.fromChunk(ctx.getWorld(), c).serialize();
Chunk c1 = SerializedChunk.fromNbt(ctx.getWorld(), ctx.getWorld().getRegistryManager(), nbt)
.convert(ctx.getWorld(), ctx.getWorld().getPointOfInterestStorage(), new StorageKey("", ctx.getWorld().getRegistryKey(), ""), pos);
GameTestUtil.assertTrue("Chunk component data should survive deserialization", c1.getComponent(Vita.KEY).getVitality() == 42);
ctx.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public ChunkVita(Chunk owner) {
public void setVitality(int value) {
super.setVitality(value);
this.owner.syncComponent(KEY);
this.owner.setNeedsSaving(true);
this.owner.markNeedsSaving();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.conversion.EntityConversionContext;
import net.minecraft.entity.conversion.EntityConversionType;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
Expand Down Expand Up @@ -106,18 +108,18 @@ public static void init() {
StaticEntityComponentPlugin.INSTANCE.ensureInitialized();
}

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

for (ComponentKey<?> key : keys) {
if (key.isProvidedBy(clone)) {
copyData(original, clone, original.getRegistryManager(), false, keepInventory, true, key);
copyData(original, clone, original.getRegistryManager(), false, context.keepEquipment(), context.type() == EntityConversionType.SINGLE, key);
}
}
}

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

for (ComponentKey<?> key : keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import org.ladysnake.cca.api.v3.entity.EntityComponentFactoryRegistry;
import org.ladysnake.cca.api.v3.entity.EntityComponentInitializer;
Expand All @@ -40,8 +42,8 @@
import org.ladysnake.cca.test.base.Vita;

public class CcaEntityTestMod implements ModInitializer, EntityComponentInitializer {

public static final EntityType<TestEntity> TEST_ENTITY = EntityType.Builder.create(TestEntity::new, SpawnGroup.MISC).build("cca-entity-test");
public static final RegistryKey<EntityType<?>> TEST_ENTITY_ID = RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of("cca-entity-test", "test"));
public static final EntityType<TestEntity> TEST_ENTITY = EntityType.Builder.create(TestEntity::new, SpawnGroup.MISC).build(TEST_ENTITY_ID);
public static final int NATURAL_VITA_CEILING = 10;
public static final int CAMEL_BASE_VITA = 50;

Expand All @@ -59,6 +61,6 @@ public void registerEntityComponentFactories(EntityComponentFactoryRegistry regi

@Override
public void onInitialize() {
Registry.register(Registries.ENTITY_TYPE, Identifier.of("cca-entity-test", "test"), TEST_ENTITY);
Registry.register(Registries.ENTITY_TYPE, TEST_ENTITY_ID, TEST_ENTITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.entity.Bucketable;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.conversion.EntityConversionContext;
import net.minecraft.entity.mob.ShulkerEntity;
import net.minecraft.entity.passive.CamelEntity;
import net.minecraft.entity.passive.CatEntity;
Expand Down Expand Up @@ -89,9 +90,10 @@ public void moddedEntitiesWork(TestContext ctx) {
@GameTest(templateName = EMPTY_STRUCTURE)
public void respawnHappensOnConversion(TestContext ctx) {
CamelEntity camel = ctx.spawnEntity(EntityType.CAMEL, 0, 0, 0);
CowEntity cow = camel.convertTo(EntityType.COW, true);
CowEntity cow = camel.convertTo(EntityType.COW, EntityConversionContext.create(camel, true, true), e -> {});
assert cow != null;
GameTestUtil.assertTrue("Component data should transfer according to RespawnCopyStrategy", Vita.get(cow).getVitality() == CcaEntityTestMod.CAMEL_BASE_VITA);
CatEntity cat = cow.convertTo(EntityType.CAT, true);
CatEntity cat = cow.convertTo(EntityType.CAT, EntityConversionContext.create(camel, true, true), e -> {});
GameTestUtil.assertTrue("Component data should not transfer by default", Vita.get(cat).getVitality() < CcaEntityTestMod.NATURAL_VITA_CEILING);
ctx.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

public class TestEntity extends Entity {
Expand All @@ -38,6 +40,11 @@ protected void initDataTracker(DataTracker.Builder builder) {

}

@Override
public boolean damage(ServerWorld world, DamageSource source, float amount) {
return false;
}

@Override
protected void readCustomDataFromNbt(NbtCompound nbt) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public interface ItemComponentInitializer extends ComponentRegistrationInitializer {
/**
* Called to register component migrations from CCA to {@link net.minecraft.component.DataComponentType}.
* Called to register component migrations from CCA to {@link net.minecraft.component.ComponentType}.
*
* @param registry an {@link ItemComponentMigrationRegistry} for component migrations
* @since 7.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.Lifecycle;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.world.gen.GeneratorOptions;
import net.minecraft.world.level.LevelInfo;
import net.minecraft.world.level.LevelProperties;
Expand All @@ -39,7 +39,7 @@
@Mixin(LevelStorage.class)
public class LevelStorageMixin {
@WrapOperation(method = "parseSaveProperties", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/LevelProperties;readProperties(Lcom/mojang/serialization/Dynamic;Lnet/minecraft/world/level/LevelInfo;Lnet/minecraft/world/level/LevelProperties$SpecialProperty;Lnet/minecraft/world/gen/GeneratorOptions;Lcom/mojang/serialization/Lifecycle;)Lnet/minecraft/world/level/LevelProperties;"))
private static LevelProperties readComponents(Dynamic<?> dynamic, LevelInfo info, LevelProperties.SpecialProperty specialProperty, GeneratorOptions generatorOptions, Lifecycle lifecycle, Operation<LevelProperties> original, @Local(argsOnly = true) DynamicRegistryManager.Immutable registryManager) {
private static LevelProperties readComponents(Dynamic<?> dynamic, LevelInfo info, LevelProperties.SpecialProperty specialProperty, GeneratorOptions generatorOptions, Lifecycle lifecycle, Operation<LevelProperties> original, @Local(argsOnly = true) RegistryWrapper.WrapperLookup registryManager) {
LevelProperties props = original.call(dynamic, info, specialProperty, generatorOptions, lifecycle);
((ComponentProvider) props).getComponentContainer().fromDynamic(dynamic, registryManager);
return props;
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
------------------------------------------------------
Version 6.2.0
------------------------------------------------------
Updated to 1.21.3

------------------------------------------------------
Version 6.1.2
------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ org.gradle.parallel=true
fabric.loom.multiProjectOptimisation=true

#see https://fabricmc.net/develop/
minecraft_version=1.21
yarn_mappings=9
loader_version=0.15.11
minecraft_version=1.21.3
yarn_mappings=2
loader_version=0.16.7
#Fabric api
fabric_api_version=0.100.7+1.21
fabric_api_version=0.106.1+1.21.3

elmendorf_version=0.13.0

#Publishing
mod_version = 6.1.2
curseforge_id = 318449
modrinth_id = K01OU20C
curseforge_versions = 1.21
modrinth_versions = 1.21
curseforge_versions = 1.21.2; 1.21.3
modrinth_versions = 1.21.2; 1.21.3
release_type = release
display_name = Cardinal-Components-API
owners = Ladysnake
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 1ac22fe

Please sign in to comment.