-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathEntityScaleHelper.java
More file actions
139 lines (122 loc) · 6.85 KB
/
EntityScaleHelper.java
File metadata and controls
139 lines (122 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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.");
}
}
}
}