Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.api.rewriters.EntityRewriter;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.Locale;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -65,6 +68,12 @@ public EntityReplacement plainName() {
return this;
}

/**
* Defines entity data to be set on the mapped entity when it is spawned.
*
* <p><b>Note:</b> when using this method in 1.19.3+ protocols, you should also handle the case where no
* default data is sent by the server. For example, see {@link EntityRewriter#defaultSpawnDataHandler}.
*/
public EntityReplacement spawnEntityData(EntityDataCreator handler) {
this.defaultData = handler;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@

package com.viaversion.viabackwards.api.entities.storage;

import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;

public record WrappedEntityData(List<EntityData> entityDataList) {
public record WrappedEntityData(List<EntityData> entityDataList, UserConnection user) {

public boolean has(EntityData data) {
return this.entityDataList.contains(data);
}

public boolean has(int index) {
return this.entityDataList.stream().anyMatch(data -> data.id() == index);
}

public void remove(EntityData data) {
this.entityDataList.remove(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viaversion.api.data.entity.TrackedEntity;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.version.Types1_14;

import java.util.ArrayList;
import java.util.List;

public abstract class EntityRewriter<C extends ClientboundPacketType, T extends BackwardsProtocol<C, ?, ?, ?>> extends EntityRewriterBase<C, T> {

protected EntityRewriter(T protocol) {
Expand Down Expand Up @@ -60,4 +65,33 @@ public void trackSpawnWithData1_19(final PacketWrapper wrapper, final int data)
wrapper.set(Types.VAR_INT, 2, protocol.getMappingData().getNewBlockStateId(data));
}
}

/**
* Spawn handler that sends default entity data in 1.19.3+.
*
* <p>Starting from 1.19.3, default entity data is no longer sent by the server, so any entity replacement
* with default data may never apply that data if no packet is sent.
*
* <p>Note: this should be the last handler in the chain.
*/
protected <CM extends ClientboundPacketType> PacketHandler defaultSpawnDataHandler(CM setEntityData, Type<List<EntityData>> mappedType) {
return wrapper -> {
final int entityId = wrapper.get(Types.VAR_INT, 0);

final List<EntityData> spawnData = new ArrayList<>();
handleEntityData(entityId, spawnData, wrapper.user());
if (!spawnData.isEmpty()) {
// we also want to overwrite the first actual data sent by the server
tracker(wrapper.user()).entity(entityId).sentEntityData(false);

wrapper.send(protocol.getClass());
wrapper.cancel();

final PacketWrapper packet = wrapper.create(setEntityData);
packet.write(Types.VAR_INT, entityId);
packet.write(mappedType, spawnData);
packet.send(protocol.getClass());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void handleEntityData(int entityId, List<EntityData> entityDataList, User

// Add any other extra data for mapped entities
if (entityMapping != null && entityMapping.hasBaseData() && initialEntityData) {
entityMapping.defaultData().createData(new WrappedEntityData(entityDataList));
entityMapping.defaultData().createData(new WrappedEntityData(entityDataList, connection));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected PacketHandler getMobSpawnRewriter(Type<List<EntityData>> dataType, IdS
if (entityReplacement != null) {
idSetter.setId(wrapper, entityReplacement.replacementId());
if (entityReplacement.hasBaseData()) {
entityReplacement.defaultData().createData(new WrappedEntityData(entityDataList));
entityReplacement.defaultData().createData(new WrappedEntityData(entityDataList, wrapper.user()));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void register() {

EntityReplacement entityReplacement = entityDataForType(type);
if (entityReplacement != null) {
WrappedEntityData storage = new WrappedEntityData(entityDataList);
WrappedEntityData storage = new WrappedEntityData(entityDataList, wrapper.user());
wrapper.set(Types.UNSIGNED_BYTE, 0, (short) entityReplacement.replacementId());
if (entityReplacement.hasBaseData())
entityReplacement.defaultData().createData(storage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void register() {
storage.setPosition(x, y, z);
entity.put(storage);
});
handler(defaultSpawnDataHandler(ClientboundPackets1_19_3.SET_ENTITY_DATA, Types1_19_3.ENTITY_DATA_LIST));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the reasoning behind your other pr correctly, to fully close that, this should get something similar to your other handler like

mapEntityTypeWithData(EntityTypes1_19_4.PAINTING, EntityTypes1_19_4.PAINTING).spawnEntityData(data -> {
    if (!data.has(8) && data.user().getProtocolInfo().serverProtocolVersion().olderThan(ProtocolVersion.v1_21)) {
        data.add(new EntityData(8, Types1_19_3.ENTITY_DATA_TYPES.paintingVariantType, 0)); // kebab
    }
});

(untested)

@roccodev roccodev Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right, I can update it in a couple days if that's ok (for those wondering, this would handle the 1.18 client on 1.19-1.21 server case where kebab would not spawn without default data)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work fine on a 1.20 server. Correct me if I'm wrong, the version check should be redundant as 1.21 will apply default data before it in the pipeline (it still works with the new 1.21 default on 1.21+ servers)

}
});

Expand Down Expand Up @@ -647,6 +648,13 @@ public void onMappingDataLoaded() {
mapEntityTypeWithData(EntityTypes1_19_4.INTERACTION, EntityTypes1_19_4.ARMOR_STAND).spawnEntityData(displayDataCreator); // Not much we can do about this one

mapEntityTypeWithData(EntityTypes1_19_4.SNIFFER, EntityTypes1_19_4.RAVAGER).jsonName();

mapEntityTypeWithData(EntityTypes1_19_4.PAINTING, EntityTypes1_19_4.PAINTING).spawnEntityData(data -> {
if (!data.has(8)) {
// 1.19->1.18 needs default data to spawn kebab (default painting)
data.add(new EntityData(8, Types1_19_3.ENTITY_DATA_TYPES.paintingVariantType, 0)); // kebab
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@
import com.viaversion.viaversion.api.minecraft.WolfVariant;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5;
import com.viaversion.viaversion.api.minecraft.entitydata.EntityData;
import com.viaversion.viaversion.api.minecraft.entitydata.types.EntityDataTypes1_20_5;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.version.VersionedTypes;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPackets1_20_5;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPackets1_20_5;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.Paintings1_20_5;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPackets1_21;
import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.KeyMappings;
import java.util.HashMap;
Expand All @@ -57,6 +60,9 @@ public EntityPacketRewriter1_21(final Protocol1_21To1_20_5 protocol) {

@Override
public void registerPackets() {
protocol.appendClientbound(ClientboundPackets1_21.ADD_ENTITY,
defaultSpawnDataHandler(ClientboundPackets1_20_5.SET_ENTITY_DATA, VersionedTypes.V1_20_5.entityDataList));

protocol.replaceClientbound(ClientboundConfigurationPackets1_21.REGISTRY_DATA, wrapper -> {
final String key = Key.stripMinecraftNamespace(wrapper.passthrough(Types.STRING));
final RegistryEntry[] entries = wrapper.passthrough(Types.REGISTRY_ENTRY_ARRAY);
Expand Down Expand Up @@ -179,6 +185,15 @@ protected void registerRewrites() {
mappedEntityDataTypes.optionalComponentType
);
registerBlockStateHandler(EntityTypes1_20_5.ABSTRACT_MINECART, 11);

// Handle new 1.21 default painting
mapEntityTypeWithData(EntityTypes1_20_5.PAINTING, EntityTypes1_20_5.PAINTING).spawnEntityData(data -> {
final EnchantmentsPaintingsStorage registry = data.user().get(EnchantmentsPaintingsStorage.class);
final int defaultPainting = registry != null ? registry.mappedDefaultPainting() : 0;
if (!data.has(8)) {
data.add(new EntityData(8, VersionedTypes.V1_20_5.entityDataTypes.paintingVariantType, defaultPainting));
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public int jubeboxSongToItem(final int id) {
}

public int mappedPainting(final int id) {
return id > 0 && id < paintingMappings.length ? paintingMappings[id] : 0;
return id >= 0 && id < paintingMappings.length ? paintingMappings[id] : 0;
}

public int mappedDefaultPainting() {
return mappedPainting(0);
}

public @Nullable Tag enchantmentDescription(final int id) {
Expand Down