Skip to content

Commit

Permalink
Port to 1.20.5-pre1
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <[email protected]>
  • Loading branch information
shedaniel committed Apr 12, 2024
1 parent b54b89c commit 8effe4a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Environment(EnvType.CLIENT)
public interface ClientTooltipEvent {
/**
* @see Item#append(ItemStack, List, TooltipFlag)
* @see Item#append(ItemStack, List, net.minecraft.world.item.Item.TooltipContext, TooltipFlag)
*/
Event<Item> ITEM = EventFactory.createLoop();
/**
Expand Down Expand Up @@ -73,11 +73,12 @@ interface Item {
* Equivalent to Forge's {@code ItemTooltipEvent} event and
* Fabric's {@code ItemTooltipCallback}.
*
* @param stack The rendered stack.
* @param lines The mutable list of tooltip components.
* @param flag A flag indicating if advanced mode is active.
* @param stack The rendered stack.
* @param lines The mutable list of tooltip components.
* @param tooltipContext The tooltip context.
* @param flag A flag indicating if advanced mode is active.
*/
void append(ItemStack stack, List<Component> lines, TooltipFlag flag);
void append(ItemStack stack, List<Component> lines, net.minecraft.world.item.Item.TooltipContext tooltipContext, TooltipFlag flag);
}

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
/**
* Forge {@code SimpleChannel} like network wrapper of {@link NetworkManager}.
*/
@Deprecated(forRemoval = true)
public final class NetworkChannel {
private final ResourceLocation id;
private final Map<Class<?>, MessageInfo<?>> encoders = Maps.newHashMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class NetworkManager {
* For S2C types, {@link #registerReceiver} should be called on the client side,
* while {@link #registerS2CPayloadType} should be called on the server side.
*/
@Deprecated(forRemoval = true)
public static void registerS2CPayloadType(ResourceLocation id) {
NetworkAggregator.registerS2CType(id, List.of());
}
Expand All @@ -67,6 +68,7 @@ public static <T extends CustomPacketPayload> void registerS2CPayloadType(Custom
* For S2C types, {@link #registerReceiver} should be called on the client side,
* while {@link #registerS2CPayloadType} should be called on the server side.
*/
@Deprecated(forRemoval = true)
public static void registerS2CPayloadType(ResourceLocation id, List<PacketTransformer> packetTransformers) {
NetworkAggregator.registerS2CType(id, packetTransformers);
}
Expand All @@ -79,11 +81,13 @@ public static <T extends CustomPacketPayload> void registerS2CPayloadType(Custom
NetworkAggregator.registerS2CType(type, codec, packetTransformers);
}

@Deprecated(forRemoval = true)
public static void registerReceiver(Side side, ResourceLocation id, NetworkReceiver<RegistryFriendlyByteBuf> receiver) {
registerReceiver(side, id, Collections.emptyList(), receiver);
}

@ApiStatus.Experimental
@Deprecated(forRemoval = true)
public static void registerReceiver(Side side, ResourceLocation id, List<PacketTransformer> packetTransformers, NetworkReceiver<RegistryFriendlyByteBuf> receiver) {
NetworkAggregator.registerReceiver(side, id, packetTransformers, receiver);
}
Expand All @@ -97,20 +101,33 @@ public static <T extends CustomPacketPayload> void registerReceiver(Side side, C
NetworkAggregator.registerReceiver(side, id, codec, packetTransformers, receiver);
}

@Deprecated
@Deprecated(forRemoval = true)
public static Packet<?> toPacket(Side side, ResourceLocation id, RegistryFriendlyByteBuf buf) {
SinglePacketCollector sink = new SinglePacketCollector(null);
collectPackets(sink, side, id, buf);
return sink.getPacket();
}

@Deprecated
@Deprecated(forRemoval = true)
public static List<Packet<?>> toPackets(Side side, ResourceLocation id, RegistryFriendlyByteBuf buf) {
PacketCollector sink = new PacketCollector(null);
collectPackets(sink, side, id, buf);
return sink.collect();
}

public static <T extends CustomPacketPayload> Packet<?> toPacket(Side side, T payload, RegistryAccess access) {
SinglePacketCollector sink = new SinglePacketCollector(null);
collectPackets(sink, side, payload, access);
return sink.getPacket();
}

public static <T extends CustomPacketPayload> List<Packet<?>> toPackets(Side side, T payload, RegistryAccess access) {
PacketCollector sink = new PacketCollector(null);
collectPackets(sink, side, payload, access);
return sink.collect();
}

@Deprecated(forRemoval = true)
public static void collectPackets(PacketSink sink, Side side, ResourceLocation id, RegistryFriendlyByteBuf buf) {
NetworkAggregator.collectPackets(sink, side, id, buf);
}
Expand All @@ -119,15 +136,18 @@ public static <T extends CustomPacketPayload> void collectPackets(PacketSink sin
NetworkAggregator.collectPackets(sink, side, payload, access);
}

@Deprecated(forRemoval = true)
public static void sendToPlayer(ServerPlayer player, ResourceLocation id, RegistryFriendlyByteBuf buf) {
collectPackets(PacketSink.ofPlayer(player), serverToClient(), id, buf);
}

@Deprecated(forRemoval = true)
public static void sendToPlayers(Iterable<ServerPlayer> players, ResourceLocation id, RegistryFriendlyByteBuf buf) {
collectPackets(PacketSink.ofPlayers(players), serverToClient(), id, buf);
}

@Environment(EnvType.CLIENT)
@Deprecated(forRemoval = true)
public static void sendToServer(ResourceLocation id, RegistryFriendlyByteBuf buf) {
collectPackets(PacketSink.client(), clientToServer(), id, buf);
}
Expand Down Expand Up @@ -160,6 +180,15 @@ public static boolean canPlayerReceive(ServerPlayer player, ResourceLocation id)
throw new AssertionError();
}

@Environment(EnvType.CLIENT)
public static boolean canServerReceive(CustomPacketPayload.Type<?> type) {
return canServerReceive(type.id());
}

public static boolean canPlayerReceive(ServerPlayer player, CustomPacketPayload.Type<?> type) {
return canPlayerReceive(player, type.id());
}

/**
* Easy to use utility method to create an entity spawn packet.
* This packet is needed everytime any mod adds a non-living entity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,104 +20,129 @@
package dev.architectury.networking;

import dev.architectury.extensions.network.EntitySpawnExtension;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;

import java.util.UUID;

/**
* @see net.minecraft.network.protocol.game.ClientboundAddEntityPacket
*/
public class SpawnEntityPacket {
private static final ResourceLocation PACKET_ID = new ResourceLocation("architectury", "spawn_entity_packet");
private static final CustomPacketPayload.Type<PacketPayload> PACKET_TYPE = new CustomPacketPayload.Type<>(PACKET_ID);
private static final StreamCodec<RegistryFriendlyByteBuf, PacketPayload> PACKET_CODEC = CustomPacketPayload.codec(PacketPayload::write, PacketPayload::new);

public static Packet<ClientGamePacketListener> create(Entity entity) {
if (entity.level().isClientSide()) {
throw new IllegalStateException("SpawnPacketUtil.create called on the logical client!");
}
var buffer = new RegistryFriendlyByteBuf(Unpooled.buffer(), entity.registryAccess());
buffer.writeVarInt(BuiltInRegistries.ENTITY_TYPE.getId(entity.getType()));
buffer.writeUUID(entity.getUUID());
buffer.writeVarInt(entity.getId());
var position = entity.position();
buffer.writeDouble(position.x);
buffer.writeDouble(position.y);
buffer.writeDouble(position.z);
buffer.writeFloat(entity.getXRot());
buffer.writeFloat(entity.getYRot());
buffer.writeFloat(entity.getYHeadRot());
var deltaMovement = entity.getDeltaMovement();
buffer.writeDouble(deltaMovement.x);
buffer.writeDouble(deltaMovement.y);
buffer.writeDouble(deltaMovement.z);
if (entity instanceof EntitySpawnExtension ext) {
ext.saveAdditionalSpawnData(buffer);
}
return (Packet<ClientGamePacketListener>) NetworkManager.toPacket(NetworkManager.s2c(), PACKET_ID, buffer);
return (Packet<ClientGamePacketListener>) NetworkManager.toPacket(NetworkManager.s2c(), new PacketPayload(entity), entity.registryAccess());
}

public static void register() {
NetworkManager.registerS2CPayloadType(PACKET_ID);
NetworkManager.registerS2CPayloadType(PACKET_TYPE, PACKET_CODEC);
}


@Environment(EnvType.CLIENT)
public static class Client {
@Environment(EnvType.CLIENT)
public static void register() {
NetworkManager.registerReceiver(NetworkManager.s2c(), PACKET_ID, Client::receive);
NetworkManager.registerReceiver(NetworkManager.s2c(), PACKET_TYPE, PACKET_CODEC, Client::receive);
}

@Environment(EnvType.CLIENT)
public static void receive(FriendlyByteBuf buf, NetworkManager.PacketContext context) {
var entityTypeId = buf.readVarInt();
var uuid = buf.readUUID();
var id = buf.readVarInt();
var x = buf.readDouble();
var y = buf.readDouble();
var z = buf.readDouble();
var xRot = buf.readFloat();
var yRot = buf.readFloat();
var yHeadRot = buf.readFloat();
var deltaX = buf.readDouble();
var deltaY = buf.readDouble();
var deltaZ = buf.readDouble();
// Retain this buffer so we can use it in the queued task (EntitySpawnExtension)
buf.retain();
private static void receive(PacketPayload payload, NetworkManager.PacketContext context) {
context.queue(() -> {
var entityType = BuiltInRegistries.ENTITY_TYPE.byId(entityTypeId);
if (entityType == null) {
throw new IllegalStateException("Entity type (" + entityTypeId + ") is unknown, spawning at (" + x + ", " + y + ", " + z + ")");
}
if (Minecraft.getInstance().level == null) {
throw new IllegalStateException("Client world is null!");
}
var entity = entityType.create(Minecraft.getInstance().level);
var entity = payload.entityType().create(Minecraft.getInstance().level);
if (entity == null) {
throw new IllegalStateException("Created entity is null!");
}
entity.setUUID(uuid);
entity.setId(id);
entity.syncPacketPositionCodec(x, y, z);
entity.moveTo(x, y, z);
entity.setXRot(xRot);
entity.setYRot(yRot);
entity.setYHeadRot(yHeadRot);
entity.setYBodyRot(yHeadRot);
entity.setUUID(payload.uuid());
entity.setId(payload.id());
entity.syncPacketPositionCodec(payload.x(), payload.y(), payload.z());
entity.moveTo(payload.x(), payload.y(), payload.z());
entity.setXRot(payload.xRot());
entity.setYRot(payload.yRot());
entity.setYHeadRot(payload.yHeadRot());
entity.setYBodyRot(payload.yHeadRot());
if (entity instanceof EntitySpawnExtension ext) {
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(payload.data()), context.registryAccess());
ext.loadAdditionalSpawnData(buf);
buf.release();
}
buf.release();
Minecraft.getInstance().level.addEntity(entity);
entity.lerpMotion(deltaX, deltaY, deltaZ);
entity.lerpMotion(payload.deltaX(), payload.deltaY(), payload.deltaZ());
});
}
}

private record PacketPayload(EntityType<?> entityType, UUID uuid, int id, double x, double y, double z, float xRot,
float yRot,
float yHeadRot,
double deltaX, double deltaY, double deltaZ,
byte[] data) implements CustomPacketPayload {
public PacketPayload(RegistryFriendlyByteBuf buf) {
this(ByteBufCodecs.registry(Registries.ENTITY_TYPE).decode(buf), buf.readUUID(), buf.readVarInt(), buf.readDouble(), buf.readDouble(), buf.readDouble(),
buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readDouble(), buf.readDouble(), buf.readDouble(),
buf.readByteArray());
}

public PacketPayload(Entity entity) {
this(entity.getType(), entity.getUUID(), entity.getId(), entity.getX(),
entity.getY(), entity.getZ(), entity.getXRot(), entity.getYRot(), entity.getYHeadRot(),
entity.getDeltaMovement().x, entity.getDeltaMovement().y, entity.getDeltaMovement().z, saveExtra(entity));
}

private static byte[] saveExtra(Entity entity) {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
try {
if (entity instanceof EntitySpawnExtension ext) {
ext.saveAdditionalSpawnData(buf);
}
return ByteBufUtil.getBytes(buf);
} finally {
buf.release();
}
}

public void write(RegistryFriendlyByteBuf buf) {
ByteBufCodecs.registry(Registries.ENTITY_TYPE).encode(buf, entityType);
buf.writeUUID(uuid);
buf.writeVarInt(id);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
buf.writeFloat(xRot);
buf.writeFloat(yRot);
buf.writeFloat(yHeadRot);
buf.writeDouble(deltaX);
buf.writeDouble(deltaY);
buf.writeDouble(deltaZ);
buf.writeByteArray(data);
}

@Override
public Type<? extends CustomPacketPayload> type() {
return PACKET_TYPE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public static <T extends ParticleOptions> void register(RegistrySupplier<? exten
supplier.listen(it -> register(it, provider));
}

@ExpectPlatform
// @ExpectPlatform
public static <T extends ParticleOptions> void register(ParticleType<T> type, ParticleProvider<T> provider) {
throw new AssertionError();
// throw new AssertionError();
}

@ExpectPlatform
// @ExpectPlatform
public static <T extends ParticleOptions> void register(ParticleType<T> type, DeferredParticleProvider<T> provider) {
throw new AssertionError();
// throw new AssertionError();
}

@FunctionalInterface
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/resources/architectury.accessWidener
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ transitive-accessible method net/minecraft/world/level/block/DecoratedPotBlock <
transitive-accessible method net/minecraft/world/level/block/DirtPathBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/DispenserBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/DoorBlock <init> (Lnet/minecraft/world/level/block/state/properties/BlockSetType;Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/EnchantmentTableBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/EnchantingTableBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/EndGatewayBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/EndPortalBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
transitive-accessible method net/minecraft/world/level/block/EndRodBlock <init> (Lnet/minecraft/world/level/block/state/BlockBehaviour$Properties;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void registerClient() {
ClientTickEvents.START_WORLD_TICK.register(instance -> ClientTickEvent.CLIENT_LEVEL_PRE.invoker().tick(instance));
ClientTickEvents.END_WORLD_TICK.register(instance -> ClientTickEvent.CLIENT_LEVEL_POST.invoker().tick(instance));

ItemTooltipCallback.EVENT.register((itemStack, tooltipFlag, list) -> ClientTooltipEvent.ITEM.invoker().append(itemStack, list, tooltipFlag));
ItemTooltipCallback.EVENT.register((itemStack, tooltipContext, tooltipFlag, list) -> ClientTooltipEvent.ITEM.invoker().append(itemStack, list, tooltipContext, tooltipFlag));
HudRenderCallback.EVENT.register((graphics, tickDelta) -> ClientGuiEvent.RENDER_HUD.invoker().renderHud(graphics, tickDelta));

ClientCommandRegistrationCallback.EVENT.register((dispatcher, access) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package dev.architectury.registry.client.particle.fabric;

import dev.architectury.registry.client.particle.ParticleProviderRegistry;
import net.fabricmc.fabric.api.client.particle.v1.FabricSpriteProvider;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
//import net.fabricmc.fabric.api.client.particle.v1.FabricSpriteProvider;
//import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.minecraft.client.particle.ParticleProvider;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand All @@ -32,7 +32,7 @@
import java.util.List;

public class ParticleProviderRegistryImpl {
public record ExtendedSpriteSetImpl(
/*public record ExtendedSpriteSetImpl(
FabricSpriteProvider delegate
) implements ParticleProviderRegistry.ExtendedSpriteSet {
@Override
Expand Down Expand Up @@ -63,5 +63,5 @@ public static <T extends ParticleOptions> void register(ParticleType<T> type, Pa
public static <T extends ParticleOptions> void register(ParticleType<T> type, ParticleProviderRegistry.DeferredParticleProvider<T> provider) {
ParticleFactoryRegistry.getInstance().register(type, sprites ->
provider.create(new ExtendedSpriteSetImpl(sprites)));
}
}*/
}
Loading

0 comments on commit 8effe4a

Please sign in to comment.