-
-
Notifications
You must be signed in to change notification settings - Fork 114
Implement modular baby entity scaling (For feature request #1058) #1216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mfishma
wants to merge
5
commits into
ViaVersion:master
Choose a base branch
from
mfishma:feature/scale-new-babies
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e25014c
Got Happy Ghasts working, still troubleshooting water babies
mfishma c6c5252
Update EntityScaleAttributeRewriter to not be as generic
mfishma ab4625b
Comments for scaleHelper updated for clarity
mfishma 48a8769
Changed dolphin scale 0.5→0.65
mfishma ef2121a
updates per comments
mfishma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
common/src/main/java/com/viaversion/viabackwards/api/entities/EntityScaleData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards | ||
| * Copyright (C) 2016-2025 ViaVersion and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.viaversion.viabackwards.api.entities; | ||
|
|
||
| import com.viaversion.viaversion.api.data.entity.StoredEntityData; | ||
|
|
||
| /** | ||
| * Storage object used within {@link StoredEntityData} to track the scaling state of an entity. | ||
| * This ensures that scale updates are only sent when the calculated scale actually changes. | ||
| */ | ||
| public final class EntityScaleData { | ||
|
|
||
| private boolean isBaby; | ||
| private float scale = 1.0f; | ||
|
|
||
| public boolean isBaby() { | ||
| return isBaby; | ||
| } | ||
|
|
||
| public void setBaby(boolean baby) { | ||
| isBaby = baby; | ||
| } | ||
|
|
||
| public float getScale() { | ||
| return scale; | ||
| } | ||
|
|
||
| public void setScale(float scale) { | ||
| this.scale = scale; | ||
| } | ||
| } |
139 changes: 139 additions & 0 deletions
139
common/src/main/java/com/viaversion/viabackwards/api/entities/EntityScaleHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards | ||
| * Copyright (C) 2016-2025 ViaVersion and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.viaversion.viabackwards.api.entities; | ||
|
|
||
| import com.viaversion.viabackwards.api.rewriters.EntityRewriter; | ||
| import com.viaversion.viaversion.api.data.entity.StoredEntityData; | ||
| import com.viaversion.viaversion.api.minecraft.entities.EntityType; | ||
| import com.viaversion.viaversion.api.minecraft.entitydata.EntityData; | ||
| import com.viaversion.viaversion.api.protocol.Protocol; | ||
| import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; | ||
| import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; | ||
| import com.viaversion.viaversion.api.type.Types; | ||
| import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; | ||
| import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; | ||
| import com.viaversion.viaversion.rewriter.entitydata.EntityDataHandlerEvent; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import com.viaversion.viaversion.api.data.FullMappings; | ||
|
|
||
| /** | ||
| * A modular helper for tracking and injecting entity scaling on older clients. | ||
| * When older clients lack a specific entity, ViaBackwards maps it to a chosen existing entity. | ||
| * Sometimes this mapping requires the entity to be scaled (e.g. mapping a tiny baby mob to a large adult mob). | ||
| * | ||
| * Example usage for future scenarios: | ||
| * If Mojang adds a Vulture and a Baby Vulture, and let's say map the adult Vulture to a Bat: | ||
| * - We'd need to scale UP the entire Bat so it looks like an adult Vulture. | ||
| * - If that happens, you'd apply a baseline scale increase (like 2.0x) to both adult and baby via the main rewriter. | ||
| * - But since there's no baby Bat, you'd also need to track `isBaby` here. | ||
| * - If `isBaby` is true, you'd apply a reduced scale relative to the adult Vulture's new scale (e.g., 0.65x). | ||
| * - Because `EntityScaleAttributeRewriter` dynamically multiplies the attribute value mid-flight, you simply | ||
| * register `0.65f` for the baby here. The final scale sent to the client will correctly be (2.0 * 0.65) = 1.3x. | ||
| */ | ||
| public class EntityScaleHelper { | ||
|
|
||
| private static final class BabyScale { | ||
| private final float scaleFactor; | ||
| private final int index; | ||
|
|
||
| private BabyScale(float scaleFactor, int index) { | ||
| this.scaleFactor = scaleFactor; | ||
| this.index = index; | ||
| } | ||
| } | ||
|
|
||
| private final Map<EntityType, BabyScale> babyScales = new HashMap<>(); | ||
| private final ClientboundPacketType updateAttributesPacket; | ||
|
|
||
| public EntityScaleHelper(ClientboundPacketType updateAttributesPacket) { | ||
| this.updateAttributesPacket = updateAttributesPacket; | ||
| } | ||
|
|
||
| /** | ||
| * Registers a scaling factor for a specific baby entity type. | ||
| * <p> | ||
| * <b>WARNING:</b> The scaling attribute was introduced in {@code 1.20.5}. | ||
| * Do NOT attempt to use this helper for protocols older than {@code 1.20.4 -> 1.20.3}. | ||
| * Sending the {@code minecraft:scale} attribute to older clients will cause an instant disconnect! | ||
| * | ||
| * @param type The entity type that requires scaling when it is a baby. | ||
| * @param babyScaleFactor The multiplier to apply to the generic.scale attribute when it is a baby. | ||
| * @param babyIndex The metadata index for the is_baby property in this specific protocol | ||
| */ | ||
| public void addBabyScale(EntityType type, float babyScaleFactor, int babyIndex) { | ||
| babyScales.put(type, new BabyScale(babyScaleFactor, babyIndex)); | ||
| } | ||
|
|
||
| public Iterable<EntityType> getRegisteredTypes() { | ||
| return babyScales.keySet(); | ||
| } | ||
|
|
||
| /** | ||
| * Checks the metadata, tracks the scale state natively, and injects an UPDATE_ATTRIBUTES | ||
| * packet down the pipeline right as the metadata is processed by the client. | ||
| * Use this inside an Entity Rewriter's filter().handler(). | ||
| * | ||
| * @param event The meta packet processing event. | ||
| * @param data The current metadata piece being processed. | ||
| * @param protocol The protocol handling the translation, used for mapping lookups. | ||
| */ | ||
| public void trackAndInject(EntityDataHandlerEvent event, EntityData data, Protocol protocol) { | ||
| StoredEntityData storedEntityData = event.user().getEntityTracker(protocol.getClass()).entityData(event.entityId()); | ||
| if (storedEntityData == null) return; | ||
|
|
||
| // Check if this protocol layer actually cares about scaling this entity | ||
| BabyScale babyScale = babyScales.get(storedEntityData.type()); | ||
| if (babyScale == null || data.id() != babyScale.index || !(data.value() instanceof Boolean)) { | ||
| return; | ||
| } | ||
|
|
||
| EntityScaleData scaleData = storedEntityData.get(EntityScaleData.class); | ||
| if (scaleData == null) { | ||
| scaleData = new EntityScaleData(); | ||
| storedEntityData.put(scaleData); | ||
| } | ||
|
|
||
| boolean isBaby = (Boolean) data.value(); | ||
| float scale = isBaby ? babyScale.scaleFactor : 1.0f; | ||
|
|
||
| if (scaleData.isBaby() != isBaby || scaleData.getScale() != scale) { | ||
| scaleData.setBaby(isBaby); | ||
| scaleData.setScale(scale); | ||
|
|
||
| // Actively inject the packet so the client receives the scale update immediately | ||
| PacketWrapper updatePacket = PacketWrapper.create(updateAttributesPacket, event.user()); | ||
| updatePacket.write(Types.VAR_INT, event.entityId()); | ||
| updatePacket.write(Types.VAR_INT, 1); // 1 attribute | ||
|
|
||
| FullMappings attributeMappings = protocol.getMappingData().getAttributeMappings(); | ||
| int unmappedId = attributeMappings != null ? attributeMappings.id("minecraft:scale") : -1; | ||
| int mappedId = unmappedId != -1 ? protocol.getMappingData().getNewAttributeId(unmappedId) : -1; | ||
|
|
||
| if (mappedId != -1) { | ||
| updatePacket.write(Types.VAR_INT, mappedId); | ||
| updatePacket.write(Types.DOUBLE, (double) scaleData.getScale()); | ||
| updatePacket.write(Types.VAR_INT, 0); // 0 modifiers | ||
| updatePacket.scheduleSend(protocol.getClass()); | ||
| } else { | ||
| com.viaversion.viaversion.api.Via.getPlatform().getLogger().warning("Could not find minecraft:scale attribute mapping! Do not use EntityScaleHelper on pre-1.20.5 protocols."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
113 changes: 113 additions & 0 deletions
113
...src/main/java/com/viaversion/viabackwards/api/rewriters/EntityScaleAttributeRewriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards | ||
| * Copyright (C) 2016-2025 ViaVersion and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.viaversion.viabackwards.api.rewriters; | ||
|
|
||
| import com.viaversion.viabackwards.api.entities.EntityScaleData; | ||
| import com.viaversion.viaversion.api.data.entity.StoredEntityData; | ||
| import com.viaversion.viaversion.api.protocol.Protocol; | ||
| import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; | ||
| import com.viaversion.viaversion.api.type.Types; | ||
| import com.viaversion.viaversion.rewriter.AttributeRewriter; | ||
|
|
||
| /** | ||
| * An extension of AttributeRewriter that intercepts UPDATE_ATTRIBUTES packets | ||
| * and dynamically multiplies the scale value if the entity's tracked state | ||
| * (EntityScaleData) currently dictates it should be shrunk (e.g. a baby mob mapped to an adult). | ||
| * | ||
| * This protects scaled entities from instantly growing to adult size when the server | ||
| * issues random attribute updates for other reasons. | ||
| */ | ||
| public class EntityScaleAttributeRewriter<C extends ClientboundPacketType> extends AttributeRewriter<C> { | ||
|
|
||
| private final Protocol<C, ?, ?, ?> scaleProtocol; | ||
|
|
||
| public EntityScaleAttributeRewriter(Protocol<C, ?, ?, ?> protocol) { | ||
| super(protocol); | ||
| this.scaleProtocol = protocol; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the scale attribute rewriter for the given UPDATE_ATTRIBUTES packet type. | ||
| * <p> | ||
| * <b>WARNING:</b> The scaling attribute was introduced in {@code 1.20.5}. | ||
| * Do NOT attempt to register this listener for protocols older than {@code 1.20.4 -> 1.20.3}. | ||
| * | ||
| * @param packetType The UPDATE_ATTRIBUTES packet type for the protocol. | ||
| */ | ||
| @Override | ||
| public void register1_21(C packetType) { | ||
| scaleProtocol.registerClientbound(packetType, wrapper -> { | ||
| final int entityId = wrapper.passthrough(Types.VAR_INT); | ||
|
|
||
| // Fast lookup for scaling factor, negligible overhead if not present | ||
| float scale = 1.0f; | ||
| com.viaversion.viaversion.api.data.entity.EntityTracker tracker = wrapper.user().getEntityTracker(scaleProtocol.getClass()); | ||
| if (tracker != null) { | ||
| StoredEntityData data = tracker.entityDataIfPresent(entityId); | ||
| if (data != null && data.has(EntityScaleData.class)) { | ||
| scale = data.get(EntityScaleData.class).getScale(); | ||
| } | ||
| } | ||
|
|
||
| final int size = wrapper.passthrough(Types.VAR_INT); | ||
| int newSize = size; | ||
|
|
||
| int scaleId = -1; | ||
| if (scaleProtocol.getMappingData().getAttributeMappings() != null) { | ||
| scaleId = scaleProtocol.getMappingData().getAttributeMappings().id("minecraft:scale"); | ||
| } | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| final int attributeId = wrapper.read(Types.VAR_INT); | ||
| final int mappedId = scaleProtocol.getMappingData().getNewAttributeId(attributeId); | ||
| if (mappedId == -1) { | ||
| newSize--; | ||
|
|
||
| wrapper.read(Types.DOUBLE); // Base | ||
| final int modifierSize = wrapper.read(Types.VAR_INT); | ||
| for (int j = 0; j < modifierSize; j++) { | ||
| wrapper.read(Types.STRING); // ID | ||
| wrapper.read(Types.DOUBLE); // Amount | ||
| wrapper.read(Types.BYTE); // Operation | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| wrapper.write(Types.VAR_INT, mappedId); | ||
| double value = wrapper.read(Types.DOUBLE); // Base | ||
|
|
||
| // Multiply the server's requested scale by our tracked scale modifier | ||
| if (scale != 1.0f && scaleId != -1 && attributeId == scaleId) { | ||
| value *= scale; | ||
| } | ||
| wrapper.write(Types.DOUBLE, value); | ||
|
|
||
| final int modifierSize = wrapper.passthrough(Types.VAR_INT); | ||
| for (int j = 0; j < modifierSize; j++) { | ||
| wrapper.passthrough(Types.STRING); // ID | ||
| wrapper.passthrough(Types.DOUBLE); // Amount | ||
| wrapper.passthrough(Types.BYTE); // Operation | ||
| } | ||
| } | ||
|
|
||
| if (size != newSize) { | ||
| wrapper.set(Types.VAR_INT, 1, newSize); | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attributes should be sent on spawn, not on entity data (both for performance reasons and because entities don't actually require entity data being sent)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, we'd want to take the packet injection out of the entity data handler. So if we send the attribute on ADD_ENTITY instead, how should we handle the initial baby scale given the server might not have sent the is_baby metadata packet yet at the exact time of spawn? Should we send the default adult scale on spawn and then intercept later when the metadata arrives, or is there a better pattern for this you prefer?