Skip to content

Commit

Permalink
Update to 1.20.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Feb 4, 2024
1 parent 1a9bf0a commit a534f05
Show file tree
Hide file tree
Showing 34 changed files with 491 additions and 368 deletions.
12 changes: 3 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import net.fabricmc.loom.task.RemapJarTask
import java.net.URI

plugins {
id("fabric-loom") version "1.3-SNAPSHOT" apply false
id("io.github.juuxel.loom-quiltflower") version "1.6.0"
id("io.github.ladysnake.chenille") version "0.11.3"
id("fabric-loom") version "1.5-SNAPSHOT"
id("io.github.ladysnake.chenille") version "0.12.0-SNAPSHOT+build.1"
id("org.cadixdev.licenser") version "0.6.1"
}

Expand All @@ -15,9 +14,8 @@ val fabricApiVersion: String = providers.gradleProperty("fabric_api_version").ge
allprojects {
apply(plugin = "java-library")
apply(plugin = "maven-publish")
apply(plugin = "io.github.ladysnake.chenille")
apply(plugin = "org.cadixdev.licenser")
apply(plugin = "fabric-loom")
apply(plugin = "io.github.ladysnake.chenille")

chenille {
javaVersion = 17
Expand Down Expand Up @@ -63,10 +61,6 @@ allprojects {
compileOnly("org.jetbrains:annotations:24.0.1")
}

repositories {
mavenLocal()
}

tasks.processResources {
inputs.property("version", project.version)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
import dev.onyxstudios.cca.api.v3.component.sync.ComponentPacketWriter;
import dev.onyxstudios.cca.api.v3.component.sync.PlayerSyncPredicate;
import dev.onyxstudios.cca.internal.base.asm.CcaBootstrap;
import net.minecraft.network.packet.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -47,6 +54,13 @@
*/
@ApiStatus.NonExtendable
public abstract class ComponentKey<C extends Component> {
public static final PacketCodec<ByteBuf, ComponentKey<? extends Component>> PACKET_CODEC = PacketCodecs.STRING.xmap(
id -> Objects.requireNonNull(
ComponentRegistry.get(new Identifier(id)),
"No component key with id " + id
),
key -> key.getId().toString()
);

public final Identifier getId() {
return this.id;
Expand Down Expand Up @@ -193,10 +207,14 @@ public void syncWith(ServerPlayerEntity player, ComponentProvider provider) {
@ApiStatus.Experimental
public void syncWith(ServerPlayerEntity player, ComponentProvider provider, ComponentPacketWriter writer, PlayerSyncPredicate predicate) {
if (predicate.shouldSyncWith(player)) {
Packet<?> packet = provider.toComponentPacket(this, writer, player);
RegistryByteBuf buf = new RegistryByteBuf(Unpooled.buffer(), player.getServerWorld().getRegistryManager());
writer.writeSyncPacket(buf, player);
CustomPayload payload = provider.toComponentPacket(this, buf);

if (packet != null) {
player.networkHandler.sendPacket(packet);
if (payload != null) {
ServerPlayNetworking.getSender(player).sendPacket(payload, PacketCallbacks.always(buf::release));
} else {
buf.release();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
package dev.onyxstudios.cca.api.v3.component;

import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import dev.onyxstudios.cca.api.v3.component.sync.ComponentPacketWriter;
import dev.onyxstudios.cca.api.v3.component.sync.PlayerSyncPredicate;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import dev.onyxstudios.cca.internal.base.ComponentUpdatePayload;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.ApiStatus;

import javax.annotation.Nullable;
import java.util.List;
Expand Down Expand Up @@ -61,15 +62,15 @@ default Iterable<ServerPlayerEntity> getRecipientsForComponentSync() {
/**
* Produces a sync packet using the given information.
*
* @param key the key describing the component being synchronized
* @param writer a {@link ComponentPacketWriter} writing the component's data to the packet
* @param recipient the player receiving the packet
* @return a {@link net.minecraft.network.packet.Packet} that has all the information required to perform the component sync
* @since 3.0.0
* <p>It is the responsibility of the caller to {@link ByteBuf#release() release} the buffer after this method returns.
*
* @param key the key describing the component being synchronized
* @param data the component's raw sync data
* @return a {@link ComponentUpdatePayload} that has all the information required to perform the component sync
* @since 6.0.0
*/
@Nullable
@ApiStatus.Experimental // TODO change the return value to Packet<ClientCommonPacketListener>
default <C extends AutoSyncedComponent> CustomPayloadS2CPacket toComponentPacket(ComponentKey<? super C> key, ComponentPacketWriter writer, ServerPlayerEntity recipient) {
default <C extends AutoSyncedComponent> CustomPayload toComponentPacket(ComponentKey<? super C> key, RegistryByteBuf data) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 OnyxStudios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.onyxstudios.cca.internal.base;

import dev.onyxstudios.cca.api.v3.component.Component;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.network.packet.CustomPayload;

import java.util.Optional;
import java.util.function.BiFunction;

public final class CcaClientInternals {
public static <T extends ComponentUpdatePayload<?>> void registerComponentSync(CustomPayload.Id<T> packetId, BiFunction<T, ClientPlayNetworking.Context, Optional<? extends Component>> getter) {
ClientPlayNetworking.registerGlobalReceiver(packetId, (payload, ctx) -> {
try {
getter.apply(payload, ctx).ifPresent(c -> {
if (c instanceof AutoSyncedComponent synced) {
synced.applySyncPacket(payload.buf());
}
});
} finally {
payload.buf().release();
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 OnyxStudios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.onyxstudios.cca.internal.base;

import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;

public record ComponentUpdatePayload<T>(
Id<ComponentUpdatePayload<T>> id,
T targetData,
ComponentKey<?> componentKey,
RegistryByteBuf buf
) implements CustomPayload {
public static <T> CustomPayload.Id<ComponentUpdatePayload<T>> id(String path) {
return CustomPayload.id("cardinal-components:" + path);
}

public static <T> void register(Id<ComponentUpdatePayload<T>> id, PacketCodec<? super RegistryByteBuf, T> targetDataCodec) {
PayloadTypeRegistry.playS2C().register(id, codec(id, targetDataCodec));
}

public static <T> PacketCodec<RegistryByteBuf, ComponentUpdatePayload<T>> codec(Id<ComponentUpdatePayload<T>> id, PacketCodec<? super RegistryByteBuf, T> targetDataCodec) {
return MorePacketCodecs.tuple(
PacketCodec.unit(id), ComponentUpdatePayload::id,
targetDataCodec, ComponentUpdatePayload::targetData,
ComponentKey.PACKET_CODEC, ComponentUpdatePayload::componentKey,
MorePacketCodecs.REG_BYTE_BUF, ComponentUpdatePayload::buf,
ComponentUpdatePayload::new
);
}

@Override
public Id<? extends CustomPayload> getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 OnyxStudios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.onyxstudios.cca.internal.base;

import com.mojang.datafixers.util.Function4;
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;

import java.util.function.Function;

public final class MorePacketCodecs {
public static final PacketCodec<ByteBuf, Unit> EMPTY = PacketCodec.unit(Unit.INSTANCE);

public static final PacketCodec<RegistryByteBuf, RegistryByteBuf> REG_BYTE_BUF = PacketCodec.ofStatic(
(buf, value) -> {
buf.writeVarInt(value.readableBytes());
buf.writeBytes(value);
},
(buf) -> {
int readableBytes = buf.readVarInt();
ByteBuf copy = Unpooled.buffer(readableBytes, readableBytes);
buf.readBytes(copy, readableBytes);
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
);

/**
* {@return a codec for encoding three values}
*/
public static <B, C, T1, T2, T3, T4> PacketCodec<B, C> tuple(
PacketCodec<? super B, T1> codec1,
Function<C, T1> from1,
PacketCodec<? super B, T2> codec2,
Function<C, T2> from2,
PacketCodec<? super B, T3> codec3,
Function<C, T3> from3,
PacketCodec<? super B, T4> codec4,
Function<C, T4> from4,
Function4<T1, T2, T3, T4, C> to
) {
return PacketCodec.ofStatic(
(buf, value) -> {
codec1.encode(buf, from1.apply(value));
codec2.encode(buf, from2.apply(value));
codec3.encode(buf, from3.apply(value));
codec4.encode(buf, from4.apply(value));
},
(buf) -> {
T1 object2 = codec1.decode(buf);
T2 object3 = codec2.decode(buf);
T3 object4 = codec3.decode(buf);
T4 object5 = codec4.decode(buf);
return to.apply(object2, object3, object4, object5);
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 OnyxStudios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package dev.onyxstudios.cca.internal;

import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.registry.Registries;
import net.minecraft.util.math.BlockPos;

public record BlockEntityAddress(
BlockEntityType<?> beType,
BlockPos bePos
) {

public static final PacketCodec<RegistryByteBuf, BlockEntityAddress> CODEC = PacketCodec.tuple(
PacketCodecs.entryOf(Registries.BLOCK_ENTITY_TYPE), BlockEntityAddress::beType,
BlockPos.PACKET_CODEC, BlockEntityAddress::bePos,
BlockEntityAddress::new
);
}
Loading

0 comments on commit a534f05

Please sign in to comment.